View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.germinus.easyconf;
17  
18  import org.apache.commons.digester.Digester;
19  import org.apache.commons.digester.Substitutor;
20  import org.apache.commons.digester.substitution.MultiVariableExpander;
21  import org.apache.commons.digester.substitution.VariableSubstitutor;
22  import org.apache.commons.digester.xmlrules.DigesterLoader;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.xml.sax.SAXException;
26  
27  import java.io.IOException;
28  import java.io.FileNotFoundException;
29  import java.net.URL;
30  
31  /***
32   * Handles the actual reading of the configuration
33   * 
34   * @author jferrer
35   */
36  class ConfigurationLoader {
37      private static final Log log = LogFactory.getLog(ConfigurationLoader.class);
38  
39  
40      public ComponentProperties readPropertiesConfiguration(String companyId, String componentName) {
41          AggregatedProperties properties = new AggregatedProperties(companyId, componentName);
42  //        if (companyId != null) {
43  //            properties.addGlobalFileName(Conventions.GLOBAL_CONFIGURATION_FILE + Conventions.SLASH + companyId
44  //                    + Conventions.PROPERTIES_EXTENSION);
45  //        }
46          properties.addGlobalFileName(Conventions.GLOBAL_CONFIGURATION_FILE + 
47                  Conventions.PROPERTIES_EXTENSION);
48  //        if (companyId != null) {
49  //            properties.addBaseFileName(componentName + Conventions.SLASH + companyId + 
50  //                    Conventions.PROPERTIES_EXTENSION);
51  //        }
52          properties.addBaseFileName(componentName + Conventions.PROPERTIES_EXTENSION);
53  
54          log.info("Properties for " + componentName + " loaded from " + properties.loadedSources());
55          return new ComponentProperties(properties);
56      }
57  
58      public ConfigurationObjectCache readConfigurationObject(String companyId,
59              String componentName, ComponentProperties properties) 
60      	throws IOException, SAXException {
61          log.info("Reading the configuration object for " + componentName);
62  
63          String confFileName = null;
64          URL confFile = null;
65          if (companyId != null) {
66              confFileName = componentName + Conventions.SLASH + companyId + Conventions.XML_EXTENSION;
67              confFile = ClasspathUtil.locateResource(null, confFileName);
68              log.info("Loaded " + confFileName + ": " + confFile);
69          }
70          if (confFile == null) { 
71  	        confFileName = componentName + Conventions.XML_EXTENSION;
72  	        confFile = ClasspathUtil.locateResource(null, confFileName);
73          }
74          if (confFile == null) {
75              throw new FileNotFoundException("File " + confFileName + " not found");
76          }
77          Object confObj = loadXMLFile(confFile, properties);
78          ConfigurationObjectCache result = new ConfigurationObjectCache(confObj, confFile, properties);
79          Long delay = properties.getDelayPeriod();
80          if (delay != null) {
81              result.setReloadingStrategy(
82                      new FileURLChangedReloadingStrategy(confFile, delay.longValue()));
83          }
84          return result;
85  
86      }
87  
88      /***
89       * Read an XML file and return an Object representation of its contents
90       */	
91      Object loadXMLFile(URL confFileUrl, ComponentProperties properties) 
92      	throws IOException, SAXException {
93          log.debug("Loading XML file: " + confFileUrl);
94          String componentName = properties.getComponentName();
95          String rulesFileName = componentName + Conventions.DIGESTERRULES_EXTENSION;
96          URL digesterRulesUrl = ClasspathUtil.locateResource(rulesFileName);
97          if (digesterRulesUrl == null) {
98              throw new DigesterRulesNotFoundException(componentName,
99                      rulesFileName);
100         }
101         Digester digester = DigesterLoader.createDigester(digesterRulesUrl);
102         digester.setUseContextClassLoader(true);
103         digester.setValidating(false);
104         
105         MultiVariableExpander expander = new MultiVariableExpander();
106         expander.addSource("$", properties.toMap());
107         Substitutor substitutor = new VariableSubstitutor(expander);
108         digester.setSubstitutor(substitutor);
109         
110         try {
111             Object confObj = digester.parse(confFileUrl.openStream());
112             log.info("Read configuration from " + confFileUrl);
113             return confObj;
114         } catch (IllegalArgumentException e) {
115             //FIXME: it does not catch the exception
116             throw new InvalidPropertyException(properties.getComponentName(), e);
117         }
118     }
119 
120 }