james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r722193 - /james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java
Date Mon, 01 Dec 2008 19:35:27 GMT
Author: rdonkin
Date: Mon Dec  1 11:35:27 2008
New Revision: 722193

URL: http://svn.apache.org/viewvc?rev=722193&view=rev
Log:
https://issues.apache.org/jira/browse/MIME4J-89  Improve thread synchronization in MultiReferenceStorage.
Contributed by Markus Wiederkehr MIME4J-89.

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java?rev=722193&r1=722192&r2=722193&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java
(original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/storage/MultiReferenceStorage.java
Mon Dec  1 11:35:27 2008
@@ -50,6 +50,7 @@
      * 
      * @param storage
      *            storage back-end that should be reference counted.
+     * @throws IllegalArgumentException when storage is null
      */
     public MultiReferenceStorage(Storage storage) {
         if (storage == null)
@@ -66,11 +67,8 @@
      *             if the reference counter is zero which implies that the
      *             backing storage has already been deleted.
      */
-    public synchronized void addReference() {
-        if (referenceCounter == 0)
-            throw new IllegalStateException("storage has been deleted");
-
-        referenceCounter++;
+    public void addReference() {
+        incrementCounter();
     }
 
     /**
@@ -79,14 +77,13 @@
      * <p>
      * A client that holds a reference to this object must make sure not to
      * invoke this method a second time.
+     * 
+     * @throws IllegalStateException
+     *             if the reference counter is zero which implies that the
+     *             backing storage has already been deleted.
      */
-    public synchronized void delete() {
-        if (referenceCounter == 0)
-            return;
-
-        referenceCounter--;
-
-        if (referenceCounter == 0) {
+    public void delete() {
+        if (decrementCounter()) {
             storage.delete();
         }
     }
@@ -100,4 +97,27 @@
         return storage.getInputStream();
     }
 
+    /**
+     * Synchronized increment of reference count.
+     */
+    private synchronized void incrementCounter() {
+        if (referenceCounter == 0)
+            throw new IllegalStateException("storage has been deleted");
+
+        referenceCounter++;
+    }
+
+    /**
+     * Synchronized decrement of reference count.
+     * @return true when counter has reached zero,
+     * false otherwise
+     * @throws IllegalArgumentException when counter
+     * is already zero
+     */
+    private synchronized boolean decrementCounter() {
+        if (referenceCounter == 0)
+            throw new IllegalStateException("storage has been deleted");
+
+        return --referenceCounter == 0;
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message