You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2012/02/29 22:11:35 UTC

svn commit: r1295276 - in /commons/proper/configuration/trunk/src: changes/ main/java/org/apache/commons/configuration/ main/java/org/apache/commons/configuration/interpol/ test/java/org/apache/commons/configuration/ test/resources/

Author: oheger
Date: Wed Feb 29 21:11:35 2012
New Revision: 1295276

URL: http://svn.apache.org/viewvc?rev=1295276&view=rev
Log:
[CONFIGURATION-481] Improved variable substitution in configuration sources created by a DefaultConfigurationBuilder. It is now possible to reference properties of other sources. (Obviously, this already worked in Configuration 1.6.)

Added:
    commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties   (with props)
    commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml   (with props)
    commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml   (with props)
Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1295276&r1=1295275&r2=1295276&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Wed Feb 29 21:11:35 2012
@@ -27,6 +27,10 @@
   <body>
     <release version="1.9" date="in SVN"
       description="TBD">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-481">
+        Variable substitution in configuration sources declared in a definition
+        file for DefaultConfigurationBuilder now works across multiple sources.
+      </action>
     </release>
 
     <release version="1.8" date="2012-02-04"

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?rev=1295276&r1=1295275&r2=1295276&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java Wed Feb 29 21:11:35 2012
@@ -414,6 +414,27 @@ public class DefaultConfigurationBuilder
      */
     private static final long serialVersionUID = -3113777854714492123L;
 
+    /**
+     * A specialized {@code StrLookup} object which operates on the combined
+     * configuration constructed by this builder. This object is used as
+     * default lookup for {@code ConfigurationInterpolator} objects assigned to
+     * newly created configuration objects.
+     */
+    private final StrLookup combinedConfigLookup = new StrLookup()
+    {
+        @Override
+        public String lookup(String key)
+        {
+            if (constructedConfiguration != null)
+            {
+                Object value =
+                        constructedConfiguration.resolveContainerStore(key);
+                return (value != null) ? value.toString() : null;
+            }
+            return null;
+        }
+    };
+
     /** Stores the configuration that is currently constructed.*/
     private CombinedConfiguration constructedConfiguration;
 
@@ -538,7 +559,7 @@ public class DefaultConfigurationBuilder
      */
     public ConfigurationProvider removeConfigurationProvider(String tagName)
     {
-        return (ConfigurationProvider) providers.remove(tagName);
+        return providers.remove(tagName);
     }
 
     /**
@@ -550,7 +571,7 @@ public class DefaultConfigurationBuilder
      */
     public ConfigurationProvider providerForTag(String tagName)
     {
-        return (ConfigurationProvider) providers.get(tagName);
+        return providers.get(tagName);
     }
 
     /**
@@ -1297,7 +1318,9 @@ public class DefaultConfigurationBuilder
 
             try
             {
-                return provider.getConfiguration(decl);
+                AbstractConfiguration config = provider.getConfiguration(decl);
+                installInterpolator(decl, config);
+                return config;
             }
             catch (Exception ex)
             {
@@ -1347,6 +1370,25 @@ public class DefaultConfigurationBuilder
             // will complain that the bean's class cannot be determined
             return Configuration.class;
         }
+
+        /**
+         * Installs a specialized {@code ConfigurationInterpolator} on a newly
+         * created configuration which also takes the combined configuration
+         * created by the builder into account. With this
+         * {@code ConfigurationInterpolator} the interpolation facilities of
+         * this child configuration are extended to include all other
+         * configurations created by this builder.
+         *
+         * @param decl the {@code ConfigurationDeclaration}
+         * @param config the newly created configuration instance
+         */
+        private void installInterpolator(ConfigurationDeclaration decl,
+                AbstractConfiguration config)
+        {
+            ConfigurationInterpolator parent = new ConfigurationInterpolator();
+            parent.setDefaultLookup(decl.getConfigurationBuilder().combinedConfigLookup);
+            config.getInterpolator().setParentInterpolator(parent);
+        }
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java?rev=1295276&r1=1295275&r2=1295276&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java Wed Feb 29 21:11:35 2012
@@ -296,7 +296,7 @@ public class ConfigurationInterpolator e
             String value = fetchLookupForPrefix(prefix).lookup(name);
             if (value == null && getParentInterpolator() != null)
             {
-                value = getParentInterpolator().fetchLookupForPrefix(prefix).lookup(name);
+                value = getParentInterpolator().lookup(name);
             }
             if (value != null)
             {
@@ -306,7 +306,7 @@ public class ConfigurationInterpolator e
         String value = fetchNoPrefixLookup().lookup(var);
         if (value == null && getParentInterpolator() != null)
         {
-            value = getParentInterpolator().fetchNoPrefixLookup().lookup(var);
+            value = getParentInterpolator().lookup(var);
         }
         return value;
     }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java?rev=1295276&r1=1295275&r2=1295276&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java Wed Feb 29 21:11:35 2012
@@ -1188,6 +1188,31 @@ public class TestDefaultConfigurationBui
         }
     }
 
+    /**
+     * Tests whether variable substitution works across multiple child
+     * configurations. This test is related to CONFIGURATION-481.
+     */
+    @Test
+    public void testInterpolationOverMultipleSources()
+            throws ConfigurationException
+    {
+        File testFile =
+                ConfigurationAssert.getTestFile("testInterpolationBuilder.xml");
+        factory.setFile(testFile);
+        CombinedConfiguration combConfig = factory.getConfiguration(true);
+        assertEquals("Wrong value", "abc-product",
+                combConfig.getString("products.product.desc"));
+        XMLConfiguration xmlConfig =
+                (XMLConfiguration) combConfig.getConfiguration("test");
+        assertEquals("Wrong value from XML config", "abc-product",
+                xmlConfig.getString("products/product/desc"));
+        SubnodeConfiguration subConfig =
+                xmlConfig
+                        .configurationAt("products/product[@name='abc']", true);
+        assertEquals("Wrong value from sub config", "abc-product",
+                subConfig.getString("desc"));
+    }
+
     private void verify(String key, CombinedConfiguration config, int rows)
     {
         System.setProperty("Id", key);

Added: commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties?rev=1295276&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties (added)
+++ commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties Wed Feb 29 21:11:35 2012
@@ -0,0 +1,22 @@
+#   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.
+
+# A test configuration file for testing variable substitution across multiple
+# configuration sources created by a DefaultConfigurationBuilder.
+# This properties configuration defines a property which is referenced by
+# another configuration source.
+# $Id:$
+myvar=abc
+

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml?rev=1295276&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml (added)
+++ commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml Wed Feb 29 21:11:35 2012
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+-->
+<!--
+  A test configuration file for testing variable substitution across multiple
+  configuration sources created by a DefaultConfigurationBuilder.
+  This XML configuration references a variable defined by another configuration
+  source.
+  $Id:$
+-->
+<test>
+  <products>
+    <product name="abc">
+      <desc>${myvar}-product</desc>
+    </product>
+  </products>
+</test>

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolation.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml?rev=1295276&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml (added)
+++ commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml Wed Feb 29 21:11:35 2012
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+-->
+<!--
+  A test configuration file for testing variable substitution across multiple
+  configuration sources created by a DefaultConfigurationBuilder.
+  This configuration definition file for a DefaultConfigurationBuilder
+  references multiple sources which contain variable references.
+  $Id:$
+-->
+<configuration>
+  <system/>
+  <properties fileName="testInterpolation.properties"/>
+  <xml fileName="testInterpolation.xml" config-name="test">
+    <expressionEngine config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine"/>
+  </xml>
+</configuration>

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/resources/testInterpolationBuilder.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml