1 23 24 package org.apache.commons.transaction.memory; 25 26 import java.util.Collection ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import org.apache.commons.transaction.locking.ReadWriteLockManager; 31 import org.apache.commons.transaction.util.LoggerFacade; 32 33 51 public class PessimisticMapWrapper extends TransactionalMapWrapper { 52 53 protected static final int READ = 1; 54 protected static final int WRITE = 2; 55 56 protected static final Object GLOBAL_LOCK = "GLOBAL"; 57 58 protected ReadWriteLockManager lockManager; 59 protected long readTimeOut = 60000; 61 62 70 public PessimisticMapWrapper(Map wrapped, LoggerFacade logger) { 71 this(wrapped, new HashMapFactory(), new HashSetFactory(), logger); 72 } 73 74 84 public PessimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory, LoggerFacade logger) { 85 super(wrapped, mapFactory, setFactory); 86 lockManager = new ReadWriteLockManager(logger, readTimeOut); 87 } 89 90 public void startTransaction() { 91 if (getActiveTx() != null) { 92 throw new IllegalStateException ( 93 "Active thread " + Thread.currentThread() + " already associated with a transaction!"); 94 } 95 LockingTxContext context = new LockingTxContext(); 96 setActiveTx(context); 97 } 98 99 public Collection values() { 100 assureGlobalReadLock(); 101 return super.values(); 102 } 103 104 public Set entrySet() { 105 assureGlobalReadLock(); 106 return super.entrySet(); 107 } 108 109 public Set keySet() { 110 assureGlobalReadLock(); 111 return super.keySet(); 112 } 113 114 public Object remove(Object key) { 115 assureWriteLock(key); 118 return super.remove(key); 119 } 120 121 public Object put(Object key, Object value) { 122 assureWriteLock(key); 125 return super.put(key, value); 126 } 127 128 protected void assureWriteLock(Object key) { 129 LockingTxContext txContext = (LockingTxContext) getActiveTx(); 130 if (txContext != null) { 131 lockManager.writeLock(txContext, key); 132 lockManager.readLock(txContext, GLOBAL_LOCK); 134 } 135 } 136 137 protected void assureGlobalReadLock() { 138 LockingTxContext txContext = (LockingTxContext) getActiveTx(); 139 if (txContext != null) { 140 lockManager.readLock(txContext, GLOBAL_LOCK); 142 } 143 } 144 145 public class LockingTxContext extends TxContext { 146 147 protected Set keys() { 148 lockManager.readLock(this, GLOBAL_LOCK); 149 return super.keys(); 150 } 151 152 protected Object get(Object key) { 153 lockManager.readLock(this, key); 154 lockManager.readLock(this, GLOBAL_LOCK); 156 return super.get(key); 157 } 158 159 protected void put(Object key, Object value) { 160 lockManager.writeLock(this, key); 161 lockManager.readLock(this, GLOBAL_LOCK); 163 super.put(key, value); 164 } 165 166 protected void remove(Object key) { 167 lockManager.writeLock(this, key); 168 lockManager.readLock(this, GLOBAL_LOCK); 170 super.remove(key); 171 } 172 173 protected int size() { 174 lockManager.readLock(this, GLOBAL_LOCK); 176 return super.size(); 177 } 178 179 protected void clear() { 180 lockManager.writeLock(this, GLOBAL_LOCK); 181 super.clear(); 182 } 183 184 protected void dispose() { 185 super.dispose(); 186 lockManager.releaseAll(this); 187 } 188 189 protected void finalize() throws Throwable { 190 dispose(); 191 super.finalize(); 192 } 193 } 194 195 } 196 | Popular Tags |