1 51 52 package org.objectweb.jass.hls.ont; 53 54 import org.apache.log4j.Logger; 55 56 import javax.activity.*; 57 import javax.activity.Status ; 58 import javax.activity.SystemException ; 59 import javax.activity.opennested.*; 60 import javax.naming.*; 61 import javax.transaction.*; 62 63 import java.io.Serializable ; 64 import java.util.Stack ; 65 66 75 public class ONT implements UserOpenNested, Serializable { 76 77 private static Logger log = Logger.getLogger(ONT.class); 78 79 81 83 private static ONT singleton = new ONT(); 84 85 private ONTServiceManager serviceManager; 87 private ActivityManager activityManager = null; 89 private TransactionManager tm = null; 91 private int defaultTimeout = 0; 93 95 100 private ONT() { 101 102 try { 103 Context ctx = new InitialContext(); 104 105 log.info( 106 getThreadName() + " Looking for an ActivityManager instance..."); 107 activityManager = (ActivityManager) ctx.lookup("ActivityManager"); 108 log.info(getThreadName() + " AM instance found!!!"); 109 110 log.info(getThreadName() + " Looking for a TransactionManager..."); 111 tm = ((TransactionManager) ctx.lookup("java:/TransactionManager")); 112 log.info(" TM found !!!"); 113 114 serviceManager = new ONTServiceManager(); 116 activityManager.registerService(serviceManager); 117 118 log.info( 119 getThreadName() 120 + " ONT service registered in Activity Service instance"); 121 } catch (NamingException e) { 122 log.error("Instance NOT found"); 123 e.printStackTrace(); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 129 131 135 public static ONT getSingleton() { 136 137 return singleton; 138 } 139 140 142 144 160 public void activityBegin(int timeout) 161 throws InvalidActivityException, TimeoutRangeException { 162 163 ThreadInfo ti = getThreadInfo(); 164 165 log.info(getThreadName() + " BEGINING ONT ACTIVITY..."); 166 167 try { 168 if (tm.getTransaction() != null) { 170 log.info("\t" + tm.getTransaction() + " is suspending..."); 171 ti.suspendedTxs.push(tm.suspend()); 172 } else 173 log.info("\tThere are no transactions to suspend"); 174 175 log.info("\tTransactions in stack: " + ti.suspendedTxs.size()); 176 if (timeout == 0) { 177 activityManager.begin(defaultTimeout); 178 } else { 179 activityManager.begin(timeout); 180 } 181 182 tm.begin(); 184 log.info("\t" + tm.getTransaction() + " started"); 185 } catch (InvalidStateException e) { 186 throw new InvalidActivityException(); 187 } catch (TimeoutRangeException e) { 188 throw e; 189 } catch (Exception e) { 190 e.printStackTrace(); 191 } 192 log.info(getThreadName() + " ONT ACTIVITY BEGUN"); 193 log.info( 194 "\n----------------------------------------------------------------------------------\n"); 195 } 196 197 207 public void activityCommit(Compensator compensator_object) 208 throws 209 NoActivityException, 210 HeuristicMixedException, 211 HeuristicRollbackException, 212 ActivityPendingException, 213 ContextPendingException, 214 NotOriginatorException, 215 ActivityRolledBackException, 216 HeuristicCompensateException, 217 HeuristicNoCompensateException { 218 219 Outcome out = null; 220 ThreadInfo ti = getThreadInfo(); 221 222 log.info(getThreadName() + " COMMITTING ONTs ACTIVITY..."); 223 224 try { 225 log.info("\t " + tm.getTransaction() + " is committing..."); 226 tm.commit(); 227 log.info("\t Transaction committed !!!!!"); 228 if (compensator_object != null) { 229 CompensatingAction action = 230 new CompensatingAction(compensator_object, 0); 231 ActivityCoordinator parent = activityManager.getParentCoordinator(); 232 if (parent != null) { 234 try { 235 parent.addAction( 238 action, 239 ONTCompletionSS.COMPLETION_SS_NAME, 240 0); 241 log.debug( 242 "Compensator added sucessfully with parent (" 243 + parent.getName() 244 + ")"); 245 } catch (Exception e) { 246 log.info("Action not added to the parent. Compensating..."); 247 compensator_object.compensate(); 248 log.info( 249 "Triying to complete activity " 250 + getActivityName() 251 + " with CompletionStatusFail *****"); 252 out = 253 activityManager.completeWithStatus( 254 CompletionStatus.CompletionStatusFail); 255 throw new ActivityRolledBackException(); 256 } 257 } else 258 log.info( 259 "There is no parent activity to register the compensator!"); 260 } 261 log.info( 262 "Triying to complete activity " 263 + getActivityName() 264 + " with CompletionStatusSuccess *****"); 265 out = 266 activityManager.completeWithStatus( 267 CompletionStatus.CompletionStatusSuccess); 268 if (out != null) 269 if (out.getName().equals("heuristic_compensate_decision")) 270 throw new HeuristicCompensateException(); 271 272 } catch (RollbackException e) { 273 try { 274 log.info("\t Transaction Rolledback !!!!!"); 275 log.info( 276 " Triying to complete activity " 277 + getActivityName() 278 + " with CompletionStatusFail *****"); 279 out = 280 activityManager.completeWithStatus( 281 CompletionStatus.CompletionStatusFail); 282 if (out == null) { 283 throw new ActivityRolledBackException(); 284 } else { 285 if (out.getName().equals("heuristic_cannot_compensate")) 286 throw new HeuristicNoCompensateException(); 287 } 288 } catch (Exception e1) { 289 e1.printStackTrace(); 290 } 291 } catch (ActivityRolledBackException e) { 292 throw e; 293 } catch (Exception e) { 294 e.printStackTrace(); 295 } finally { 296 log.info(getThreadName() + " ONT ACTIVITY COMPLETED"); 297 if (!ti.suspendedTxs.empty()) { 299 try { 300 tm.resume((Transaction) ti.suspendedTxs.pop()); 301 log.info("\t" + tm.getTransaction() + " has been resumed"); 302 log.info("\tTransactions in stack: " + ti.suspendedTxs.size()); 303 } catch (Exception e) { 304 e.printStackTrace(); 305 } 306 } 307 log.info( 308 "\n----------------------------------------------------------------------------------\n"); 309 } 310 } 311 312 317 public void activityRollback() 318 throws NoActivityException, HeuristicNoCompensateException { 319 320 Outcome out = null; 321 ThreadInfo ti = getThreadInfo(); 322 323 log.info(getThreadName() + " ROLLING-BACK ONT ACTIVITY..."); 324 325 try { 326 if (activityManager.getCoordinator() == null) 327 throw new NoActivityException(); 328 329 log.info("\t" + tm.getTransaction() + " is rolling-back..."); 330 tm.rollback(); 331 log.info("\tTransaction rolled-back !!!!!"); 332 log.info( 333 "Completing activity " + getActivityName() + " with fail *****"); 334 335 out = activityManager.completeWithStatus( 336 CompletionStatus.CompletionStatusFail); 337 338 if (out != null) { 339 if (out.getName().equals("heuristic_cannot_compensate")) 340 throw new HeuristicNoCompensateException(); 341 } 342 } catch (Exception e) { 343 e.printStackTrace(); 344 } finally { 345 log.info(getThreadName() + " ONT ACTIVITY COMPLETED"); 346 if (!ti.suspendedTxs.empty()) { 347 try { 348 tm.resume((Transaction) ti.suspendedTxs.pop()); 349 log.info("\t" + tm.getTransaction() + " has been resumed"); 350 log.info("\tTransactions in stack: " + ti.suspendedTxs.size()); 351 } catch (Exception e) { 352 e.printStackTrace(); 353 } 354 } 355 log.info( 356 "\n----------------------------------------------------------------------------------\n"); 357 } 358 } 359 360 364 public void activitySetRollbackOnly() throws NoActivityException { 365 try { 366 tm.setRollbackOnly(); 367 activityManager.setCompletionStatus( 368 CompletionStatus.CompletionStatusFailOnly); 369 } catch (NoActivityException e) { 370 e.printStackTrace(); 371 throw e; 372 } catch (Exception e) { 373 e.printStackTrace(); 374 } 375 } 376 377 382 public void activitySetTimeout(int seconds) throws TimeoutRangeException { 383 if (seconds < -1) 384 throw new TimeoutRangeException(); 385 defaultTimeout = seconds; 386 } 387 388 392 public int getActivityTimeout() { 393 return defaultTimeout; 394 } 395 396 401 public int activityGetStatus() { 402 int status = Status.StatusNoActivity; 403 try { 404 status = activityManager.getStatus(); 405 } catch (Exception e) { 406 e.printStackTrace(); 407 } 408 return status; 409 } 410 411 416 public String getActivityName() { 417 String name = null; 418 try { 419 name = activityManager.getName(); 420 } catch (Exception e) { 421 e.printStackTrace(); 422 } 423 return name; 424 } 425 426 431 public String getTransactionName() { 432 String name = null; 433 try { 434 name = tm.getTransaction().toString(); 435 } catch (javax.transaction.SystemException e) { 436 e.printStackTrace(); 437 } 438 return name; 439 } 440 441 443 445 private ThreadLocal threadActivityInfo = new ThreadLocal (); 447 448 452 private ThreadInfo getThreadInfo() { 453 454 ThreadInfo ret = (ThreadInfo) threadActivityInfo.get(); 455 456 if (ret == null) { 457 ret = new ThreadInfo(); 458 threadActivityInfo.set(ret); 459 } else 460 log.debug("Thread info != null"); 461 462 return ret; 463 } 464 465 469 private String getThreadName() { 470 return Thread.currentThread().getName(); 471 } 472 473 475 480 static class ThreadInfo { 481 Stack suspendedTxs = new Stack (); 482 Stack suspendedAct = new Stack (); 483 } 484 485 487 492 public ONTActivity suspend() { 493 ActivityToken at = null; 494 Transaction tx = null; 495 ONTActivityImpl activity = null; 496 497 try { 498 tx = tm.suspend(); 499 at = activityManager.suspend(); 500 activity = new ONTActivityImpl(tx, at); 501 } catch (javax.transaction.SystemException e) { 502 e.printStackTrace(); 503 } catch (ServiceNotRegisteredException e) { 504 e.printStackTrace(); 505 } catch (SystemException e) { 506 e.printStackTrace(); 507 } 508 509 517 return activity; 518 } 519 520 525 public void resume(ONTActivity activity) { 526 527 if (activity != null) { 528 try { 529 tm.resume(activity.getTransaction()); 530 activityManager.resume(activity.getActivityToken()); 531 } catch (Exception e) { 532 e.printStackTrace(); 533 } 534 } 535 536 548 } 549 550 552 } | Popular Tags |