1 22 package org.jboss.tm.iiop.client; 23 24 import java.util.Collections ; 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import org.omg.CORBA.LocalObject ; 28 import org.omg.CosNaming.NamingContextExt ; 29 import org.omg.CosNaming.NamingContextPackage.CannotProceed ; 30 import org.omg.CosNaming.NamingContextPackage.InvalidName ; 31 import org.omg.CosNaming.NamingContextPackage.NotFound ; 32 import org.omg.CosTransactions.Control; 33 import org.omg.CosTransactions.Coordinator; 34 import org.omg.CosTransactions.Current; 35 import org.omg.CosTransactions.HeuristicHazard; 36 import org.omg.CosTransactions.HeuristicMixed; 37 import org.omg.CosTransactions.Inactive; 38 import org.omg.CosTransactions.InvalidControl; 39 import org.omg.CosTransactions.NoTransaction; 40 import org.omg.CosTransactions.PropagationContext; 41 import org.omg.CosTransactions.Status; 42 import org.omg.CosTransactions.SubtransactionsUnavailable; 43 import org.omg.CosTransactions.Terminator; 44 import org.omg.CosTransactions.Unavailable; 45 46 import org.jboss.tm.iiop.TransactionDesc; 47 import org.jboss.tm.iiop.TransactionFactoryExt; 48 import org.jboss.tm.iiop.TransactionFactoryExtHelper; 49 import org.jboss.tm.iiop.TxClientInterceptor; 50 51 56 57 public class TransactionCurrent 58 extends LocalObject 59 implements Current 60 { 61 63 private static TransactionCurrent instance = null; private static TransactionFactoryExt txFactory; 65 private static Set suspendedTransactions = 66 Collections.synchronizedSet (new HashSet ()); 67 private static ThreadLocal threadLocalData = new ThreadLocal () { 68 protected synchronized Object initialValue() 69 { 70 return new TransactionInfo(); } 72 }; 73 74 76 84 private static class TransactionInfo 85 { 86 int timeout = 0; Control control; Coordinator coord; Terminator term; } 91 92 94 private static void setThreadLocalTimeout(int timeout) 95 { 96 ((TransactionInfo)threadLocalData.get()).timeout = timeout; 97 } 98 99 private static int getThreadLocalTimeout() 100 { 101 return ((TransactionInfo)threadLocalData.get()).timeout; 102 } 103 104 private static void setThreadLocalControl(Control control) 105 { 106 ((TransactionInfo)threadLocalData.get()).control = control; 107 } 108 109 private static Control getThreadLocalControl() 110 { 111 return ((TransactionInfo)threadLocalData.get()).control; 112 } 113 114 private static void setThreadLocalCoordinator(Coordinator coord) 115 { 116 ((TransactionInfo)threadLocalData.get()).coord = coord; 117 } 118 119 private static Coordinator getThreadLocalCoordinator() 120 { 121 return ((TransactionInfo)threadLocalData.get()).coord; 122 } 123 124 private static void setThreadLocalTerminator(Terminator term) 125 { 126 ((TransactionInfo)threadLocalData.get()).term = term; 127 } 128 129 private static Terminator getThreadLocalTerminator() 130 throws NoTransaction 131 { 132 Terminator term = ((TransactionInfo)threadLocalData.get()).term; 133 134 if (term == null) 135 throw new NoTransaction(); 136 137 return term; 138 } 139 140 142 145 private static void setCurrentTransaction(Control control, 146 PropagationContext pc) 147 { 148 setThreadLocalControl(control); 149 setThreadLocalCoordinator(pc.current.coord); 150 setThreadLocalTerminator(pc.current.term); 151 TxClientInterceptor.setOutgoingPropagationContext(pc); 152 } 153 154 157 private static void unsetCurrentTransaction() 158 { 159 setThreadLocalControl(null); 160 setThreadLocalCoordinator(null); 161 setThreadLocalTerminator(null); 162 TxClientInterceptor.unsetOutgoingPropagationContext(); 163 } 164 165 167 public static void init(NamingContextExt nc) 168 { 169 try 170 { 171 org.omg.CORBA.Object txFactoryObj = 172 nc.resolve_str("TransactionService"); 173 txFactory = TransactionFactoryExtHelper.narrow(txFactoryObj); 174 } 175 catch (CannotProceed e) 176 { 177 throw new RuntimeException ( 178 "Exception initializing TransactionCurrent: " + e); 179 } 180 catch (NotFound e) 181 { 182 throw new RuntimeException ( 183 "Exception initializing TransactionCurrent: " + e); 184 } 185 catch (InvalidName e) 186 { 187 throw new RuntimeException ( 188 "Exception initializing TransactionCurrent: " + e); 189 } 190 } 191 192 194 public static synchronized TransactionCurrent getInstance() 195 { 196 if (instance == null) 197 instance = new TransactionCurrent(); 198 return instance; 199 } 200 201 203 209 public void begin() 210 throws SubtransactionsUnavailable 211 { 212 if (getThreadLocalControl() != null) 213 throw new SubtransactionsUnavailable(); 214 TransactionDesc td = txFactory.create_transaction( 215 getThreadLocalTimeout()); 216 setCurrentTransaction(td.control, td.propagationContext); 217 } 218 219 224 public void commit(boolean reportHeuristics) 225 throws NoTransaction, 226 HeuristicHazard, 227 HeuristicMixed 228 { 229 getThreadLocalTerminator().commit(reportHeuristics); 230 unsetCurrentTransaction(); 231 } 232 233 238 public void rollback() 239 throws NoTransaction 240 { 241 getThreadLocalTerminator().rollback(); 242 unsetCurrentTransaction(); 243 } 244 245 250 public void rollback_only() 251 throws NoTransaction 252 { 253 try 254 { 255 Coordinator coord = getThreadLocalCoordinator(); 256 257 if (coord == null) 258 throw new NoTransaction(); 259 260 coord.rollback_only(); 261 } 262 catch (Inactive e) 263 { 264 throw new RuntimeException ("Current transaction already prepared: " 265 + e); 266 } 267 } 268 269 274 public Status get_status() 275 { 276 Coordinator coord = getThreadLocalCoordinator(); 277 return (coord == null) ? Status.StatusNoTransaction : coord.get_status(); 278 } 279 280 285 public String get_transaction_name() 286 { 287 Coordinator coord = getThreadLocalCoordinator(); 288 return (coord == null) ? "" : coord.get_transaction_name(); 289 } 290 291 297 public void set_timeout(int timeOut) 298 { 299 setThreadLocalTimeout(timeOut); 300 } 301 302 307 public Control get_control() 308 { 309 return getThreadLocalControl(); 310 } 311 312 317 public Control suspend() 318 { 319 Control control = getThreadLocalControl(); 320 if (control != null) 321 { 322 unsetCurrentTransaction(); 323 suspendedTransactions.add(control); 324 } 325 return control; 326 } 327 328 333 public void resume(Control whichTransaction) 334 throws InvalidControl 335 { 336 try 337 { 338 if (whichTransaction == null) 339 throw new InvalidControl(); 340 if (!suspendedTransactions.remove(whichTransaction)) 341 throw new InvalidControl(); 342 Coordinator coord = whichTransaction.get_coordinator(); 343 if (coord == null) 344 throw new InvalidControl(); 345 PropagationContext pc = coord.get_txcontext(); setCurrentTransaction(whichTransaction, pc); 347 } 348 catch (Unavailable e) 349 { 350 throw new InvalidControl(); 351 } 352 } 353 354 } 355 | Popular Tags |