org.apache.commons.transaction.locking
Class DefaultHierarchicalLockManager<M>

java.lang.Object
  extended by org.apache.commons.transaction.locking.DefaultHierarchicalLockManager<M>
All Implemented Interfaces:
HierarchicalLockManager<Object,M>, LockManager<Object,M>

public class DefaultHierarchicalLockManager<M>
extends Object
implements HierarchicalLockManager<Object,M>

Default implementation of the HierarchicalLockManager.

It splits the path into segments and non-exclusively locks each segment beginning from the root. The final segment - which is supposed to be the resource to be locked itself - can either be locked exclusively or non-exclusively. Too choose between the two, provide the flag in lockInHierarchy(Object, String, boolean).

This implementation needs an ordinary lock manager that it delegates all locking calls to.

This implementation is thread-safe.


Constructor Summary
DefaultHierarchicalLockManager(String rootPath, LockManager<Object,M> lm)
           
 
Method Summary
 void endWork()
          Ends a block of work that has been started in LockManager.startWork(long, TimeUnit).
 void lock(M managedResource, Object key, boolean exclusive)
          Locks a resource denoted by a key and a resource manager.
 void lockInHierarchy(M resourceManager, String path, boolean exclusive)
          Locks a specific resource denoted by a resource manager it holds and a path.
 void startWork(long timeout, TimeUnit unit)
          Starts a block of work for which a certain set of locks is required.
 boolean tryLock(M managedResource, Object key, boolean exclusive)
          Tries to acquire a lock on a resource denoted by a key and a resource manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultHierarchicalLockManager

public DefaultHierarchicalLockManager(String rootPath,
                                      LockManager<Object,M> lm)
Method Detail

lockInHierarchy

public void lockInHierarchy(M resourceManager,
                            String path,
                            boolean exclusive)
                     throws LockException
Description copied from interface: HierarchicalLockManager
Locks a specific resource denoted by a resource manager it holds and a path. This requests ensures that the resource is properly locked in its hierarchy, possibly by issuing a number of additional lock requests.

Specified by:
lockInHierarchy in interface HierarchicalLockManager<Object,M>
Parameters:
resourceManager - resource manager that tries to acquire a lock
path - the complete path to the resource to be locked
exclusive - true if this lock shall be acquired in exclusive mode, false if it can be shared by other threads
Throws:
LockException - if the lock could not be acquired, possibly because of a timeout or a deadlock

endWork

public void endWork()
Description copied from interface: LockManager
Ends a block of work that has been started in LockManager.startWork(long, TimeUnit). All locks acquired will be released. All registered locks will be unregistered from this lock manager.

Specified by:
endWork in interface LockManager<Object,M>

lock

public void lock(M managedResource,
                 Object key,
                 boolean exclusive)
          throws LockException
Description copied from interface: LockManager
Locks a resource denoted by a key and a resource manager.

Specified by:
lock in interface LockManager<Object,M>
Parameters:
managedResource - resource manager that tries to acquire a lock
key - the key for the resource to be locked
exclusive - true if this lock shall be acquired in exclusive mode, false if it can be shared by other threads
Throws:
LockException - if the lock could not be acquired, possibly because of a timeout or a deadlock

startWork

public void startWork(long timeout,
                      TimeUnit unit)
Description copied from interface: LockManager
Starts a block of work for which a certain set of locks is required.

Specified by:
startWork in interface LockManager<Object,M>
Parameters:
timeout - the maximum time for the whole work to take before it times out
unit - the time unit of the timeout argument

tryLock

public boolean tryLock(M managedResource,
                       Object key,
                       boolean exclusive)
Description copied from interface: LockManager
Tries to acquire a lock on a resource denoted by a key and a resource manager.

Specified by:
tryLock in interface LockManager<Object,M>
Parameters:
managedResource - resource manager that tries to acquire a lock
key - the key for the resource to be locked
exclusive - true if this lock shall be acquired in exclusive mode, false if it can be shared by other threads
Returns:
true if the lock was acquired, false otherwise


Copyright © 2004-2007 The Apache Software Foundation. All Rights Reserved.