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/12/27 21:33:17 UTC

svn commit: r1426307 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/ test/resources/

Author: oheger
Date: Thu Dec 27 20:33:16 2012
New Revision: 1426307

URL: http://svn.apache.org/viewvc?rev=1426307&view=rev
Log:
CombinedConfigurationBuilder now supports declarations of custom Lookup objects.

Added:
    commons/proper/configuration/trunk/src/test/resources/testCCLookup.xml   (with props)
Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java?rev=1426307&r1=1426306&r2=1426307&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java Thu Dec 27 20:33:16 2012
@@ -39,6 +39,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.beanutils.BeanHelper;
 import org.apache.commons.configuration.beanutils.CombinedBeanDeclaration;
 import org.apache.commons.configuration.beanutils.XMLBeanDeclaration;
+import org.apache.commons.configuration.builder.BasicBuilderParameters;
 import org.apache.commons.configuration.builder.BasicConfigurationBuilder;
 import org.apache.commons.configuration.builder.BuilderListener;
 import org.apache.commons.configuration.builder.BuilderParameters;
@@ -541,30 +542,6 @@ public class CombinedConfigurationBuilde
     }
 
     /**
-     * Returns the configuration provided by this builder. If the boolean
-     * parameter is <b>true</b>, the configuration definition file will be
-     * loaded. It will then be parsed, and instances for the declared
-     * configurations will be created.
-     *
-     * @param load a flag whether the configuration definition file should be
-     * loaded; a value of <b>false</b> would make sense if the file has already
-     * been created or its content was manipulated using some of the property
-     * accessor methods
-     * @return the configuration
-     * @throws ConfigurationException if an error occurs
-     */
-    public CombinedConfiguration getConfiguration(boolean load)
-            throws ConfigurationException
-    {
-        registerConfiguredLookups();
-
-//        CombinedConfiguration result = createResultConfiguration();
-//        constructedConfiguration = result;
-
-        return null;
-    }
-
-    /**
      * Returns the {@code ConfigurationBuilder} which creates the definition
      * configuration.
      *
@@ -760,6 +737,7 @@ public class CombinedConfigurationBuilde
         setUpCurrentXMLParameters();
         currentXMLParameters.setFileSystem(initFileSystem(config));
         initSystemProperties(config, getBasePath());
+        registerConfiguredLookups(config, result);
         configureEntityResolver(config, currentXMLParameters);
 
         ConfigurationSourceData data = getSourceData();
@@ -801,22 +779,40 @@ public class CombinedConfigurationBuilde
     }
 
     /**
-     * Registers StrLookups defined in the configuration.
+     * Processes custom {@link Lookup} objects that might be declared in the
+     * definition configuration. Each {@code Lookup} object is registered at the
+     * definition configuration and at the result configuration. It is also
+     * added to all child configurations added to the resulting combined
+     * configuration.
      *
+     * @param defConfig the definition configuration
+     * @param resultConfig the resulting configuration
      * @throws ConfigurationException if an error occurs
      */
