org.entityfs.lock
Class AbstractLockable

java.lang.Object
  extended by org.entityfs.lock.AbstractLockable
All Implemented Interfaces:
Lockable, ReadLockable, ReadWriteLockable, WriteLockable
Direct Known Subclasses:
AbstractEntityImpl, ManualEntityListable, ManualNamedReadableFile

public abstract class AbstractLockable
extends Object
implements ReadWriteLockable

This is an abstract base class that may be inherited by classes that want to implement ReadLockable and WriteLockable.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
entityfs-core

Constructor Summary
protected AbstractLockable(EntityLockAdapterFactory laf, LockAcquiringStrategy las, Object[] lockIdentifiers)
           
 
Method Summary
 void assertIsReadLocked()
          Throw an exception if this lockable object is not read locked by the current thread.
 void assertIsWriteLocked()
          Throw an exception if this lockable object is not write locked by the current thread.
protected  LockAcquiringStrategy getLockAcquiringStrategy()
           
protected  EntityLockAdapter getLockAdapter()
           
protected  Lockable getLocked()
          Get the object that will be the locked object of the locks.
protected  LogAdapter getLogAdapter()
          Subclasses should override this to return a log adapter if they have access to one.
 EntityLock getReadLock()
          Get the read lock without locking it.
 EntityLock getWriteLock()
          Get the write lock without locking it.
 boolean isReadLockedByCurrentThread()
          Is this lockable object read locked by the current thread?
 boolean isWriteLockedByCurrentThread()
          Is this lockable object write locked by the current thread?
 EntityLock lock(Object identifier)
           
 EntityLock lockForReading()
          Lock object for reading and return the lock.
 EntityLock lockForWriting()
          Lock the object for writing and return the lock.
protected  void logLockStack(LockTimeoutException e)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractLockable

protected AbstractLockable(EntityLockAdapterFactory laf,
                           LockAcquiringStrategy las,
                           Object[] lockIdentifiers)
Parameters:
laf - The lock adapter factory. Set to null to disable logging.
las - The lock acquiring strategy. Set to null if logging is disabled.
lockIdentifiers - An array of identifiers
Method Detail

getLockAdapter

protected EntityLockAdapter getLockAdapter()

getLockAcquiringStrategy

protected LockAcquiringStrategy getLockAcquiringStrategy()

getLogAdapter

protected LogAdapter getLogAdapter()
Subclasses should override this to return a log adapter if they have access to one.

Returns:
null (no logging).

logLockStack

protected void logLockStack(LockTimeoutException e)

getLocked

protected Lockable getLocked()
Get the object that will be the locked object of the locks. This implementation returns this. It may be overridden to return something else.

Returns:
this.

lock

public EntityLock lock(Object identifier)

getReadLock

public EntityLock getReadLock()
Description copied from interface: ReadLockable
Get the read lock without locking it.

This method always returns a lock, even if the object's file system is not locking or if the object is read only.

Specified by:
getReadLock in interface ReadLockable
Returns:
The lockable's read lock. This might be unlocked or locked by someone else.

lockForReading

public EntityLock lockForReading()
Description copied from interface: ReadLockable
Lock object for reading and return the lock. How the lock is acquired is determined by the EntityLockAdapterFactory and LockAcquiringStrategy used. If this is called on an object that somehow is connected to an entity object, that entity's file system's entity lock adapter factory and lock acquiring strategy is used.

A read lock cannot be upgraded to a write lock (just like how Java's ReadWriteLock works).

For more discussions on locks and locking, see Entity and FileSystem.

This method always returns a lock, even if the object's file system is not locking or if the object is read only.

Specified by:
lockForReading in interface ReadLockable
Returns:
A locked read lock.

getWriteLock

public EntityLock getWriteLock()
Description copied from interface: WriteLockable
Get the write lock without locking it.

This method always returns a lock, even if the object's file system is not locking or if the object is read only.

Specified by:
getWriteLock in interface WriteLockable
Returns:
The lockable's write lock. This might be unlocked or locked by someone else.

lockForWriting

public EntityLock lockForWriting()
Description copied from interface: WriteLockable
Lock the object for writing and return the lock. How the lock is acquired is determined by the EntityLockAdapterFactory and LockAcquiringStrategy used. If this is called on an object that is somehow connected to an Entity object, that entity's file systems' lock adapter factory and lock acquiring strategy is used. A write lock can be downgraded to a read lock (by calling ReadLockable.lockForReading() and then releasing the write lock).

An entity in a locking read only file system may still be locked for writing.

For more discussions on locks and locking, see Entity and FileSystem.

This method always returns a lock, even if the file system is not locking.

Specified by:
lockForWriting in interface WriteLockable
Returns:
A locked write lock.

isReadLockedByCurrentThread

public boolean isReadLockedByCurrentThread()
Is this lockable object read locked by the current thread?

Specified by:
isReadLockedByCurrentThread in interface ReadLockable
Returns:
If this object is read locked by the current thread, or if locking is disabled altogether.
See Also:
assertIsReadLocked()

isWriteLockedByCurrentThread

public boolean isWriteLockedByCurrentThread()
Is this lockable object write locked by the current thread?

Specified by:
isWriteLockedByCurrentThread in interface WriteLockable
Returns:
If this object is write locked by the current thread, or if locking is disabled altogether.
See Also:
assertIsWriteLocked()

assertIsReadLocked

public void assertIsReadLocked()
                        throws ReadLockRequiredException
Throw an exception if this lockable object is not read locked by the current thread.

Throws:
ReadLockRequiredException - If this lockable object is not locked for reading by the current thread.
See Also:
isReadLockedByCurrentThread()

assertIsWriteLocked

public void assertIsWriteLocked()
                         throws WriteLockRequiredException
Throw an exception if this lockable object is not write locked by the current thread.

Throws:
WriteLockRequiredException - If this lockable object is not locked for writing by the current thread.
See Also:
isWriteLockedByCurrentThread()