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/06 16:07:31 UTC

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

Author: oheger
Date: Sat Oct  6 14:07:31 2012
New Revision: 1395066

URL: http://svn.apache.org/viewvc?rev=1395066&view=rev
Log:
Extended ConfigurationBuilder interface by support for BuilderListeners.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilder.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=1395066&r1=1395065&r2=1395066&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 Sat Oct  6 14:07:31 2012
@@ -30,6 +30,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.event.EventSource;
+import org.apache.commons.lang3.event.EventListenerSupport;
 
 /**
  * <p>
@@ -128,6 +129,9 @@ public class BasicConfigurationBuilder<T
      */
     private final Collection<ConfigurationErrorListener> errorListeners;
 
+    /** An object managing the builder listeners registered at this builder. */
+    private final EventListenerSupport<BuilderListener> builderListeners;
+
     /** The map with current initialization parameters. */
     private Map<String, Object> parameters;
 
@@ -170,6 +174,7 @@ public class BasicConfigurationBuilder<T
         resultClass = resCls;
         configListeners = new ArrayList<ConfigurationListener>();
         errorListeners = new ArrayList<ConfigurationErrorListener>();
+        builderListeners = EventListenerSupport.create(BuilderListener.class);
         updateParameters(params);
     }
 
@@ -327,14 +332,44 @@ public class BasicConfigurationBuilder<T
     }
 
     /**
+     * {@inheritDoc} The listener must not be <b>null</b>, otherwise an
+     * exception is thrown.
+     *
+     * @throws IllegalArgumentException if the listener is <b>null</b>
+     */
+    public void addBuilderListener(BuilderListener l)
+    {
+        if (l == null)
+        {
+            throw new IllegalArgumentException(
+                    "Builder listener must not be null!");
+        }
+        builderListeners.addListener(l);
+    }
+
+    /**
+     * {@inheritDoc} If the specified listener is not registered at this object,
+     * this method has no effect.
+     */
+    public void removeBuilderListener(BuilderListener l)
+    {
+        builderListeners.removeListener(l);
+    }
+
+    /**
      * Clears an existing result object. An invocation of this method causes a
      * new {@code Configuration} object to be created the next time
      * {@link #getConfiguration()} is called.
      */
     public synchronized void resetResult()
     {
-        result = null;
-        resultDeclaration = null;
+        synchronized (this)
+        {
+            result = null;
+            resultDeclaration = null;
+        }
+
+        builderListeners.fire().builderReset(this);
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilder.java?rev=1395066&r1=1395065&r2=1395066&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilder.java Sat Oct  6 14:07:31 2012
@@ -47,4 +47,18 @@ public interface ConfigurationBuilder<T 
      * @throws ConfigurationException if an error occurs
      */
     T getConfiguration() throws ConfigurationException;
+
+    /**
+     * Adds the specified {@code BuilderListener} to this builder.
+     *
+     * @param l the listener to be registered
+     */
+    void addBuilderListener(BuilderListener l);
+
+    /**
+     * Removes the specified {@code BuilderListener} from this builder.
+     *
+     * @param l the listener to be removed
+     */
+    void removeBuilderListener(BuilderListener l);
 }

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=1395066&r1=1395065&r2=1395066&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 Sat Oct  6 14:07:31 2012
@@ -409,6 +409,41 @@ public class TestBasicConfigurationBuild
     }
 
     /**
+     * Tries to add a null builder listener.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddBuilderListenerNull()
+    {
+        BasicConfigurationBuilder<PropertiesConfiguration> builder =
+                new BasicConfigurationBuilder<PropertiesConfiguration>(
+                        PropertiesConfiguration.class);
+        builder.addBuilderListener(null);
+    }
+
+    /**
+     * Tests whether builder listeners can be added and removed.
+     */
+    @Test
+    public void testAddAndRemoveBuilderListener()
+    {
+        BuilderListener l1 = EasyMock.createMock(BuilderListener.class);
+        BuilderListener l2 = EasyMock.createMock(BuilderListener.class);
+        BasicConfigurationBuilder<PropertiesConfiguration> builder =
+                new BasicConfigurationBuilder<PropertiesConfiguration>(
+                        PropertiesConfiguration.class);
+        builder.addBuilderListener(l1);
+        builder.addBuilderListener(l2);
+        l1.builderReset(builder);
+        EasyMock.expectLastCall().times(2);
+        l2.builderReset(builder);
+        EasyMock.replay(l1, l2);
+        builder.reset();
+        builder.removeBuilderListener(l2);
+        builder.resetResult();
+        EasyMock.verify(l1, l2);
+    }
+
+    /**
      * A test thread class for testing whether the builder's result object can
      * be requested concurrently.
      */