You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2011/02/15 15:23:56 UTC

svn commit: r1070916 - in /jackrabbit/trunk: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java

Author: jukka
Date: Tue Feb 15 14:23:55 2011
New Revision: 1070916

URL: http://svn.apache.org/viewvc?rev=1070916&view=rev
Log:
JCR-2874: Locked helper class improvements

Patch by Alex Parvulescu
Also fixed a few typos and whitespace issues

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java?rev=1070916&r1=1070915&r2=1070916&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Locked.java Tue Feb 15 14:23:55 2011
@@ -23,6 +23,7 @@ import javax.jcr.Session;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.lock.Lock;
 import javax.jcr.lock.LockException;
+import javax.jcr.lock.LockManager;
 import javax.jcr.observation.Event;
 import javax.jcr.observation.EventIterator;
 import javax.jcr.observation.EventListener;
@@ -99,7 +100,28 @@ public abstract class Locked {
      */
     public Object with(Node lockable, boolean isDeep)
             throws RepositoryException, InterruptedException {
-        return with(lockable, isDeep, Long.MAX_VALUE);
+        return with(lockable, isDeep, true);
+    }
+
+    /**
+     * Executes {@link #run} while the lock on <code>lockable</code> is held.
+     * This method will block until {@link #run} is executed while holding the
+     * lock on node <code>lockable</code>.
+     *
+     * @param lockable a lockable node.
+     * @param isDeep   <code>true</code> if <code>lockable</code> will be locked
+     *                 deep.
+     * @param isSessionScoped <code>true</code> if the lock is session scoped.
+     * @return the object returned by {@link #run}.
+     * @throws IllegalArgumentException if <code>lockable</code> is not
+     *      <i>mix:lockable</i>.
+     * @throws RepositoryException  if {@link #run} throws an exception.
+     * @throws InterruptedException if this thread is interrupted while waiting
+     *                              for the lock on node <code>lockable</code>.
+     */
+    public Object with(Node lockable, boolean isDeep, boolean isSessionScoped)
+            throws RepositoryException, InterruptedException {
+        return with(lockable, isDeep, Long.MAX_VALUE, isSessionScoped);
     }
 
     /**
@@ -109,9 +131,9 @@ public abstract class Locked {
      * @param lockable the node where the lock is obtained from.
      * @param isDeep   <code>true</code> if <code>lockable</code> will be locked
      *                 deep.
-     * @param timeout  time in milliseconds to wait at most to aquire the lock.
+     * @param timeout  time in milliseconds to wait at most to acquire the lock.
      * @return the object returned by {@link #run} or {@link #TIMED_OUT} if the
-     *         lock on <code>lockable</code> could not be aquired within the
+     *         lock on <code>lockable</code> could not be acquired within the
      *         specified timeout.
      * @throws IllegalArgumentException if <code>timeout</code> is negative or
      *                                  <code>lockable</code> is not
@@ -126,6 +148,34 @@ public abstract class Locked {
      */
     public Object with(Node lockable, boolean isDeep, long timeout)
             throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException {
+        return with(lockable, isDeep, timeout, true);
+    }
+
+    /**
+     * Executes the method {@link #run} within the scope of a lock held on
+     * <code>lockable</code>.
+     *
+     * @param lockable the node where the lock is obtained from.
+     * @param isDeep   <code>true</code> if <code>lockable</code> will be locked
+     *                 deep.
+     * @param timeout  time in milliseconds to wait at most to acquire the lock.
+     * @param isSessionScoped <code>true</code> if the lock is session scoped.
+     * @return the object returned by {@link #run} or {@link #TIMED_OUT} if the
+     *         lock on <code>lockable</code> could not be acquired within the
+     *         specified timeout.
+     * @throws IllegalArgumentException if <code>timeout</code> is negative or
+     *                                  <code>lockable</code> is not
+     *                                  <i>mix:lockable</i>.
+     * @throws RepositoryException      if {@link #run} throws an exception.
+     * @throws UnsupportedRepositoryOperationException
+     *                                  if this repository does not support
+     *                                  locking.
+     * @throws InterruptedException     if this thread is interrupted while
+     *                                  waiting for the lock on node
+     *                                  <code>lockable</code>.
+     */
+    public Object with(Node lockable, boolean isDeep, long timeout, boolean isSessionScoped)
+            throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException {
         if (timeout < 0) {
             throw new IllegalArgumentException("timeout must be >= 0");
         }
@@ -140,7 +190,7 @@ public abstract class Locked {
                 throw new IllegalArgumentException("Node is not lockable");
             }
 
-            Lock lock = tryLock(lockable, isDeep);
+            Lock lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
             if (lock != null) {
                 return runAndUnlock(lock);
             }
@@ -170,12 +220,12 @@ public abstract class Locked {
                         lockable.getPath(), false, null, null, true);
             }
 
-            // now keep trying to aquire the lock
+            // now keep trying to acquire the lock
             // using 'this' as a monitor allows the event listener to notify
             // the current thread when the lockable node is possibly unlocked
             for (; ;) {
                 synchronized (this) {
-                    lock = tryLock(lockable, isDeep);
+                    lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
                     if (lock != null) {
                         return runAndUnlock(lock);
                     } else {
@@ -223,28 +273,32 @@ public abstract class Locked {
      * @throws RepositoryException if an error occurs.
      */
     private Object runAndUnlock(Lock lock) throws RepositoryException {
+        Node node = lock.getNode();
         try {
-            return run(lock.getNode());
+            return run(node);
         } finally {
-            lock.getNode().unlock();
+            node.getSession().getWorkspace().getLockManager().unlock(node.getPath());
         }
     }
 
     /**
-     * Tries to aquire a session scoped lock on <code>lockable</code>.
+     * Tries to acquire a session scoped lock on <code>lockable</code>.
      *
      * @param lockable the lockable node
      * @param isDeep   <code>true</code> if the lock should be deep
+     * @param timeout  time in milliseconds to wait at most to acquire the lock.
+     * @param isSessionScoped <code>true</code> if the lock is session scoped.
      * @return The <code>Lock</code> or <code>null</code> if the
      *         <code>lockable</code> cannot be locked.
      * @throws UnsupportedRepositoryOperationException
      *                             if this repository does not support locking.
      * @throws RepositoryException if an error occurs
      */
-    private static Lock tryLock(Node lockable, boolean isDeep)
+    private static Lock tryLock(Node lockable, boolean isDeep, long timeout, boolean isSessionScoped)
             throws UnsupportedRepositoryOperationException, RepositoryException {
         try {
-            return lockable.lock(isDeep, true);
+            LockManager lm = lockable.getSession().getWorkspace().getLockManager();
+            return lm.lock(lockable.getPath(), isDeep, isSessionScoped, timeout, null);
         } catch (LockException e) {
             // locked by some other session
         }

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java?rev=1070916&r1=1070915&r2=1070916&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/lock/Locked.java Tue Feb 15 14:23:55 2011
@@ -75,6 +75,8 @@ import javax.jcr.lock.LockException;
  *     long nextValue = ((Long) ret).longValue();
  * }
  * </pre>
+ *
+ * @deprecated Use org.apache.jackrabbit.util.Locked instead.
  */
 public abstract class Locked {