FileDocCategorySizeDatePackage
Lock.javaAPI DocApache James 2.3.13487Fri Jan 12 12:56:34 GMT 2007org.apache.james.util

Lock

public class Lock extends Object
Provides Lock functionality

Fields Summary
private Hashtable
locks
An internal hash table of keys to locks
Constructors Summary
Methods Summary
public booleancanI(java.lang.Object key)
Check to see if we can lock on a given object.

param
key the Object on which to lock
return
true if the calling thread can lock, false otherwise

        Object o = locks.get( key );

        if (null == o || o == this.getCallerId()) {
            return true;
        }

        return false;
    
private java.lang.ObjectgetCallerId()
Private helper method to abstract away caller ID.

return
the id of the caller (i.e. the Thread reference)

        return Thread.currentThread();
    
public booleanisLocked(java.lang.Object key)
Check to see if the object is locked

param
key the Object on which to check the lock
return
true if the object is locked, false otherwise


                                    
         
        return (locks.get(key) != null);
    
public booleanlock(java.lang.Object key)
Lock on a given object.

param
key the Object on which to lock
return
true if the locking was successful, false otherwise

        Object theLock;

        synchronized(this) {
            theLock = locks.get(key);

            if (null == theLock) {
                locks.put(key, getCallerId());
                return true;
            } else if (getCallerId() == theLock) {
                return true;
            } else {
                return false;
            }
        }
    
public booleanunlock(java.lang.Object key)
Release the lock on a given object.

param
key the Object on which the lock is held
return
true if the unlocking was successful, false otherwise

        Object theLock;
        synchronized (this) {
            theLock = locks.get(key);

            if (null == theLock) {
                return true;
            } else if (getCallerId() == theLock) {
                locks.remove(key);
                return true;
            } else {
                return false;
            }
        }