Added an indefinite acquire version of the constructor

Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/543860f4
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/543860f4
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/543860f4

Branch: refs/heads/CURATOR-3.0
Commit: 543860f4811fa5327cabe524dad786363eb3f504
Parents: 58a8818
Author: randgalt <randg...@apache.org>
Authored: Sun Sep 6 15:43:58 2015 -0700
Committer: randgalt <randg...@apache.org>
Committed: Sun Sep 6 15:43:58 2015 -0700

----------------------------------------------------------------------
 .../curator/framework/recipes/locks/Locker.java | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/543860f4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/Locker.java
----------------------------------------------------------------------
diff --git 
a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/Locker.java
 
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/Locker.java
index 97788af..7eb362d 100644
--- 
a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/Locker.java
+++ 
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/Locker.java
@@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 public class Locker implements AutoCloseable
 {
     private final InterProcessLock lock;
-    private final AtomicBoolean acquired;
+    private final AtomicBoolean acquired = new AtomicBoolean(false);
 
     /**
      * @param lock a lock implementation (e.g. {@link InterProcessMutex}, 
{@link InterProcessSemaphoreV2}, etc.)
@@ -35,13 +35,24 @@ public class Locker implements AutoCloseable
     public Locker(InterProcessLock lock, long timeout, TimeUnit unit) throws 
Exception
     {
         this.lock = lock;
-        acquired = new AtomicBoolean(acquireLock(lock, timeout, unit));
+        acquired.set(acquireLock(lock, timeout, unit));
         if ( !acquired.get() )
         {
             throw new TimeoutException("Could not acquire lock within timeout 
of " + unit.toMillis(timeout) + "ms");
         }
     }
 
+    /**
+     * @param lock a lock implementation (e.g. {@link InterProcessMutex}, 
{@link InterProcessSemaphoreV2}, etc.)
+     * @throws Exception errors
+     */
+    public Locker(InterProcessLock lock) throws Exception
+    {
+        this.lock = lock;
+        acquireLock(lock);
+        acquired.set(true);
+    }
+
     @Override
     /**
      * Relase the lock if it has been acquired. Can be safely called multiple 
times.
@@ -60,6 +71,11 @@ public class Locker implements AutoCloseable
         lock.release();
     }
 
+    protected void acquireLock(InterProcessLock lock) throws Exception
+    {
+        lock.acquire();
+    }
+
     protected boolean acquireLock(InterProcessLock lock, long timeout, 
TimeUnit unit) throws Exception
     {
         return lock.acquire(timeout, unit);

Reply via email to