1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.InputStream;
24 import java.io.IOException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Modifier;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.List;
31 import org.apache.velocity.exception.ResourceNotFoundException;
32 import org.apache.velocity.tools.ClassUtils;
33 import org.apache.velocity.tools.ToolboxFactory;
34
35
36
37
38
39
40
41 public class ConfigurationUtils
42 {
43 public static final String GENERIC_DEFAULTS_PATH =
44 "/org/apache/velocity/tools/generic/tools.xml";
45 public static final String VIEW_DEFAULTS_PATH =
46 "/org/apache/velocity/tools/view/tools.xml";
47 public static final String STRUTS_DEFAULTS_PATH =
48 "/org/apache/velocity/tools/struts/tools.xml";
49
50 public static final String AUTOLOADED_XML_PATH = "tools.xml";
51 public static final String AUTOLOADED_PROPS_PATH = "tools.properties";
52
53 public static final String SYSTEM_PROPERTY_KEY =
54 "org.apache.velocity.tools";
55 public static final ConfigurationUtils INSTANCE = new ConfigurationUtils();
56
57 private ConfigurationUtils() {}
58
59 public ConfigurationUtils getInstance()
60 {
61 return INSTANCE;
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75 public static FactoryConfiguration getDefaultTools()
76 {
77 FileFactoryConfiguration config =
78 new XmlFactoryConfiguration("ConfigurationUtils.getDefaultTools()");
79 config.read(GENERIC_DEFAULTS_PATH);
80
81
82 config.read(VIEW_DEFAULTS_PATH, false);
83 config.read(STRUTS_DEFAULTS_PATH, false);
84
85
86 clean(config);
87 return config;
88 }
89
90
91
92
93
94 public static FactoryConfiguration getGenericTools()
95 {
96 FileFactoryConfiguration config =
97 new XmlFactoryConfiguration("ConfigurationUtils.getGenericTools()");
98 config.read(GENERIC_DEFAULTS_PATH);
99
100
101 clean(config);
102 return config;
103 }
104
105
106
107
108
109
110
111 public static FactoryConfiguration getVelocityView()
112 {
113 FileFactoryConfiguration config =
114 new XmlFactoryConfiguration("ConfigurationUtils.getVelocityView()");
115 config.read(GENERIC_DEFAULTS_PATH);
116 config.read(VIEW_DEFAULTS_PATH);
117
118
119 clean(config);
120 return config;
121 }
122
123
124
125
126
127
128
129
130 public static FactoryConfiguration getVelocityStruts()
131 {
132 FileFactoryConfiguration config =
133 new XmlFactoryConfiguration("ConfigurationUtils.getVelocityStruts()");
134 config.read(GENERIC_DEFAULTS_PATH);
135 config.read(VIEW_DEFAULTS_PATH);
136 config.read(STRUTS_DEFAULTS_PATH);
137
138
139 clean(config);
140 return config;
141 }
142
143
144
145
146
147
148
149
150
151 public static FactoryConfiguration getAutoLoaded()
152 {
153 return getAutoLoaded(true);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public static FactoryConfiguration getAutoLoaded(boolean includeDefaults)
171 {
172 FactoryConfiguration auto;
173 if (includeDefaults)
174 {
175
176 auto = getDefaultTools();
177 }
178 else
179 {
180
181 auto = new FactoryConfiguration("ConfigurationUtils.getAutoLoaded(false)");
182 }
183
184
185
186
187 FactoryConfiguration cpXml = findInClasspath(AUTOLOADED_XML_PATH);
188 if (cpXml != null)
189 {
190 auto.addConfiguration(cpXml);
191 }
192
193
194 FactoryConfiguration cpProps = findInClasspath(AUTOLOADED_PROPS_PATH);
195 if (cpProps != null)
196 {
197 auto.addConfiguration(cpProps);
198 }
199
200
201 FactoryConfiguration fsXml = findInFileSystem(AUTOLOADED_XML_PATH);
202 if (fsXml != null)
203 {
204 auto.addConfiguration(fsXml);
205 }
206
207
208 FactoryConfiguration fsProps = findInFileSystem(AUTOLOADED_PROPS_PATH);
209 if (fsProps != null)
210 {
211 auto.addConfiguration(fsProps);
212 }
213
214
215 return auto;
216 }
217
218
219
220
221
222
223
224
225 public static FactoryConfiguration findFromSystemProperty()
226 {
227 String path = System.getProperty(SYSTEM_PROPERTY_KEY);
228 if (path == null || path.length() == 0)
229 {
230 return null;
231 }
232 return load(path);
233 }
234
235
236
237
238
239
240 public static ToolboxFactory createFactory()
241 {
242
243 FactoryConfiguration auto = getAutoLoaded();
244
245
246 FactoryConfiguration sys = findFromSystemProperty();
247 if (sys != null)
248 {
249 auto.addConfiguration(sys);
250 }
251
252 ToolboxFactory factory = new ToolboxFactory();
253 factory.configure(auto);
254 return factory;
255 }
256
257
258
259
260
261
262 public static void clean(Configuration config)
263 {
264
265
266 ConfigurationCleaner cleaner = new ConfigurationCleaner();
267 cleaner.clean(config);
268 }
269
270
271
272
273
274
275
276
277 public static FactoryConfiguration load(String path)
278 {
279 FactoryConfiguration config = find(path);
280 if (config == null)
281 {
282 throw new ResourceNotFoundException("Could not find configuration at "+path);
283 }
284 return config;
285 }
286
287
288
289
290
291
292
293
294
295
296 public static FactoryConfiguration find(String path)
297 {
298 FactoryConfiguration cp = findInClasspath(path);
299 FactoryConfiguration fs = findInFileSystem(path);
300 if (cp != null)
301 {
302 if (fs != null)
303 {
304 cp.addConfiguration(fs);
305 }
306 return cp;
307 }
308 else
309 {
310 return fs;
311 }
312 }
313
314
315
316
317
318
319
320
321 public static FactoryConfiguration findInFileSystem(String path)
322 {
323 File file = new File(path);
324 if (file.exists())
325 {
326 try
327 {
328 return read(file.toURL());
329 }
330 catch (MalformedURLException mue)
331 {
332 throw new IllegalStateException("Could not convert existing file path \""+path+"\" to URL", mue);
333 }
334 }
335 return null;
336 }
337
338
339
340
341 public static FactoryConfiguration findInClasspath(String path)
342 {
343
344 return findInClasspath(path, new ConfigurationUtils());
345 }
346
347
348
349
350
351
352
353
354
355
356 public static FactoryConfiguration findInClasspath(String path, Object caller)
357 {
358
359 List<URL> found = ClassUtils.getResources(path, caller);
360 if (found.isEmpty())
361 {
362 return null;
363 }
364 else if (found.size() == 1)
365 {
366
367 return read(found.get(0));
368 }
369 else
370 {
371
372 FactoryConfiguration config =
373 new FactoryConfiguration("ConfigurationUtils.findInClassPath("+path+","+caller+")");
374 boolean readAConfig = false;
375 for (URL resource : found)
376 {
377 FactoryConfiguration c = read(resource);
378 if (c != null)
379 {
380 readAConfig = true;
381 config.addConfiguration(c);
382 }
383 }
384
385 if (readAConfig)
386 {
387 return config;
388 }
389 else
390 {
391 return null;
392 }
393 }
394 }
395
396
397
398
399
400
401
402
403
404 public static FactoryConfiguration read(URL url)
405 {
406 FileFactoryConfiguration config = null;
407 String path = url.toString();
408 String source = "ConfigurationUtils.read("+url.toString()+")";
409 if (path.endsWith(".xml"))
410 {
411 config = new XmlFactoryConfiguration(source);
412 }
413 else if (path.endsWith(".properties"))
414 {
415 config = new PropertiesFactoryConfiguration(source);
416 }
417 else if (path.endsWith(".class"))
418 {
419
420 String fqn = path.substring(0, path.indexOf('.')).replace('/','.');
421
422 return getFromClass(fqn);
423 }
424 else
425 {
426 String msg = "Unknown configuration file type: " + url.toString() +
427 "\nOnly .xml and .properties configuration files are supported at this time.";
428 throw new UnsupportedOperationException(msg);
429 }
430
431
432 InputStream inputStream = null;
433 try
434 {
435 inputStream = url.openStream();
436 config.read(inputStream);
437 }
438 catch (IOException ioe)
439 {
440 return null;
441 }
442 finally
443 {
444 if (inputStream != null)
445 {
446 try
447 {
448 inputStream.close();
449 }
450 catch (IOException ioe)
451 {
452 throw new RuntimeException("Could not close input stream for "+path, ioe);
453 }
454 }
455 }
456 return config;
457 }
458
459 public static FactoryConfiguration getFromClass(String classname)
460 {
461 try
462 {
463 Class configFactory = ClassUtils.getClass(classname);
464 return getFromClass(configFactory);
465 }
466 catch (ClassNotFoundException cnfe)
467 {
468 throw new IllegalArgumentException("Could not find class "+classname, cnfe);
469 }
470 }
471
472 public static final String CONFIG_FACTORY_METHOD = "getConfiguration";
473 public static FactoryConfiguration getFromClass(Class factory)
474 {
475
476 Method getConf = null;
477 try
478 {
479
480 getConf = factory.getMethod(CONFIG_FACTORY_METHOD, (Class[])null);
481 }
482 catch (NoSuchMethodException nsme)
483 {
484 throw new IllegalArgumentException("Could not find "+CONFIG_FACTORY_METHOD+" in class "+factory.getName(), nsme);
485 }
486
487
488 Object instance = null;
489 if (!Modifier.isStatic(getConf.getModifiers()))
490 {
491 try
492 {
493 instance = factory.newInstance();
494 }
495 catch (Exception e)
496 {
497 throw new IllegalArgumentException(factory.getName()+" must have usable default constructor or else "+CONFIG_FACTORY_METHOD+" must be declared static", e);
498 }
499 }
500
501
502 try
503 {
504 FactoryConfiguration result =
505 (FactoryConfiguration)getConf.invoke(instance, (Object[])null);
506 if (result == null)
507 {
508 throw new IllegalArgumentException("Method "+CONFIG_FACTORY_METHOD+" in class "+factory.getName()+" should not return null or void");
509 }
510 else
511 {
512 return result;
513 }
514 }
515 catch (IllegalAccessException iae)
516 {
517 throw new IllegalArgumentException("Failed to invoke "+CONFIG_FACTORY_METHOD+" in class "+factory.getName(), iae);
518 }
519 catch (IllegalArgumentException iae)
520 {
521
522 throw iae;
523 }
524 catch (InvocationTargetException ite)
525 {
526
527 throw new IllegalArgumentException("There was an exception while executing "+CONFIG_FACTORY_METHOD+" in class "+factory.getName(), ite.getCause());
528 }
529 }
530
531 }