-    protected void registerConfiguredLookups() throws ConfigurationException
+    protected void registerConfiguredLookups(
+            HierarchicalConfiguration defConfig, Configuration resultConfig)
+            throws ConfigurationException
     {
-//        List<SubnodeConfiguration> nodes = configurationsAt(KEY_CONFIGURATION_LOOKUPS);
-//        for (SubnodeConfiguration config : nodes)
-//        {
-//            XMLBeanDeclaration decl = new XMLBeanDeclaration(config);
-//            String key = config.getString(KEY_LOOKUP_KEY);
-//            StrLookup lookup = (StrLookup) BeanHelper.createBean(decl);
-//            BeanHelper.setProperty(lookup, "configuration", this);
-//            ConfigurationInterpolator.registerGlobalLookup(key, lookup);
-//            this.getInterpolator().registerLookup(key, lookup);
-//        }
+        Map<String, Lookup> lookups = new HashMap<String, Lookup>();
+        List<SubnodeConfiguration> nodes =
+                defConfig.configurationsAt(KEY_CONFIGURATION_LOOKUPS);
+        for (SubnodeConfiguration config : nodes)
+        {
+            XMLBeanDeclaration decl = new XMLBeanDeclaration(config);
+            String key = config.getString(KEY_LOOKUP_KEY);
+            Lookup lookup = (Lookup) BeanHelper.createBean(decl);
+            lookups.put(key, lookup);
+        }
+
+        if (!lookups.isEmpty())
+        {
+            ConfigurationInterpolator defCI = defConfig.getInterpolator();
+            if (defCI != null)
+            {
+                defCI.registerLookups(lookups);
+            }
+            resultConfig.getInterpolator().registerLookups(lookups);
+        }
     }
 
     /**
@@ -956,6 +952,10 @@ public class CombinedConfigurationBuilde
      */
     protected void initChildBuilderParameters(BuilderParameters params)
     {
+        if (params instanceof BasicBuilderParameters)
+        {
+            initChildBasicParameters((BasicBuilderParameters) params);
+        }
         if (params instanceof XMLBuilderProperties<?>)
         {
             initChildXMLParameters((XMLBuilderProperties<?>) params);
@@ -964,7 +964,8 @@ public class CombinedConfigurationBuilde
         {
             initChildFileBasedParameters((FileBasedBuilderProperties<?>) params);
         }
-        if(params instanceof CombinedBuilderParametersImpl) {
+        if (params instanceof CombinedBuilderParametersImpl)
+        {
             initChildCombinedParameters((CombinedBuilderParametersImpl) params);
         }
     }
@@ -1052,6 +1053,18 @@ public class CombinedConfigurationBuilde
     }
 
     /**
+     * Initializes basic builder parameters for a child configuration with
+     * default settings set for this builder. This implementation ensures that
+     * all {@code Lookup} objects are propagated to child configurations.
+     *
+     * @param params the parameters object
+     */
+    private void initChildBasicParameters(BasicBuilderParameters params)
+    {
+        params.setPrefixLookups(fetchPrefixLookups());
+    }
+
+    /**
      * Initializes a parameters object for a file-based configuration with
      * properties already set for this parent builder. This method handles
      * properties like a default file system or a base path.

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedConfigurationBuilder.java?rev=1426307&r1=1426306&r2=1426307&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedConfigurationBuilder.java Thu Dec 27 20:33:16 2012
@@ -52,6 +52,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+import org.apache.commons.configuration.interpol.Lookup;
 import org.apache.commons.configuration.resolver.CatalogResolver;
 import org.easymock.EasyMock;
 import org.junit.Before;
@@ -897,6 +898,24 @@ public class TestCombinedConfigurationBu
     }
 
     /**
+     * Tests whether a Lookup object can be declared in the definition
+     * configuration.
+     */
+    @Test
+    public void testCustomLookup() throws ConfigurationException
+    {
+        File testFile = ConfigurationAssert.getTestFile("testCCLookup.xml");
+        factory.configure(new FileBasedBuilderParametersImpl()
+                .setFile(testFile));
+        CombinedConfiguration cc = factory.getConfiguration();
+        assertTrue("Lookup not registered in CC", cc.getInterpolator()
+                .getLookups().containsKey("test"));
+        Configuration xmlConf = cc.getConfiguration("xml");
+        assertTrue("Lookup not registered in sub config", xmlConf
+                .getInterpolator().getLookups().containsKey("test"));
+    }
+
+    /**
      * A test builder provider implementation for testing whether providers can
      * be defined in the definition file.
      */
@@ -1021,4 +1040,26 @@ public class TestCombinedConfigurationBu
          */
         private static final long serialVersionUID = 20121216L;
     }
+
+    /**
+     * A custom Lookup implementation for testing whether lookups can be defined
+     * in the definition configuration. This lookup supports some variables
+     * referencing test files.
+     */
+    public static class TestLookup implements Lookup
+    {
+        private final Map<String, String> map = new HashMap<String, String>();
+
+        public TestLookup()
+        {
+            map.put("test_file_xml", "test.xml");
+            map.put("test_file_combine", "testcombine1.xml");
+            map.put("test_key", "test.value");
+        }
+
+        public String lookup(String key)
+        {
+            return map.get(key);
+        }
+    }
 }

Added: commons/proper/configuration/trunk/src/test/resources/testCCLookup.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testCCLookup.xml?rev=1426307&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testCCLookup.xml (added)
+++ commons/proper/configuration/trunk/src/test/resources/testCCLookup.xml Thu Dec 27 20:33:16 2012
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!--
+   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.
+-->
+<!-- Test configuration definition file that contains a declaration of a
+     custom Lookup object. It is also tested whether the Lookup can be
+     directly accessed for referencing configuration files.
+     $Id$
+-->
+<configuration>
+  <header>
+    <lookups>
+      <lookup config-prefix="test"
+              config-class="org.apache.commons.configuration.builder.combined.TestCombinedConfigurationBuilder$TestLookup"/>
+    </lookups>
+  </header>
+  <!-- Fetch the file name from a variable -->
+  <xml fileName="${test:test_file_xml}" config-name="xml"/>
+  <additional>
+    <xml config-name="combiner1" fileName="${test:test_file_combine}"/>  -->
+    <xml config-name="combiner2" fileName="testcombine2.xml"/>
+  </additional>
+</configuration>
\ No newline at end of file

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

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

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