commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r766915 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/beanutils/ main/java/org/apache/commons/configuration2/interpol/ test/java/org/apache/commons/configuration2/ t...
Date Mon, 20 Apr 2009 23:39:15 GMT
Author: rgoers
Date: Mon Apr 20 23:39:14 2009
New Revision: 766915

URL: http://svn.apache.org/viewvc?rev=766915&view=rev
Log:
Allow Collections in DefaultConfigurationBuilder so ExprLookup can have multiple variables

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanDeclaration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/XMLBeanDeclaration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ExprLookup.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestBeanHelper.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestXMLBeanDeclaration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExpression.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanDeclaration.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanDeclaration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanDeclaration.java
Mon Apr 20 23:39:14 2009
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration2.beanutils;
 
 import java.util.Map;
+import java.util.List;
 
 /**
  * <p>
@@ -96,5 +97,5 @@
      *
      * @return a map with nested bean declarations
      */
-    Map<String, BeanDeclaration> getNestedBeanDeclarations();
+    Map<String, List<BeanDeclaration>> getNestedBeanDeclarations();
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
Mon Apr 20 23:39:14 2009
@@ -21,6 +21,8 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.Collection;
+import java.util.List;
 import java.lang.reflect.InvocationTargetException;
 import java.beans.PropertyDescriptor;
 
@@ -175,14 +177,31 @@
     {
         initBeanProperties(bean, data, lenient);
 
-        Map<String, BeanDeclaration> nestedBeans = data.getNestedBeanDeclarations();
+        Map<String, List<BeanDeclaration>> nestedBeans = data.getNestedBeanDeclarations();
         if (nestedBeans != null)
         {
-            for (Map.Entry<String, BeanDeclaration> e : nestedBeans.entrySet())
+            if (bean instanceof Collection)
             {
-                String propName = e.getKey();
-                Class defaultClass = getDefaultClass(bean, propName);
-                initProperty(bean, propName, createBean(e.getValue(), defaultClass), lenient);
+                Collection<Object> coll = (Collection<Object>) bean;
+                if (nestedBeans.size() == 1)
+                {
+                    Map.Entry<String, List<BeanDeclaration>> e = nestedBeans.entrySet().iterator().next();
+                    String propName = e.getKey();
+                    Class defaultClass = getDefaultClass(bean, propName);
+                    for (BeanDeclaration decl : e.getValue())
+                    {
+                        coll.add(createBean(decl, defaultClass));
+                    }
+                }
+            }
+            else
+            {
+                for (Map.Entry<String, List<BeanDeclaration>> e : nestedBeans.entrySet())
+                {
+                    String propName = e.getKey();
+                    Class defaultClass = getDefaultClass(bean, propName);
+                    initProperty(bean, propName, createBean(e.getValue().get(0), defaultClass),
lenient);
+                }
             }
         }
     }
@@ -238,7 +257,8 @@
      * @param lenient  ignore a missing or read only property, throws an exception otherwise
      * @throws ConfigurationRuntimeException if the property is not writeable or an error
occurred
      */
