1 package org.apache.velocity.tools.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
33
34
35
36
37
38
39
40
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
57
58
59
60
61
62
63
64
65
66
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
80
81 public void setKey(String key)
82 {
83 this.key = key;
84 }
85
86
87
88
89
90
91
92 public void setClassname(String classname) throws Exception
93 {
94 if (classname != null && classname.length() != 0)
95 {
96 this.clazz = getApplicationClass(classname);
97
98 clazz.newInstance();
99 try
100 {
101
102 this.init = clazz.getMethod("init", new Class[]{ Object.class });
103 }
104 catch (NoSuchMethodException nsme)
105 {
106
107 }
108 try
109 {
110
111 this.configure = clazz.getMethod("configure", new Class[]{ Map.class });
112 }
113 catch (NoSuchMethodException nsme)
114 {
115
116 }
117 }
118 else
119 {
120 this.clazz = null;
121 }
122 }
123
124
125
126
127
128
129 public void setParameters(Map parameters)
130 {
131 this.parameters = parameters;
132 }
133
134
135
136
137
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
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
164
165
166 public Map getParameters()
167 {
168 return parameters;
169 }
170
171
172
173
174
175
176
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
187 Object tool = null;
188 try
189 {
190 tool = clazz.newInstance();
191 }
192
193
194
195
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
208 if (configure != null && parameters != null)
209 {
210 try
211 {
212
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
226 if (init != null)
227 {
228 try
229 {
230
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 }