commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1427351 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/ test/resources/
Date Tue, 01 Jan 2013 10:47:07 GMT
Author: oheger
Date: Tue Jan  1 10:47:07 2013
New Revision: 1427351

URL: http://svn.apache.org/viewvc?rev=1427351&view=rev
Log:
Added a specialized combined configuration builder with reloading support.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
  (with props)
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
  (with props)
    commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml   (with props)
    commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml   (with
props)
Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilderProvider.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilderProvider.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilderProvider.java?rev=1427351&r1=1427350&r2=1427351&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilderProvider.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilderProvider.java
Tue Jan  1 10:47:07 2013
@@ -47,6 +47,10 @@ public class CombinedConfigurationBuilde
     private static final String BUILDER_CLASS =
             "org.apache.commons.configuration.builder.combined.CombinedConfigurationBuilder";
 
+    /** Constant for the name of the supported reloading builder class. */
+    private static final String RELOADING_BUILDER_CLASS =
+            "org.apache.commons.configuration.builder.combined.ReloadingCombinedConfigurationBuilder";
+
     /** Constant for the name of the supported configuration class. */
     private static final String CONFIGURATION_CLASS =
             "org.apache.commons.configuration.CombinedConfiguration";
@@ -64,8 +68,8 @@ public class CombinedConfigurationBuilde
      */
     public CombinedConfigurationBuilderProvider()
     {
-        super(BUILDER_CLASS, BUILDER_CLASS, CONFIGURATION_CLASS, Arrays.asList(
-                COMBINED_PARAMS, FILE_PARAMS));
+        super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, CONFIGURATION_CLASS,
+                Arrays.asList(COMBINED_PARAMS, FILE_PARAMS));
     }
 
     /**
@@ -80,8 +84,15 @@ public class CombinedConfigurationBuilde
             ConfigurationDeclaration decl, Collection<BuilderParameters> params)
             throws Exception
     {
-        CombinedConfigurationBuilder builder =
-                new CombinedConfigurationBuilder();
+        CombinedConfigurationBuilder builder;
+        if (decl.isReload())
+        {
+            builder = new ReloadingCombinedConfigurationBuilder();
+        }
+        else
+        {
+            builder = new CombinedConfigurationBuilder();
+        }
         decl.getConfigurationBuilder().initChildEventListeners(builder);
         return builder;
     }

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java?rev=1427351&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
(added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
Tue Jan  1 10:47:07 2013
@@ -0,0 +1,182 @@
+/*
+ * 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.combined;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.apache.commons.configuration.CombinedConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.builder.BuilderParameters;
+import org.apache.commons.configuration.builder.ConfigurationBuilder;
+import org.apache.commons.configuration.builder.ReloadingFileBasedConfigurationBuilder;
+import org.apache.commons.configuration.reloading.CombinedReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingControllerSupport;
+
+/**
+ * <p>
+ * An extension of {@code CombinedConfigurationBuilder} which also supports
+ * reloading operations.
+ * </p>
+ * <p>
+ * This class differs from its super class in the following aspects:
+ * <ul>
+ * <li>A {@link ReloadingController} is created which manages all child
+ * configuration builders supporting reloading operations.</li>
+ * <li>If no {@code ConfigurationBuilder} is provided for the definition
+ * configuration, a builder with reloading support is created.</li>
+ * </ul>
+ * </p>
+ * <p>
+ * This class can be used exactly as its super class for creating combined
+ * configurations from multiple configuration sources. In addition, the combined
+ * reloading controller managed by an instance can be used to react on changes
+ * in one of these configuration sources or in the definition configuration.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class ReloadingCombinedConfigurationBuilder extends
+        CombinedConfigurationBuilder implements ReloadingControllerSupport
+{
+    /** The reloading controller used by this builder. */
+    private ReloadingController reloadingController;
+
+    /**
+     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder}.
+     * No parameters are set.
+     */
+    public ReloadingCombinedConfigurationBuilder()
+    {
+        super();
+    }
+
+    /**
+     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder}
+     * and sets the specified initialization parameters and the
+     * <em>allowFailOnInit</em> flag.
+     *
+     * @param params a map with initialization parameters
+     * @param allowFailOnInit the <em>allowFailOnInit</em> flag
+     */
+    public ReloadingCombinedConfigurationBuilder(Map<String, Object> params,
+            boolean allowFailOnInit)
+    {
+        super(params, allowFailOnInit);
+    }
+
+    /**
+     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder}
+     * and sets the specified initialization parameters.
+     *
+     * @param params a map with initialization parameters
+     */
+    public ReloadingCombinedConfigurationBuilder(Map<String, Object> params)
+    {
+        super(params);
+    }
+
+    /**
+     * {@inheritDoc} This implementation returns a
+     * {@link CombinedReloadingController} which contains sub controllers for
+     * all child configuration sources with reloading support. If the definition
+     * builder supports reloading, its controller is contained, too. Note that
+     * the combined reloading controller is initialized when the result
+     * configuration is created (i.e. when calling {@code getConfiguration()}
+     * for the first time). So this method does not return a meaningful result
+     * before.
+     */
+    public synchronized ReloadingController getReloadingController()
+    {
+        return reloadingController;
+    }
+
+    /**
+     * {@inheritDoc} This implementation creates a builder for XML
+     * configurations with reloading support.
+     */
+    @Override
+    protected ConfigurationBuilder<? extends HierarchicalConfiguration> createXMLDefinitionBuilder(
+            BuilderParameters builderParams)
+    {
+        return new ReloadingFileBasedConfigurationBuilder<XMLConfiguration>(
+                XMLConfiguration.class).configure(builderParams);
+    }
+
+    /**
+     * {@inheritDoc} This implementation first calls the super method to
+     * actually initialize the result configuration. Then it creates the
+     * {@link CombinedReloadingController} for all child configuration sources
+     * with reloading support.
+     */
+    @Override
+    protected void initResultInstance(CombinedConfiguration result)
+            throws ConfigurationException
+    {
+        super.initResultInstance(result);
+        reloadingController = createReloadingController();
+    }
+
+    /**
+     * Creates the {@code ReloadingController} for this builder. This method is
+     * called after the result configuration has been created and initialized.
+     * It is called from a synchronized block. This implementation creates a
+     * {@link CombinedReloadingController}.
+     *
+     * @return the {@code ReloadingController} for this builder
+     * @throws ConfigurationException if an error occurs
+     */
+    protected ReloadingController createReloadingController()
+            throws ConfigurationException
+    {
+        Collection<ReloadingController> subControllers =
+                new LinkedList<ReloadingController>();
+        ConfigurationBuilder<? extends HierarchicalConfiguration> defBuilder =
+                getDefinitionBuilder();
+        obtainReloadingController(subControllers, defBuilder);
+
+        for (ConfigurationBuilder<? extends Configuration> b : getChildBuilders())
+        {
+            obtainReloadingController(subControllers, b);
+        }
+
+        return new CombinedReloadingController(subControllers);
+    }
+
+    /**
+     * Checks whether the passed in builder object supports reloading. If yes,
+     * its reloading controller is obtained and added to the given list.
+     *
+     * @param subControllers the list with sub controllers
+     * @param builder the builder object to be checked
+     */
+    public static void obtainReloadingController(
+            Collection<ReloadingController> subControllers, Object builder)
+    {
+        if (builder instanceof ReloadingControllerSupport)
+        {
+            subControllers.add(((ReloadingControllerSupport) builder)
+                    .getReloadingController());
+        }
+    }
+}

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/ReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java?rev=1427351&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
(added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
Tue Jan  1 10:47:07 2013
@@ -0,0 +1,156 @@
+/*
+ * 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.combined;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.util.Collection;
+
+import org.apache.commons.configuration.CombinedConfiguration;
+import org.apache.commons.configuration.ConfigurationAssert;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.builder.FileBasedBuilderParametersImpl;
+import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration.reloading.CombinedReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingController;
+import org.apache.commons.configuration.reloading.ReloadingControllerSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code ReloadingCombinedConfigurationBuilder}.
+ *
+ * @version $Id$
+ */
+public class TestReloadingCombinedConfigurationBuilder
+{
+    /** The builder to be tested. */
+    private ReloadingCombinedConfigurationBuilder builder;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        builder = new ReloadingCombinedConfigurationBuilder();
+    }
+
+    /**
+     * Tests a definition configuration which does not contain sources with
+     * reloading support.
+     */
+    @Test
+    public void testNoReloadableSources() throws ConfigurationException
+    {
+        File testFile =
+                ConfigurationAssert
+                        .getTestFile("testDigesterConfiguration.xml");
+        builder.configure(new CombinedBuilderParametersImpl()
+                .setDefinitionBuilder(
+                        new FileBasedConfigurationBuilder<XMLConfiguration>(
+                                XMLConfiguration.class))
+                .setDefinitionBuilderParameters(
+                        new FileBasedBuilderParametersImpl().setFile(testFile)));
+        builder.getConfiguration();
+        CombinedReloadingController rc =
+                (CombinedReloadingController) builder.getReloadingController();
+        assertTrue("Got sub reloading controllers", rc.getSubControllers()
+                .isEmpty());
+    }
+
+    /**
+     * Tests whether the definition builder created by default supports
+     * reloading.
+     */
+    @Test
+    public void testReloadableDefinitionBuilder() throws ConfigurationException
+    {
+        File testFile =
+                ConfigurationAssert
+                        .getTestFile("testDigesterConfiguration.xml");
+        builder.configure(new FileBasedBuilderParametersImpl()
+                .setFile(testFile));
+        builder.getConfiguration();
+        CombinedReloadingController rc =
+                (CombinedReloadingController) builder.getReloadingController();
+        Collection<ReloadingController> subControllers = rc.getSubControllers();
+        assertEquals("Wrong number of sub controllers", 1,
+                subControllers.size());
+        ReloadingController subctrl =
+                ((ReloadingControllerSupport) builder.getDefinitionBuilder())
+                        .getReloadingController();
+        assertSame("Wrong sub controller", subctrl, subControllers.iterator()
+                .next());
+    }
+
+    /**
+     * Tests whether a nested combined configuration definition can be loaded
+     * with reloading support.
+     */
+    @Test
+    public void testNestedReloadableSources() throws ConfigurationException
+    {
+        File testFile =
+                ConfigurationAssert.getTestFile("testCCReloadingNested.xml");
+        builder.configure(new FileBasedBuilderParametersImpl()
+                .setFile(testFile));
+        builder.getConfiguration();
+        CombinedReloadingController rc =
+                (CombinedReloadingController) builder.getReloadingController();
+        Collection<ReloadingController> subControllers = rc.getSubControllers();
+        assertEquals("Wrong number of sub controllers", 2,
+                subControllers.size());
+        ReloadingControllerSupport ccBuilder =
+                (ReloadingControllerSupport) builder.getNamedBuilder("cc");
+        assertTrue("Sub controller not found",
+                subControllers.contains(ccBuilder.getReloadingController()));
+        CombinedReloadingController rc2 =
+                (CombinedReloadingController) ccBuilder
+                        .getReloadingController();
+        assertEquals("Wrong number of sub controllers (2)", 3, rc2
+                .getSubControllers().size());
+    }
+
+    /**
+     * Tests whether initialization parameters are correctly processed.
+     */
+    @Test
+    public void testInitWithParameters() throws ConfigurationException
+    {
+        FileBasedBuilderParametersImpl params =
+                new FileBasedBuilderParametersImpl();
+        params.setFile(ConfigurationAssert
+                .getTestFile("testDigesterConfiguration.xml"));
+        builder =
+                new ReloadingCombinedConfigurationBuilder(
+                        params.getParameters());
+        CombinedConfiguration cc = builder.getConfiguration();
+        assertTrue("Property not found", cc.getBoolean("test.boolean"));
+    }
+
+    /**
+     * Tests whether the failOnInit flag is passed to the super constructor.
+     */
+    @Test
+    public void testInitWithFailOnInitFlag()
+    {
+        builder = new ReloadingCombinedConfigurationBuilder(null, true);
+        assertTrue("Flag not set", builder.isAllowFailOnInit());
+    }
+}

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestReloadingCombinedConfigurationBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml?rev=1427351&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml (added)
+++ commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml Tue Jan  1 10:47:07
2013
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!--
+   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.
+-->
+<!-- Test configuration definition file that contains some sources which
+     support reloading.
+     $Id$
+-->
+
+<configuration>
+  <properties fileName="test.properties" config-name="props" config-reload="true"/>
+  <properties fileName="test.properties.xml"/>
+  <xml fileName="test.xml" config-name="xml" config-reload="true"/>
+</configuration>

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloading.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml?rev=1427351&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml (added)
+++ commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml Tue Jan
 1 10:47:07 2013
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!--
+   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.
+-->
+<!-- Test configuration definition file that contains a nested combined
+     configuration definition with reloading support.
+     $Id$
+-->
+
+<configuration>
+  <configuration fileName="testCCReloading.xml" config-reload="true"
+    config-name="cc"/>
+</configuration>

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/resources/testCCReloadingNested.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message