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/10/07 19:50:47 UTC

svn commit: r1395350 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java

Author: oheger
Date: Sun Oct  7 17:50:46 2012
New Revision: 1395350

URL: http://svn.apache.org/viewvc?rev=1395350&view=rev
Log:
BasicConfigurationBuilder now supports initialization parameters which do not represent properties of the result object.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java?rev=1395350&r1=1395349&r2=1395350&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java Sun Oct  7 17:50:46 2012
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.commons.configuration.Configuration;
@@ -85,6 +86,16 @@ public class BasicConfigurationBuilder<T
         ConfigurationBuilder<T>
 {
     /**
+     * Constant for a prefix for reserved initialization parameter keys. If a
+     * parameter was set whose key starts with this prefix, it is filtered out
+     * before the initialization of a newly created result object. This
+     * mechanism allows derived classes to store specific configuration data in
+     * the parameters map which does not represent a property value for the
+     * result object.
+     */
+    public static final String RESERVED_PARAMETER = "config-";
+
+    /**
      * A dummy event source that is used for registering listeners if no
      * compatible result object is available. This source has empty dummy
      * implementations for listener registration methods.
@@ -461,7 +472,7 @@ public class BasicConfigurationBuilder<T
     {
         if (resultDeclaration == null)
         {
-            resultDeclaration = createResultDeclaration(getParameters());
+            resultDeclaration = createResultDeclaration(getFilteredParameters());
             checkResultClass(resultDeclaration);
         }
         return resultDeclaration;
@@ -594,6 +605,28 @@ public class BasicConfigurationBuilder<T
     }
 
     /**
+     * Returns a map with initialization parameters where all parameters
+     * starting with the reserved prefix have been filtered out.
+     *
+     * @return the filtered parameters map
+     */
+    private Map<String, Object> getFilteredParameters()
+    {
+        Map<String, Object> filteredMap =
+                new HashMap<String, Object>(getParameters());
+        for (Iterator<String> it = filteredMap.keySet().iterator(); it
+                .hasNext();)
+        {
+            String key = it.next();
+            if (key.startsWith(RESERVED_PARAMETER))
+            {
+                it.remove();
+            }
+        }
+        return filteredMap;
+    }
+
+    /**
      * Returns an {@code EventSource} for the specified object. If the object is
      * an {@code EventSource}, it is returned. Otherwise, a dummy event source
      * is returned.

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java?rev=1395350&r1=1395349&r2=1395350&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java Sun Oct  7 17:50:46 2012
@@ -444,6 +444,23 @@ public class TestBasicConfigurationBuild
     }
 
     /**
+     * Tests whether parameters starting with a reserved prefix are filtered out
+     * before result objects are initialized.
+     */
+    @Test
+    public void testReservedParameter() throws ConfigurationException
+    {
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("throwExceptionOnMissing", Boolean.TRUE);
+        params.put("config-test", "a test");
+        BasicConfigurationBuilder<PropertiesConfiguration> builder =
+                new BasicConfigurationBuilder<PropertiesConfiguration>(
+                        PropertiesConfiguration.class, params);
+        PropertiesConfiguration config = builder.getConfiguration();
+        assertTrue("Flag not set", config.isThrowExceptionOnMissing());
+    }
+
+    /**
      * A test thread class for testing whether the builder's result object can
      * be requested concurrently.
      */