1 17 18 package org.apache.james.util; 19 20 import java.util.Hashtable ; 21 22 26 public class Lock { 27 30 private Hashtable locks = new Hashtable (); 31 32 38 public boolean isLocked(final Object key) { 39 return (locks.get(key) != null); 40 } 41 42 48 public boolean canI(final Object key) { 49 Object o = locks.get( key ); 50 51 if (null == o || o == this.getCallerId()) { 52 return true; 53 } 54 55 return false; 56 } 57 58 64 public boolean lock(final Object key) { 65 Object theLock; 66 67 synchronized(this) { 68 theLock = locks.get(key); 69 70 if (null == theLock) { 71 locks.put(key, getCallerId()); 72 return true; 73 } else if (getCallerId() == theLock) { 74 return true; 75 } else { 76 return false; 77 } 78 } 79 } 80 81 87 public boolean unlock(final Object key) { 88 Object theLock; 89 synchronized (this) { 90 theLock = locks.get(key); 91 92 if (null == theLock) { 93 return true; 94 } else if (getCallerId() == theLock) { 95 locks.remove(key); 96 return true; 97 } else { 98 return false; 99 } 100 } 101 } 102 103 108 private Object getCallerId() { 109 return Thread.currentThread(); 110 } 111 } 112 | Popular Tags |