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/05/24 15:52:37 UTC

svn commit: r1597288 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java

Author: oheger
Date: Sat May 24 13:52:37 2014
New Revision: 1597288

URL: http://svn.apache.org/r1597288
Log:
Added ReloadingBuilderSupportListener class.

This is an internally used helper class which adds support for reloading to an
arbitrary BasicConfigurationBuilder by associating it with a reloading
controller.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java?rev=1597288&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingBuilderSupportListener.java Sat May 24 13:52:37 2014
@@ -0,0 +1,115 @@
+/*
+ * 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.event.EventListener;
+import org.apache.commons.configuration.reloading.ReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingEvent;
+import org.apache.commons.configuration.reloading.ReloadingListener;
+
+/**
+ * <p>
+ * An internally helper class for adding reloading support to an arbitrary
+ * {@link ConfigurationBuilder}.
+ * </p>
+ * <p>
+ * This class connects a configuration builder with a
+ * {@link ReloadingController}. This is done in the following way:
+ * <ul>
+ * <li>An instance is registered as listener at a {@code ReloadingController}.
+ * Whenever the controller indicates that a reload should happen, the associated
+ * configuration builder's {@link BasicConfigurationBuilder#resetResult()}
+ * method is called.</li>
+ * <li>When the builder fires a {@link ConfigurationBuilderResultCreatedEvent}
+ * event the reloading controller's reloading state is reset. At that time the
+ * reload has actually happened, and the controller is prepared to observe new
+ * changes.</li>
+ * </ul>
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+class ReloadingBuilderSupportListener implements ReloadingListener,
+        EventListener<ConfigurationBuilderEvent>
+{
+    /** Stores the associated configuration builder. */
+    private final BasicConfigurationBuilder<?> builder;
+
+    /** Stores the associated reloading controller. */
+    private final ReloadingController reloadingController;
+
+    /**
+     * Creates a new instance of {@code ReloadingBuilderSupportListener} and
+     * initializes it with the associated objects.
+     *
+     * @param configBuilder the configuration builder
+     * @param controller the {@code ReloadingController}
+     */
+    private ReloadingBuilderSupportListener(
+            BasicConfigurationBuilder<?> configBuilder,
+            ReloadingController controller)
+    {
+        builder = configBuilder;
+        reloadingController = controller;
+    }
+
+    /**
+     * Creates a new instance of {@code ReloadingBuilderSupportListener} which
+     * connects the specified {@code ConfigurationBuilder} with the given
+     * {@code ReloadingController}. Listeners are registered to react on
+     * notifications and implement a reloading protocol as described in the
+     * class comment.
+     *
+     * @param configBuilder the {@code ConfigurationBuilder}
+     * @param controller the {@code ReloadingController}
+     * @return the newly created listener object
+     */
+    public static ReloadingBuilderSupportListener connect(
+            BasicConfigurationBuilder<?> configBuilder,
+            ReloadingController controller)
+    {
+        ReloadingBuilderSupportListener listener =
+                new ReloadingBuilderSupportListener(configBuilder, controller);
+        controller.addReloadingListener(listener);
+        configBuilder
+                .addEventListener(
+                        ConfigurationBuilderResultCreatedEvent.RESULT_CREATED,
+                        listener);
+        return listener;
+    }
+
+    /**
+     * {@inheritDoc} This implementation resets the builder's managed
+     * configuration.
+     */
+    @Override
+    public void reloadingRequired(ReloadingEvent event)
+    {
+        builder.resetResult();
+    }
+
+    /**
+     * {@inheritDoc} This implementation resets the controller's reloading
+     * state.
+     */
+    @Override
+    public void onEvent(ConfigurationBuilderEvent event)
+    {
+        reloadingController.resetReloadingState();
+    }
+}

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java?rev=1597288&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingBuilderSupportListener.java Sat May 24 13:52:37 2014
@@ -0,0 +1,85 @@
+/*
+ * 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 static org.junit.Assert.assertNotNull;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.ex.ConfigurationException;
+import org.apache.commons.configuration.reloading.ReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingDetector;
+import org.apache.commons.configuration.reloading.ReloadingListener;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+/**
+ * Test class for {@code ReloadingBuilderSupportListener}.
+ */
+public class TestReloadingBuilderSupportListener
+{
+    /**
+     * Tests that the builder is reset when a reloading event notification
+     * occurs.
+     */
+    @Test
+    public void testResetBuilderOnReloadingEvent()
+    {
+        ReloadingDetector detector =
+                EasyMock.createMock(ReloadingDetector.class);
+        EasyMock.expect(detector.isReloadingRequired()).andReturn(Boolean.TRUE);
+        EasyMock.replay(detector);
+        ReloadingController controller = new ReloadingController(detector);
+        BasicConfigurationBuilder<Configuration> builder =
+                new BasicConfigurationBuilder<Configuration>(
+                        PropertiesConfiguration.class);
+        BuilderEventListenerImpl builderListener =
+                new BuilderEventListenerImpl();
+        builder.addEventListener(ConfigurationBuilderEvent.ANY, builderListener);
+
+        ReloadingBuilderSupportListener listener =
+                ReloadingBuilderSupportListener.connect(builder, controller);
+        assertNotNull("No listener returned", listener);
+        controller.checkForReloading(null);
+        builderListener.nextEvent(ConfigurationBuilderEvent.RESET);
+        builderListener.assertNoMoreEvents();
+    }
+
+    /**
+     * Tests that the controller's reloading state is reset when a new result
+     * object is created.
+     */
+    @Test
+    public void testResetReloadingStateOnResultCreation()
+            throws ConfigurationException
+    {
+        ReloadingController controller =
+                EasyMock.createMock(ReloadingController.class);
+        controller.addReloadingListener(EasyMock
+                .anyObject(ReloadingListener.class));
+        controller.resetReloadingState();
+        EasyMock.replay(controller);
+        BasicConfigurationBuilder<Configuration> builder =
+                new BasicConfigurationBuilder<Configuration>(
+                        PropertiesConfiguration.class);
+
+        ReloadingBuilderSupportListener listener =
+                ReloadingBuilderSupportListener.connect(builder, controller);
+        builder.getConfiguration();
+        EasyMock.verify(controller);
+    }
+}