You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by de...@apache.org on 2016/05/08 18:06:08 UTC
svn commit: r1742847 - in /commons/proper/configuration/trunk: ./
src/main/java/org/apache/commons/configuration2/spring/
src/test/java/org/apache/commons/configuration2/spring/ src/test/resources/
Author: deki
Date: Sun May 8 18:06:07 2016
New Revision: 1742847
URL: http://svn.apache.org/viewvc?rev=1742847&view=rev
Log:
[CONFIGURATION-624] added config factory bean
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigFactoryBean.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigFactoryBeanTest.java
commons/proper/configuration/trunk/src/test/resources/testConfigurationFactoryBean.file
Modified:
commons/proper/configuration/trunk/build.xml
commons/proper/configuration/trunk/pom.xml
Modified: commons/proper/configuration/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/build.xml?rev=1742847&r1=1742846&r2=1742847&view=diff
==============================================================================
--- commons/proper/configuration/trunk/build.xml (original)
+++ commons/proper/configuration/trunk/build.xml Sun May 8 18:06:07 2016
@@ -72,6 +72,7 @@
<pathelement location="${lib}/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar"/>
<pathelement location="${lib}/log4j/log4j/1.2.8/log4j-1.2.8.jar"/>
<pathelement location="${lib}/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar"/>
+ <pathelement location="${lib}/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar"/>
</path>
<path id="build.test.classpath">
<pathelement location="${lib}/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar"/>
Modified: commons/proper/configuration/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/pom.xml?rev=1742847&r1=1742846&r2=1742847&view=diff
==============================================================================
--- commons/proper/configuration/trunk/pom.xml (original)
+++ commons/proper/configuration/trunk/pom.xml Sun May 8 18:06:07 2016
@@ -302,6 +302,13 @@
<dependency>
<groupId>org.springframework</groupId>
+ <artifactId>spring-beans</artifactId>
+ <version>${spring.version}</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<optional>true</optional>
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigFactoryBean.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigFactoryBean.java?rev=1742847&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigFactoryBean.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigFactoryBean.java Sun May 8 18:06:07 2016
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.configuration2.spring;
+
+import org.apache.commons.configuration2.CompositeConfiguration;
+import org.apache.commons.configuration2.Configuration;
+import org.apache.commons.configuration2.ConfigurationConverter;
+import org.apache.commons.configuration2.builder.fluent.Configurations;
+import org.apache.commons.lang3.ArrayUtils;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.core.io.Resource;
+import org.springframework.util.Assert;
+
+import java.net.URL;
+import java.util.Properties;
+
+/**
+ * FactoryBean which wraps a Commons CompositeConfiguration object for usage
+ * with PropertiesLoaderSupport. This allows the compositeConfiguration object to behave
+ * like a normal java.util.Properties object which can be passed on to
+ * setProperties() method allowing PropertyOverrideConfigurer and
+ * PropertyPlaceholderConfigurer to take advantage of Commons Configuration.
+ * <p/> Internally a CompositeConfiguration object is used for merging multiple
+ * Configuration objects.
+ *
+ * @see java.util.Properties
+ * @see org.springframework.core.io.support.PropertiesLoaderSupport
+ *
+ */
+public class ConfigFactoryBean implements InitializingBean, FactoryBean<Properties>
+{
+
+ /** internal CompositeConfiguration containing the merged configuration objects **/
+ private CompositeConfiguration compositeConfiguration;
+
+ /** supplied configurations that will be merged in compositeConfiguration **/
+ private Configuration[] configurations;
+
+ /** Spring resources for loading configurations **/
+ private Resource[] locations;
+
+ /** @see org.apache.commons.configuration2.AbstractConfiguration#throwExceptionOnMissing **/
+ private boolean throwExceptionOnMissing = true;
+
+ public ConfigFactoryBean()
+ {
+ }
+
+ public ConfigFactoryBean(Configuration configuration)
+ {
+ Assert.notNull(configuration);
+ this.compositeConfiguration = new CompositeConfiguration(configuration);
+ }
+
+ /**
+ * @see org.springframework.beans.factory.FactoryBean#getObject()
+ */
+ @Override
+ public Properties getObject() throws Exception
+ {
+ return (compositeConfiguration != null) ? ConfigurationConverter.getProperties(compositeConfiguration) : null;
+ }
+
+ /**
+ * @see org.springframework.beans.factory.FactoryBean#getObjectType()
+ */
+ @Override
+ public Class getObjectType()
+ {
+ return java.util.Properties.class;
+ }
+
+ /**
+ * @see org.springframework.beans.factory.FactoryBean#isSingleton()
+ */
+ @Override
+ public boolean isSingleton()
+ {
+ return true;
+ }
+
+ /**
+ * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
+ */
+ @Override
+ public void afterPropertiesSet() throws Exception
+ {
+ if (compositeConfiguration == null && ArrayUtils.isEmpty(configurations) && ArrayUtils.isEmpty(locations))
+ {
+ throw new IllegalArgumentException("no configuration object or location specified");
+ }
+
+ if (compositeConfiguration == null)
+ {
+ compositeConfiguration = new CompositeConfiguration();
+ }
+
+ compositeConfiguration.setThrowExceptionOnMissing(throwExceptionOnMissing);
+
+ if (configurations != null)
+ {
+ for (Configuration configuration : configurations)
+ {
+ compositeConfiguration.addConfiguration(configuration);
+ }
+ }
+
+ if (locations != null)
+ {
+ for (Resource location : locations)
+ {
+ URL url = location.getURL();
+ Configuration props = new Configurations().properties(url);
+ compositeConfiguration.addConfiguration(props);
+ }
+ }
+ }
+
+ public Configuration[] getConfigurations()
+ {
+ return configurations;
+ }
+
+ /**
+ * Set the commons configurations objects which will be used as properties.
+ *
+ * @param configurations commons configurations objects which will be used as properties.
+ */
+ public void setConfigurations(Configuration[] configurations)
+ {
+ this.configurations = configurations;
+ }
+
+ public Resource[] getLocations()
+ {
+ return locations;
+ }
+
+ /**
+ * Shortcut for loading compositeConfiguration from Spring resources. It will
+ * internally create a PropertiesConfiguration object based on the URL
+ * retrieved from the given Resources.
+ *
+ * @param locations resources of configuration files
+ */
+ public void setLocations(Resource[] locations)
+ {
+ this.locations = locations;
+ }
+
+ public boolean isThrowExceptionOnMissing()
+ {
+ return throwExceptionOnMissing;
+ }
+
+ /**
+ * Set the underlying Commons CompositeConfiguration throwExceptionOnMissing flag.
+ *
+ * @see org.apache.commons.configuration2.AbstractConfiguration#setThrowExceptionOnMissing(boolean)
+ * @param throwExceptionOnMissing The new value for the property
+ */
+ public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
+ {
+ this.throwExceptionOnMissing = throwExceptionOnMissing;
+ }
+
+ public CompositeConfiguration getConfiguration()
+ {
+ return compositeConfiguration;
+ }
+
+}
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigFactoryBeanTest.java?rev=1742847&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigFactoryBeanTest.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigFactoryBeanTest.java Sun May 8 18:06:07 2016
@@ -0,0 +1,75 @@
+package org.apache.commons.configuration2.spring;
+
+import org.apache.commons.configuration2.BaseConfiguration;
+import org.apache.commons.configuration2.Configuration;
+import org.apache.commons.configuration2.PropertiesConfiguration;
+import org.apache.commons.configuration2.PropertiesConfigurationLayout;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import java.io.StringReader;
+import java.util.Properties;
+
+/**
+ * Spring FactoryBean test.
+ *
+ */
+public class ConfigFactoryBeanTest {
+
+ private ConfigFactoryBean configurationFactory;
+
+ @Before
+ public void setUp() {
+ configurationFactory = new ConfigFactoryBean();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testAfterPropertiesSet() throws Exception {
+ configurationFactory.afterPropertiesSet();
+ }
+
+ @Test
+ public void testGetObject() throws Exception {
+ configurationFactory.setConfigurations(new Configuration[] { new BaseConfiguration() });
+ Assert.assertNull(configurationFactory.getObject());
+ configurationFactory.afterPropertiesSet();
+ Assert.assertNotNull(configurationFactory.getObject());
+ }
+
+ @Test
+ public void testMergeConfigurations() throws Exception {
+ Configuration one = new BaseConfiguration();
+ one.setProperty("foo", "bar");
+ String properties = "## some header \n" + "foo = bar1\n" + "bar = foo\n";
+
+ PropertiesConfiguration two = new PropertiesConfiguration();
+ PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
+ layout.load(two, new StringReader(properties));
+
+ configurationFactory.setConfigurations(new Configuration[] { one, two });
+ configurationFactory.afterPropertiesSet();
+ Properties props = configurationFactory.getObject();
+ Assert.assertEquals("foo", props.getProperty("bar"));
+ Assert.assertEquals("bar", props.getProperty("foo"));
+ }
+
+ @Test
+ public void testLoadResources() throws Exception {
+ configurationFactory.setLocations(new Resource[] { new ClassPathResource("testConfigurationFactoryBean.file") });
+ configurationFactory.setConfigurations(new Configuration[] { new BaseConfiguration() });
+ configurationFactory.afterPropertiesSet();
+
+ Properties props = configurationFactory.getObject();
+ Assert.assertEquals("duke", props.getProperty("java"));
+ }
+
+ @Test
+ public void testInitialConfiguration() throws Exception {
+ configurationFactory = new ConfigFactoryBean(new BaseConfiguration());
+ configurationFactory.afterPropertiesSet();
+ Assert.assertNotNull(configurationFactory.getConfiguration());
+ }
+}
Added: commons/proper/configuration/trunk/src/test/resources/testConfigurationFactoryBean.file
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testConfigurationFactoryBean.file?rev=1742847&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testConfigurationFactoryBean.file (added)
+++ commons/proper/configuration/trunk/src/test/resources/testConfigurationFactoryBean.file Sun May 8 18:06:07 2016
@@ -0,0 +1,2 @@
+robin=hood
+java=duke