1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.Map;
27
28 import org.apache.commons.configuration.plist.PropertyListConfiguration;
29 import org.apache.commons.configuration.plist.XMLPropertyListConfiguration;
30 import org.apache.commons.digester.AbstractObjectCreationFactory;
31 import org.apache.commons.digester.CallMethodRule;
32 import org.apache.commons.digester.Digester;
33 import org.apache.commons.digester.ObjectCreationFactory;
34 import org.apache.commons.digester.Substitutor;
35 import org.apache.commons.digester.substitution.MultiVariableExpander;
36 import org.apache.commons.digester.substitution.VariableSubstitutor;
37 import org.apache.commons.digester.xmlrules.DigesterLoader;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.xml.sax.Attributes;
42 import org.xml.sax.SAXException;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public class ConfigurationFactory
69 {
70
71 private static final String SEC_ROOT = "configuration/";
72
73
74 private static final String SEC_OVERRIDE = SEC_ROOT + "override/";
75
76
77 private static final String SEC_ADDITIONAL = SEC_ROOT + "additional/";
78
79
80 private static final String ATTR_OPTIONAL = "optional";
81
82
83 private static final String ATTR_FILENAME = "fileName";
84
85
86 private static final String METH_LOAD = "load";
87
88
89 private static final String DEF_BASE_PATH = ".";
90
91
92 private static Log log = LogFactory.getLog(ConfigurationFactory.class);
93
94
95 private String configurationFileName;
96
97
98 private URL configurationURL;
99
100
101
102
103
104
105 private String implicitBasePath;
106
107
108 private String basePath;
109
110
111 private URL digesterRules;
112
113
114 private String digesterRuleNamespaceURI;
115
116
117
118
119 public ConfigurationFactory()
120 {
121 setBasePath(DEF_BASE_PATH);
122 }
123
124
125
126
127
128 public ConfigurationFactory(String configurationFileName)
129 {
130 setConfigurationFileName(configurationFileName);
131 }
132
133
134
135
136
137
138
139
140
141
142
143 public Configuration getConfiguration() throws ConfigurationException
144 {
145 Digester digester;
146 InputStream input = null;
147 ConfigurationBuilder builder = new ConfigurationBuilder();
148 URL url = getConfigurationURL();
149 try
150 {
151 if (url == null)
152 {
153 url = ConfigurationUtils.locate(implicitBasePath, getConfigurationFileName());
154 }
155 input = url.openStream();
156 }
157 catch (Exception e)
158 {
159 log.error("Exception caught opening stream to URL", e);
160 throw new ConfigurationException("Exception caught opening stream to URL", e);
161 }
162
163 if (getDigesterRules() == null)
164 {
165 digester = new Digester();
166 configureNamespace(digester);
167 initDefaultDigesterRules(digester);
168 }
169 else
170 {
171 digester = DigesterLoader.createDigester(getDigesterRules());
172
173
174 configureNamespace(digester);
175 }
176
177
178 digester.setUseContextClassLoader(true);
179
180 enableDigesterSubstitutor(digester);
181
182 digester.push(builder);
183
184 try
185 {
186 digester.parse(input);
187 input.close();
188 }
189 catch (SAXException saxe)
190 {
191 log.error("SAX Exception caught", saxe);
192 throw new ConfigurationException("SAX Exception caught", saxe);
193 }
194 catch (IOException ioe)
195 {
196 log.error("IO Exception caught", ioe);
197 throw new ConfigurationException("IO Exception caught", ioe);
198 }
199 return builder.getConfiguration();
200 }
201
202
203
204
205
206
207 public String getConfigurationFileName()
208 {
209 return configurationFileName;
210 }
211
212
213
214
215
216
217 public void setConfigurationFileName(String configurationFileName)
218 {
219 File file = new File(configurationFileName).getAbsoluteFile();
220 this.configurationFileName = file.getName();
221 implicitBasePath = file.getParent();
222 }
223
224
225
226
227
228
229 public URL getConfigurationURL()
230 {
231 return configurationURL;
232 }
233
234
235
236
237
238
239
240 public void setConfigurationURL(URL url)
241 {
242 configurationURL = url;
243 implicitBasePath = url.toString();
244 }
245
246
247
248
249
250
251 public URL getDigesterRules()
252 {
253 return digesterRules;
254 }
255
256
257
258
259
260
261 public void setDigesterRules(URL digesterRules)
262 {
263 this.digesterRules = digesterRules;
264 }
265
266
267
268
269
270
271 protected void enableDigesterSubstitutor(Digester digester)
272 {
273
274
275
276 @SuppressWarnings("unchecked")
277 Map<String, Object> systemProperties =
278 (Map<String, Object>) (Object) System.getProperties();
279 MultiVariableExpander expander = new MultiVariableExpander();
280 expander.addSource("$", systemProperties);
281
282
283 Substitutor substitutor = new VariableSubstitutor(expander);
284 digester.setSubstitutor(substitutor);
285 }
286
287
288
289
290
291
292
293
294
295
296 protected void initDefaultDigesterRules(Digester digester)
297 {
298 initDigesterSectionRules(digester, SEC_ROOT, false);
299 initDigesterSectionRules(digester, SEC_OVERRIDE, false);
300 initDigesterSectionRules(digester, SEC_ADDITIONAL, true);
301 }
302
303
304
305
306
307
308
309
310
311
312 protected void initDigesterSectionRules(Digester digester, String matchString, boolean additional)
313 {
314 setupDigesterInstance(
315 digester,
316 matchString + "properties",
317 new PropertiesConfigurationFactory(),
318 METH_LOAD,
319 additional);
320
321 setupDigesterInstance(
322 digester,
323 matchString + "plist",
324 new PropertyListConfigurationFactory(),
325 METH_LOAD,
326 additional);
327
328 setupDigesterInstance(
329 digester,
330 matchString + "xml",
331 new FileConfigurationFactory(XMLConfiguration.class),
332 METH_LOAD,
333 additional);
334
335 setupDigesterInstance(
336 digester,
337 matchString + "hierarchicalXml",
338 new FileConfigurationFactory(XMLConfiguration.class),
339 METH_LOAD,
340 additional);
341
342 setupDigesterInstance(
343 digester,
344 matchString + "jndi",
345 new JNDIConfigurationFactory(),
346 null,
347 additional);
348
349 setupDigesterInstance(
350 digester,
351 matchString + "system",
352 new SystemConfigurationFactory(),
353 null,
354 additional);
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368 protected void setupDigesterInstance(
369 Digester digester,
370 String matchString,
371 ObjectCreationFactory factory,
372 String method,
373 boolean additional)
374 {
375 if (additional)
376 {
377 setupUnionRules(digester, matchString);
378 }
379
380 digester.addFactoryCreate(matchString, factory);
381 digester.addSetProperties(matchString);
382
383 if (method != null)
384 {
385 digester.addRule(matchString, new CallOptionalMethodRule(method));
386 }
387
388 digester.addSetNext(matchString, "addConfiguration", Configuration.class.getName());
389 }
390
391
392
393
394
395
396
397 protected void setupUnionRules(Digester digester, String matchString)
398 {
399 digester.addObjectCreate(matchString,
400 AdditionalConfigurationData.class);
401 digester.addSetProperties(matchString);
402 digester.addSetNext(matchString, "addAdditionalConfig",
403 AdditionalConfigurationData.class.getName());
404 }
405
406
407
408
409
410
411 public String getDigesterRuleNamespaceURI()
412 {
413 return digesterRuleNamespaceURI;
414 }
415
416
417
418
419
420
421 public void setDigesterRuleNamespaceURI(String digesterRuleNamespaceURI)
422 {
423 this.digesterRuleNamespaceURI = digesterRuleNamespaceURI;
424 }
425
426
427
428
429
430
431
432
433 private void configureNamespace(Digester digester)
434 {
435 if (getDigesterRuleNamespaceURI() != null)
436 {
437 digester.setNamespaceAware(true);
438 digester.setRuleNamespaceURI(getDigesterRuleNamespaceURI());
439 }
440 else
441 {
442 digester.setNamespaceAware(false);
443 }
444 digester.setValidating(false);
445 }
446
447
448
449
450
451
452
453
454 public String getBasePath()
455 {
456 String path = StringUtils.isEmpty(basePath)
457 || DEF_BASE_PATH.equals(basePath) ? implicitBasePath : basePath;
458 return StringUtils.isEmpty(path) ? DEF_BASE_PATH : path;
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472 public void setBasePath(String basePath)
473 {
474 this.basePath = basePath;
475 }
476
477
478
479
480
481
482 public class DigesterConfigurationFactory extends AbstractObjectCreationFactory
483 {
484
485 private Class<?> clazz;
486
487
488
489
490
491
492 public DigesterConfigurationFactory(Class<?> clazz)
493 {
494 this.clazz = clazz;
495 }
496
497
498
499
500
501
502
503
504 @Override
505 public Object createObject(Attributes attribs) throws Exception
506 {
507 return clazz.newInstance();
508 }
509 }
510
511
512
513
514
515
516
517 public class FileConfigurationFactory extends DigesterConfigurationFactory
518 {
519
520
521
522
523
524 public FileConfigurationFactory(Class<?> clazz)
525 {
526 super(clazz);
527 }
528
529
530
531
532
533
534
535
536 @Override
537 public Object createObject(Attributes attributes) throws Exception
538 {
539 FileConfiguration conf = createConfiguration(attributes);
540 conf.setBasePath(getBasePath());
541 return conf;
542 }
543
544
545
546
547
548
549
550
551 protected FileConfiguration createConfiguration(Attributes attributes) throws Exception
552 {
553 return (FileConfiguration) super.createObject(attributes);
554 }
555 }
556
557
558
559
560
561
562
563 public class PropertiesConfigurationFactory extends FileConfigurationFactory
564 {
565
566
567
568 public PropertiesConfigurationFactory()
569 {
570 super(null);
571 }
572
573
574
575
576
577
578
579
580
581
582
583 @Override
584 protected FileConfiguration createConfiguration(Attributes attributes) throws Exception
585 {
586 String filename = attributes.getValue(ATTR_FILENAME);
587
588 if (filename != null && filename.toLowerCase().trim().endsWith(".xml"))
589 {
590 return new XMLPropertiesConfiguration();
591 }
592 else
593 {
594 return new PropertiesConfiguration();
595 }
596 }
597 }
598
599
600
601
602
603
604
605 public class PropertyListConfigurationFactory extends FileConfigurationFactory
606 {
607
608
609
610 public PropertyListConfigurationFactory()
611 {
612 super(null);
613 }
614
615
616
617
618
619
620
621
622
623
624
625 @Override
626 protected FileConfiguration createConfiguration(Attributes attributes) throws Exception
627 {
628 String filename = attributes.getValue(ATTR_FILENAME);
629
630 if (filename != null && filename.toLowerCase().trim().endsWith(".xml"))
631 {
632 return new XMLPropertyListConfiguration();
633 }
634 else
635 {
636 return new PropertyListConfiguration();
637 }
638 }
639 }
640
641
642
643
644
645 private class JNDIConfigurationFactory extends DigesterConfigurationFactory
646 {
647
648
649
650 public JNDIConfigurationFactory()
651 {
652 super(JNDIConfiguration.class);
653 }
654 }
655
656
657
658
659
660 private class SystemConfigurationFactory extends DigesterConfigurationFactory
661 {
662
663
664
665 public SystemConfigurationFactory()
666 {
667 super(SystemConfiguration.class);
668 }
669 }
670
671
672
673
674
675 public static class AdditionalConfigurationData
676 {
677
678 private Configuration configuration;
679
680
681 private String at;
682
683
684
685
686
687
688 public String getAt()
689 {
690 return at;
691 }
692
693
694
695
696
697
698 public void setAt(String string)
699 {
700 at = string;
701 }
702
703
704
705
706
707
708 public Configuration getConfiguration()
709 {
710 return configuration;
711 }
712
713
714
715
716
717
718
719
720
721 public void addConfiguration(Configuration config)
722 {
723 configuration = config;
724 }
725 }
726
727
728
729
730
731 public static class ConfigurationBuilder
732 {
733
734 private CompositeConfiguration config;
735
736
737 private Collection<AdditionalConfigurationData> additionalConfigs;
738
739
740
741
742 public ConfigurationBuilder()
743 {
744 config = new CompositeConfiguration();
745 additionalConfigs = new LinkedList<AdditionalConfigurationData>();
746 }
747
748
749
750
751
752
753
754 public void addConfiguration(Configuration conf)
755 {
756 config.addConfiguration(conf);
757 }
758
759
760
761
762
763
764
765 public void addAdditionalConfig(AdditionalConfigurationData data)
766 {
767 additionalConfigs.add(data);
768 }
769
770
771
772
773
774
775 public CompositeConfiguration getConfiguration()
776 {
777 if (!additionalConfigs.isEmpty())
778 {
779 Configuration unionConfig = createAdditionalConfiguration(additionalConfigs);
780 if (unionConfig != null)
781 {
782 addConfiguration(unionConfig);
783 }
784 additionalConfigs.clear();
785 }
786
787 return config;
788 }
789
790
791
792
793
794
795
796
797
798
799
800 protected Configuration createAdditionalConfiguration(Collection<AdditionalConfigurationData> configs)
801 {
802 HierarchicalConfiguration result = new HierarchicalConfiguration();
803
804 for (AdditionalConfigurationData cdata : configs)
805 {
806 result.addNodes(cdata.getAt(),
807 createRootNode(cdata).getChildren());
808 }
809
810 return result.isEmpty() ? null : result;
811 }
812
813
814
815
816
817
818
819 private HierarchicalConfiguration.Node createRootNode(AdditionalConfigurationData cdata)
820 {
821 if (cdata.getConfiguration() instanceof HierarchicalConfiguration)
822 {
823
824 return ((HierarchicalConfiguration) cdata.getConfiguration()).getRoot();
825 }
826 else
827 {
828
829 HierarchicalConfiguration hc = new HierarchicalConfiguration();
830 ConfigurationUtils.copy(cdata.getConfiguration(), hc);
831 return hc.getRoot();
832 }
833 }
834 }
835
836
837
838
839
840
841
842
843
844
845
846 private static class CallOptionalMethodRule extends CallMethodRule
847 {
848
849 private boolean optional;
850
851
852
853
854
855
856
857 public CallOptionalMethodRule(String methodName)
858 {
859 super(methodName);
860 }
861
862
863
864
865
866
867
868 @Override
869 public void begin(Attributes attrs) throws Exception
870 {
871 optional = attrs.getValue(ATTR_OPTIONAL) != null
872 && PropertyConverter.toBoolean(
873 attrs.getValue(ATTR_OPTIONAL)).booleanValue();
874 super.begin(attrs);
875 }
876
877
878
879
880
881
882
883 @Override
884 public void end() throws Exception
885 {
886 try
887 {
888 super.end();
889 }
890 catch (Exception ex)
891 {
892 if (optional)
893 {
894 log.warn("Could not create optional configuration!", ex);
895 }
896 else
897 {
898 throw ex;
899 }
900 }
901 }
902 }
903 }