1 22 package org.jboss.tm.remoting.client; 23 24 import java.io.Serializable ; 25 import java.rmi.AccessException ; 26 import java.rmi.NoSuchObjectException ; 27 import java.rmi.RemoteException ; 28 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.naming.Reference ; 33 import javax.naming.Referenceable ; 34 import javax.transaction.HeuristicMixedException ; 35 import javax.transaction.HeuristicRollbackException ; 36 import javax.transaction.NotSupportedException ; 37 import javax.transaction.RollbackException ; 38 import javax.transaction.SystemException ; 39 import javax.transaction.Transaction ; 40 import javax.transaction.TransactionRolledbackException ; 41 import javax.transaction.UserTransaction ; 42 43 import org.jboss.tm.TransactionPropagationContextFactory; 44 import org.jboss.tm.remoting.interfaces.HeuristicHazardException; 45 import org.jboss.tm.remoting.interfaces.Status; 46 import org.jboss.tm.remoting.interfaces.TransactionFactory; 47 import org.jboss.tm.remoting.interfaces.TransactionInactiveException; 48 import org.jboss.tm.remoting.interfaces.TxPropagationContext; 49 50 51 63 public class ClientUserTransaction 64 implements UserTransaction , 65 TransactionPropagationContextFactory, 66 Referenceable , 67 Serializable 68 { 69 71 static final long serialVersionUID = -3704980350844202097L; 72 73 74 private static ClientUserTransaction singleton = null; 75 76 77 public static final String TX_FACTORY_JNDI_NAME = "DTMTransactionFactory"; 78 79 80 private static TransactionFactory txFactory; 81 82 83 private static ThreadLocal threadLocalData = new ThreadLocal () { 84 protected synchronized Object initialValue() 85 { 86 return new TransactionInfo(); } 88 }; 89 90 92 101 private static class TransactionInfo 102 { 103 int timeout = 0; TxPropagationContext tpc; } 106 107 109 private static void setThreadLocalTimeout(int timeout) 110 { 111 ((TransactionInfo)threadLocalData.get()).timeout = timeout; 112 } 113 114 private static int getThreadLocalTimeout() 115 { 116 return ((TransactionInfo)threadLocalData.get()).timeout; 117 } 118 119 private static void setThreadLocalTPC(TxPropagationContext tpc) 120 { 121 ((TransactionInfo)threadLocalData.get()).tpc = tpc; 122 } 123 124 private static TxPropagationContext getThreadLocalTPC() 125 throws IllegalStateException 126 { 127 return ((TransactionInfo)threadLocalData.get()).tpc; 128 } 129 130 132 136 private static TransactionFactory getTxFactory() 137 { 138 if (txFactory == null) 139 { 140 try 141 { 142 Context ctx = new InitialContext (); 143 txFactory = (TransactionFactory) ctx.lookup(TX_FACTORY_JNDI_NAME); 144 } 145 catch (Exception e) 146 { 147 throw new RuntimeException ("Could not get transaction factory: ", e); 148 } 149 } 150 return txFactory; 151 } 152 153 157 private static int jbossToJavax(Status status) 158 { 159 return status.toInteger(); 160 } 161 162 164 167 private ClientUserTransaction() 168 { 169 } 170 171 173 176 public static ClientUserTransaction getSingleton() 177 { 178 if (singleton == null) 179 singleton = new ClientUserTransaction(); 180 return singleton; 181 } 182 183 187 public void begin() 188 throws NotSupportedException , SystemException 189 { 190 if (getThreadLocalTPC() != null) 191 throw new NotSupportedException (); 192 try 193 { 194 TxPropagationContext tpc = 195 getTxFactory().create(getThreadLocalTimeout()); 196 setThreadLocalTPC(tpc); 197 } 198 catch (RemoteException e) 199 { 200 SystemException e2 = new SystemException ("Unable to begin transaction"); 201 e2.initCause(e); 202 throw e2; 203 } 204 } 205 206 public void commit() 207 throws RollbackException , 208 HeuristicMixedException , 209 HeuristicRollbackException , 210 SecurityException , 211 IllegalStateException , 212 SystemException 213 { 214 try 215 { 216 TxPropagationContext tpc = getThreadLocalTPC(); 217 218 if (tpc == null) 219 throw new IllegalStateException (); 220 221 tpc.terminator.commit(true ); 222 } 223 catch (TransactionRolledbackException e) 224 { 225 RollbackException e2 = new RollbackException ("Transaction rolled back"); 226 e2.initCause(e); 227 throw e2; 228 } 229 catch (HeuristicHazardException e) 230 { 231 HeuristicRollbackException e2 = new HeuristicRollbackException ("Heuristic hazard"); 232 e2.initCause(e); 233 throw e2; 234 } 235 catch (AccessException e) 236 { 237 SecurityException e2 = new SecurityException ("Access denied"); 238 e2.initCause(e); 239 throw e2; 240 } 241 catch (RemoteException e) 242 { 243 SystemException e2 = new SystemException ("Error during commit"); 244 e2.initCause(e); 245 throw e2; 246 } 247 finally 248 { 249 setThreadLocalTPC(null); 250 } 251 } 252 253 public void rollback() 254 throws SecurityException , 255 IllegalStateException , 256 SystemException 257 { 258 try 259 { 260 TxPropagationContext tpc = getThreadLocalTPC(); 261 262 if (tpc == null) 263 throw new IllegalStateException (); 264 265 tpc.terminator.rollback(); 266 } 267 catch (AccessException e) 268 { 269 SecurityException e2 = new SecurityException ("Access denied"); 270 e2.initCause(e); 271 throw e2; 272 } 273 catch (RemoteException e) 274 { 275 SystemException e2 = new SystemException ("Error during rollback"); 276 e2.initCause(e); 277 throw e2; 278 } 279 finally 280 { 281 setThreadLocalTPC(null); 282 } 283 } 284 285 public void setRollbackOnly() 286 throws IllegalStateException , 287 SystemException 288 { 289 try 290 { 291 TxPropagationContext tpc = getThreadLocalTPC(); 292 293 if (tpc == null) 294 throw new IllegalStateException (); 295 296 tpc.coordinator.rollbackOnly(); 297 } 298 catch (TransactionInactiveException e) 299 { 300 IllegalStateException e2 = new IllegalStateException ("Transaction is not active"); 301 e2.initCause(e); 302 throw e2; 303 } 304 catch (RemoteException e) 305 { 306 SystemException e2 = new SystemException ("Error during rollback"); 307 e2.initCause(e); 308 throw e2; 309 } 310 } 311 312 public int getStatus() 313 throws SystemException 314 { 315 try 316 { 317 TxPropagationContext tpc = getThreadLocalTPC(); 318 319 if (tpc == null) 320 return javax.transaction.Status.STATUS_NO_TRANSACTION; 321 else 322 return jbossToJavax(tpc.coordinator.getStatus()); 323 } 324 catch (NoSuchObjectException e) 325 { 326 return javax.transaction.Status.STATUS_NO_TRANSACTION; 327 } 328 catch (RemoteException e) 329 { 330 SystemException e2 = new SystemException ("Error getting status"); 331 e2.initCause(e); 332 throw e2; 333 } 334 } 335 336 public void setTransactionTimeout(int seconds) 337 throws SystemException 338 { 339 setThreadLocalTimeout(seconds); 340 } 341 342 346 public Object getTransactionPropagationContext() 347 { 348 return getThreadLocalTPC(); 349 } 350 351 public Object getTransactionPropagationContext(Transaction tx) 352 { 353 throw new InternalError ("Should not have been used."); 355 } 356 357 361 public Reference getReference() 362 throws NamingException 363 { 364 Reference ref = new Reference ( 365 "org.jboss.tm.remoting.client.ClientUserTransaction", 366 "org.jboss.tm.remoting.client.ClientUserTransactionObjectFactory", 367 null); 368 return ref; 369 } 370 371 } 372 | Popular Tags |