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/07 10:01:16 UTC

svn commit: r1742698 - in /commons/proper/configuration/trunk: ./ src/changes/ src/main/java/org/apache/commons/configuration2/spring/ src/site/xdoc/ src/test/java/org/apache/commons/configuration2/spring/

Author: deki
Date: Sat May  7 10:01:15 2016
New Revision: 1742698

URL: http://svn.apache.org/viewvc?rev=1742698&view=rev
Log:
[CONFIGURATION-624] Support Commons Configuration as PropertySource in Spring

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigPropertySource.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigPropertySourceTest.java
Modified:
    commons/proper/configuration/trunk/build.xml
    commons/proper/configuration/trunk/pom.xml
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/site/xdoc/dependencies.xml

Modified: commons/proper/configuration/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/build.xml?rev=1742698&r1=1742697&r2=1742698&view=diff
==============================================================================
--- commons/proper/configuration/trunk/build.xml (original)
+++ commons/proper/configuration/trunk/build.xml Sat May  7 10:01:15 2016
@@ -71,6 +71,7 @@
         <pathelement location="${lib}/org/apache/ant/ant-launcher/1.8.2/ant-launcher-1.8.2.jar"/>
         <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"/>
 	</path>
 	<path id="build.test.classpath">
 	    <pathelement location="${lib}/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar"/>
@@ -110,6 +111,12 @@
 	    <pathelement location="${lib}/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar"/>
 	    <pathelement location="${lib}/org/slf4j/slf4j-ext/1.5.6/slf4j-ext-1.5.6.jar"/>
 	    <pathelement location="${lib}/org/slf4j/slf4j-log4j12/1.5.6/slf4j-log4j12-1.5.6.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-context/4.2.5.RELEASE/spring-context-4.2.5.RELEASE.jar"/>
+        <pathelement location="${lib}/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar"/>
+        <pathelement location="${lib}/org/springframework/spring-test/4.2.5.RELEASE/spring-test-4.2.5.RELEASE.jar"/>
+        <pathelement location="${lib}/org/springframework/spring-aop/4.2.5.RELEASE/spring-aop-4.2.5.RELEASE.jar"/>
+        <pathelement location="${lib}/org/springframework/spring-expression/4.2.5.RELEASE/spring-expression-4.2.5.RELEASE.jar"/>
 	</path>
   </target>
 	
@@ -451,6 +458,11 @@
          dest="${lib}/log4j/log4j/1.2.8/log4j-1.2.8.jar"
          usetimestamp="true"
          ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-core/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
     <mkdir dir="${lib}/org/slf4j/slf4j-api/1.5.6"/>
     <get src="http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar" 
          dest="${lib}/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar" 
@@ -461,6 +473,31 @@
          dest="${lib}/org/slf4j/slf4j-ext/1.5.6/slf4j-ext-1.5.6.jar" 
          usetimestamp="true" 
          ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-context/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-context/4.2.5.RELEASE/spring-context-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-context/4.2.5.RELEASE/spring-context-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-beans/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-test/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-test/4.2.5.RELEASE/spring-test-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-test/4.2.5.RELEASE/spring-test-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-aop/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-aop/4.2.5.RELEASE/spring-aop-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-aop/4.2.5.RELEASE/spring-aop-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
+    <mkdir dir="${lib}/org/springframework/spring-expression/4.2.5.RELEASE"/>
+    <get src="http://repo1.maven.org/maven2/org/springframework/spring-expression/4.2.5.RELEASE/spring-expression-4.2.5.RELEASE.jar"
+         dest="${lib}/org/springframework/spring-expression/4.2.5.RELEASE/spring-expression-4.2.5.RELEASE.jar"
+         usetimestamp="true"
+         ignoreerrors="true"/>
     <mkdir dir="${lib}/org/slf4j/slf4j-log4j12/1.5.6"/>
     <get src="http://repo1.maven.org/maven2/org/slf4j/slf4j-log4j12/1.5.6/slf4j-log4j12-1.5.6.jar" 
          dest="${lib}/org/slf4j/slf4j-log4j12/1.5.6/slf4j-log4j12-1.5.6.jar"

Modified: commons/proper/configuration/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/pom.xml?rev=1742698&r1=1742697&r2=1742698&view=diff
==============================================================================
--- commons/proper/configuration/trunk/pom.xml (original)
+++ commons/proper/configuration/trunk/pom.xml Sat May  7 10:01:15 2016
@@ -210,6 +210,12 @@
           <name>Oliver Kopp</name>
           <email>koppdev@gmail.com</email>
         </contributor>
