1 22 package org.jboss.tm.iiop.client; 23 24 import java.io.Serializable ; 25 26 import javax.naming.NamingException ; 27 import javax.naming.Reference ; 28 import javax.naming.Referenceable ; 29 30 import javax.transaction.HeuristicMixedException ; 31 import javax.transaction.HeuristicRollbackException ; 32 import javax.transaction.NotSupportedException ; 33 import javax.transaction.RollbackException ; 34 import javax.transaction.Status ; 35 import javax.transaction.SystemException ; 36 import javax.transaction.UserTransaction ; 37 38 import org.omg.CORBA.BAD_INV_ORDER ; 39 import org.omg.CORBA.NO_PERMISSION ; 40 import org.omg.CORBA.OBJECT_NOT_EXIST ; 41 import org.omg.CORBA.ORB ; 42 import org.omg.CORBA.TRANSACTION_ROLLEDBACK ; 43 import org.omg.CosNaming.NamingContextExt ; 44 import org.omg.CosNaming.NamingContextExtHelper ; 45 import org.omg.CosTransactions.Control; 46 import org.omg.CosTransactions.Coordinator; 47 import org.omg.CosTransactions.HeuristicMixed; 48 import org.omg.CosTransactions.HeuristicHazard; 49 import org.omg.CosTransactions.Inactive; 50 import org.omg.CosTransactions.NoTransaction; 51 import org.omg.CosTransactions.PropagationContext; 52 import org.omg.CosTransactions.Terminator; 53 54 import org.jboss.iiop.CorbaORB; 55 import org.jboss.tm.iiop.TransactionDesc; 56 import org.jboss.tm.iiop.TransactionFactoryExt; 57 import org.jboss.tm.iiop.TransactionFactoryExtHelper; 58 import org.jboss.tm.iiop.TxClientInterceptor; 59 60 72 public class IIOPClientUserTransaction 73 implements UserTransaction , 74 Referenceable , 75 Serializable 76 { 77 static final long serialVersionUID = 6653800687253055416L; 79 80 81 private static IIOPClientUserTransaction singleton = null; 82 83 84 private static TransactionFactoryExt txFactory; 85 86 87 private static ThreadLocal threadLocalData = new ThreadLocal () { 88 protected synchronized Object initialValue() 89 { 90 return new TransactionInfo(); } 92 }; 93 94 96 104 private static class TransactionInfo 105 { 106 int timeout = 0; Control control; Coordinator coord; Terminator term; } 111 112 114 private static void setThreadLocalTimeout(int timeout) 115 { 116 ((TransactionInfo)threadLocalData.get()).timeout = timeout; 117 } 118 119 private static int getThreadLocalTimeout() 120 { 121 return ((TransactionInfo)threadLocalData.get()).timeout; 122 } 123 124 private static void setThreadLocalControl(Control control) 125 { 126 ((TransactionInfo)threadLocalData.get()).control = control; 127 } 128 129 private static Control getThreadLocalControl() 130 { 131 return ((TransactionInfo)threadLocalData.get()).control; 132 } 133 134 private static void setThreadLocalCoordinator(Coordinator coord) 135 { 136 ((TransactionInfo)threadLocalData.get()).coord = coord; 137 } 138 139 private static Coordinator getThreadLocalCoordinator() 140 { 141 return ((TransactionInfo)threadLocalData.get()).coord; 142 } 143 144 private static void setThreadLocalTerminator(Terminator term) 145 { 146 ((TransactionInfo)threadLocalData.get()).term = term; 147 } 148 149 private static Terminator getThreadLocalTerminator() 150 throws NoTransaction 151 { 152 Terminator term = ((TransactionInfo)threadLocalData.get()).term; 153 154 if (term == null) 155 throw new NoTransaction(); 156 157 return term; 158 } 159 160 162 165 private static void setCurrentTransaction(Control control, 166 PropagationContext pc) 167 { 168 setThreadLocalControl(control); 169 setThreadLocalCoordinator(pc.current.coord); 170 setThreadLocalTerminator(pc.current.term); 171 TxClientInterceptor.setOutgoingPropagationContext(pc); 172 } 173 174 177 private static void unsetCurrentTransaction() 178 { 179 setThreadLocalControl(null); 180 setThreadLocalCoordinator(null); 181 setThreadLocalTerminator(null); 182 TxClientInterceptor.unsetOutgoingPropagationContext(); 183 } 184 185 187 191 private static TransactionFactoryExt getTxFactory() 192 { 193 if (txFactory == null) 194 { 195 try 196 { 197 ORB orb = CorbaORB.getInstance(); 198 org.omg.CORBA.Object obj = 199 orb.resolve_initial_references("NameService"); 200 NamingContextExt rootContext = NamingContextExtHelper.narrow(obj); 201 org.omg.CORBA.Object txFactoryObj = 202 rootContext.resolve_str("TransactionService"); 203 txFactory = TransactionFactoryExtHelper.narrow(txFactoryObj); 204 } 205 catch (Exception e) 206 { 207 throw new RuntimeException ("Could not get transaction factory: " 208 + e); 209 } 210 } 211 return txFactory; 212 } 213 214 218 private static int cosTransactionsToJavax( 219 org.omg.CosTransactions.Status status) 220 { 221 switch (status.value()) 222 { 223 case org.omg.CosTransactions.Status._StatusActive: 224 return javax.transaction.Status.STATUS_ACTIVE; 225 case org.omg.CosTransactions.Status._StatusCommitted: 226 return javax.transaction.Status.STATUS_COMMITTED; 227 case org.omg.CosTransactions.Status._StatusCommitting: 228 return javax.transaction.Status.STATUS_COMMITTING; 229 case org.omg.CosTransactions.Status._StatusMarkedRollback: 230 return javax.transaction.Status.STATUS_MARKED_ROLLBACK; 231 case org.omg.CosTransactions.Status._StatusNoTransaction: 232 return javax.transaction.Status.STATUS_NO_TRANSACTION; 233 case org.omg.CosTransactions.Status._StatusPrepared: 234 return javax.transaction.Status.STATUS_PREPARED; 235 case org.omg.CosTransactions.Status._StatusPreparing: 236 return javax.transaction.Status.STATUS_PREPARING; 237 case org.omg.CosTransactions.Status._StatusRolledBack: 238 return javax.transaction.Status.STATUS_ROLLEDBACK; 239 case org.omg.CosTransactions.Status._StatusRollingBack: 240 return javax.transaction.Status.STATUS_ROLLING_BACK; 241 case org.omg.CosTransactions.Status._StatusUnknown: 242 return javax.transaction.Status.STATUS_UNKNOWN; 243 default: 244 return javax.transaction.Status.STATUS_UNKNOWN; 245 } 246 } 247 248 250 253 private IIOPClientUserTransaction() 254 { 255 } 256 257 259 262 public static IIOPClientUserTransaction getSingleton() 263 { 264 if (singleton == null) 265 singleton = new IIOPClientUserTransaction(); 266 return singleton; 267 } 268 269 273 public void begin() 274 throws NotSupportedException , SystemException 275 { 276 if (getThreadLocalControl() != null) 277 throw new NotSupportedException (); 278 try 279 { 280 TransactionDesc td = 281 getTxFactory().create_transaction(getThreadLocalTimeout()); 282 setCurrentTransaction(td.control, td.propagationContext); 283 } 284 catch (RuntimeException e) 285 { 286 SystemException se = new SystemException ("Failed to create tx"); 287 se.initCause(e); 288 throw se; 289 } 290 } 291 292 public void commit() 293 throws RollbackException , 294 HeuristicMixedException , 295 HeuristicRollbackException , 296 SecurityException , 297 IllegalStateException , 298 SystemException 299 { 300 try 301 { 302 getThreadLocalTerminator().commit(true ); 303 } 304 catch (NoTransaction e) 305 { 306 IllegalStateException ex = new IllegalStateException (); 307 ex.initCause(e); 308 throw ex; 309 } 310 catch (HeuristicMixed e) 311 { 312 HeuristicMixedException ex = new HeuristicMixedException (); 313 ex.initCause(e); 314 throw ex; 315 } 316 catch (HeuristicHazard e) 317 { 318 HeuristicRollbackException ex = new HeuristicRollbackException (); 319 ex.initCause(e); 320 throw ex; 321 } 322 catch (TRANSACTION_ROLLEDBACK e) 323 { 324 RollbackException ex = new RollbackException (); 325 ex.initCause(e); 326 throw ex; 327 } 328 catch (BAD_INV_ORDER e) 329 { 330 IllegalStateException ex = new IllegalStateException (); 331 ex.initCause(e); 332 throw ex; 333 } 334 catch (NO_PERMISSION e) 335 { 336 SecurityException ex = new SecurityException (); 337 ex.initCause(e); 338 throw ex; 339 } 340 catch (RuntimeException e) 341 { 342 SystemException ex = new SystemException (); 343 ex.initCause(e); 344 throw ex; 345 } 346 finally 347 { 348 unsetCurrentTransaction(); 349 } 350 } 351 352 public void rollback() 353 throws SecurityException , 354 IllegalStateException , 355 SystemException 356 { 357 try 358 { 359 getThreadLocalTerminator().rollback(); 360 } 361 catch (NoTransaction e) 362 { 363 IllegalStateException ex = new IllegalStateException (); 364 ex.initCause(e); 365 throw ex; 366 } 367 catch (BAD_INV_ORDER e) 368 { 369 IllegalStateException ex = new IllegalStateException (); 370 ex.initCause(e); 371 throw ex; 372 } 373 catch (NO_PERMISSION e) 374 { 375 SecurityException ex = new SecurityException (); 376 ex.initCause(e); 377 throw ex; 378 } 379 catch (RuntimeException e) 380 { 381 SystemException ex = new SystemException (); 382 ex.initCause(e); 383 throw ex; 384 } 385 finally 386 { 387 unsetCurrentTransaction(); 388 } 389 } 390 391 public void setRollbackOnly() 392 throws IllegalStateException , 393 SystemException 394 { 395 Coordinator coord = getThreadLocalCoordinator(); 396 397 if (coord == null) 398 throw new IllegalStateException (); 399 400 try 401 { 402 coord.rollback_only(); 403 } 404 catch (Inactive e) 405 { 406 SystemException ex = new SystemException (); 407 ex.initCause(e); 408 throw ex; 409 } 410 catch (BAD_INV_ORDER e) 411 { 412 IllegalStateException ex = new IllegalStateException (); 413 ex.initCause(e); 414 throw ex; 415 } 416 catch (RuntimeException e) 417 { 418 SystemException ex = new SystemException (); 419 ex.initCause(e); 420 throw ex; 421 } 422 } 423 424 public int getStatus() 425 throws SystemException 426 { 427 try 428 { 429 Coordinator coord = getThreadLocalCoordinator(); 430 if (coord == null) 431 return Status.STATUS_NO_TRANSACTION; 432 else 433 return cosTransactionsToJavax(coord.get_status()); 434 } 435 catch (OBJECT_NOT_EXIST e) 436 { 437 return Status.STATUS_NO_TRANSACTION; 438 } 439 catch (RuntimeException e) 440 { 441 SystemException ex = new SystemException (); 442 ex.initCause(e); 443 throw ex; 444 } 445 } 446 447 public void setTransactionTimeout(int seconds) 448 throws SystemException 449 { 450 setThreadLocalTimeout(seconds); 451 } 452 453 457 public Reference getReference() 458 throws NamingException 459 { 460 Reference ref = new Reference ( 461 "org.jboss.tm.iiop.client.IIOPClientUserTransaction", 462 "org.jboss.tm.iiop.client.IIOPClientUserTransactionObjectFactory", 463 null); 464 return ref; 465 } 466 467 } 468 | Popular Tags |