-    private static void initProperty(Object bean, String propName, Object value, boolean
lenient) throws ConfigurationRuntimeException
+    private static void initProperty(Object bean, String propName, Object value, boolean
lenient)
+            throws ConfigurationRuntimeException
     {
         if (!PropertyUtils.isWriteable(bean, propName))
         {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/XMLBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/XMLBeanDeclaration.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/XMLBeanDeclaration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/XMLBeanDeclaration.java
Mon Apr 20 23:39:14 2009
@@ -19,10 +19,13 @@
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.commons.configuration2.AbstractHierarchicalConfiguration;
 import org.apache.commons.configuration2.PropertyConverter;
 import org.apache.commons.configuration2.SubConfiguration;
+import org.apache.commons.configuration2.ConfigurationRuntimeException;
 import org.apache.commons.configuration2.expr.NodeHandler;
 
 /**
@@ -305,17 +308,27 @@
      *
      * @return a map with bean declarations for complex properties
      */
-    public Map<String, BeanDeclaration> getNestedBeanDeclarations()
+    public Map<String, List<BeanDeclaration>> getNestedBeanDeclarations()
     {
-        Map<String, BeanDeclaration> nested = new HashMap<String, BeanDeclaration>();
+        Map<String, List<BeanDeclaration>> nested = new HashMap<String, List<BeanDeclaration>>();
         if (getNode() != null)
         {
             for (T child : getNodeHandler().getChildren(getNode()))
             {
                 if (!isReservedNode(child))
                 {
+                    List<BeanDeclaration> list;
                     String nodeName = getNodeHandler().nodeName(child);
-                    nested.put(nodeName, createBeanDeclaration(child));
+                    if (nested.containsKey(nodeName))
+                    {
+                        list = nested.get(nodeName);
+                    }
+                    else
+                    {
+                        list = new ArrayList<BeanDeclaration>();
+                        nested.put(nodeName, list);
+                    }
+                    list.add(createBeanDeclaration(child));
                 }
             }
         }
@@ -398,8 +411,23 @@
      */
     protected BeanDeclaration createBeanDeclaration(T node)
     {
-        return new XMLBeanDeclaration<T>(getConfiguration().configurationAt(
-                getNodeHandler().nodeName(node)), node);
+        String nodeName = getNodeHandler().nodeName(node);
+        List<SubConfiguration<T>> list = getConfiguration().configurationsAt(nodeName);
+        if (list.size() == 1)
+        {
+            return new XMLBeanDeclaration<T>(list.get(0), node);
+        }
+        else
+        {
+            for (SubConfiguration<T> config : list)
+            {
+                if (config.getRootNode().equals(node))
+                {
+                    return new XMLBeanDeclaration<T>(config, node);
+                }
+            }
+            throw new ConfigurationRuntimeException("Unable to match node for " + nodeName);
+        }
     }
 
     /**

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ExprLookup.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ExprLookup.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ExprLookup.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ExprLookup.java
Mon Apr 20 23:39:14 2009
@@ -201,10 +201,11 @@
      */
     public static class Variables extends ArrayList<Variable>
     {
+        /*
         public void setVariable(Variable var)
         {
             add(var);
-        }
+        } */
 
         public Variable getVariable()
         {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
Mon Apr 20 23:39:14 2009
@@ -29,8 +29,6 @@
 import java.util.logging.Level;
 import java.util.logging.StreamHandler;
 import java.util.logging.SimpleFormatter;
-import java.util.logging.ConsoleHandler;
-import java.util.logging.Handler;
 
 import junit.framework.TestCase;
 
@@ -896,7 +894,7 @@
         assertNotNull("No XML returned", xml);
         assertTrue("Incorect configuration data: " + xml, xml.contains("<rowsPerPage>15</rowsPerPage>"));
         logger.removeHandler(handler);
-        logger.setLevel(Level.OFF);   
+        logger.setLevel(Level.OFF);
         verify("1002", config, 25);
         verify("1003", config, 35);
         verify("1004", config, 50);
@@ -963,6 +961,7 @@
         factory.setFile(EXPRESSION_FILE);
         factory.setAttributeSplittingDisabled(true);
         System.getProperties().remove("Id");
+        org.slf4j.MDC.clear();
 
         CombinedConfiguration config = factory.getConfiguration(true);
         assertTrue("Incorrect configuration", config instanceof DynamicCombinedConfiguration);
@@ -973,6 +972,7 @@
     private void verify(String key, CombinedConfiguration config, int rows)
     {
         System.setProperty("Id", key);
+        org.slf4j.MDC.put("Id", key);
         int actual = config.getInt("rowsPerPage");
         assertTrue("expected: " + rows + " actual: " + actual, actual == rows);
     }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestBeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestBeanHelper.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestBeanHelper.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestBeanHelper.java
Mon Apr 20 23:39:14 2009
@@ -19,6 +19,8 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.commons.configuration2.ConfigurationRuntimeException;
 import org.apache.commons.configuration2.beanutils.BeanDeclaration;
@@ -398,8 +400,10 @@
         {
             buddyData.setBeanFactoryName(TEST_FACTORY);
         }
-        Map nested = new HashMap();
-        nested.put("buddy", buddyData);
+        Map<String, List<BeanDeclaration>> nested = new HashMap<String, List<BeanDeclaration>>();
+        List<BeanDeclaration> list = new ArrayList<BeanDeclaration>();
+        list.add(buddyData);
+        nested.put("buddy", list);
         data.setNestedBeanDeclarations(nested);
         return data;
     }
@@ -517,7 +521,7 @@
 
         private Map beanProperties;
 
-        private Map nestedBeanDeclarations;
+        private Map<String, List<BeanDeclaration>> nestedBeanDeclarations;
 
         public String getBeanClassName()
         {
@@ -559,12 +563,12 @@
             this.beanProperties = beanProperties;
         }
 
-        public Map getNestedBeanDeclarations()
+        public Map<String, List<BeanDeclaration>> getNestedBeanDeclarations()
         {
             return nestedBeanDeclarations;
         }
 
-        public void setNestedBeanDeclarations(Map nestedBeanDeclarations)
+        public void setNestedBeanDeclarations(Map<String, List<BeanDeclaration>>
nestedBeanDeclarations)
         {
             this.nestedBeanDeclarations = nestedBeanDeclarations;
         }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestXMLBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestXMLBeanDeclaration.java?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestXMLBeanDeclaration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestXMLBeanDeclaration.java
Mon Apr 20 23:39:14 2009
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration2.beanutils;
 
 import java.util.Map;
+import java.util.List;
 
 import junit.framework.TestCase;
 
@@ -272,13 +273,13 @@
         decl = new XMLBeanDeclaration<ConfigurationNode>(config, KEY);
         checkProperties(decl, TEST_PROPS, TEST_VALUES);
 
-        Map<String, BeanDeclaration> nested = decl.getNestedBeanDeclarations();
+        Map<String, List<BeanDeclaration>> nested = decl.getNestedBeanDeclarations();
         assertEquals("Wrong number of nested declarations",
                 COMPLEX_PROPS.length, nested.size());
         for (int i = 0; i < COMPLEX_PROPS.length; i++)
         {
             XMLBeanDeclaration<ConfigurationNode> d = (XMLBeanDeclaration<ConfigurationNode>)
nested
-                    .get(COMPLEX_PROPS[i]);
+                    .get(COMPLEX_PROPS[i]).get(0);
             assertNotNull("No declaration found for " + COMPLEX_PROPS[i], d);
             checkProperties(d, COMPLEX_ATTRIBUTES[i], COMPLEX_VALUES[i]);
             assertEquals("Wrong bean class", COMPLEX_CLASSES[i], d
@@ -304,12 +305,12 @@
             }
         };
 
-        Map<String, BeanDeclaration> nested = decl.getNestedBeanDeclarations();
+        Map<String, List<BeanDeclaration>> nested = decl.getNestedBeanDeclarations();
         assertEquals("Wrong number of nested declarations",
                 COMPLEX_PROPS.length, nested.size());
         for (int i = 0; i < COMPLEX_PROPS.length; i++)
         {
-            BeanDeclaration d = nested.get(COMPLEX_PROPS[i]);
+            BeanDeclaration d = nested.get(COMPLEX_PROPS[i]).get(0);
             assertTrue("Wrong declaration class: " + d,
                     d instanceof XMLBeanDeclarationTestImpl);
         }
@@ -323,7 +324,7 @@
         InMemoryConfiguration config = new InMemoryConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
         decl = new XMLBeanDeclaration<ConfigurationNode>(config, KEY);
-        Map<String, BeanDeclaration> nested = decl.getNestedBeanDeclarations();
+        Map<String, List<BeanDeclaration>> nested = decl.getNestedBeanDeclarations();
         assertTrue("Found nested declarations", nested == null
                 || nested.isEmpty());
     }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExpression.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExpression.xml?rev=766915&r1=766914&r2=766915&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExpression.xml
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExpression.xml
Mon Apr 20 23:39:14 2009
@@ -10,10 +10,11 @@
           config-class="org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine"/>
     </result>
     <lookups>
-      <lookup config-prefix="expr" 
+      <lookup config-prefix="expr"
               config-class="org.apache.commons.configuration2.interpol.ExprLookup">
         <variables>
           <variable name="String" value="Class:org.apache.commons.lang.StringUtils"/>
+          <variable name="MDC" value="Class:org.slf4j.MDC"/>
         </variables>
       </lookup>
     </lookups>
@@ -24,7 +25,7 @@
     </providers>
   </header>
   <override>
-    <multifile filePattern='$$${expr:String.right("$[sys:Id]", 2)}/testMultiConfiguration_$$${sys:Id}.xml'
+    <multifile filePattern='$$${expr:String.right(MDC.get("Id"), 2)}/testMultiConfiguration_$$${sys:Id}.xml'
                config-name="clientConfig" delimiterParsingDisabled="true">
        <expressionEngine
           config-class="org.apache.commons.configuration2.expr.xpath.XPathExpressionEngine"/>



Mime
View raw message