1 package org.reactive.configuration;
2
3 import org.springframework.beans.factory.config.AbstractFactoryBean;
4 import org.springframework.core.io.Resource;
5 import org.apache.commons.configuration.*;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import java.util.List;
10 import java.util.Set;
11 import java.util.Iterator;
12 import java.io.IOException;
13
14 /***
15 * A factory bean used to create instances of commons-configuration's composite configurations from a variety of resources.
16 * <p>An example of the factory bean configuration required for Commons Configuration</p>
17 * <pre>
18 * <bean id="configuration" class="org.reactive.configuration.CommonsConfigurationFactoryBean">
19 * <property name="xmlResources">
20 * <set>
21 * <bean class="org.springframework.beans.factory.config.ResourceFactoryBean">
22 * <property name="location">
23 * <value>classpath:/org/reactive/beans/factory/config/ConfigurationPlaceholderProcessorTest_placeholders.xml</value>
24 * </property>
25 * </bean>
26 * </set>
27 * </property>
28 * <property name="propertiesResources">
29 * <set>
30 * <bean class="org.springframework.beans.factory.config.ResourceFactoryBean">
31 * <property name="location">
32 * <value>classpath:/org/reactive/beans/factory/config/ConfigurationPlaceholderProcessorTest_placeholders.properties</value>
33 * </property>
34 * </bean>
35 * </set>
36 * </property>
37 * </bean>
38 * </pre>
39 *
40 * @author Dan Washusen
41 * @version $Id: CommonsConfigurationFactoryBean.java,v 1.3 2005/06/16 07:02:08 dan_washusen Exp $
42 * @see org.springframework.core.io.DefaultResourceLoader
43 * @since 9.6.2005
44 * @todo A lot of code duplication when adding configuration resources, clean it up.
45 */
46 public class CommonsConfigurationFactoryBean
47 extends AbstractFactoryBean {
48 private static Log log = LogFactory.getLog(CommonsConfigurationFactoryBean.class);
49
50 /***
51 * A list of resources used to create xml based configurations.
52 */
53 private Set xmlResources;
54 /***
55 * A list of resources used to create xml properties based configurations.
56 */
57 private Set xmlPropertiesResources;
58 /***
59 * A list of resources used to create properties based configurations.
60 */
61 private Set propertiesResources;
62 /***
63 * The prefix used when looking up JNDI entries.
64 */
65 private String jndiPrefix;
66 /***
67 * A boolean flag determining if the JNDI configuration should be added to the composite configuration.
68 */
69 private boolean isJNDIConfigurationEnabled = false;
70
71 /***
72 * Sets the prefix used when querying a JNDI context.
73 * @param jndiPrefix the prefix used when querying a JNDI context
74 */
75 public void setJndiPrefix(String jndiPrefix) {
76 this.jndiPrefix = jndiPrefix;
77 }
78
79 /***
80 * Sets the boolean flag determining if the JNDI configuration should be added to the composite configuration
81 * @param JNDIConfigurationEnabled true if JNDI configuration should be enabled, otherwise false
82 */
83 public void setJNDIConfigurationEnabled(boolean JNDIConfigurationEnabled) {
84 isJNDIConfigurationEnabled = JNDIConfigurationEnabled;
85 }
86
87 /***
88 * Sets a set of resources used to create properties based configurations.
89 * @param propertiesResources a list of resources used to create properties based configurations
90 */
91 public void setPropertiesResources(Set propertiesResources) {
92 this.propertiesResources = propertiesResources;
93 }
94
95 /***
96 * Sets a ste of resources used to create xml based configurations.
97 * @param xmlResources a list of resources used to create xml based configurations
98 */
99 public void setXmlResources(Set xmlResources) {
100 this.xmlResources = xmlResources;
101 }
102
103 /***
104 * Sets a set of resources used to create xml properies configuration.
105 * @param xmlPropertiesResources a list of resources used to create xml based configurations
106 */
107 public void setXmlPropertiesResources(Set xmlPropertiesResources) {
108 this.xmlPropertiesResources = xmlPropertiesResources;
109 }
110
111 /***
112 * Creates an instance of composite configuration based using the resources provided by
113 * {@link #setPropertiesResources(java.util.Set)}, {@link #setXmlPropertiesResources(java.util.Set)} and
114 * {@link #setXmlResources(java.util.Set)}.
115 * @return An instance of commons-configurations composite configuratoin
116 * @throws Exception If an error occurrs while creating the configuration from the provided resources
117 */
118 protected Object createInstance()
119 throws Exception {
120 CompositeConfiguration configuration = new CompositeConfiguration();
121
122
123 addXMLConfigurationResources(configuration);
124 addXMLPropertiesConfigurationResources(configuration);
125 addPropertiesConfigurationResources(configuration);
126
127
128 if (isJNDIConfigurationEnabled) {
129 log.info("JDNI Configuration enabled");
130 JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
131
132 if (jndiPrefix != null || jndiPrefix.length() != 0) {
133 jndiConfiguration.setPrefix(jndiPrefix);
134 }
135 configuration.addConfiguration(jndiConfiguration);
136 }
137
138 return configuration;
139 }
140
141 public Class getObjectType() {
142 return Configuration.class;
143 }
144
145 /***
146 * Add all the XML Configuration resources to the compositeConfiguration.
147 * @param compositeConfiguration The composite compositeConfiguration
148 * @throws ConfigurationException If thrown by {@link CompositeConfiguration#addConfiguration(org.apache.commons.configuration.Configuration)}.
149 */
150 private void addXMLConfigurationResources(CompositeConfiguration compositeConfiguration)
151 throws ConfigurationException
152 {
153 if (xmlResources == null)
154 return;
155
156 for (Iterator iterator = xmlResources.iterator(); iterator.hasNext();) {
157 Resource resource = (Resource) iterator.next();
158
159
160 if (!resource.exists())
161 log.warn("XML configuration resource: '" + resource + "' doesn't exist.");
162
163
164 XMLConfiguration configuration = new XMLConfiguration();
165 try {
166 configuration.load(resource.getInputStream());
167 } catch (IOException e) {
168 log.error("Could not load load XML configuration resource: '" + resource + "'");
169 continue;
170 }
171
172 log.debug("Adding XML configuration using resource: '" + resource + "'");
173 compositeConfiguration.addConfiguration(
174 configuration
175 );
176 }
177 }
178
179 /***
180 * Add all the XML Properties Configuration resources to the compositeConfiguration.
181 * @param compositeConfiguration The composite compositeConfiguration
182 * @throws ConfigurationException If thrown by {@link CompositeConfiguration#addConfiguration(org.apache.commons.configuration.Configuration)}.
183 */
184 private void addXMLPropertiesConfigurationResources(CompositeConfiguration compositeConfiguration)
185 throws ConfigurationException
186 {
187 if (xmlPropertiesResources == null)
188 return;
189
190 for (Iterator iterator = xmlPropertiesResources.iterator(); iterator.hasNext();) {
191 Resource resource = (Resource) iterator.next();
192
193
194 if (!resource.exists())
195 log.warn("XML configuration resource: '" + resource + "' doesn't exist.");
196
197
198 XMLPropertiesConfiguration configuration = new XMLPropertiesConfiguration();
199 try {
200 configuration.load(resource.getInputStream());
201 } catch (IOException e) {
202 log.error("Could not load load XML properties configuration resource: '" + resource + "'");
203 continue;
204 }
205
206 log.debug("Adding XML properties configuration using resource: '" + resource + "'");
207 compositeConfiguration.addConfiguration(
208 configuration
209 );
210 }
211 }
212
213
214 /***
215 * Add all the Properties Configuration resources to the compositeConfiguration.
216 * @param compositeConfiguration The composite compositeConfiguration
217 * @throws ConfigurationException If thrown by {@link CompositeConfiguration#addConfiguration(org.apache.commons.configuration.Configuration)}.
218 */
219 private void addPropertiesConfigurationResources(CompositeConfiguration compositeConfiguration)
220 throws ConfigurationException
221 {
222 if (propertiesResources == null)
223 return;
224
225 for (Iterator iterator = propertiesResources.iterator(); iterator.hasNext();) {
226 Resource resource = (Resource) iterator.next();
227
228
229 if (!resource.exists())
230 log.warn("XML configuration resource: '" + resource + "' doesn't exist.");
231
232
233 PropertiesConfiguration configuration = new PropertiesConfiguration();
234 try {
235 configuration.load(resource.getInputStream());
236 } catch (IOException e) {
237 log.error("Could not load load XML properties configuration resource: '" + resource + "'");
238 continue;
239 }
240
241 log.debug("Adding XML properties configuration using resource: '" + resource + "'");
242 compositeConfiguration.addConfiguration(
243 configuration
244 );
245 }
246 }
247 }