commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1597287 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/ test/java/org/apache/commons/configuration/builder/
Date Sat, 24 May 2014 13:51:54 GMT
Author: oheger
Date: Sat May 24 13:51:54 2014
New Revision: 1597287

URL: http://svn.apache.org/r1597287
Log:
Added a new builder event type.

The event ConfigurationBuilderResultCreatedEvent is now generated every time a
new result object of the builder is created.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderResultCreatedEvent.java
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/TestBasicConfigurationBuilderEvents.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=1597287&r1=1597286&r2=1597287&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 May 24 13:51:54 2014
@@ -350,6 +350,7 @@ public class BasicConfigurationBuilder<T
                 ConfigurationBuilderEvent.CONFIGURATION_REQUEST));
 
         T resObj = result;
+        boolean created = false;
         if (resObj == null)
         {
             synchronized (this)
@@ -358,9 +359,18 @@ public class BasicConfigurationBuilder<T
                 if (resObj == null)
                 {
                     result = resObj = createResult();
+                    created = true;
                 }
             }
         }
+
+        if (created)
+        {
+            eventListeners.fire(new ConfigurationBuilderResultCreatedEvent(
+                    this,
+                    ConfigurationBuilderResultCreatedEvent.RESULT_CREATED,
+                    resObj));
+        }
         return resObj;
     }
 

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderResultCreatedEvent.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderResultCreatedEvent.java?rev=1597287&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderResultCreatedEvent.java
(added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderResultCreatedEvent.java
Sat May 24 13:51:54 2014
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.event.EventType;
+
+/**
+ * <p>
+ * A specialized event class which is generated by a
+ * {@link ConfigurationBuilder} when a result configuration has been created.
+ * </p>
+ * <p>
+ * Events of this type are fired in the {@code getConfiguration()} method of a
+ * configuration builder each time a new result object is created. At the time
+ * the event is fired, no lock is held. The newly created {@code Configuration}
+ * object is available as a property of this event.
+ * </p>
+ * <p>
+ * A use case for this event is to perform special initializations on newly
+ * created configuration objects. It is also an indication that a builder is now
+ * fully initialized; i.e. the managed configuration is available.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class ConfigurationBuilderResultCreatedEvent extends
+        ConfigurationBuilderEvent
+{
+    /**
+     * The specialized event type for a newly created result configuration.
+     * Events of this type are generated by a configuration builder when a
+     * result configuration has been created.
+     */
+    public static final EventType<ConfigurationBuilderResultCreatedEvent> RESULT_CREATED
=
+            new EventType<ConfigurationBuilderResultCreatedEvent>(ANY,
+                    "RESULT_CREATED");
+
+    /** The newly created configuration object. */
+    private final Configuration configuration;
+
+    /**
+     * Creates a new instance of {@code ConfigurationBuilderResultCreatedEvent}
+     * and initializes its properties.
+     *
+     * @param source the {@code ConfigurationBuilder} object which triggered
+     *        this event (must not be <b>null</b>)
+     * @param evType the type of this event (must not be <b>null</b>)
+     * @param createdConfiguration the newly created {@code Configuration}
+     *        object (must not be <b>null</b>)
+     * @throws IllegalArgumentException if a required parameter is null
+     */
+    public ConfigurationBuilderResultCreatedEvent(
+            ConfigurationBuilder<?> source,
+            EventType<? extends ConfigurationBuilderResultCreatedEvent> evType,
+            Configuration createdConfiguration)
+    {
+        super(source, evType);
+        if (createdConfiguration == null)
+        {
+            throw new IllegalArgumentException(
+                    "Configuration must not be null!");
+        }
+        configuration = createdConfiguration;
+    }
+
+    /**
+     * Returns the newly created {@code Configuration} object.
+     *
+     * @return the newly created {@code Configuration}
+     */
+    public Configuration getConfiguration()
+    {
+        return configuration;
+    }
+}

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java?rev=1597287&r1=1597286&r2=1597287&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java
Sat May 24 13:51:54 2014
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 
+import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.configuration.event.Event;
 import org.apache.commons.configuration.event.EventListener;
@@ -163,4 +164,49 @@ public class TestBasicConfigurationBuild
         listener.nextEvent(ConfigurationBuilderEvent.RESET);
         listener.assertNoMoreEvents();
     }
+
+    /**
+     * Tries to create an event about a newly created configuration without a
+     * configuration instance.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testResultCreatedEventNoConfiguration()
+    {
+        new ConfigurationBuilderResultCreatedEvent(
+                new BasicConfigurationBuilder<Configuration>(
+                        Configuration.class),
+                ConfigurationBuilderResultCreatedEvent.RESULT_CREATED, null);
+    }
+
+    /**
+     * Tests whether the type of a result created event is correctly configured.
+     */
+    @Test
+    public void testResultCreatedEventType()
+    {
+        assertEquals("Wrong super type", ConfigurationBuilderEvent.ANY,
+                ConfigurationBuilderResultCreatedEvent.RESULT_CREATED
+                        .getSuperType());
+    }
+
+    /**
+     * Tests whether a result created event is correctly generated.
+     */
+    @Test
+    public void testResultCreatedEvent() throws ConfigurationException
+    {
+        BasicConfigurationBuilder<PropertiesConfiguration> builder =
+                new BasicConfigurationBuilder<PropertiesConfiguration>(
+                        PropertiesConfiguration.class);
+        BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
+        builder.addEventListener(ConfigurationBuilderEvent.ANY, listener);
+
+        PropertiesConfiguration configuration = builder.getConfiguration();
+        listener.nextEvent(ConfigurationBuilderEvent.CONFIGURATION_REQUEST);
+        ConfigurationBuilderResultCreatedEvent event =
+                listener.nextEvent(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED);
+        assertSame("Wrong builder", builder, event.getSource());
+        assertSame("Wrong configuration", configuration,
+                event.getConfiguration());
+    }
 }



Mime
View raw message