1 23 24 package com.sun.ejb.containers; 25 26 import java.io.Serializable ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.IOException ; 31 import java.util.Date ; 32 import java.util.Set ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 36 import javax.ejb.EJBContext ; 37 import javax.ejb.EntityBean ; 38 import javax.ejb.EJBException ; 39 import javax.ejb.CreateException ; 40 import javax.ejb.FinderException ; 41 import javax.ejb.EntityContext ; 42 import javax.ejb.EJBLocalObject ; 43 import javax.ejb.Timer ; 44 45 import javax.transaction.Synchronization ; 46 import javax.transaction.Transaction ; 47 import javax.transaction.Status ; 48 49 import java.util.logging.Logger ; 50 import java.util.logging.Level ; 51 52 import com.sun.logging.LogDomains; 53 import com.sun.enterprise.ComponentInvocation; 54 import com.sun.enterprise.InvocationManager; 55 import com.sun.enterprise.Switch; 56 57 import com.sun.enterprise.server.ApplicationServer; 58 import com.sun.enterprise.instance.InstanceEnvironment; 59 import com.sun.enterprise.instance.ServerManager; 60 import com.sun.enterprise.deployment.EjbDescriptor; 61 62 import com.sun.ejb.EJBUtils; 63 import com.sun.ejb.ContainerFactory; 64 65 import javax.naming.InitialContext ; 66 import javax.sql.DataSource ; 67 import java.sql.Connection ; 68 69 84 public abstract class TimerBean implements EntityBean { 85 86 private static Logger logger = LogDomains.getLogger(LogDomains.EJB_LOGGER); 87 88 private static final int ACTIVE = 0; 90 private static final int CANCELLED = 1; 91 92 private EJBContextImpl context_; 93 94 98 public abstract String getTimerId(); 100 public abstract void setTimerId(String timerId); 101 102 public abstract String getOwnerId(); 103 public abstract void setOwnerId(String ownerId); 104 105 public abstract long getCreationTimeRaw(); 106 public abstract void setCreationTimeRaw(long creationTime); 107 108 public abstract long getInitialExpirationRaw(); 109 public abstract void setInitialExpirationRaw(long initialExpiration); 110 111 public abstract long getLastExpirationRaw(); 112 public abstract void setLastExpirationRaw(long lastExpiration); 113 114 public abstract long getIntervalDuration(); 115 public abstract void setIntervalDuration(long intervalDuration); 116 117 public abstract int getState(); 118 public abstract void setState(int state); 119 120 public abstract long getContainerId(); 121 public abstract void setContainerId(long containerId); 122 123 public abstract Blob getBlob(); 124 public abstract void setBlob(Blob blob); 125 126 public abstract int getPkHashCode(); 127 public abstract void setPkHashCode(int pkHash); 128 129 133 public abstract Set ejbSelectTimerIdsByContainer(long containerId) 134 throws FinderException ; 135 public abstract Set ejbSelectTimerIdsByContainerAndState 136 (long containerId, int state) throws FinderException ; 137 138 public abstract Set ejbSelectTimerIdsByContainerAndOwner 139 (long containerId, String ownerId) 140 throws FinderException ; 141 public abstract Set ejbSelectTimerIdsByContainerAndOwnerAndState 142 (long containerId, String ownerId, int state) throws FinderException ; 143 144 public abstract Set ejbSelectAllTimerIdsByOwner(String ownerId) 145 throws FinderException ; 146 public abstract Set ejbSelectAllTimerIdsByOwnerAndState 147 (String ownerId, int state) throws FinderException ; 148 149 150 154 public abstract Set ejbSelectTimersByContainer(long containerId) 155 throws FinderException ; 156 public abstract Set ejbSelectTimersByContainerAndState 157 (long containerId, int state) throws FinderException ; 158 159 public abstract Set ejbSelectTimersByContainerAndOwner 160 (long containerId, String ownerId) 161 throws FinderException ; 162 public abstract Set ejbSelectTimersByContainerAndOwnerAndState 163 (long containerId, String ownerId, int state) throws FinderException ; 164 165 public abstract Set ejbSelectAllTimersByOwner(String ownerId) 166 throws FinderException ; 167 public abstract Set ejbSelectAllTimersByOwnerAndState 168 (String ownerId, int state) throws FinderException ; 169 170 171 175 public abstract int ejbSelectCountTimersByContainer(long containerId) 176 throws FinderException ; 177 public abstract int ejbSelectCountTimersByContainerAndState 178 (long containerId, int state) throws FinderException ; 179 180 public abstract int ejbSelectCountTimersByContainerAndOwner 181 (long containerId, String ownerId) 182 throws FinderException ; 183 public abstract int ejbSelectCountTimersByContainerAndOwnerAndState 184 (long containerId, String ownerId, int state) throws FinderException ; 185 186 public abstract int ejbSelectCountAllTimersByOwner(String ownerId) 187 throws FinderException ; 188 public abstract int ejbSelectCountAllTimersByOwnerAndState 189 (String ownerId, int state) throws FinderException ; 190 191 196 private boolean blobLoaded_; 198 private Object timedObjectPrimaryKey_; 199 private transient Serializable info_; 200 201 private transient Date creationTime_; 203 private transient Date initialExpiration_; 204 private transient Date lastExpiration_; 205 206 public TimerPrimaryKey ejbCreate 207 (String timerId, long containerId, String ownerId, 208 Object timedObjectPrimaryKey, 209 Date initialExpiration, long intervalDuration, Serializable info) 210 throws CreateException { 211 212 setTimerId(timerId); 213 214 setOwnerId(ownerId); 215 216 return null; 217 } 218 219 public void ejbPostCreate(String timerId, long containerId, String ownerId, 220 Object timedObjectPrimaryKey, 221 Date initialExpiration, 222 long intervalDuration, Serializable info) 223 throws CreateException { 224 225 Date creationTime = new Date (); 226 setCreationTimeRaw(creationTime.getTime()); 227 creationTime_ = creationTime; 228 229 setInitialExpirationRaw(initialExpiration.getTime()); 230 initialExpiration_ = initialExpiration; 231 232 setLastExpirationRaw(0); 233 lastExpiration_ = null; 234 235 setIntervalDuration(intervalDuration); 236 237 setContainerId(containerId); 238 239 timedObjectPrimaryKey_ = timedObjectPrimaryKey; 240 info_ = info; 241 blobLoaded_ = true; 242 243 Blob blob = null; 244 try { 245 blob = new Blob(timedObjectPrimaryKey, info); 246 } catch(IOException ioe) { 247 CreateException ce = new CreateException (); 248 ce.initCause(ioe); 249 throw ce; 250 } 251 252 setBlob(blob); 253 setState(ACTIVE); 254 255 if( logger.isLoggable(Level.FINE) ) { 256 logger.log(Level.FINE, "TimerBean.postCreate() ::timerId=" + 257 getTimerId() + " ::containerId=" + getContainerId() + 258 " ::timedObjectPK=" + timedObjectPrimaryKey + 259 " ::info=" + info + 260 " ::initialExpiration=" + initialExpiration + 261 " ::intervalDuration=" + intervalDuration + 262 " :::state=" + stateToString(getState()) + 263 " :::creationTime=" + creationTime + 264 " :::ownerId=" + getOwnerId()); 265 } 266 267 if( timerOwnedByThisServer() ) { 282 283 Synchronization timerSynch = 286 new TimerSynch(new TimerPrimaryKey(getTimerId()), ACTIVE, 287 getInitialExpiration(), 288 getContainer(containerId)); 289 290 try { 291 ContainerSynchronization containerSynch = getContainerSynch(); 292 containerSynch.addTimerSynchronization 293 (new TimerPrimaryKey(getTimerId()), timerSynch); 294 } catch(Exception e) { 295 CreateException ce = new CreateException (); 296 ce.initCause(e); 297 throw ce; 298 } 299 } 300 } 301 302 306 private boolean timerOwnedByThisServer() { 307 String ownerIdOfThisServer = getOwnerIdOfThisServer(); 308 return ( (ownerIdOfThisServer != null) && 309 (ownerIdOfThisServer.equals(getOwnerId())) ); 310 } 311 312 private String getOwnerIdOfThisServer() { 313 return getEJBTimerService().getOwnerIdOfThisServer(); 314 } 315 316 private static String stateToString(int state) { 317 String stateStr = "UNKNOWN_TIMER_STATE"; 318 319 switch(state) { 320 case ACTIVE : 321 stateStr = "TIMER_ACTIVE"; 322 break; 323 case CANCELLED : 324 stateStr = "TIMER_CANCELLED"; 325 break; 326 default : 327 stateStr = "UNKNOWN_TIMER_STATE"; 328 break; 329 } 330 331 return stateStr; 332 } 333 334 private static String txStatusToString(int txStatus) { 335 String txStatusStr = "UNMATCHED TX STATUS"; 336 337 switch(txStatus) { 338 case Status.STATUS_ACTIVE : 339 txStatusStr = "TX_STATUS_ACTIVE"; 340 break; 341 case Status.STATUS_COMMITTED : 342 txStatusStr = "TX_STATUS_COMMITTED"; 343 break; 344 case Status.STATUS_COMMITTING : 345 txStatusStr = "TX_STATUS_COMMITTING"; 346 break; 347 case Status.STATUS_MARKED_ROLLBACK : 348 txStatusStr = "TX_STATUS_MARKED_ROLLBACK"; 349 break; 350 case Status.STATUS_NO_TRANSACTION : 351 txStatusStr = "TX_STATUS_NO_TRANSACTION"; 352 break; 353 case Status.STATUS_PREPARED : 354 txStatusStr = "TX_STATUS_PREPARED"; 355 break; 356 case Status.STATUS_PREPARING : 357 txStatusStr = "TX_STATUS_PREPARING"; 358 break; 359 case Status.STATUS_ROLLEDBACK : 360 txStatusStr = "TX_STATUS_ROLLEDBACK"; 361 break; 362 case Status.STATUS_ROLLING_BACK : 363 txStatusStr = "TX_STATUS_ROLLING_BACK"; 364 break; 365 case Status.STATUS_UNKNOWN : 366 txStatusStr = "TX_STATUS_UNKNOWN"; 367 break; 368 default : 369 txStatusStr = "UNMATCHED TX STATUS"; 370 break; 371 } 372 373 return txStatusStr; 374 } 375 376 377 private ContainerSynchronization getContainerSynch() throws Exception { 378 379 EntityContainer container = (EntityContainer) context_.getContainer(); 380 ContainerFactoryImpl containerFactory = (ContainerFactoryImpl) 381 Switch.getSwitch().getContainerFactory(); 382 Transaction transaction = context_.getTransaction(); 383 384 if( transaction == null ) { 385 logger.log(Level.FINE, "Context transaction = null. Using " + 386 "invocation instead."); 387 InvocationManager iMgr = Switch.getSwitch().getInvocationManager(); 388 ComponentInvocation i = iMgr.getCurrentInvocation(); 389 transaction = i.transaction; 390 } 391 if( transaction == null ) { 392 throw new Exception ("transaction = null in getContainerSynch " + 393 "for timerId = " + getTimerId()); 394 } 395 396 ContainerSynchronization containerSync = 397 containerFactory.getContainerSync(transaction); 398 return containerSync; 399 } 400 401 private static EJBTimerService getEJBTimerService() { 402 ContainerFactoryImpl containerFactory = (ContainerFactoryImpl) 403 Switch.getSwitch().getContainerFactory(); 404 return containerFactory.getEJBTimerService(); 405 } 406 407 private void loadBlob() { 408 EJBTimerService timerService = getEJBTimerService(); 409 ClassLoader cl = timerService.getTimerClassLoader(getContainerId()); 410 if( cl != null ) { 411 loadBlob(cl); 412 } else { 413 throw new EJBException ("No timer classloader for " + getTimerId()); 414 } 415 } 416 417 private void loadBlob(ClassLoader cl) { 418 try { 419 Blob blob = getBlob(); 420 timedObjectPrimaryKey_ = blob.getTimedObjectPrimaryKey(cl); 421 info_ = blob.getInfo(cl); 422 blobLoaded_ = true; 423 } catch(Exception e) { 424 EJBException ejbEx = new EJBException (); 425 ejbEx.initCause(e); 426 throw ejbEx; 427 } 428 } 429 430 public void setEntityContext(EntityContext context) { 431 context_ = (EJBContextImpl) context; 432 } 433 434 public void unsetEntityContext() { 435 context_ = null; 436 } 437 438 public void ejbRemove() {} 439 440 public void ejbLoad() { 441 442 long lastExpirationRaw = getLastExpirationRaw(); 443 lastExpiration_ = (lastExpirationRaw > 0) ? 444 new Date (lastExpirationRaw) : null; 445 446 creationTime_ = new Date (getCreationTimeRaw()); 448 initialExpiration_ = new Date (getInitialExpirationRaw()); 449 450 timedObjectPrimaryKey_ = null; 455 info_ = null; 456 blobLoaded_ = false; 457 } 458 459 public void ejbStore() {} 460 461 public void ejbPassivate() {} 462 463 public void ejbActivate() {} 464 465 public boolean repeats() { 466 return (getIntervalDuration() > 0); 467 } 468 469 public void cancel() throws Exception { 470 471 475 if( getState() == CANCELLED ) { 476 return; 478 } 479 480 setState(CANCELLED); 481 482 if( timerOwnedByThisServer() ) { 485 486 TimerPrimaryKey timerId = new TimerPrimaryKey(getTimerId()); 487 488 Date nextTimeout = getEJBTimerService().cancelTask(timerId); 493 494 ContainerSynchronization containerSynch = getContainerSynch(); 495 Synchronization timerSynch = 496 containerSynch.getTimerSynchronization(timerId); 497 498 if( timerSynch != null ) { 499 containerSynch.removeTimerSynchronization(timerId); 504 getEJBTimerService().expungeTimer(timerId); 505 } else { 506 timerSynch = new TimerSynch(timerId, CANCELLED, nextTimeout, 508 getContainer(getContainerId())); 509 containerSynch.addTimerSynchronization(timerId, timerSynch); 510 } 511 512 } 513 514 return; 516 } 517 518 public Serializable getInfo() { 519 if( !blobLoaded_ ) { 520 loadBlob(); 521 } 522 return info_; 523 } 524 525 public Object getTimedObjectPrimaryKey() { 526 if( !blobLoaded_ ) { 527 loadBlob(); 528 } 529 return timedObjectPrimaryKey_; 530 } 531 532 public Date getCreationTime() { 533 return creationTime_; 534 } 535 536 public Date getInitialExpiration() { 537 return initialExpiration_; 538 } 539 540 public Date getLastExpiration() { 541 return lastExpiration_; 542 } 543 544 public void setLastExpiration(Date lastExpiration) { 545 lastExpiration_ = lastExpiration; 547 long lastExpirationRaw = (lastExpiration != null) ? 548 lastExpiration.getTime() : 0; 549 setLastExpirationRaw(lastExpirationRaw); 550 } 551 552 public boolean isActive() { 553 return (getState() == ACTIVE); 554 } 555 556 public boolean isCancelled() { 557 return (getState() == CANCELLED); 558 } 559 560 private Set toPKeys(Set ids) { 561 Set pkeys = new HashSet (); 562 for(Iterator iter = ids.iterator(); iter.hasNext();) { 563 pkeys.add(new TimerPrimaryKey((String ) iter.next())); 564 } 565 return pkeys; 566 } 567 568 572 public Set ejbHomeSelectTimerIdsByContainer(long containerId) 573 throws FinderException { 574 return toPKeys(ejbSelectTimerIdsByContainer(containerId)); 575 } 576 577 public Set ejbHomeSelectActiveTimerIdsByContainer(long containerId) 578 throws FinderException { 579 return toPKeys(ejbSelectTimerIdsByContainerAndState(containerId, 580 ACTIVE)); 581 } 582 583 public Set ejbHomeSelectCancelledTimerIdsByContainer(long containerId) 584 throws FinderException { 585 return toPKeys(ejbSelectTimerIdsByContainerAndState 586 (containerId, CANCELLED)); 587 } 588 589 public Set ejbHomeSelectTimerIdsOwnedByThisServerByContainer 590 (long containerId) 591 throws FinderException { 592 return toPKeys(ejbSelectTimerIdsByContainerAndOwner 593 (containerId, getOwnerIdOfThisServer())); 594 } 595 596 public Set ejbHomeSelectActiveTimerIdsOwnedByThisServerByContainer 597 (long containerId) 598 throws FinderException { 599 return toPKeys(ejbSelectTimerIdsByContainerAndOwnerAndState 600 (containerId, getOwnerIdOfThisServer(), ACTIVE)); 601 } 602 603 public Set ejbHomeSelectCancelledTimerIdsOwnedByThisServerByContainer 604 (long containerId) 605 throws FinderException { 606 return toPKeys(ejbSelectTimerIdsByContainerAndOwnerAndState 607 (containerId, getOwnerIdOfThisServer(), CANCELLED)); 608 } 609 610 611 public Set ejbHomeSelectAllTimerIdsOwnedByThisServer() 612 throws FinderException { 613 return toPKeys(ejbSelectAllTimerIdsByOwner(getOwnerIdOfThisServer())); 614 } 615 616 public Set ejbHomeSelectAllActiveTimerIdsOwnedByThisServer() 617 throws FinderException { 618 return toPKeys(ejbSelectAllTimerIdsByOwnerAndState 619 (getOwnerIdOfThisServer(), ACTIVE)); 620 } 621 622 public Set ejbHomeSelectAllCancelledTimerIdsOwnedByThisServer() 623 throws FinderException { 624 return toPKeys(ejbSelectAllTimerIdsByOwnerAndState 625 (getOwnerIdOfThisServer(), CANCELLED)); 626 } 627 628 629 public Set ejbHomeSelectAllTimerIdsOwnedBy(String ownerId) 630 throws FinderException { 631 return toPKeys(ejbSelectAllTimerIdsByOwner(ownerId)); 632 } 633 634 public Set ejbHomeSelectAllActiveTimerIdsOwnedBy(String ownerId) 635 throws FinderException { 636 return toPKeys(ejbSelectAllTimerIdsByOwnerAndState 637 (ownerId, ACTIVE)); 638 } 639 640 public Set ejbHomeSelectAllCancelledTimerIdsOwnedBy(String ownerId) 641 throws FinderException { 642 return toPKeys(ejbSelectAllTimerIdsByOwnerAndState 643 (ownerId, CANCELLED)); 644 } 645 646 650 public Set ejbHomeSelectTimersByContainer(long containerId) 651 throws FinderException { 652 return ejbSelectTimersByContainer(containerId); 653 } 654 655 public Set ejbHomeSelectActiveTimersByContainer(long containerId) 656 throws FinderException { 657 return ejbSelectTimersByContainerAndState(containerId, 658 ACTIVE); 659 } 660 661 public Set ejbHomeSelectCancelledTimersByContainer(long containerId) 662 throws FinderException { 663 return ejbSelectTimersByContainerAndState 664 (containerId, CANCELLED); 665 } 666 667 public Set ejbHomeSelectTimersOwnedByThisServerByContainer 668 (long containerId) 669 throws FinderException { 670 return ejbSelectTimersByContainerAndOwner 671 (containerId, getOwnerIdOfThisServer()); 672 } 673 674 public Set ejbHomeSelectActiveTimersOwnedByThisServerByContainer 675 (long containerId) 676 throws FinderException { 677 return ejbSelectTimersByContainerAndOwnerAndState 678 (containerId, getOwnerIdOfThisServer(), ACTIVE); 679 } 680 681 public Set ejbHomeSelectCancelledTimersOwnedByThisServerByContainer 682 (long containerId) 683 throws FinderException { 684 return ejbSelectTimersByContainerAndOwnerAndState 685 (containerId, getOwnerIdOfThisServer(), CANCELLED); 686 } 687 688 689 public Set ejbHomeSelectAllTimersOwnedByThisServer() 690 throws FinderException { 691 return ejbSelectAllTimersByOwner(getOwnerIdOfThisServer()); 692 } 693 694 public Set ejbHomeSelectAllActiveTimersOwnedByThisServer() 695 throws FinderException { 696 return ejbSelectAllTimersByOwnerAndState 697 (getOwnerIdOfThisServer(), ACTIVE); 698 } 699 700 public Set ejbHomeSelectAllCancelledTimersOwnedByThisServer() 701 throws FinderException { 702 return ejbSelectAllTimersByOwnerAndState 703 (getOwnerIdOfThisServer(), CANCELLED); 704 } 705 706 707 public Set ejbHomeSelectAllTimersOwnedBy(String ownerId) 708 throws FinderException { 709 return ejbSelectAllTimersByOwner(ownerId); 710 } 711 712 public Set ejbHomeSelectAllActiveTimersOwnedBy(String ownerId) 713 throws FinderException { 714 return ejbSelectAllTimersByOwnerAndState 715 (ownerId, ACTIVE); 716 } 717 718 public Set ejbHomeSelectAllCancelledTimersOwnedBy(String ownerId) 719 throws FinderException { 720 return ejbSelectAllTimersByOwnerAndState 721 (ownerId, CANCELLED); 722 } 723 724 725 729 public int ejbHomeSelectCountTimersByContainer(long containerId) 730 throws FinderException { 731 return ejbSelectCountTimersByContainer(containerId); 732 } 733 734 public int ejbHomeSelectCountActiveTimersByContainer(long containerId) 735 throws FinderException { 736 return ejbSelectCountTimersByContainerAndState(containerId, 737 ACTIVE); 738 } 739 740 public int ejbHomeSelectCountCancelledTimersByContainer(long containerId) 741 throws FinderException { 742 return ejbSelectCountTimersByContainerAndState 743 (containerId, CANCELLED); 744 } 745 746 public int ejbHomeSelectCountTimersOwnedByThisServerByContainer 747 (long containerId) 748 throws FinderException { 749 return ejbSelectCountTimersByContainerAndOwner 750 (containerId, getOwnerIdOfThisServer()); 751 } 752 753 public int ejbHomeSelectCountActiveTimersOwnedByThisServerByContainer 754 (long containerId) 755 throws FinderException { 756 return ejbSelectCountTimersByContainerAndOwnerAndState 757 (containerId, getOwnerIdOfThisServer(), ACTIVE); 758 } 759 760 public int ejbHomeSelectCountCancelledTimersOwnedByThisServerByContainer 761 (long containerId) 762 throws FinderException { 763 return ejbSelectCountTimersByContainerAndOwnerAndState 764 (containerId, getOwnerIdOfThisServer(), CANCELLED); 765 } 766 767 768 public int ejbHomeSelectCountAllTimersOwnedByThisServer() 769 throws FinderException { 770 return ejbSelectCountAllTimersByOwner(getOwnerIdOfThisServer()); 771 } 772 773 public int ejbHomeSelectCountAllActiveTimersOwnedByThisServer() 774 throws FinderException { 775 return ejbSelectCountAllTimersByOwnerAndState 776 (getOwnerIdOfThisServer(), ACTIVE); 777 } 778 779 public int ejbHomeSelectCountAllCancelledTimersOwnedByThisServer() 780 throws FinderException { 781 return ejbSelectCountAllTimersByOwnerAndState 782 (getOwnerIdOfThisServer(), CANCELLED); 783 } 784 785 786 public int ejbHomeSelectCountAllTimersOwnedBy(String ownerId) 787 throws FinderException { 788 return ejbSelectCountAllTimersByOwner(ownerId); 789 } 790 791 public int ejbHomeSelectCountAllActiveTimersOwnedBy(String ownerId) 792 throws FinderException { 793 return ejbSelectCountAllTimersByOwnerAndState 794 (ownerId, ACTIVE); 795 } 796 797 public int ejbHomeSelectCountAllCancelledTimersOwnedBy(String ownerId) 798 throws FinderException { 799 return ejbSelectCountAllTimersByOwnerAndState 800 (ownerId, CANCELLED); 801 } 802 803 public boolean ejbHomeCheckStatus(String resourceJndiName, 804 boolean checkDatabase) { 805 806 boolean success = false; 807 808 Connection connection = null; 809 810 try { 811 812 InitialContext ic = new InitialContext (); 813 814 DataSource dataSource = (DataSource ) ic.lookup(resourceJndiName); 815 816 if( checkDatabase ) { 817 connection = dataSource.getConnection(); 818 819 connection.close(); 820 821 connection = null; 822 823 ejbSelectCountTimersByContainer(0); 827 } 828 829 success = true; 830 831 } catch(Exception e) { 832 833 logger.log(Level.WARNING, "ejb.timer_service_init_error", 834 ""); 835 logger.log(Level.FINE, "ejb.timer_service_init_error", e); 839 840 } finally { 841 if( connection != null ) { 842 try { 843 connection.close(); 844 } catch(Exception e) { 845 logger.log(Level.FINE, "timer connection close exception", 846 e); 847 } 848 } 849 } 850 851 return success; 852 } 853 854 866 public static class Blob implements Serializable { 867 868 private byte[] primaryKeyBytes_ = null; 869 private byte[] infoBytes_ = null; 870 871 public Blob() { 872 } 873 874 public Blob(Object primaryKey, Serializable info) 875 throws IOException { 876 if( primaryKey != null ) { 877 primaryKeyBytes_ = EJBUtils.serializeObject(primaryKey); 878 } 879 if( info != null ) { 880 infoBytes_ = EJBUtils.serializeObject(info); 881 } 882 } 883 884 public Object getTimedObjectPrimaryKey(ClassLoader cl) 885 throws Exception { 886 Object pKey = null; 887 if( primaryKeyBytes_ != null) { 888 pKey = EJBUtils.deserializeObject(primaryKeyBytes_, cl); 889 if( logger.isLoggable(Level.FINER) ) { 890 logger.log(Level.FINER, "Deserialized blob : " + pKey); 891 } 892 } 893 return pKey; 894 } 895 896 public Serializable getInfo(ClassLoader cl) throws Exception { 897 Serializable info = null; 898 if( infoBytes_ != null) { 899 info = (Serializable )EJBUtils.deserializeObject(infoBytes_, cl); 900 if( logger.isLoggable(Level.FINER) ) { 901 logger.log(Level.FINER, "Deserialized blob : " + info); 902 } 903 } 904 return info; 905 } 906 } 907 908 private static class TimerSynch implements Synchronization { 909 910 private TimerPrimaryKey timerId_; 911 private int state_; 912 private Date timeout_; 913 private BaseContainer container_; 914 915 public TimerSynch(TimerPrimaryKey timerId, int state, Date timeout, 916 BaseContainer container) { 917 timerId_ = timerId; 918 state_ = state; 919 timeout_ = timeout; 920 container_ = container; 921 } 922 923 public void afterCompletion(int status) { 924 EJBTimerService timerService = getEJBTimerService(); 925 926 if( logger.isLoggable(Level.FINE) ) { 927 logger.log(Level.FINE, "TimerSynch::afterCompletion. " + 928 "timer state = " + stateToString(state_) + 929 " , " + "timer id = " + 930 timerId_ + " , JTA TX status = " + 931 txStatusToString(status) + " , " + 932 "timeout = " + timeout_); 933 } 934 935 switch(state_) { 936 case ACTIVE : 937 if( status == Status.STATUS_COMMITTED ) { 938 timerService.scheduleTask(timerId_, timeout_); 939 container_.incrementCreatedTimedObject(); 940 } else { 941 timerService.expungeTimer(timerId_); 942 } 943 break; 944 case CANCELLED : 945 if( status == Status.STATUS_ROLLEDBACK ) { 946 if( timeout_ != null ) { 947 timerService.scheduleTask(timerId_, timeout_); 950 } else { 951 timerService.restoreTaskToDelivered(timerId_); 954 } 955 } else { 956 timerService.expungeTimer(timerId_); 957 container_.incrementRemovedTimedObject(); 958 } 959 break; 960 } 961 } 962 963 public void beforeCompletion() {} 964 965 } 966 967 public static void testCreate(String timerId, EJBContext context, 968 String ownerId, 969 Date initialExpiration, 970 long intervalDuration, 971 Serializable info) throws CreateException { 972 973 EJBTimerService ejbTimerService = getEJBTimerService(); 974 TimerLocalHome timerLocalHome = ejbTimerService.getTimerBeanHome(); 975 976 EjbDescriptor ejbDesc = (EjbDescriptor) 977 Switch.getSwitch(). 978 getDescriptorFor(((EJBContextImpl) context).getContainer()); 979 long containerId = ejbDesc.getUniqueId(); 980 981 Object timedObjectPrimaryKey = (context instanceof EntityContext ) ? 982 ((EntityContext )context).getPrimaryKey() : null; 983 984 timerLocalHome.create(timerId, containerId, ownerId, 985 timedObjectPrimaryKey, initialExpiration, 986 intervalDuration, info); 987 return; 988 } 989 990 public static void testMigrate(String fromOwnerId) { 991 992 EJBTimerService ejbTimerService = getEJBTimerService(); 993 ejbTimerService.migrateTimers(fromOwnerId); 994 995 } 996 997 private BaseContainer getContainer(long containerId) { 998 ContainerFactory cf = Switch.getSwitch().getContainerFactory(); 999 return (BaseContainer) cf.getContainer(containerId); 1000 } 1001 1002} 1003 | Popular Tags |