1 7 package org.jboss.cache; 8 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.jboss.cache.config.Option; 13 import org.jboss.cache.lock.IdentityLock; 14 import org.jboss.cache.lock.NodeLock; 15 import org.jboss.cache.marshall.MethodCall; 16 17 import javax.transaction.Transaction ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.LinkedHashSet ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 26 42 public class TransactionEntry 43 { 44 45 static Log log = LogFactory.getLog(TransactionEntry.class); 46 47 50 private Transaction ltx = null; 51 private Option option; 52 53 56 private List <MethodCall> modification_list = new LinkedList <MethodCall>(); 57 private List <MethodCall> cl_mod_list = new LinkedList <MethodCall>(); 58 59 65 private List undo_list = new LinkedList (); 66 67 73 private LinkedHashSet locks = new LinkedHashSet (); 74 75 79 private List dummyNodesCreatedByCacheLoader; 80 81 84 private List <Fqn> removedNodes = new LinkedList <Fqn>(); 85 private List <MethodCall> cacheListenerEvents = new LinkedList <MethodCall>(); 86 87 90 public TransactionEntry() 91 { 92 } 93 94 97 public void addModification(MethodCall m) 98 { 99 if (m == null) return; 100 modification_list.add(m); 101 } 102 103 public void addCacheLoaderModification(MethodCall m) 104 { 105 if (m != null) cl_mod_list.add(m); 106 } 107 108 109 112 public List <MethodCall> getModifications() 113 { 114 return modification_list; 115 } 116 117 public List <MethodCall> getCacheLoaderModifications() 118 { 119 return cl_mod_list; 120 } 121 122 127 public void addUndoOperation(MethodCall m) 128 { 129 undo_list.add(m); 130 } 131 132 137 public void addRemovedNode(Fqn fqn) 138 { 139 removedNodes.add(fqn); 140 } 141 142 145 public List <Fqn> getRemovedNodes() 146 { 147 return new ArrayList (removedNodes); 148 } 149 150 151 155 public List getUndoOperations() 156 { 157 return undo_list; 158 } 159 160 163 public void setTransaction(Transaction tx) 164 { 165 ltx = tx; 166 } 167 168 171 public Transaction getTransaction() 172 { 173 return ltx; 174 } 175 176 179 public void addLock(NodeLock l) 180 { 181 if (l != null) 182 { 183 synchronized (locks) 184 { 185 locks.add(l); 186 } 187 } 188 } 189 190 195 public void addLocks(Collection newLocks) 196 { 197 if (newLocks != null) 198 { 199 synchronized (locks) 200 { 201 locks.addAll(newLocks); 202 } 203 } 204 } 205 206 211 public List getLocks() 212 { 213 synchronized (locks) 214 { 215 return new ArrayList (locks); 216 } 217 } 218 219 224 public void releaseAllLocks(Object owner) 225 { 226 releaseAllLocksFIFO(owner); 227 synchronized (locks) 228 { 229 locks.clear(); 230 } 231 } 232 233 237 public void releaseAllLocksLIFO(Object owner) 238 { 239 240 synchronized (locks) 241 { 242 IdentityLock[] lockArray = (IdentityLock[]) locks.toArray(new IdentityLock[locks.size()]); 245 for (int i = lockArray.length - 1; i >= 0; i--) 246 { 247 if (log.isTraceEnabled()) 248 { 249 log.trace("releasing lock for " + lockArray[i].getFqn() + " (" + lockArray[i] + ")"); 250 } 251 lockArray[i].release(owner); 252 } 253 locks.clear(); 254 } 255 } 256 257 261 public void releaseAllLocksFIFO(Object owner) 262 { 263 synchronized (locks) 266 { 267 for (Iterator i = locks.iterator(); i.hasNext();) 268 { 269 IdentityLock lock = (IdentityLock) i.next(); 270 lock.release(owner); 271 if (log.isTraceEnabled()) 272 { 273 log.trace("releasing lock for " + lock.getFqn() + " (" + lock + ")"); 274 } 275 } 276 } 277 } 278 279 282 public void undoOperations(CacheSPI cache) 283 { 284 if (log.isTraceEnabled()) 285 { 286 log.trace("undoOperations " + undo_list); 287 } 288 ArrayList l; 289 synchronized (undo_list) 290 { 291 l = new ArrayList (undo_list); 292 } 293 for (ListIterator i = l.listIterator(l.size()); i.hasPrevious();) 294 { 295 MethodCall undo_op = (MethodCall) i.previous(); 296 undo(undo_op, cache); 297 } 298 } 299 300 private void undo(MethodCall undo_op, CacheSPI cache) 301 { 302 try 303 { 304 Object retval = undo_op.invoke(cache); 305 if (retval instanceof Throwable ) 306 { 307 throw (Throwable ) retval; 308 } 309 } 310 catch (Throwable t) 311 { 312 log.error("undo operation failed, error=" + t); 313 log.trace(t, t); 314 } 315 } 316 317 320 public String toString() 321 { 322 StringBuffer sb = new StringBuffer (); 323 sb.append("\nmodification_list: ").append(modification_list); 324 synchronized (undo_list) 325 { 326 sb.append("\nundo_list: ").append(undo_list); 327 } 328 synchronized (locks) 329 { 330 sb.append("\nlocks: ").append(locks); 331 } 332 return sb.toString(); 333 } 334 335 public void loadUninitialisedNode(Fqn fqn) 336 { 337 if (dummyNodesCreatedByCacheLoader == null) dummyNodesCreatedByCacheLoader = new LinkedList (); 338 dummyNodesCreatedByCacheLoader.add(fqn); 339 } 340 341 public List getDummyNodesCreatedByCacheLoader() 342 { 343 return dummyNodesCreatedByCacheLoader; 344 } 345 346 351 public void setOption(Option o) 352 { 353 this.option = o; 354 } 355 356 359 public Option getOption() 360 { 361 return this.option; 362 } 363 364 public void addCacheListenerEvents(List <MethodCall> cacheListenerEvents) 365 { 366 this.cacheListenerEvents.addAll(cacheListenerEvents); 367 } 368 369 public List <MethodCall> getCacheListenerEvents() 370 { 371 return cacheListenerEvents; 372 } 373 } 374 | Popular Tags |