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.xml.sax.SAXException;
19  import java.io.IOException;
20  
21  /***
22   * Contains the configuration of an EasyConf component including properties 
23   * configuration and an object graph configuration.
24   *
25   * @author  Jorge Ferrer
26   * @version $Revision: 1.9 $
27   *
28   */
29  public class ComponentConfiguration {
30  
31      private ComponentProperties properties;
32      private String componentName;
33      private ConfigurationLoader confManager = new ConfigurationLoader();
34      private String companyId;
35      private ConfigurationObjectCache configurationObjectCache;
36  
37      public ComponentConfiguration(String componentName) {
38          this(null, componentName);
39      }
40      
41      public ComponentConfiguration(String companyId, String componentName) {
42          this.companyId = companyId;
43          this.componentName = componentName;
44      }
45  
46      /***
47       * Get the name of the component which is associated with this configuration
48       */
49      public String getComponentName() {
50          return componentName;
51      }
52      
53      /***
54       * Get an object which represents the configuration of the given component
55       * 
56       * The object is populated using the digester rules defined in the file
57       * componentName.digesterRules.xml which must be found in the classpath (first it is
58       * searched in the context of the current thread and then in the context of the system
59       * classpath)
60       * @throws ConfigurationException if the object graph cannot be read
61       */
62      public Object getConfigurationObject() {
63          if (configurationObjectCache != null) {
64              return configurationObjectCache.getConfigurationObject();
65          }
66          try {
67              configurationObjectCache = getConfigurationManager().
68                      readConfigurationObject(companyId,
69                                              componentName, 
70                                              getAvailableProperties());
71          } catch (IOException e) {
72              throw new ConfigurationException(componentName, "Error reading object configuration", e);
73          } catch (SAXException e) {
74              throw new ConfigurationException(componentName, "Error parsing the XML file", e);
75          }
76          return configurationObjectCache.getConfigurationObject();
77      }
78  
79      private ConfigurationLoader getConfigurationManager() {
80          return confManager;
81      }
82  
83      /***
84       * Get a typed map of the properties associated with this component
85       */
86      public ComponentProperties getProperties() {
87          ComponentProperties properties = getAvailableProperties();
88          if ((!properties.hasBaseConfiguration())){
89              String msg = "The base properties file was not found";
90              throw new ConfigurationNotFoundException(componentName, msg);
91          }
92          return properties;
93      }
94  
95      private ComponentProperties getAvailableProperties() {
96          if (properties != null) {
97              return properties;
98          }
99          properties = getConfigurationManager().
100         	readPropertiesConfiguration(companyId, componentName);
101         return properties;
102     }
103 
104     
105 	public boolean equals(Object obj) {
106 		if (!(obj instanceof ComponentConfiguration)) {
107 			return false;
108 		}
109 		if (this == obj) {
110 			return true;
111 		}
112 		ComponentConfiguration cconf = (ComponentConfiguration) obj;
113 		return componentName.equals(cconf.getComponentName());
114 	}
115 	
116 	public int hashCode() {
117 		return componentName.hashCode();
118 	}
119 }