1 22 package org.jboss.aspects.versioned; 23 24 import org.jboss.aop.InstanceAdvised; 25 import org.jboss.aop.proxy.ClassProxy; 26 import org.jboss.aop.proxy.ClassProxyFactory; 27 import org.jboss.logging.Logger; 28 import org.jboss.tm.TransactionLocal; 29 import org.jboss.util.id.GUID; 30 31 import javax.naming.InitialContext ; 32 import javax.transaction.Transaction ; 33 import javax.transaction.TransactionManager ; 34 import java.lang.reflect.Method ; 35 import java.util.Collection ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.Set ; 40 41 42 47 public class DistributedSetState extends CollectionStateManager implements Set , DistributedState, java.io.Externalizable 48 { 49 private static final long serialVersionUID = -6272170697169509758L; 50 51 private static HashMap setMethodMap; 52 53 protected static Logger log = Logger.getLogger(DistributedSetState.class); 54 static 55 { 56 try 57 { 58 setMethodMap = new HashMap (); 59 Method [] methods = Set .class.getDeclaredMethods(); 60 for (int i = 0; i < methods.length; i++) 61 { 62 long hash = org.jboss.aop.util.MethodHashing.methodHash(methods[i]); 63 setMethodMap.put(new Long (hash), methods[i]); 64 } 65 66 } 67 catch (Exception ignored) 68 { 69 ignored.printStackTrace(); 70 } 71 } 72 73 protected volatile long versionId; 74 protected HashSet updates; 75 protected String classname; 76 transient protected Set base; 77 transient protected TransactionLocal txState = new TransactionLocal(); 78 transient protected TransactionLocal txVersion = new TransactionLocal(); 79 transient protected DistributedVersionManager versionManager; 80 transient protected SynchronizationManager synchManager; 81 transient protected TransactionManager tm; 82 transient protected ClassProxy proxy; 83 84 87 public DistributedSetState() {} 88 89 90 public DistributedSetState(GUID guid, long timeout, ClassProxy proxy, Set obj, DistributedVersionManager versionManager, SynchronizationManager synchManager) 91 throws Exception 92 { 93 super(guid, timeout, setMethodMap); 94 this.base = obj; 95 this.classname = obj.getClass().getName(); 96 this.versionManager = versionManager; 97 this.synchManager = synchManager; 98 this.proxy = proxy; 99 InitialContext ctx = new InitialContext (); 100 this.tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 101 this.updates = createSetUpdates(base); 102 } 103 104 public HashMap getMethodMap() 105 { 106 return ClassProxyFactory.getMethodMap(base.getClass().getName()); 107 } 108 109 public InstanceAdvised getObject() { return proxy; } 110 111 113 protected Set getCurrentState(boolean forUpdate) throws Exception 114 { 115 Transaction tx = tm.getTransaction(); 116 if (tx == null) 117 { 118 if (forUpdate) versionId++; 119 return base; 120 } 121 122 Set state = (Set )txState.get(tx); 123 if (state == null && forUpdate) 124 { 125 state = (Set )base.getClass().newInstance(); 126 state.addAll(base); 127 txState.set(tx, state); 128 long newId = versionId + 1; 129 synchManager.registerUpdate(tx, this); 130 txVersion.set(tx, new Long (newId)); 131 return state; 132 } 133 return base; 134 } 135 136 137 protected HashSet createSetUpdates(Set state) 138 { 139 HashSet setUpdates = new HashSet (state.size()); 140 Iterator it = state.iterator(); 141 while (it.hasNext()) 142 { 143 Object obj = it.next(); 144 if (versionManager.isVersioned(obj)) 145 { 146 setUpdates.add(new VersionReference(VersionManager.getGUID((InstanceAdvised)obj))); 147 } 148 else 149 { 150 setUpdates.add(obj); 151 } 152 } 153 return setUpdates; 154 } 155 156 public DistributedUpdate createTxUpdate(Transaction tx) 157 { 158 Set state = (Set )txState.get(tx); 159 long newId = ((Long )txVersion.get(tx)).longValue(); 160 DistributedSetUpdate update = new DistributedSetUpdate(guid, createSetUpdates(state), newId); 161 return update; 162 } 163 164 public InstanceAdvised buildObject(SynchronizationManager manager, DistributedVersionManager versionManager) 165 throws Exception 166 { 167 log.trace("building a Set"); 168 this.versionManager = versionManager; 169 this.synchManager = manager; 170 log.trace("DistributedSetState: classname: " + classname); 171 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(classname); 172 base = (Set )clazz.newInstance(); 173 Iterator it = updates.iterator(); 174 while (it.hasNext()) 175 { 176 Object val = it.next(); 177 if (val instanceof VersionReference) 178 { 179 VersionReference ref = (VersionReference)val; 180 val = manager.getObject(ref.getGUID()); 181 if (val == null) 182 { 183 DistributedState fieldVal = manager.getState(ref.getGUID()); 184 val = fieldVal.buildObject(manager, versionManager); 185 ref.set((InstanceAdvised)val); 186 } 187 } 188 base.add(val); 189 } 190 proxy = versionManager.addSetVersioning(base, this); 191 return proxy; 192 } 193 194 public void checkOptimisticLock(Transaction tx) 195 { 196 Long version = (Long )txVersion.get(tx); 198 if (version.longValue() <= versionId) 199 throw new OptimisticLockFailure("optimistic lock failure for set"); 200 } 201 202 public void mergeState(Transaction tx) throws Exception 203 { 204 Set current = (Set )txState.get(tx); 206 base = current; 207 Long version = (Long )txVersion.get(tx); 208 versionId = version.longValue(); 209 } 210 211 public void mergeState(DistributedUpdate update) throws Exception 212 { 213 DistributedSetUpdate setUpdate = (DistributedSetUpdate)update; 214 this.versionId = setUpdate.versionId; 215 base.clear(); 216 Iterator it = setUpdate.setUpdates.iterator(); 217 while (it.hasNext()) 218 { 219 Object val = it.next(); 220 if (val instanceof VersionReference) 221 { 222 VersionReference ref = (VersionReference)val; 223 val = synchManager.getObject(ref.getGUID()); 224 ref.set((InstanceAdvised)val); 225 } 226 base.add(val); 227 } 228 updates = setUpdate.setUpdates; 229 } 230 231 233 public boolean add(Object val) 234 { 235 try 236 { 237 lock.readLock().acquire(); 238 try 239 { 240 val = versionManager.makeVersioned(val); 241 Set state = getCurrentState(true); 242 return state.add(val); 243 } 244 finally 245 { 246 lock.readLock().release(); 247 } 248 } 249 catch (Exception ex) 250 { 251 throw new RuntimeException (ex); 252 } 253 } 254 255 public boolean addAll(Collection c) 256 { 257 try 258 { 259 lock.readLock().acquire(); 260 try 261 { 262 Set state = getCurrentState(true); 263 Object [] copy = c.toArray(); 264 for (int i = 0; i < copy.length; i++) 265 { 266 Object item = versionManager.makeVersioned(copy[i]); 267 state.add(item); 268 } 269 return true; 270 } 271 finally 272 { 273 lock.readLock().release(); 274 } 275 } 276 catch (Exception ex) 277 { 278 throw new RuntimeException (ex); 279 } 280 } 281 282 public void clear() 283 { 284 try 285 { 286 lock.readLock().acquire(); 287 try 288 { 289 Set state = getCurrentState(true); 290 state.clear(); 291 } 292 finally 293 { 294 lock.readLock().release(); 295 } 296 } 297 catch (Exception ex) 298 { 299 throw new RuntimeException (ex); 300 } 301 } 302 303 public boolean contains(Object o) 304 { 305 try 306 { 307 lock.readLock().acquire(); 308 try 309 { 310 Set state = getCurrentState(false); 311 return state.contains(o); 312 } 313 finally 314 { 315 lock.readLock().release(); 316 } 317 } 318 catch (Exception ex) 319 { 320 throw new RuntimeException (ex); 321 } 322 } 323 public boolean containsAll(Collection c) 324 { 325 try 326 { 327 lock.readLock().acquire(); 328 try 329 { 330 Set state = getCurrentState(false); 331 return state.containsAll(c); 332 } 333 finally 334 { 335 lock.readLock().release(); 336 } 337 } 338 catch (Exception ex) 339 { 340 throw new RuntimeException (ex); 341 } 342 } 343 public boolean equals(Object o) 344 { 345 try 346 { 347 lock.readLock().acquire(); 348 try 349 { 350 Set state = getCurrentState(false); 351 return state.equals(o); 352 } 353 finally 354 { 355 lock.readLock().release(); 356 } 357 } 358 catch (Exception ex) 359 { 360 throw new RuntimeException (ex); 361 } 362 } 363 public int hashCode() 364 { 365 try 366 { 367 lock.readLock().acquire(); 368 try 369 { 370 Set state = getCurrentState(false); 371 return state.hashCode(); 372 } 373 finally 374 { 375 lock.readLock().release(); 376 } 377 } 378 catch (Exception ex) 379 { 380 throw new RuntimeException (ex); 381 } 382 } 383 public boolean isEmpty() 384 { 385 try 386 { 387 lock.readLock().acquire(); 388 try 389 { 390 Set state = getCurrentState(false); 391 return state.isEmpty(); 392 } 393 finally 394 { 395 lock.readLock().release(); 396 } 397 } 398 catch (Exception ex) 399 { 400 throw new RuntimeException (ex); 401 } 402 } 403 public Iterator iterator() 404 { 405 try 406 { 407 lock.readLock().acquire(); 408 try 409 { 410 Set state = getCurrentState(false); 411 return state.iterator(); 412 } 413 finally 414 { 415 lock.readLock().release(); 416 } 417 } 418 catch (Exception ex) 419 { 420 throw new RuntimeException (ex); 421 } 422 } 423 public boolean remove(Object o) 424 { 425 try 426 { 427 lock.readLock().acquire(); 428 try 429 { 430 Set state = getCurrentState(true); 431 return state.remove(o); 432 } 433 finally 434 { 435 lock.readLock().release(); 436 } 437 } 438 catch (Exception ex) 439 { 440 throw new RuntimeException (ex); 441 } 442 } 443 public boolean removeAll(Collection c) 444 { 445 try 446 { 447 lock.readLock().acquire(); 448 try 449 { 450 Set state = getCurrentState(true); 451 return state.removeAll(c); 452 } 453 finally 454 { 455 lock.readLock().release(); 456 } 457 } 458 catch (Exception ex) 459 { 460 throw new RuntimeException (ex); 461 } 462 } 463 public boolean retainAll(Collection c) 464 { 465 try 466 { 467 lock.readLock().acquire(); 468 try 469 { 470 Set state = getCurrentState(true); 471 return state.retainAll(c); 472 } 473 finally 474 { 475 lock.readLock().release(); 476 } 477 } 478 catch (Exception ex) 479 { 480 throw new RuntimeException (ex); 481 } 482 } 483 public int size() 484 { 485 try 486 { 487 lock.readLock().acquire(); 488 try 489 { 490 Set state = getCurrentState(false); 491 return state.size(); 492 } 493 finally 494 { 495 lock.readLock().release(); 496 } 497 } 498 catch (Exception ex) 499 { 500 throw new RuntimeException (ex); 501 } 502 } 503 public Object [] toArray() 504 { 505 try 506 { 507 lock.readLock().acquire(); 508 try 509 { 510 Set state = getCurrentState(false); 511 return state.toArray(); 512 } 513 finally 514 { 515 lock.readLock().release(); 516 } 517 } 518 catch (Exception ex) 519 { 520 throw new RuntimeException (ex); 521 } 522 } 523 public Object [] toArray(Object [] a) 524 { 525 try 526 { 527 lock.readLock().acquire(); 528 try 529 { 530 Set state = getCurrentState(false); 531 return state.toArray(a); 532 } 533 finally 534 { 535 lock.readLock().release(); 536 } 537 } 538 catch (Exception ex) 539 { 540 throw new RuntimeException (ex); 541 } 542 } 543 544 public void writeExternal(java.io.ObjectOutput out) 545 throws java.io.IOException 546 { 547 super.writeExternal(out); 548 out.writeLong(versionId); 549 out.writeObject(updates); 550 out.writeObject(classname); 551 } 552 553 public void readExternal(java.io.ObjectInput in) 554 throws java.io.IOException , ClassNotFoundException 555 { 556 super.readExternal(in); 557 versionId = in.readLong(); 558 this.updates = (HashSet )in.readObject(); 559 this.classname = (String )in.readObject(); 560 try 561 { 562 InitialContext ctx = new InitialContext (); 563 this.tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 564 } 565 catch (Exception ex) 566 { 567 throw new RuntimeException (ex); 568 } 569 this.txState = new TransactionLocal(); 570 this.txVersion = new TransactionLocal(); 571 this.methodMap = setMethodMap; 572 } 573 574 } 575 | Popular Tags |