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/15 22:05:43 UTC

svn commit: r1483059 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/PropertiesConfiguration.java test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java

Author: oheger
Date: Wed May 15 20:05:43 2013
New Revision: 1483059

URL: http://svn.apache.org/r1483059
Log:
Reworked PropertiesConfiguration regarding thread-safety.

Some special methods of PropertiesConfiguration now call the Synchronizer
correctly. Removed synchronization from properties set via the builder.
Updated Javadoc regarding thread-safety.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1483059&r1=1483058&r2=1483059&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java Wed May 15 20:05:43 2013
@@ -159,9 +159,16 @@ import org.apache.commons.lang3.text.tra
  * method can be used to obtain this layout object. With {@code setLayout()}
  * a new layout object can be set. This should be done before a properties file
  * was loaded.
- * <p><em>Note:</em>Configuration objects of this type can be read concurrently
- * by multiple threads. However if one of these threads modifies the object,
- * synchronization has to be performed manually.
+ * <p>Like other {@code Configuration} implementations, this class uses a
+ * {@code Synchronizer} object to control concurrent access. By choosing a
+ * suitable implementation of the {@code Synchronizer} interface, an instance
+ * can be made thread-safe or not. Note that access to most of the properties
+ * typically set through a builder is not protected by the {@code Synchronizer}.
+ * The intended usage is that these properties are set once at construction
+ * time through the builder and after that remain constant. If you wish to
+ * change such properties during life time of an instance, you have to use
+ * the {@code lock()} and {@code unlock()} methods manually to ensure that
+ * other threads see your changes.
  *
  * @see java.util.Properties#load
  *
@@ -232,7 +239,7 @@ public class PropertiesConfiguration ext
     private PropertiesConfigurationLayout layout;
 
     /** The IOFactory for creating readers and writers.*/
-    private volatile IOFactory ioFactory;
+    private IOFactory ioFactory;
 
     /** The current {@code FileLocator}. */
     private FileLocator locator;
@@ -301,7 +308,15 @@ public class PropertiesConfiguration ext
      */
     public String getHeader()
     {
-        return getLayout().getHeaderComment();
+        beginRead();
+        try
+        {
+            return getLayout().getHeaderComment();
+        }
+        finally
+        {
+            endRead();
+        }
     }
 
     /**
@@ -312,7 +327,15 @@ public class PropertiesConfiguration ext
      */
     public void setHeader(String header)
     {
-        getLayout().setHeaderComment(header);
+        beginWrite();
+        try
+        {
+            getLayout().setHeaderComment(header);
+        }
+        finally
+        {
+            endWrite();
+        }
     }
 
     /**
@@ -324,7 +347,15 @@ public class PropertiesConfiguration ext
      */
     public String getFooter()
     {
-        return getLayout().getFooterComment();
+        beginRead();
+        try
+        {
+            return getLayout().getFooterComment();
+        }
+        finally
+        {
+            endRead();
+        }
     }
 
     /**
@@ -336,7 +367,15 @@ public class PropertiesConfiguration ext
      */
     public void setFooter(String footer)
     {
-        getLayout().setFooterComment(footer);
+        beginWrite();
+        try
+        {
+            getLayout().setFooterComment(footer);
+        }
+        finally
+        {
+            endWrite();
+        }
     }
 
     /**
@@ -345,7 +384,7 @@ public class PropertiesConfiguration ext
      * @return the associated layout object
      * @since 1.3
      */
-    public synchronized PropertiesConfigurationLayout getLayout()
+    public PropertiesConfigurationLayout getLayout()
     {
         return layout;
     }
@@ -357,7 +396,7 @@ public class PropertiesConfiguration ext
      * layout object will be created
      * @since 1.3
      */
-    public synchronized void setLayout(PropertiesConfigurationLayout layout)
+    public void setLayout(PropertiesConfigurationLayout layout)
     {
         installLayout(layout);
     }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java?rev=1483059&r1=1483058&r2=1483059&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java Wed May 15 20:05:43 2013
@@ -48,6 +48,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.commons.configuration.SynchronizerTestImpl.Methods;
 import org.apache.commons.configuration.builder.FileBasedBuilderParametersImpl;
 import org.apache.commons.configuration.builder.combined.CombinedConfigurationBuilder;
 import org.apache.commons.configuration.io.FileHandler;
@@ -1001,6 +1002,54 @@ public class TestPropertiesConfiguration
     }
 
     /**
+     * Tests whether read access to the footer comment is synchronized.
+     */
+    @Test
+    public void testGetFooterSynchronized()
+    {
+        SynchronizerTestImpl sync = new SynchronizerTestImpl();
+        conf.setSynchronizer(sync);
+        assertNotNull("No footer comment", conf.getFooter());
+        sync.verify(Methods.BEGIN_READ, Methods.END_READ);
+    }
+
+    /**
+     * Tests whether write access to the footer comment is synchronized.
+     */
+    @Test
+    public void testSetFooterSynchronized()
+    {
+        SynchronizerTestImpl sync = new SynchronizerTestImpl();
+        conf.setSynchronizer(sync);
+        conf.setFooter("new comment");
+        sync.verify(Methods.BEGIN_WRITE, Methods.END_WRITE);
+    }
+
+    /**
+     * Tests whether read access to the header comment is synchronized.
+     */
+    @Test
+    public void testGetHeaderSynchronized()
+    {
+        SynchronizerTestImpl sync = new SynchronizerTestImpl();
+        conf.setSynchronizer(sync);
+        assertNull("Got a header comment", conf.getHeader());
+        sync.verify(Methods.BEGIN_READ, Methods.END_READ);
+    }
+
+    /**
+     * Tests whether write access to the header comment is synchronized.
+     */
+    @Test
+    public void testSetHeaderSynchronized()
+    {
+        SynchronizerTestImpl sync = new SynchronizerTestImpl();
+        conf.setSynchronizer(sync);
+        conf.setHeader("new comment");
+        sync.verify(Methods.BEGIN_WRITE, Methods.END_WRITE);
+    }
+
+    /**
      * Tests the escaping of quotation marks in a properties value. This test is
      * related to CONFIGURATION-516.
      */