1 22 package org.jboss.tm.remoting; 23 24 import java.io.Serializable ; 25 import java.lang.reflect.Method ; 26 import java.rmi.RemoteException ; 27 28 import javax.transaction.HeuristicCommitException ; 29 import javax.transaction.HeuristicMixedException ; 30 import javax.transaction.HeuristicRollbackException ; 31 32 import org.jboss.remoting.InvokerLocator; 33 import org.jboss.tm.GlobalId; 34 import org.jboss.tm.remoting.interfaces.Coordinator; 35 import org.jboss.tm.remoting.interfaces.HeuristicHazardException; 36 import org.jboss.tm.remoting.interfaces.RecoveryCoordinator; 37 import org.jboss.tm.remoting.interfaces.Resource; 38 import org.jboss.tm.remoting.interfaces.Status; 39 import org.jboss.tm.remoting.interfaces.Synchronization; 40 import org.jboss.tm.remoting.interfaces.Terminator; 41 import org.jboss.tm.remoting.interfaces.TxPropagationContext; 42 import org.jboss.tm.remoting.interfaces.TransactionFactory; 43 import org.jboss.tm.remoting.interfaces.TransactionInactiveException; 44 import org.jboss.tm.remoting.interfaces.TransactionNotPreparedException; 45 import org.jboss.tm.remoting.interfaces.Vote; 46 47 48 56 public class Invocation 57 implements Serializable 58 { 59 static final long serialVersionUID = -7256134284357215230L; 60 61 63 81 private static interface Invoker 82 { 83 Object invoke(Object servant, long targetId, Object [] args) 84 throws Throwable ; 85 } 86 87 94 public static interface ITransactionFactory 95 { 96 97 static final int M_CREATE = 0; 98 99 100 static final int M_RECREATE = 1; 101 102 103 static final Invoker CREATE = new Invoker() 104 { 105 public Object invoke(Object servant, long targetId, Object [] args) 106 throws Throwable 107 { 108 int timeout = ((Integer )args[0]).intValue(); 109 return ((ITransactionFactory)servant).create(targetId, timeout); 110 } 111 }; 112 113 114 static final Invoker RECREATE = new Invoker() 115 { 116 public Object invoke(Object servant, long targetId, Object [] args) 117 throws Throwable 118 { 119 TxPropagationContext tpc = (TxPropagationContext)args[0]; 120 return ((ITransactionFactory)servant).recreate(targetId, tpc); 121 } 122 }; 123 124 125 TxPropagationContext create(long targetId, int timeout) 126 throws RemoteException ; 127 128 129 TxPropagationContext recreate(long targetId, TxPropagationContext tpc) 130 throws RemoteException ; 131 132 } 133 134 141 public static interface ICoordinator 142 { 143 144 static final int M_GET_STATUS = 2; 145 146 147 static final int M_IS_SAME_TRANSACTION = 3; 148 149 150 static final int M_HASH_TRANSACTION = 4; 151 152 153 static final int M_REGISTER_RESOURCE = 5; 154 155 156 static final int M_REGISTER_SYNCHRONIZATION = 6; 157 158 159 static final int M_ROLLBACK_ONLY = 7; 160 161 162 static final int M_GET_TRANSACTION_CONTEXT = 8; 163 164 165 static final int M_GET_TRANSACTION_ID = 9; 166 167 168 static final Invoker GET_STATUS = new Invoker() 169 { 170 public Object invoke(Object servant, long targetId, Object [] args) 171 throws Throwable 172 { 173 return ((ICoordinator)servant).getStatus(targetId); 174 } 175 }; 176 177 178 static final Invoker IS_SAME_TRANSACTION = new Invoker() 179 { 180 public Object invoke(Object servant, long targetId, Object [] args) 181 throws Throwable 182 { 183 Coordinator other = (Coordinator)args[0]; 184 boolean retVal = 185 ((ICoordinator)servant).isSameTransaction(targetId, other); 186 return Boolean.valueOf(retVal); 187 } 188 }; 189 190 191 static final Invoker HASH_TRANSACTION = new Invoker() 192 { 193 public Object invoke(Object servant, long targetId, Object [] args) 194 throws Throwable 195 { 196 int retVal = ((ICoordinator)servant).hashTransaction(targetId); 197 return new Integer (retVal); 198 } 199 }; 200 201 202 static final Invoker REGISTER_RESOURCE = new Invoker() 203 { 204 public Object invoke(Object servant, long targetId, Object [] args) 205 throws Throwable 206 { 207 Resource r = (Resource)args[0]; 208 return ((ICoordinator)servant).registerResource(targetId, r); 209 } 210 }; 211 212 213 static final Invoker REGISTER_SYNCHRONIZATION = new Invoker() 214 { 215 public Object invoke(Object servant, long targetId, Object [] args) 216 throws Throwable 217 { 218 Synchronization sync = (Synchronization)args[0]; 219 ((ICoordinator)servant).registerSynchronization(targetId, sync); 220 return null; 221 } 222 }; 223 224 225 static final Invoker ROLLBACK_ONLY = new Invoker() 226 { 227 public Object invoke(Object servant, long targetId, Object [] args) 228 throws Throwable 229 { 230 ((ICoordinator)servant).rollbackOnly(targetId); 231 return null; 232 } 233 }; 234 235 236 static final Invoker GET_TRANSACTION_CONTEXT = new Invoker() 237 { 238 public Object invoke(Object servant, long targetId, Object [] args) 239 throws Throwable 240 { 241 return ((ICoordinator)servant).getTransactionContext(targetId); 242 } 243 }; 244 245 246 static final Invoker GET_TRANSACTION_ID = new Invoker() 247 { 248 public Object invoke(Object servant, long targetId, Object [] args) 249 throws Throwable 250 { 251 return ((ICoordinator)servant).getTransactionId(targetId); 252 } 253 }; 254 255 256 Status getStatus(long targetId) 257 throws RemoteException ; 258 259 260 boolean isSameTransaction(long targetId, Coordinator c) 261 throws RemoteException ; 262 263 264 int hashTransaction(long targetId) 265 throws RemoteException ; 266 267 268 RecoveryCoordinator registerResource(long targetId, Resource r) 269 throws RemoteException , 270 TransactionInactiveException; 271 272 273 void registerSynchronization(long targetId, Synchronization sync) 274 throws RemoteException , 275 TransactionInactiveException; 276 277 278 void rollbackOnly(long targetId) 279 throws RemoteException , 280 TransactionInactiveException; 281 282 283 TxPropagationContext getTransactionContext(long targetId) 284 throws RemoteException , 285 TransactionInactiveException; 286 287 288 GlobalId getTransactionId(long targetId) 289 throws RemoteException ; 290 } 291 292 299 public static interface ITerminator 300 { 301 302 static final int M_COMMIT = 10; 303 304 305 static final int M_ROLLBACK = 11; 306 307 308 static final Invoker COMMIT = new Invoker() 309 { 310 public Object invoke(Object servant, long targetId, Object [] args) 311 throws Throwable 312 { 313 boolean reportHeuristics = ((Boolean )args[0]).booleanValue(); 314 ((ITerminator)servant).commit(targetId, reportHeuristics); 315 return null; 316 } 317 }; 318 319 320 static final Invoker ROLLBACK = new Invoker() 321 { 322 public Object invoke(Object servant, long targetId, Object [] args) 323 throws Throwable 324 { 325 ((ITerminator)servant).rollback(targetId); 326 return null; 327 } 328 }; 329 330 331 void commit(long targetId, boolean reportHeuristics) 332 throws RemoteException , 333 HeuristicMixedException , 334 HeuristicHazardException; 335 336 337 void rollback(long targetId) 338 throws RemoteException ; 339 } 340 341 348 public static interface IResource 349 { 350 351 static final int M_PREPARE = 12; 352 353 354 static final int M_ROLLBACK = 13; 355 356 357 static final int M_COMMIT = 14; 358 359 360 static final int M_COMMIT_ONE_PHASE = 15; 361 362 363 static final int M_FORGET = 16; 364 365 366 static final Invoker PREPARE = new Invoker() 367 { 368 public Object invoke(Object servant, long targetId, Object [] args) 369 throws Throwable 370 { 371 return ((IResource)servant).prepare(targetId); 372 } 373 }; 374 375 376 static final Invoker ROLLBACK = new Invoker() 377 { 378 public Object invoke(Object servant, long targetId, Object [] args) 379 throws Throwable 380 { 381 ((IResource)servant).rollbackResource(targetId); 382 return null; 383 } 384 }; 385 386 387 static final Invoker COMMIT = new Invoker() 388 { 389 public Object invoke(Object servant, long targetId, Object [] args) 390 throws Throwable 391 { 392 ((IResource)servant).commit(targetId); 393 return null; 394 } 395 }; 396 397 398 static final Invoker COMMIT_ONE_PHASE = new Invoker() 399 { 400 public Object invoke(Object servant, long targetId, Object [] args) 401 throws Throwable 402 { 403 ((IResource)servant).commitOnePhase(targetId); 404 return null; 405 } 406 }; 407 408 409 static final Invoker FORGET = new Invoker() 410 { 411 public Object invoke(Object servant, long targetId, Object [] args) 412 throws Throwable 413 { 414 ((IResource)servant).forget(targetId); 415 return null; 416 } 417 }; 418 419 420 Vote prepare(long targetId) 421 throws RemoteException , 422 HeuristicMixedException , 423 HeuristicHazardException; 424 425 428 void rollbackResource(long targetId) 429 throws RemoteException , 430 HeuristicCommitException , 431 HeuristicMixedException , 432 HeuristicHazardException; 433 434 435 void commit(long targetId) 436 throws RemoteException , 437 TransactionNotPreparedException, 438 HeuristicRollbackException , 439 HeuristicMixedException , 440 HeuristicHazardException; 441 442 443 void commitOnePhase(long targetId) 444 throws RemoteException , 445 HeuristicHazardException; 446 447 448 void forget(long targetId) 449 throws RemoteException ; 450 } 451 452 459 public static interface IRecoveryCoordinator 460 { 461 462 static final int M_REPLAY_COMPLETION = 17; 463 464 465 static final Invoker REPLAY_COMPLETION = new Invoker() 466 { 467 public Object invoke(Object servant, long targetId, Object [] args) 468 throws Throwable 469 { 470 Resource r = (Resource)args[0]; 471 return ((IRecoveryCoordinator)servant).replayCompletion(targetId, 472 r); 473 } 474 }; 475 476 477 Status replayCompletion(long targetId, Resource r) 478 throws RemoteException , 479 TransactionNotPreparedException; 480 } 481 482 489 public static interface ISynchronization 490 { 491 492 static final int M_BEFORE_COMPLETION = 18; 493 494 495 static final int M_AFTER_COMPLETION = 19; 496 497 498 static final Invoker BEFORE_COMPLETION = new Invoker() 499 { 500 public Object invoke(Object servant, long targetId, Object [] args) 501 throws Throwable 502 { 503 ((ISynchronization)servant).beforeCompletion(targetId); 504 return null; 505 } 506 }; 507 508 509 static final Invoker AFTER_COMPLETION = new Invoker() 510 { 511 public Object invoke(Object servant, long targetId, Object [] args) 512 throws Throwable 513 { 514 ((ISynchronization)servant).afterCompletion(targetId); 515 return null; 516 } 517 }; 518 519 520 void beforeCompletion(long targetId); 521 522 523 void afterCompletion(long targetId); 524 } 525 526 528 533 private static final Invoker[] invokerArray = 534 { 535 ITransactionFactory.CREATE, 536 ITransactionFactory.RECREATE, 537 ICoordinator.GET_STATUS, 538 ICoordinator.IS_SAME_TRANSACTION, 539 ICoordinator.HASH_TRANSACTION, 540 ICoordinator.REGISTER_RESOURCE, 541 ICoordinator.REGISTER_SYNCHRONIZATION, 542 ICoordinator.ROLLBACK_ONLY, 543 ICoordinator.GET_TRANSACTION_CONTEXT, 544 ICoordinator.GET_TRANSACTION_ID, 545 ITerminator.COMMIT, 546 ITerminator.ROLLBACK, 547 IResource.PREPARE, 548 IResource.ROLLBACK, 549 IResource.COMMIT, 550 IResource.COMMIT_ONE_PHASE, 551 IResource.FORGET, 552 IRecoveryCoordinator.REPLAY_COMPLETION, 553 ISynchronization.BEFORE_COMPLETION, 554 ISynchronization.AFTER_COMPLETION 555 }; 556 557 559 562 private static int getMethodId(Method m) 563 { 564 Class clz = m.getDeclaringClass(); 565 566 if (clz == TransactionFactory.class) 567 { 568 String name = m.getName(); 569 570 if (name.equals("create")) 571 return ITransactionFactory.M_CREATE; 572 else 573 return ITransactionFactory.M_RECREATE; 574 } 575 else if (clz == Coordinator.class) 576 { 577 String name = m.getName(); 578 579 if (name.equals("getStatus")) 580 return ICoordinator.M_GET_STATUS; 581 else if (name.equals("isSameTransaction")) 582 return ICoordinator.M_IS_SAME_TRANSACTION; 583 else if (name.equals("hashTransaction")) 584 return ICoordinator.M_HASH_TRANSACTION; 585 else if (name.equals("registerResource")) 586 return ICoordinator.M_REGISTER_RESOURCE; 587 else if (name.equals("registerSynchronization")) 588 return ICoordinator.M_REGISTER_SYNCHRONIZATION; 589 else if (name.equals("rollbackOnly")) 590 return ICoordinator.M_ROLLBACK_ONLY; 591 else if (name.equals("getTransactionContext")) 592 return ICoordinator.M_GET_TRANSACTION_CONTEXT; 593 else 594 return ICoordinator.M_GET_TRANSACTION_ID; 595 } 596 else if (clz == Terminator .class) 597 { 598 String name = m.getName(); 599 600 if (name.equals("commit")) 601 return ITerminator.M_COMMIT; 602 else 603 return ITerminator.M_ROLLBACK; 604 605 } 606 else if (clz == Resource.class) 607 { 608 String name = m.getName(); 609 610 if (name.equals("prepare")) 611 return IResource.M_PREPARE; 612 else if (name.equals("rollback")) 613 return IResource.M_ROLLBACK; 614 else if (name.equals("commit")) 615 return IResource.M_COMMIT; 616 else if (name.equals("commitOnePhase")) 617 return IResource.M_COMMIT_ONE_PHASE; 618 else 619 return IResource.M_FORGET; 620 } 621 else if (clz == RecoveryCoordinator.class) 622 { 623 return IRecoveryCoordinator.M_REPLAY_COMPLETION; 624 } 625 else if (clz == Synchronization.class) 626 { 627 String name = m.getName(); 628 629 if (name.equals("beforeCompletion")) 630 return ISynchronization.M_BEFORE_COMPLETION; 631 else 632 return ISynchronization.M_AFTER_COMPLETION; 633 } 634 else 635 { 636 throw new RuntimeException ("Method " + m + " does not belong to" + 637 " a transaction service interface"); 638 } 639 } 640 641 643 644 private long targetId; 645 646 647 private int methodId; 648 649 650 private Object [] args; 651 652 654 655 public Invocation(long targetId, 656 Method method, 657 Object [] args) 658 { 659 this.targetId = targetId; 660 this.methodId = getMethodId(method); 661 this.args = args; 662 } 663 664 665 public Object perform(InvokerLocator locator, Object servant) 666 throws Throwable 667 { 668 return invokerArray[methodId].invoke(servant, targetId, args); 669 } 670 671 } 672 | Popular Tags |