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 2014/07/22 22:10:32 UTC

svn commit: r1612686 - 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: Tue Jul 22 20:10:31 2014
New Revision: 1612686

URL: http://svn.apache.org/r1612686
Log:
BasicConfigurationBuilder now removes event listeners when the result is reset.

The now obsolete configuration object should no longer generate events, even
if it is still referenced and accessed.

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=1612686&r1=1612685&r2=1612686&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 Tue Jul 22 20:10:31 2014
@@ -363,12 +363,18 @@ public class BasicConfigurationBuilder<T
      */
     public void resetResult()
     {
+        T oldResult;
         synchronized (this)
         {
+            oldResult = result;
             result = null;
             resultDeclaration = null;
         }
 
+        if (oldResult != null)
+        {
+            removeEventListeners(oldResult);
+        }
         fireBuilderEvent(new ConfigurationBuilderEvent(this,
                 ConfigurationBuilderEvent.RESET));
     }
@@ -694,6 +700,23 @@ public class BasicConfigurationBuilder<T
     }
 
     /**
+     * Removes all available event listeners from the given result object. This
+     * method is called when the result of this builder is reset. Then the old
+     * managed configuration should no longer generate events.
+     *
+     * @param obj the affected result object
+     */
+    private void removeEventListeners(T obj)
+    {
+        EventSource evSrc = ConfigurationUtils.asEventSource(obj, true);
+        for (EventListenerRegistrationData<?> regData : eventListeners
+                .getRegistrations())
+        {
+            removeListener(evSrc, regData);
+        }
+    }
+
+    /**
      * Returns an {@code EventSource} for the current result object. If there is
      * no current result or if it does not extend {@code EventSource}, a dummy
      * event source is returned.
@@ -789,4 +812,17 @@ public class BasicConfigurationBuilder<T
     {
         evSrc.addEventListener(regData.getEventType(), regData.getListener());
     }
+
+    /**
+     * Removes an event listener from an event source object.
+     *
+     * @param evSrc the event source
+     * @param regData the registration data object
+     * @param <E> the type of the event listener
+     */
+    private static <E extends Event> void removeListener(EventSource evSrc,
+            EventListenerRegistrationData<E> regData)
+    {
+        evSrc.removeEventListener(regData.getEventType(), regData.getListener());
+    }
 }

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=1612686&r1=1612685&r2=1612686&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 Tue Jul 22 20:10:31 2014
@@ -462,6 +462,27 @@ public class TestBasicConfigurationBuild
     }
 
     /**
+     * Tests whether configuration listeners are removed from the managed
+     * configuration when the builder's result object is reset.
+     */
+    @Test
+    public void testRemoveConfigurationListenersOnReset()
+            throws ConfigurationException
+    {
+        EventListenerTestImpl listener = new EventListenerTestImpl(null);
+        BasicConfigurationBuilder<PropertiesConfiguration> builder =
+                new BasicConfigurationBuilder<PropertiesConfiguration>(
+                        PropertiesConfiguration.class)
+                        .configure(new EventListenerParameters()
+                                .addEventListener(ConfigurationEvent.ANY,
+                                        listener));
+        PropertiesConfiguration config = builder.getConfiguration();
+        builder.resetResult();
+        config.addProperty("foo", "bar");
+        listener.done();
+    }
+
+    /**
      * Tests whether parameters starting with a reserved prefix are filtered out
      * before result objects are initialized.
      */