FileDocCategorySizeDatePackage
LockManager.javaAPI DocExample1315Mon Nov 24 09:38:42 GMT 2003com.oreilly.patterns.chapter10

LockManager

public class LockManager extends Object

Fields Summary
private HashMap
locks
private static HashMap
managers
Constructors Summary
public LockManager()
Create a new LockManager instance.

    locks = new HashMap();
  
Methods Summary
public static synchronized com.oreilly.patterns.chapter10.LockManagergetLockManager(java.lang.String managerName)
Get a named Lock Manager. The manager will be created if not found.


                  
        
    LockManager manager = (LockManager)managers.get(managerName);
    if(manager == null) {
        manager = new LockManager();
        managers.put(managerName, manager);
    }
    return manager;
  
public java.lang.ObjectreleaseLock(java.lang.Object lockable)
Release a Lockable object.

    return locks.remove(lockable);
  
public booleanrequestLock(java.lang.String username, java.lang.Object lockable)
Request a lock from this LockManager instance.

    if(username == null)
      return false; // or raise exception
      
    synchronized(locks) {
      if(!locks.containsKey(lockable)) {
        locks.put(lockable, username);
        return true;
      }
      // Return true if this user already has a lock
      return (username.equals(locks.get(lockable)));
    }