1 package org.apache.velocity.tools; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.lang.reflect.Method; 23 import java.util.Map; 24 import org.apache.velocity.tools.ToolContext; 25 26 /** 27 * Manages old tools which still use the deprecated init() method. 28 * 29 * @author Nathan Bubna 30 * @version $Id: OldToolInfo.java 511959 2007-02-26 19:24:39Z nbubna $ 31 */ 32 public class OldToolInfo extends ToolInfo 33 { 34 private static final long serialVersionUID = -4062162635847288761L; 35 public static final String INIT_METHOD_NAME = "init"; 36 37 private transient Method init; 38 39 /** 40 * Creates a new instance using the minimum required info 41 * necessary for a tool. 42 */ 43 public OldToolInfo(String key, Class clazz) 44 { 45 super(key, clazz); 46 } 47 48 protected Method getInit() 49 { 50 if (this.init == null) 51 { 52 try 53 { 54 // try to get an init(Object) method 55 this.init = 56 getToolClass().getMethod("init", new Class[]{ Object.class }); 57 } 58 catch (NoSuchMethodException nsme) 59 { 60 // ignore 61 } 62 } 63 return this.init; 64 } 65 66 /** 67 * 68 * 69 * @param clazz the java.lang.Class of the tool 70 */ 71 @Override 72 public void setClass(Class clazz) 73 { 74 super.setClass(clazz); 75 76 // clear any existing init method 77 this.init = null; 78 } 79 80 81 @Override 82 protected void configure(Object tool, Map<String,Object> configuration) 83 { 84 // have specific setters and configure(Map) called first 85 super.configure(tool, configuration); 86 87 Method init = getInit(); 88 if (init != null) 89 { 90 // ctx should, in all cases where a tool has such a method, 91 // actually be a View(Tool)Context, but we don't want to link 92 // to that class here, so as not to pollute the generic jar 93 Object ctx = configuration.get(ToolContext.CONTEXT_KEY); 94 if (ctx != null) 95 { 96 invoke(init, tool, ctx); 97 } 98 } 99 } 100 101 }