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 2013/05/05 22:24:26 UTC

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

Author: oheger
Date: Sun May  5 20:24:25 2013
New Revision: 1479379

URL: http://svn.apache.org/r1479379
Log:
Added synchronization to read methods of BaseHierarchicalConfiguration.

Some methods have been made final to prevent subclasses from breaking
synchronization. Corresponding protected methods with the suffix 'Internal'
were added.

Added:
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseHierarchicalConfigurationSynchronization.java
Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DynamicCombinedConfiguration.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PatternSubtreeConfigurationWrapper.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java?rev=1479379&r1=1479378&r2=1479379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java Sun May  5 20:24:25 2013
@@ -224,9 +224,31 @@ public class BaseHierarchicalConfigurati
     }
 
     /**
-     * {@inheritDoc} This implementation just returns the name of the root node.
+     * {@inheritDoc} This implementation handles synchronization and delegates
+     * to {@code getRootElementNameInternal()}.
      */
-    public String getRootElementName()
+    public final String getRootElementName()
+    {
+        beginRead();
+        try
+        {
+            return getRootElementNameInternal();
+        }
+        finally
+        {
+            endRead();
+        }
+    }
+
+    /**
+     * Actually obtains the name of the root element. This method is called by
+     * {@code getRootElementName()}. It just returns the name of the root node.
+     * Subclasses that treat the root element name differently can override this
+     * method.
+     * @return the name of this configuration's root element
+     * @since 2.0
+     */
+    protected String getRootElementNameInternal()
     {
         return getRootNode().getName();
     }
@@ -908,12 +930,35 @@ public class BaseHierarchicalConfigurati
      * Returns the maximum defined index for the given key. This is useful if
      * there are multiple values for this key. They can then be addressed
      * separately by specifying indices from 0 to the return value of this
-     * method.
+     * method. If the passed in key is not contained in this configuration,
+     * result is -1.
      *
      * @param key the key to be checked
      * @return the maximum defined index for this key
      */
-    public int getMaxIndex(String key)
+    public final int getMaxIndex(String key)
+    {
+        beginRead();
+        try
+        {
+            return getMaxIndexInternal(key);
+        }
+        finally
+        {
+            endRead();
+        }
+    }
+
+    /**
+     * Actually retrieves the maximum defined index for the given key. This
+     * method is called by {@code getMaxIndex()}. Subclasses that need to adapt
+     * this operation have to override this method.
+     *
+     * @param key the key to be checked
+     * @return the maximum defined index for this key
+     * @since 2.0
+     */
+    protected int getMaxIndexInternal(String key)
     {
         return fetchNodeList(key).size() - 1;
     }
@@ -929,6 +974,7 @@ public class BaseHierarchicalConfigurati
     @Override
     public Object clone()
     {
+        beginRead();
         try
         {
             BaseHierarchicalConfiguration copy = (BaseHierarchicalConfiguration) super
@@ -947,6 +993,10 @@ public class BaseHierarchicalConfigurati
             // should not happen
             throw new ConfigurationRuntimeException(cex);
         }
+        finally
+        {
+            endRead();
+        }
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DynamicCombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DynamicCombinedConfiguration.java?rev=1479379&r1=1479378&r2=1479379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DynamicCombinedConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DynamicCombinedConfiguration.java Sun May  5 20:24:25 2013
@@ -624,7 +624,7 @@ public class DynamicCombinedConfiguratio
     }
 
     @Override
-    public int getMaxIndex(String key)
+    protected int getMaxIndexInternal(String key)
     {
         return this.getCurrentConfig().getMaxIndex(key);
     }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PatternSubtreeConfigurationWrapper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PatternSubtreeConfigurationWrapper.java?rev=1479379&r1=1479378&r2=1479379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PatternSubtreeConfigurationWrapper.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PatternSubtreeConfigurationWrapper.java Sun May  5 20:24:25 2013
@@ -397,7 +397,7 @@ public class PatternSubtreeConfiguration
     }
 
     @Override
-    public int getMaxIndex(String key)
+    protected int getMaxIndexInternal(String key)
     {
         return config.getMaxIndex(makePath(key));
     }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1479379&r1=1479378&r2=1479379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java Sun May  5 20:24:25 2013
@@ -257,7 +257,7 @@ public class XMLConfiguration extends Ba
      * @return the name of the root element
      */
     @Override
-    public String getRootElementName()
+    protected String getRootElementNameInternal()
     {
         if (getDocument() == null)
         {

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseHierarchicalConfigurationSynchronization.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseHierarchicalConfigurationSynchronization.java?rev=1479379&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseHierarchicalConfigurationSynchronization.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseHierarchicalConfigurationSynchronization.java Sun May  5 20:24:25 2013
@@ -0,0 +1,82 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.configuration.SynchronizerTestImpl.Methods;
+import org.apache.commons.configuration.io.FileHandler;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * A test class for {@code BaseHierarchicalConfiguration} which checks whether
+ * the Synchronizer is called correctly by the methods specific for hierarchical
+ * configurations.
+ *
+ * @version $Id: $
+ */
+public class TestBaseHierarchicalConfigurationSynchronization
+{
+    /** The test synchronizer. */
+    private SynchronizerTestImpl sync;
+
+    /** The test configuration. */
+    private BaseHierarchicalConfiguration config;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        XMLConfiguration c = new XMLConfiguration();
+        new FileHandler(c).load(ConfigurationAssert.getTestFile("test.xml"));
+        sync = new SynchronizerTestImpl();
+        c.setSynchronizer(sync);
+        config = c;
+    }
+
+    /**
+     * Tests whether getMaxIndex() is correctly synchronized.
+     */
+    @Test
+    public void testGetMaxIndexSynchronized()
+    {
+        assertTrue("Wrong max index", config.getMaxIndex("list.item") > 0);
+        sync.verify(Methods.BEGIN_READ, Methods.END_READ);
+    }
+
+    /**
+     * Tests whether getRootElementName() is correctly synchronized.
+     */
+    @Test
+    public void testGetRootElementNameSynchronized()
+    {
+        assertEquals("Wrong root element name", "testconfig",
+                config.getRootElementName());
+        sync.verify(Methods.BEGIN_READ, Methods.END_READ);
+    }
+
+    /**
+     * Tests whether clone() is correctly synchronized.
+     */
+    @Test
+    public void testCloneSynchronized()
+    {
+        config.clone();
+        sync.verify(Methods.BEGIN_READ, Methods.END_READ);
+    }
+}