1 51 52 package org.objectweb.jass.as; 53 54 import java.util.Collections ; 55 import java.util.HashMap ; 56 import java.util.Map ; 57 58 import javax.activity.ActivityCompletedException ; 59 import javax.activity.ActivityCoordinator ; 60 import javax.activity.ActivityNotProcessedException ; 61 import javax.activity.ActivityPendingException ; 62 import javax.activity.ActivityToken ; 63 import javax.activity.CompletionStatus ; 64 import javax.activity.ContextPendingException ; 65 import javax.activity.GlobalId ; 66 import javax.activity.InvalidActivityException ; 67 import javax.activity.InvalidParentContextException ; 68 import javax.activity.InvalidStateException ; 69 import javax.activity.NoActivityException ; 70 import javax.activity.NoImplementException ; 71 import javax.activity.NotOriginatorException ; 72 import javax.activity.Outcome ; 73 import javax.activity.PropertyGroupUnknownException ; 74 import javax.activity.ServiceAlreadyRegisteredException ; 75 import javax.activity.ServiceInformation ; 76 import javax.activity.ServiceNotRegisteredException ; 77 import javax.activity.SignalSetUnknownException ; 78 import javax.activity.Status ; 79 import javax.activity.SystemException ; 80 import javax.activity.TimeoutRangeException ; 81 import javax.activity.ActivityManager ; 82 import javax.activity.coordination.ServiceManager; 83 import javax.activity.propertygroup.PropertyGroup; 84 import javax.activity.propertygroup.PropertyGroupManager; 85 import javax.naming.Context ; 86 import javax.naming.InitialContext ; 87 import javax.naming.NamingException ; 88 import javax.transaction.Transaction ; 89 90 import org.apache.log4j.Logger; 91 import org.jboss.tm.TxManager; 92 93 99 public class ActivityService implements ActivityManager { 100 101 103 public static int DEFAULT_TIMEOUT = 0; 105 public static String SYNCHRONIZATION_SS_NAME = 106 "org.omg.CosActivity.Syncrhonization"; 107 public static String CHILDLIFETIME_SS_NAME = 108 "org.omg.CosActivity.ChildLifetime"; 109 110 112 private static Logger log = Logger.getLogger(ActivityService.class); 113 114 private static ActivityIdGenerator activityIdGenerator = null; 116 117 protected ServiceManager sm = null; 119 private ServiceInformation serviceInformation = null; 121 private HashMap propertyGroupManagers = new HashMap (); 123 private Map activities = Collections.synchronizedMap(new HashMap ()); 126 private int defaultTimeout = DEFAULT_TIMEOUT; 128 129 131 135 public ActivityService() { 136 137 try { 138 Context ctx = new InitialContext (); 139 log.info("Looking for a Transaction Manager..."); 140 jbossTm = (TxManager) ctx.lookup("java:/TransactionManager"); 141 log.info("TM found !!!"); 142 } catch (NamingException e) { 145 e.printStackTrace(); 146 } 147 148 instances = instances + 1; 149 log.debug("UserActivity instance number " + instances + " created!!!"); 150 } 151 152 154 156 172 public void begin(int timeout) 173 throws 174 InvalidStateException , 175 ServiceNotRegisteredException , 176 TimeoutRangeException , 177 SystemException { 178 179 ActivityImpl newActivity = null; ActivityIdImpl activityId = null; ActivityImpl superior = null; 183 184 boolean child = false; 186 if (activityIdGenerator == null) 187 throw new SystemException ("ActivityIdGenerator is not set !!!!!"); 188 if (sm == null) 189 throw new ServiceNotRegisteredException (); 190 if (timeout < -1) 191 throw new TimeoutRangeException (); 192 193 ThreadInfo ti = getThreadInfo(); 194 ActivityImpl currentActivity = ti.activity; 195 196 if (currentActivity != null) { 199 superior = currentActivity; 200 if (superior.getCompletionStatus() 201 == CompletionStatus.CompletionStatusFailOnly) 202 throw new InvalidStateException (); 203 child = true; 204 disassociateThread(ti); 205 ti.tx = null; log.info("Starting a CHILD activity of " + superior.getName()); 207 } else 208 log.info("Starting a ROOT activity"); 209 210 activityId = activityIdGenerator.newActivityId(); 212 213 if (timeout == -1) 214 newActivity = new ActivityImpl(this, activityId, superior, 0); 215 else if (timeout == 0) 216 newActivity = 217 new ActivityImpl(this, activityId, superior, defaultTimeout); 218 else 219 newActivity = new ActivityImpl(this, activityId, superior, timeout); 220 221 activities.put(activityId.print(), newActivity); 223 associateThread(ti, newActivity); 225 226 if (child) { 227 superior.addChild(newActivity); 229 } 233 } 234 235 251 public Outcome completeWithStatus(int completionStatus) 252 throws 253 NoActivityException , 254 ActivityPendingException , 255 ContextPendingException , 256 ServiceNotRegisteredException , 257 NotOriginatorException , 258 InvalidStateException , 259 ActivityNotProcessedException , 260 SystemException { 261 262 log.debug("completeWithStatus " + Thread.currentThread().getName()); 263 Outcome out = null; 264 265 if (sm == null) 266 throw new ServiceNotRegisteredException (); 267 268 ThreadInfo ti = getThreadInfo(); 269 ActivityImpl currentActivity = ti.activity; 270 if (currentActivity == null) 271 throw new NoActivityException (); 272 273 int compStat = getCompletionStatus(); 274 ActivityImpl[] childs = currentActivity.getChilds(); 275 if ((compStat == CompletionStatus.CompletionStatusSuccess) 276 && (childs != null)) 277 throw new ContextPendingException (); 278 279 try { 280 currentActivity.setCompletionStatus(completionStatus); 282 out = currentActivity.completeActivity(); 283 currentActivity = (ActivityImpl) disassociateThread(ti); 284 if (currentActivity.getParent() != null) { 285 associateThread(ti, (ActivityImpl) currentActivity.getParent()); 286 ti.tx = 287 ((ActivityImpl) currentActivity.getParent()).getTransaction(); 288 } else ti.tx = null; } catch (Exception e) { 292 e.printStackTrace(); 293 } 294 295 return out; 296 } 297 298 315 public Outcome complete() 316 throws 317 NoActivityException , 318 ActivityPendingException , 319 ContextPendingException , 320 ServiceNotRegisteredException , 321 NotOriginatorException , 322 ActivityNotProcessedException , 323 SystemException { 324 325 log.debug("complete " + Thread.currentThread().getName()); 326 Outcome out = null; 327 328 if (sm == null) 329 throw new ServiceNotRegisteredException (); 330 331 ThreadInfo ti = getThreadInfo(); 332 ActivityImpl currentActivity = ti.activity; 333 if (currentActivity == null) 334 throw new NoActivityException (); 335 336 int compStat = getCompletionStatus(); 337 ActivityImpl[] childs = currentActivity.getChilds(); 338 if ((compStat == CompletionStatus.CompletionStatusSuccess) 339 && (childs != null)) { 340 341 log.info("There are alive childs"); 342 for(int i = 0;i<childs.length;i++) 343 log.info("Child " + childs[i].getName()); 344 throw new ContextPendingException (); 345 } 346 try { 347 out = currentActivity.completeActivity(); 348 currentActivity = (ActivityImpl) disassociateThread(ti); 349 if (currentActivity.getParent() != null) { 350 associateThread(ti, (ActivityImpl) currentActivity.getParent()); 351 ti.tx = 353 ((ActivityImpl) currentActivity.getParent()).getTransaction(); 354 } else ti.tx = null; } catch (Exception e) { 358 e.printStackTrace(); 359 } 360 return out; 361 } 362 363 381 public void setCompletionStatus(int completionStatus) 382 throws 383 NoActivityException , 384 InvalidStateException , 385 ServiceNotRegisteredException , 386 SystemException { 387 388 if (sm == null) 389 throw new ServiceNotRegisteredException (); 390 391 ThreadInfo ti = getThreadInfo(); 392 ActivityImpl currentActivity = ti.activity; 393 394 if (currentActivity == null) 395 throw new NoActivityException (); 396 if (currentActivity.getCompletionStatus() 397 == CompletionStatus.CompletionStatusFailOnly) 398 throw new InvalidStateException (); 399 400 currentActivity.setCompletionStatus(completionStatus); 402 } 403 404 417 public int getCompletionStatus() 418 throws ServiceNotRegisteredException , NoActivityException , SystemException { 419 420 if (sm == null) 421 throw new ServiceNotRegisteredException (); 422 423 ThreadInfo ti = getThreadInfo(); 424 ActivityImpl currentActivity = ti.activity; 425 426 if (currentActivity == null) 427 throw new NoActivityException (); 428 429 return currentActivity.getCompletionStatus(); 430 } 431 432 441 public int getStatus() 442 throws ServiceNotRegisteredException , SystemException { 443 444 if (sm == null) 445 throw new ServiceNotRegisteredException (); 446 447 ThreadInfo ti = getThreadInfo(); 448 ActivityImpl currentActivity = ti.activity; 449 450 if (currentActivity != null) 451 return currentActivity.getStatus(); 452 else 453 throw new SystemException (); 454 } 455 456 465 public String getName() 466 throws ServiceNotRegisteredException , SystemException { 467 468 if (sm == null) 469 throw new ServiceNotRegisteredException (); 470 471 ThreadInfo ti = getThreadInfo(); 472 ActivityImpl currentActivity = ti.activity; 473 474 if (currentActivity != null) 475 return currentActivity.getName(); 476 else 477 return null; 478 } 479 480 501 public void setTimeout(int timeout) 502 throws ServiceNotRegisteredException , TimeoutRangeException , SystemException { 503 504 if (timeout < -1) 505 throw new TimeoutRangeException (); 506 507 if (timeout == 0) 510 defaultTimeout = DEFAULT_TIMEOUT; 511 else 512 defaultTimeout = timeout; 513 } 514 515 524 public int getTimeout() 525 throws ServiceNotRegisteredException , SystemException { 526 527 if (sm == null) 528 throw new ServiceNotRegisteredException (); 529 530 return defaultTimeout; 531 } 532 533 542 public GlobalId getGlobalId() 543 throws ServiceNotRegisteredException , SystemException { 544 545 if (sm == null) 546 throw new ServiceNotRegisteredException (); 547 548 ThreadInfo ti = getThreadInfo(); 549 ActivityImpl currentActivity = ti.activity; 550 551 if (currentActivity != null) 552 return currentActivity.getGlobalId(); 553 else 554 return null; 555 } 556 557 580 public Outcome broadcast(java.lang.String signalSetName) 581 throws 582 NoActivityException , 583 SignalSetUnknownException , 584 ServiceNotRegisteredException , 585 InvalidActivityException , 586 ActivityNotProcessedException , 587 SystemException { 588 589 if (sm == null) 590 throw new ServiceNotRegisteredException (); 591 592 ThreadInfo ti = getThreadInfo(); 593 ActivityImpl currentActivity = ti.activity; 594 595 if (currentActivity == null) 596 throw new NoActivityException (); 597 if ((signalSetName.equals(SYNCHRONIZATION_SS_NAME)) 598 || (signalSetName.equals(SYNCHRONIZATION_SS_NAME)) 599 || currentActivity.getStatus() == Status.StatusCompleting) 600 throw new InvalidActivityException (); 601 602 return currentActivity.processSignalSet( 603 signalSetName, 604 currentActivity.getStatus()); 605 } 606 607 617 public ActivityCoordinator getCoordinator() 618 throws ServiceNotRegisteredException , NoImplementException , SystemException { 619 620 if (sm == null) 621 throw new ServiceNotRegisteredException (); 622 623 ThreadInfo ti = getThreadInfo(); 624 ActivityImpl currentActivity = ti.activity; 625 626 if (currentActivity != null) 627 return currentActivity; 628 else 629 return null; 630 } 631 632 647 public ActivityCoordinator getParentCoordinator() 648 throws ServiceNotRegisteredException , NoImplementException , SystemException { 649 650 if (sm == null) 651 throw new ServiceNotRegisteredException (); 652 653 ThreadInfo ti = getThreadInfo(); 654 ActivityImpl currentActivity = ti.activity; 655 656 if ((currentActivity != null) && (currentActivity.getParent() != null)) 657 return currentActivity.getParent(); 658 else 659 return null; 660 } 661 662 665 public PropertyGroup getPropertyGroup(java.lang.String name) 666 throws 667 PropertyGroupUnknownException , 668 ServiceNotRegisteredException , 669 NoActivityException , 670 SystemException { 671 672 return null; 673 } 674 675 690 public void registerService(ServiceManager service) 691 throws 692 PropertyGroupUnknownException , 693 ServiceAlreadyRegisteredException , 694 SystemException { 695 696 String [] propertyGroupNames = null; 697 698 if (sm != null) 699 throw new ServiceAlreadyRegisteredException (); 700 701 sm = service; 702 serviceInformation = sm.getServiceInformation(); 703 propertyGroupNames = sm.getPropertyGroupNames(); 704 if (propertyGroupNames != null) { 705 for (int i = 0; i < propertyGroupNames.length; i++) 706 try { 707 PropertyGroupManager pgm = 708 sm.getPropertyGroupManager(propertyGroupNames[i]); 709 propertyGroupManagers.put(propertyGroupNames[i], pgm); 710 } catch (PropertyGroupUnknownException e) { 711 throw new PropertyGroupUnknownException (); 712 } 713 } 714 log.info( 715 "Service Manager " 716 + serviceInformation.getServiceName() 717 + " has been registered"); 718 719 } 720 721 728 public ServiceManager getService() throws SystemException { 729 730 if (sm != null) 731 return sm; 732 else 733 return null; 734 } 735 736 739 public ActivityCoordinator recreate( 740 GlobalId activity, 741 GlobalId parent, 742 boolean resume) 743 throws 744 ServiceNotRegisteredException , 745 ActivityCompletedException , 746 java.lang.IllegalArgumentException , 747 SystemException { 748 749 return null; 750 } 751 752 755 public GlobalId [] recover() 756 throws ServiceNotRegisteredException , SystemException { 757 758 return null; 759 } 760 761 764 public void forget(GlobalId globalId) 765 throws ServiceNotRegisteredException , SystemException { 766 767 } 768 769 771 788 public ActivityToken suspend() 789 throws ServiceNotRegisteredException , SystemException { 790 791 ActivityContext context = null; 792 793 ThreadInfo ti = getThreadInfo(); 794 ActivityImpl currentActivity = ti.activity; 795 Transaction currentTx = ti.tx; 797 if (currentActivity != null) { 798 ActivityImpl parent = (ActivityImpl) currentActivity.getParent(); 799 800 log.warn( 801 "SUSPEND: Disassociating activity " 802 + ti.activity.getName() 803 + " from thread " 804 + Thread.currentThread().toString()); 805 try { 806 807 if ((currentTx != null) 808 && currentTx.equals(jbossTm.getTransaction())) { jbossTm.suspend(); 810 log.info("TX SUSPENDED"); 811 } 812 } catch (Exception e) { 813 e.printStackTrace(); 814 } 815 disassociateThread(ti); 816 817 if (parent != null) { 818 log.warn( 819 "SUSPEND: Associating activity " 820 + parent.getName() 821 + " to thread " 822 + Thread.currentThread().toString()); 823 associateThread(ti, parent); 824 ti.tx = parent.getTransaction(); 826 if (ti.tx != null) { try { 828 jbossTm.resume(ti.tx); 829 log.info("TX RESUMED"); 830 } catch (Exception e) { 831 e.printStackTrace(); 832 } 833 } 834 context = 835 new ActivityContext(currentActivity, parent.getGlobalId()); 836 } else 837 context = new ActivityContext(currentActivity, null); 838 } 839 840 return context; 841 } 842 843 862 public void resume(ActivityToken activityToken) 863 throws 864 InvalidActivityException , 865 InvalidParentContextException , 866 ServiceNotRegisteredException , 867 SystemException { 868 869 if (sm == null) 870 throw new ServiceNotRegisteredException (); 871 if (activityToken == null) 872 throw new InvalidActivityException (); 873 874 ActivityContext context = (ActivityContext) activityToken; 875 876 ThreadInfo ti = getThreadInfo(); 877 ActivityImpl currentActivity = ti.activity; 878 879 if (currentActivity != null) { 880 log.warn("current " + currentActivity.getName()); 881 log.warn("parent " + context.getParent().print()); 882 if (!currentActivity.getGlobalId().equals(context.getParent())) 883 throw new InvalidParentContextException (); 884 885 log.warn( 886 "RESUME: Disassociating activity " 887 + ti.activity.getName() 888 + " from thread " 889 + Thread.currentThread().toString()); 890 disassociateThread(ti); 891 ti.tx = null; } 893 log.warn( 894 "RESUME: Associating activity " 895 + context.getActivity().getName() 896 + " to thread " 897 + Thread.currentThread().toString()); 898 associateThread(ti, context.getActivity()); 899 ti.tx = context.getActivity().getTransaction(); 901 if (ti.tx != null) { try { 903 jbossTm.resume(ti.tx); 904 log.info("TX RESUMED"); 905 } catch (Exception e) { 906 e.printStackTrace(); 907 } 908 } 909 } 910 911 914 public ActivityToken suspendGroup() 915 throws ServiceNotRegisteredException , SystemException { 916 return null; 917 } 918 919 922 public void resumeGroup(ActivityToken activityToken) 923 throws 924 InvalidActivityException , 925 InvalidParentContextException , 926 ServiceNotRegisteredException , 927 SystemException { 928 } 929 930 933 public ActivityToken suspendAll() 934 throws ServiceNotRegisteredException , SystemException { 935 return null; 936 } 937 938 941 public void resumeAll(ActivityToken activityToken) 942 throws 943 InvalidActivityException , 944 InvalidParentContextException , 945 ServiceNotRegisteredException , 946 SystemException { 947 } 948 949 952 public GlobalId hibernate() 953 throws 954 ServiceNotRegisteredException , 955 InvalidActivityException , 956 SystemException { 957 return null; 958 } 959 960 963 public void reactivate(GlobalId globalId) 964 throws 965 ActivityCompletedException , 966 InvalidParentContextException , 967 ServiceNotRegisteredException , 968 SystemException { 969 } 970 971 973 978 public static void setActivityIdGenerator(ActivityIdGenerator generator) { 979 activityIdGenerator = generator; 980 } 981 982 987 public int getNumberOfActivities() { 988 return activities.size(); 989 } 990 991 993 997 private String getThreadName() { 998 return Thread.currentThread().getName(); 999 } 1000 1001 private void completeChilds(ActivityImpl currentActivity) { 1002 try { 1003 int compStat = getCompletionStatus(); 1004 if ((compStat == CompletionStatus.CompletionStatusFail) 1005 || (compStat == CompletionStatus.CompletionStatusFailOnly)) { 1006 ActivityImpl[] childs = currentActivity.getChilds(); 1009 1010 for (int i = 0; i < childs.length; i++) { 1011 if (childs[i].getStatus() == Status.StatusActive) 1012 childs[i].setCompletionStatus( 1013 CompletionStatus.CompletionStatusFailOnly); 1014 } 1015 } 1016 } catch (ServiceNotRegisteredException e) { 1017 e.printStackTrace(); 1018 } catch (NoActivityException e) { 1019 e.printStackTrace(); 1020 } catch (SystemException e) { 1021 e.printStackTrace(); 1022 } catch (InvalidStateException e) { 1023 e.printStackTrace(); 1024 } 1025 } 1026 1027 1029 protected ThreadLocal threadActivity = new ThreadLocal (); 1031 1032 1036 protected ThreadInfo getThreadInfo() { 1037 1038 ThreadInfo ret = (ThreadInfo) threadActivity.get(); 1039 1040 if (ret == null) { 1041 ret = new ThreadInfo(); 1042 threadActivity.set(ret); 1043 } 1044 1045 return ret; 1046 } 1047 1048 1054 protected void associateThread(ThreadInfo ti, ActivityImpl activity) { 1055 1056 ti.activity = activity; 1057 activity.associateCurrentThread(); 1058 } 1059 1060 1066 protected ActivityCoordinator disassociateThread(ThreadInfo ti) { 1067 1068 ActivityImpl current = ti.activity; 1069 ti.activity = null; 1070 current.disassociateCurrentThread(); 1071 1072 return current; 1073 } 1074 1075 1076 public void dissasociateThread() { 1077 ThreadInfo ti = getThreadInfo(); 1078 ActivityImpl currentActivity = ti.activity; 1079 if (currentActivity != null) { 1080 currentActivity.disassociateCurrentThread(); 1081 ti.activity = null; 1082 ti.tx = null; } 1084 } 1085 1086 1091 protected void releaseActivityImpl(ActivityImpl activity) { 1092 1093 try { 1094 activities.remove(activity.getGlobalId().print()); 1095 } catch (SystemException e) { 1096 e.printStackTrace(); 1097 } 1098 log.debug("Activities in this instance " + activities.size()); 1099 } 1100 1101 1103 1106 static class ThreadInfo { 1107 ActivityImpl activity = null; 1108 Transaction tx = null; 1110 } 1111 1112 1114 1117 private static int instances = 0; 1118 1119 public static int getInstances() { 1120 return instances; 1121 } 1122 1123 1126 private static TxManager jbossTm = null; 1128 1129 public void txBegin(Transaction tx) throws SystemException { 1130 1131 ThreadInfo ti = getThreadInfo(); 1132 ActivityImpl currentActivity = ti.activity; 1133 Transaction currentTx = ti.tx; 1134 1135 log.info(getThreadName() + " " + tx + " has begin in JBoss TM"); 1136 1137 if (currentActivity != null) { 1138 if (currentTx != null) { 1139 log.warn("A transaction exists!!! -> " + currentTx.toString()); 1140 } else { 1141 ti.tx = tx; 1142 currentActivity.setTransaction(tx); 1143 log.info("Associated with " + currentActivity.getName()); 1144 } 1145 } 1146 } 1147 1148 public void txCommit(Transaction tx) throws SystemException { 1149 1150 ThreadInfo ti = getThreadInfo(); 1151 ActivityImpl currentActivity = ti.activity; 1152 Transaction currentTx = ti.tx; 1153 1154 log.info(getThreadName() + " " + tx + " has committed in JBoss TM"); 1155 1156 if (currentActivity != null) { 1157 if (currentTx != null) { 1158 ti.tx = null; 1159 currentActivity.setTransaction(null); 1160 log.info("Disassociated from " + currentActivity.getName()); 1161 } else 1162 log.warn("There is no transaction in " + currentActivity.getName()); 1163 } 1164 1165 } 1166 1167 public void txRollback(Transaction tx) throws SystemException { 1168 1169 ThreadInfo ti = getThreadInfo(); 1170 ActivityImpl currentActivity = ti.activity; 1171 Transaction currentTx = ti.tx; 1172 1173 log.info(getThreadName() + " " + tx + " has rolled-back in JBoss TM"); 1174 1175 if (currentActivity != null) { 1176 if (currentTx != null) { 1177 ti.tx = null; 1178 currentActivity.setTransaction(null); 1179 log.info("Disassociated from " + currentActivity.getName()); 1180 } else 1181 log.warn("There is no transaction in " + currentActivity.getName()); 1182 } 1183 1184 } 1185 1186 1188} | Popular Tags |