+
+        <contributor>
+          <name>Dennis Kieselhorst</name>
+          <email>deki@apache.org</email>
+          <organization>IRIAN Deutschland</organization>
+        </contributor>
     </contributors>
 
   <dependencies>
@@ -288,6 +294,29 @@
     </dependency>
 
     <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-core</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>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-test</artifactId>
+      <version>${spring.version}</version>
+      <optional>true</optional>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.4</version>
@@ -445,6 +474,7 @@
       *
     </commons.osgi.import>
     <slf4j.version>1.7.7</slf4j.version>
+    <spring.version>4.2.5.RELEASE</spring.version>
   </properties>
 
   <build>

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1742698&r1=1742697&r2=1742698&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat May  7 10:01:15 2016
@@ -27,6 +27,9 @@
   <body>
     <release version="2.1" date="in SVN"
       description="TBD">
+      <action dev="deki" type="add" issue="CONFIGURATION-624">
+        Support Commons Configuration as PropertySource in Spring.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-622">
         Fixed a problem in INIConfiguration.write() with keys containing a
         separator character. This separator had been duplicated. Such keys

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigPropertySource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigPropertySource.java?rev=1742698&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigPropertySource.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/spring/ConfigPropertySource.java Sat May  7 10:01:15 2016
@@ -0,0 +1,54 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.configuration2.Configuration;
+import org.springframework.core.env.EnumerablePropertySource;
+
+/**
+ * Allow use of Apache Commons Configuration Objects as Spring PropertySources
+ */
+public class ConfigPropertySource extends EnumerablePropertySource<Configuration> {
+
+    public ConfigPropertySource(String name, Configuration source) {
+       super(name, source);
+    }
+
+    protected ConfigPropertySource(String name) {
+       super(name);
+    }
+
+    @Override
+    public String[] getPropertyNames() {
+       final List<String> keys = new ArrayList<String>();
+       final Iterator<String> keysIterator = source.getKeys();
+       while (keysIterator.hasNext()) {
+          keys.add(keysIterator.next());
+       }
+       return keys.toArray(new String[keys.size()]);
+    }
+
+    @Override
+    public Object getProperty(String name) {
+       return source.getProperty(name);
+   }
+}

Modified: commons/proper/configuration/trunk/src/site/xdoc/dependencies.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/site/xdoc/dependencies.xml?rev=1742698&r1=1742697&r2=1742698&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/site/xdoc/dependencies.xml (original)
+++ commons/proper/configuration/trunk/src/site/xdoc/dependencies.xml Sat May  7 10:01:15 2016
@@ -91,6 +91,10 @@
                         <td>VFSFileSystem, VFSFileChangedReloadingStrategy</td>
                         <td>commons-vfs</td>
                     </tr>
+                    <tr>
+                        <td>ConfigPropertySource</td>
+                        <td>spring-core</td>
+                    </tr>
                 </tbody>
             </table>
 

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigPropertySourceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigPropertySourceTest.java?rev=1742698&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigPropertySourceTest.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/spring/ConfigPropertySourceTest.java Sat May  7 10:01:15 2016
@@ -0,0 +1,73 @@
+/*
+ * 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.PropertiesConfiguration;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MutablePropertySources;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+
+/**
+ * test for ConfigPropertySource
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+public class ConfigPropertySourceTest {
+
+    private static final String TEST_PROPERTY = "test.property";
+    private static final String TEST_VALUE = "testVALUE";
+
+    @Value("${" + TEST_PROPERTY + "}")
+    private String value;
+
+    @Test
+    public void testValueInjection() {
+        Assert.assertEquals(TEST_VALUE, value);
+    }
+
+    @Configuration
+    static class Config {
+
+        @Bean
+        public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment
+                                                                                                        env) {
+            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
+            MutablePropertySources sources = new MutablePropertySources();
+            sources.addLast(createConfigPropertySource());
+            configurer.setPropertySources(sources);
+            configurer.setEnvironment(env);
+            return configurer;
+        }
+    }
+
+    private static ConfigPropertySource createConfigPropertySource() {
+        PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
+        propertiesConfiguration.addProperty(TEST_PROPERTY, TEST_VALUE);
+        return new ConfigPropertySource("test configuration", propertiesConfiguration);
+    }
+
+}