org.apache.commons.transaction.locking
Class AbstractLockManager<K,M>

java.lang.Object
  extended by org.apache.commons.transaction.locking.AbstractLockManager<K,M>
All Implemented Interfaces:
LockManager<K,M>
Direct Known Subclasses:
RWLockManager, SimpleLockManager

public abstract class AbstractLockManager<K,M>
extends Object
implements LockManager<K,M>

Abstract implementation of LockManager. You can use this implementation as a base for your custom implementations.

This implementation is thread-safe.


Nested Class Summary
protected static class AbstractLockManager.KeyEntry<K,M>
           
 
Field Summary
protected  Map<Thread,Long> effectiveGlobalTimeouts
           
protected  Map<Thread,Set<Lock>> locksForThreads
           
 
Constructor Summary
AbstractLockManager()
           
 
Method Summary
protected  void checkIsStarted()
           
protected  long computeRemainingTime(Thread thread)
           
 void endWork()
          Ends a block of work that has been started in LockManager.startWork(long, TimeUnit).
protected  boolean hasTimedOut(Thread thread)
           
 boolean isWorking()
           
 void lock(M resourceManager, K key, boolean exclusive)
          Locks a resource denoted by a key and a resource manager.
protected abstract  void release()
           
protected  void reportTimeout(Thread thread)
           
 void startWork(long timeout, TimeUnit unit)
          Starts a block of work for which a certain set of locks is required.
 boolean tryLock(M resourceManager, K key, boolean exclusive)
          Tries to acquire a lock on a resource denoted by a key and a resource manager.
protected abstract  boolean tryLockInternal(M resourceManager, K key, boolean exclusive, long time, TimeUnit unit)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locksForThreads

protected Map<Thread,Set<Lock>> locksForThreads

effectiveGlobalTimeouts

protected Map<Thread,Long> effectiveGlobalTimeouts
Constructor Detail

AbstractLockManager

public AbstractLockManager()
Method Detail

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<K,M>

release

protected abstract void release()

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<K,M>
Parameters:
timeout - the maximum time for the whole work to take before it times out
unit - the time unit of the timeout argument

checkIsStarted

protected void checkIsStarted()

computeRemainingTime

protected long computeRemainingTime(Thread thread)

isWorking

public boolean isWorking()

lock

public void lock(M resourceManager,
                 K 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<K,M>
Parameters:
resourceManager - 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

tryLock

public boolean tryLock(M resourceManager,
                       K 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<K,M>
Parameters:
resourceManager - 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

tryLockInternal

protected abstract boolean tryLockInternal(M resourceManager,
                                           K key,
                                           boolean exclusive,
                                           long time,
                                           TimeUnit unit)
                                    throws LockException
Throws:
LockException

reportTimeout

protected void reportTimeout(Thread thread)
                      throws LockException
Throws:
LockException

hasTimedOut

protected boolean hasTimedOut(Thread thread)


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