1 package org.apache.velocity.tools.view;
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.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30 * ToolInfo implementation for view tools. New instances
31 * are returned for every call to getInstance(obj), and tools
32 * that have an init(Object) method are initialized with the
33 * given object before being returned. And tools that have a
34 * configure(Map) method will be configured before being returned
35 * if there are parameters specified for the tool.
36 *
37 * @author Nathan Bubna
38 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
39 * @version $Id: ViewToolInfo.java 961796 2010-07-08 15:18:15Z apetrelli $
40 * @deprecated Use {@link org.apache.velocity.tools.ToolInfo}
41 */
42 @Deprecated
43 public class ViewToolInfo implements ToolInfo
44 {
45 protected static final Log LOG = LogFactory.getLog(ViewToolInfo.class);
46
47 private String key;
48 private Class clazz;
49 private Map parameters;
50 private Method init = null;
51 private Method configure = null;
52
53 public ViewToolInfo() {}
54
55
56 //TODO: if classloading becomes needed elsewhere, move this to a utils class
57 /**
58 * Return the <code>Class</code> object for the specified fully qualified
59 * class name, from this web application's class loader. If no
60 * class loader is set for the current thread, then the class loader
61 * that loaded this class will be used.
62 *
63 * @param name Fully qualified class name to be loaded
64 * @return Class object
65 * @exception ClassNotFoundException if the class cannot be found
66 * @since VelocityTools 1.1
67 */
68 protected Class getApplicationClass(String name) throws ClassNotFoundException
69 {
70 ClassLoader loader = Thread.currentThread().getContextClassLoader();
71 if (loader == null)
72 {
73 loader = ViewToolInfo.class.getClassLoader();
74 }
75 return loader.loadClass(name);
76 }
77
78
79 /*********************** Mutators *************************/
80
81 public void setKey(String key)
82 {
83 this.key = key;
84 }
85
86 /**
87 * If an instance of the tool cannot be created from
88 * the classname passed to this method, it will throw an exception.
89 *
90 * @param classname the fully qualified java.lang.Class name of the tool
91 */
92 public void setClassname(String classname) throws Exception
93 {
94 if (classname != null && classname.length() != 0)
95 {
96 this.clazz = getApplicationClass(classname);
97 // create an instance to make sure we can
98 clazz.newInstance();
99 try
100 {
101 // try to get an init(Object) method
102 this.init = clazz.getMethod("init", new Class[]{ Object.class });
103 }
104 catch (NoSuchMethodException nsme)
105 {
106 // ignore
107 }
108 try
109 {
110 // check for a configure(Map) method
111 this.configure = clazz.getMethod("configure", new Class[]{ Map.class });
112 }
113 catch (NoSuchMethodException nsme)
114 {
115 // ignore
116 }
117 }
118 else
119 {
120 this.clazz = null;
121 }
122 }
123
124 /**
125 * Set parameter map for this tool.
126 *
127 * @since VelocityTools 1.1
128 */
129 public void setParameters(Map parameters)
130 {
131 this.parameters = parameters;
132 }
133
134 /**
135 * Set/add new parameter for this tool.
136 *
137 * @since VelocityTools 1.1
138 */
139 public void setParameter(String name, String value)
140 {
141 if (parameters == null)
142 {
143 parameters = new HashMap();
144 }
145 parameters.put(name, value);
146 }
147
148
149 /*********************** Accessors *************************/
150
151 public String getKey()
152 {
153 return key;
154 }
155
156
157 public String getClassname()
158 {
159 return clazz != null ? clazz.getName() : null;
160 }
161
162 /**
163 * Get parameters for this tool.
164 * @since VelocityTools 1.1
165 */
166 public Map getParameters()
167 {
168 return parameters;
169 }
170
171 /**
172 * Returns a new instance of the tool. If the tool
173 * has an init(Object) method, the new instance
174 * will be initialized using the given data. If parameters
175 * have been specified and the tool has a configure(Map)
176 * method, the tool will be passed the parameters also.
177 */
178 public Object getInstance(Object initData)
179 {
180 if (clazz == null)
181 {
182 LOG.error("Tool "+this.key+" has no Class definition!");
183 return null;
184 }
185
186 /* Get the tool instance */
187 Object tool = null;
188 try
189 {
190 tool = clazz.newInstance();
191 }
192 /* we shouldn't get exceptions here because we already
193 * got an instance of this class during setClassname().
194 * but to be safe, let's catch the declared ones and give
195 * notice of them, and let other exceptions slip by. */
196 catch (IllegalAccessException e)
197 {
198 LOG.error("Exception while instantiating instance of \"" +
199 getClassname() + "\"", e);
200 }
201 catch (InstantiationException e)
202 {
203 LOG.error("Exception while instantiating instance of \"" +
204 getClassname() + "\"", e);
205 }
206
207 /* if the tool is configurable and we have parameters... */
208 if (configure != null && parameters != null)
209 {
210 try
211 {
212 // call the configure method on the instance
213 configure.invoke(tool, new Object[]{ parameters });
214 }
215 catch (IllegalAccessException iae)
216 {
217 LOG.error("Exception when calling configure(Map) on "+tool, iae);
218 }
219 catch (InvocationTargetException ite)
220 {
221 LOG.error("Exception when calling configure(Map) on "+tool, ite);
222 }
223 }
224
225 /* if the tool is initializable... */
226 if (init != null)
227 {
228 try
229 {
230 // call the init method on the instance
231 init.invoke(tool, new Object[]{ initData });
232 }
233 catch (IllegalAccessException iae)
234 {
235 LOG.error("Exception when calling init(Object) on "+tool, iae);
236 }
237 catch (InvocationTargetException ite)
238 {
239 LOG.error("Exception when calling init(Object) on "+tool, ite);
240 }
241 }
242 return tool;
243 }
244
245 }