commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1578588 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/ test/java/org/apache/commons/configuration/ test/java/org/apache/commons/configuration/event/
Date Mon, 17 Mar 2014 21:15:47 GMT
Author: oheger
Date: Mon Mar 17 21:15:47 2014
New Revision: 1578588

URL: http://svn.apache.org/r1578588
Log:
Reworked update and event handling for sub configurations.

The complicated mechanism to detect updates in sub configurations, propagate
them, and validate root nodes is no longer needed. This is all handled by
tracked nodes. Now a simple change listener is registered at connected sub
configurations which sends events of type EVENT_SUBNODE_CHANGED.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestSubnodeConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java?rev=1578588&r1=1578587&r2=1578588&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
Mon Mar 17 21:15:47 2014
@@ -24,8 +24,6 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
-import java.util.WeakHashMap;
 
 import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.event.ConfigurationListener;
@@ -49,7 +47,7 @@ import org.apache.commons.configuration.
  * @version $Id$
  */
 public class BaseHierarchicalConfiguration extends AbstractHierarchicalConfiguration<ImmutableNode>
-    implements Serializable, Cloneable, Initializable
+    implements Serializable, Cloneable
 {
     /**
      * Constant for the subnode configuration modified event.
@@ -62,14 +60,8 @@ public class BaseHierarchicalConfigurati
      */
     private static final long serialVersionUID = 3373812230395363192L;
 
-    /**
-     * A map for managing the {@code SubnodeConfiguration} instances created
-     * from this configuration.
-     */
-    private Map<SubnodeConfiguration, Object> subConfigs;
-
-    /** A listener for reacting on changes to update sub configurations. */
-    private ConfigurationListener changeListener;
+    /** A listener for reacting on changes caused by sub configurations. */
+    private final ConfigurationListener changeListener;
 
     /**
      * Creates a new instance of {@code BaseHierarchicalConfiguration}.
@@ -102,18 +94,7 @@ public class BaseHierarchicalConfigurati
     protected BaseHierarchicalConfiguration(NodeModel<ImmutableNode> model)
     {
         super(model);
-    }
-
-    /**
-     * Performs special initialization of this configuration. This
-     * implementation ensures that internal data structures for managing
-     * {@code SubnodeConfiguration} objects are initialized. If this is done
-     * directly after the creation of an instance, this instance can be accessed
-     * in a read-only manner without requiring a specific {@code Synchronizer}.
-     */
-    public void initialize()
-    {
-        ensureSubConfigManagementDataSetUp();
+        changeListener = createChangeListener();
     }
 
     /**
@@ -296,8 +277,11 @@ public class BaseHierarchicalConfigurati
         InMemoryNodeModel myModel = getSubConfigurationParentModel();
         NodeSelector selector = getSubConfigurationNodeSelector(key);
         myModel.trackNode(selector, this);
-        return new SubnodeConfiguration(this, new TrackedNodeModel(myModel,
-                selector, true));
+        SubnodeConfiguration subConfig =
+                new SubnodeConfiguration(this, new TrackedNodeModel(myModel,
+                        selector, true));
+        subConfig.addConfigurationListener(changeListener);
+        return subConfig;
     }
 
     /**
@@ -515,20 +499,8 @@ public class BaseHierarchicalConfigurati
     protected final SubnodeConfiguration createAndInitializeSubnodeConfiguration(
             ConfigurationNode node, String key, boolean supportUpdates)
     {
-        String subnodeKey = supportUpdates ? key : null;
-        SubnodeConfiguration sub = createSubnodeConfiguration(node, subnodeKey);
-
-        ensureSubConfigManagementDataSetUp();
-        sub.addConfigurationListener(changeListener);
-        sub.initSubConfigManagementData(subConfigs, changeListener);
-        sub.setSynchronizer(getSynchronizer());
-
-        if (supportUpdates)
-        {
-            // store this configuration so it can later be validated
-            subConfigs.put(sub, Boolean.TRUE);
-        }
-        return sub;
+        //TODO adapt clients
+        return null;
     }
 
     /**
@@ -548,45 +520,6 @@ public class BaseHierarchicalConfigurati
     }
 
     /**
-     * Initializes the data related to the management of
-     * {@code SubnodeConfiguration} instances. This method is called each time a
-     * new {@code SubnodeConfiguration} was created. A configuration and its
-     * {@code SubnodeConfiguration} instances operate on the same set of data.
-     *
-     * @param subMap the map with all {@code SubnodeConfiguration} instances
-     * @param listener the listener for reacting on changes
-     */
-    void initSubConfigManagementData(Map<SubnodeConfiguration, Object> subMap,
-            ConfigurationListener listener)
-    {
-        subConfigs = subMap;
-        changeListener = listener;
-    }
-
-    /**
-     * Ensures that internal data structures for managing associated
-     * {@code SubnodeConfiguration} objects are initialized.
-     */
-    private void ensureSubConfigManagementDataSetUp()
-    {
-        if (changeListener == null)
-        {
-            setUpSubConfigManagementData();
-        }
-    }
-
-    /**
-     * Initializes internal data structures for managing associated
-     * {@code SubnodeConfiguration} objects.
-     */
-    private void setUpSubConfigManagementData()
-    {
-        changeListener = createChangeListener();
-        subConfigs = new WeakHashMap<SubnodeConfiguration, Object>();
-        addConfigurationListener(changeListener);
-    }
-
-    /**
      * Creates a listener which reacts on all changes on this configuration or
      * one of its {@code SubnodeConfiguration} instances. If such a change is
      * detected, some updates have to be performed.
@@ -599,55 +532,12 @@ public class BaseHierarchicalConfigurati
         {
             public void configurationChanged(ConfigurationEvent event)
             {
-                nodeStructureChanged(event);
+                subnodeConfigurationChanged(event);
             }
         };
     }
 
     /**
-     * A change on the node structure of this configuration has been detected.
-     * This can be caused either by an update of this configuration or by one if
-     * its {@code SubnodeConfiguration} instances. This method calls
-     * {@link #subnodeConfigurationChanged(ConfigurationEvent)} if necessary and
-     * ensures that all {@code SubnodeConfiguration} instances are validated.
-     * Note: when this method is called, a write lock is held on this
-     * configuration.
-     *
-     * @param event the change event
-     */
-    private void nodeStructureChanged(ConfigurationEvent event)
-    {
-        if (this != event.getSource())
-        {
-            subnodeConfigurationChanged(event);
-        }
-
-        if (!event.isBeforeUpdate() && EVENT_SUBNODE_CHANGED != event.getType())
-        {
-            validSubnodeConfigurations(event);
-        }
-    }
-
-    /**
-     * Triggers validation on all {@code SubnodeConfiguration} instances created
-     * by this configuration.
-     *
-     * @param event the change event
-     */
-    private void validSubnodeConfigurations(ConfigurationEvent event)
-    {
-//        Set<SubnodeConfiguration> subs =
-//                new HashSet<SubnodeConfiguration>(subConfigs.keySet());
-//        for (SubnodeConfiguration sub : subs)
-//        {
-//            if (sub != event.getSource())
-//            {
-//                sub.validateRootNode();
-//            }
-//        }
-    }
-
-    /**
      * Returns a configuration with the same content as this configuration, but
      * with all variables replaced by their actual values. This implementation
      * is specific for hierarchical configurations. It clones the current

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=1578588&r1=1578587&r2=1578588&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java
Mon Mar 17 21:15:47 2014
@@ -25,7 +25,6 @@ import java.util.Collection;
 import java.util.List;
 
 import org.apache.commons.configuration.convert.DefaultListDelimiterHandler;
-import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.ex.ConfigurationRuntimeException;
 import org.apache.commons.configuration.tree.DefaultConfigurationKey;
 import org.apache.commons.configuration.tree.DefaultExpressionEngine;
@@ -560,35 +559,6 @@ public class TestHierarchicalConfigurati
     }
 
     /**
-     * Tests the initialize() method. We can only test that a new configuration
-     * listener was added.
-     */
-    @Test
-    public void testInitialize()
-    {
-        Collection<ConfigurationListener> listeners =
-                config.getConfigurationListeners();
-        config.initialize();
-        assertEquals("No new listener added", listeners.size() + 1, config
-                .getConfigurationListeners().size());
-    }
-
-    /**
-     * Tests that calling initialize() multiple times does not initialize
-     * internal structures more than once.
-     */
-    @Test
-    public void testInitializeTwice()
-    {
-        Collection<ConfigurationListener> listeners =
-                config.getConfigurationListeners();
-        config.initialize();
-        config.initialize();
-        assertEquals("Too many listener added", listeners.size() + 1, config
-                .getConfigurationListeners().size());
-    }
-
-    /**
      * Checks the content of the passed in configuration object. Used by some
      * tests that copy a configuration.
      *

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestSubnodeConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestSubnodeConfiguration.java?rev=1578588&r1=1578587&r2=1578588&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestSubnodeConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestSubnodeConfiguration.java
Mon Mar 17 21:15:47 2014
@@ -18,11 +18,9 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -33,9 +31,6 @@ import org.apache.commons.collections.Co
 import org.apache.commons.configuration.convert.DefaultListDelimiterHandler;
 import org.apache.commons.configuration.convert.DisabledListDelimiterHandler;
 import org.apache.commons.configuration.convert.ListDelimiterHandler;
-import org.apache.commons.configuration.event.ConfigurationEvent;
-import org.apache.commons.configuration.event.ConfigurationListener;
-import org.apache.commons.configuration.ex.ConfigurationException;
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
 import org.apache.commons.configuration.interpol.Lookup;
 import org.apache.commons.configuration.tree.ImmutableNode;
@@ -54,9 +49,6 @@ import org.junit.Test;
  */
 public class TestSubnodeConfiguration
 {
-    /** Constant for an updated table name. */
-    private static final String NEW_TABLE_NAME = "newTable";
-
     /** The key used for the SubnodeConfiguration. */
     private static final String SUB_KEY = "tables.table(0)";
 
@@ -449,11 +441,9 @@ public class TestSubnodeConfiguration
         parent.addProperty("tables.table(0).var", "${brackets:x}");
 
         ConfigurationInterpolator interpolator = parent.getInterpolator();
-        interpolator.registerLookup("brackets", new Lookup()
-        {
+        interpolator.registerLookup("brackets", new Lookup() {
 
-            public String lookup(String key)
-            {
+            public String lookup(String key) {
                 return "(" + key + ")";
             }
 
@@ -464,47 +454,6 @@ public class TestSubnodeConfiguration
     }
 
     /**
-     * Tests whether events are fired if a change of the parent is detected.
-     */
-    @Test
-    public void testUpdateEvents() throws ConfigurationException
-    {
-        BaseHierarchicalConfiguration config =
-                (BaseHierarchicalConfiguration) parent.configurationAt(SUB_KEY,
-                        true);
-        ConfigurationListenerTestImpl l = new ConfigurationListenerTestImpl();
-        config.addConfigurationListener(l);
-        updateParent();
-        assertEquals("Wrong number of events", 4, l.events.size());
-        boolean before = true;
-        for (ConfigurationEvent e : l.events)
-        {
-            assertEquals("Wrong configuration", config, e.getSource());
-            assertEquals("Wrong event type",
-                    BaseHierarchicalConfiguration.EVENT_SUBNODE_CHANGED,
-                    e.getType());
-            assertNull("Got a property name", e.getPropertyName());
-            assertNull("Got a property value", e.getPropertyValue());
-            assertEquals("Wrong before flag", before, e.isBeforeUpdate());
-            before = !before;
-        }
-    }
-
-    /**
-     * Updates the parent configuration. Replaces the complete node structure.
-     */
-    private void updateParent()
-    {
-        String[] tableNamesNew = NodeStructureHelper.getClonedTables();
-        String[][] fieldNamesNew = NodeStructureHelper.getClonedFields();
-        tableNamesNew[0] = NEW_TABLE_NAME;
-        fieldNamesNew[0][0] = "newField";
-        parent.clear();
-        appendTree(parent, NodeStructureHelper.createTablesTree(tableNamesNew,
-                fieldNamesNew));
-    }
-
-    /**
      * Tests a manipulation of the parent configuration that causes the subnode
      * configuration to become invalid. In this case the sub config should be
      * detached and keep its old values.
@@ -552,21 +501,4 @@ public class TestSubnodeConfiguration
         assertTrue("Wrong finalize flag",
                 subModel.isReleaseTrackedNodeOnFinalize());
     }
-
-    /**
-     * A specialized configuration listener for testing whether the expected
-     * events are fired.
-     */
-    private static class ConfigurationListenerTestImpl implements
-            ConfigurationListener
-    {
-        /** Stores the events received. */
-        final List<ConfigurationEvent> events =
-                new ArrayList<ConfigurationEvent>();
-
-        public void configurationChanged(ConfigurationEvent event)
-        {
-            events.add(event);
-        }
-    }
 }

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java?rev=1578588&r1=1578587&r2=1578588&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
Mon Mar 17 21:15:47 2014
@@ -25,9 +25,8 @@ import java.util.Collection;
 import org.apache.commons.configuration.AbstractConfiguration;
 import org.apache.commons.configuration.BaseHierarchicalConfiguration;
 import org.apache.commons.configuration.HierarchicalConfiguration;
-import org.apache.commons.configuration.SubnodeConfiguration;
-import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.DefaultConfigurationNode;
+import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.NodeStructureHelper;
 import org.junit.Test;
 
 /**
@@ -50,14 +49,14 @@ public class TestHierarchicalConfigurati
     @Test
     public void testClearTreeEvent()
     {
-        HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
+        BaseHierarchicalConfiguration hc = (BaseHierarchicalConfiguration) config;
         String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
-        Collection<ConfigurationNode> nodes = hc.getExpressionEngine()
-                .query(hc.getRootNode(), key);
+//        Collection<ImmutableNode> nodes = hc.getExpressionEngine()
+//                .query(hc.getRootNode(), key, hc.getModel().getNodeHandler());
         hc.clearTree(key);
         l.checkEvent(BaseHierarchicalConfiguration.EVENT_CLEAR_TREE, key, null,
                 true);
-        l.checkEvent(BaseHierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes,
+        l.checkEvent(BaseHierarchicalConfiguration.EVENT_CLEAR_TREE, key, null /* nodes*/,
                 false);
         l.done();
     }
@@ -68,9 +67,9 @@ public class TestHierarchicalConfigurati
     @Test
     public void testAddNodesEvent()
     {
-        HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
-        Collection<ConfigurationNode> nodes = new ArrayList<ConfigurationNode>(1);
-        nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE));
+        BaseHierarchicalConfiguration hc = (BaseHierarchicalConfiguration) config;
+        Collection<ImmutableNode> nodes = new ArrayList<ImmutableNode>(1);
+        nodes.add(NodeStructureHelper.createNode("a_key", TEST_PROPVALUE));
         hc.addNodes(TEST_PROPNAME, nodes);
         l.checkEvent(BaseHierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
                 nodes, true);
@@ -86,31 +85,45 @@ public class TestHierarchicalConfigurati
     @Test
     public void testAddNodesEmptyEvent()
     {
-        ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
-                new ArrayList<ConfigurationNode>());
+        ((BaseHierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
+                new ArrayList<ImmutableNode>());
         l.done();
     }
 
     /**
-     * Tests whether manipulations of a subnode configuration trigger correct
+     * Tests whether manipulations of a connected sub configuration trigger correct
      * events.
      */
     @Test
-    public void testSubnodeChangedEvent()
+    public void testSubConfigurationChangedEventConnected()
     {
-        SubnodeConfiguration sub = ((HierarchicalConfiguration) config)
-                .configurationAt(EXIST_PROPERTY);
+        HierarchicalConfiguration<ImmutableNode> sub =
+                ((BaseHierarchicalConfiguration) config)
+                        .configurationAt(EXIST_PROPERTY, true);
         sub.addProperty("newProp", "newValue");
-        checkSubnodeEvent(l
-                .nextEvent(BaseHierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
+        checkSubnodeEvent(
+                l.nextEvent(BaseHierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
                 true);
-        checkSubnodeEvent(l
-                .nextEvent(BaseHierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
+        checkSubnodeEvent(
+                l.nextEvent(BaseHierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
                 false);
         l.done();
     }
 
     /**
+     * Tests that no events are generated for a disconnected sub configuration.
+     */
+    @Test
+    public void testSubConfigurationChangedEventNotConnected()
+    {
+        HierarchicalConfiguration<ImmutableNode> sub =
+                ((BaseHierarchicalConfiguration) config)
+                        .configurationAt(EXIST_PROPERTY);
+        sub.addProperty("newProp", "newValue");
+        l.done();
+    }
+
+    /**
      * Tests whether a received event contains a correct subnode event.
      *
      * @param event the event object



Mime
View raw message