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.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import java.util.HashMap;
21  import java.util.Hashtable;
22  import java.util.Map;
23  
24  /***
25   * Main class to obtain the configuration of a software component.
26   *
27   * The main method is <code>getConfiguration</code> which must be
28   * given the name of a component.
29   *
30   * @author jferrer@germinus.com
31   */
32  public class EasyConf {
33      private static final Log log = LogFactory.getLog(EasyConf.class);
34      private static Map cache = new HashMap();
35  
36      private EasyConf() {
37      }
38  
39      /***
40       * Get the full configuration of the given component.
41       * 
42       * The configuration will be cached so that next calls will not need
43       * to reload the properties or XML files again.
44       * @param componentName any String which can be used to identified a
45       * configuration component.
46       * @return a <code>ComponentConf</code> instance 
47       */
48      public static ComponentConfiguration getConfiguration(String componentName) {
49          try {
50              ComponentConfiguration componentConf = (ComponentConfiguration)
51              cache.get(componentName);
52              if (componentConf == null) {
53                  componentConf = new ComponentConfiguration(componentName);
54                  cache.put(componentName, componentConf);
55              }
56              return componentConf;
57          } catch (ConfigurationException e) {
58              throw e;
59          } catch (Exception e) {
60              throw new ConfigurationException(componentName, 
61                      "Error reading the configuration", e); 
62          }    
63      }
64  
65      /***
66       * Get the full configuration of the given component, for the given company.
67       * This method should be used when the application is to be deployed in
68       * an ASP model where several companies want different configurations for
69       * the same running applications
70       * 
71       * 
72       * The configuration will be cached so that next calls will not need
73       * to reload the properties or XML files again.
74       * @param companyId the identifier of the company whose specific 
75       * configuration should be read
76       * @param componentName any String which can be used to identified a
77       * configuration component.
78       * @return a <code>ComponentConf</code> instance 
79       */
80      public static ComponentConfiguration getConfiguration(String companyId, 
81                                                            String componentName) {
82          try {
83              final String cacheKey = companyId + componentName;
84              ComponentConfiguration componentConf = (ComponentConfiguration)
85              cache.get(cacheKey);
86              if (componentConf == null) {
87                  componentConf = new ComponentConfiguration(companyId, componentName);
88                  cache.put(cacheKey, componentConf);
89              }
90              return componentConf;
91          } catch (ConfigurationException e) {
92              throw e;
93          } catch (Exception e) {
94              throw new ConfigurationException(componentName, 
95                      "Error reading the configuration for " + companyId, e); 
96          }
97      }
98  
99      /***
100      * Refresh the configuration of the given component
101      * 
102      * KNOWN BUG: this method does not refresh the properties configuration because the underlying
103      * library Jakarta Commons Configuration also contains a cache which is not refreshable. This 
104      * issue is scheduled to be solved after version 1.0 of such library. 
105      */
106     public static void refreshComponent(String componentName) {
107         ComponentConfiguration componentConf = (ComponentConfiguration) cache.get(componentName);
108         if (componentConf != null) {
109             cache.remove(componentName);
110             log.info("Refreshed the configuration of component " + componentName);
111         }
112     }
113     /***
114      * Refresh the configuration of all components
115      * 
116      * KNOWN BUG: this method does not refresh the properties configuration because the underlying
117      * library Jakarta Commons Configuration also contains a cache which is not refreshable. This 
118      * issue is scheduled to be solved after version 1.0 of such library. 
119      */
120     public static void refreshAll() {
121         cache = new HashMap();
122         log.info("Refreshed the configuration of all components");
123     }
124 
125     // ************************** Deprecated methods ***************************
126 
127 
128 }