1 23 package com.sun.enterprise.distributedtx; 24 25 import java.rmi.RemoteException ; 26 import java.io.Serializable ; 27 import javax.transaction.*; 28 29 import com.sun.ejb.*; 30 import com.sun.enterprise.*; 31 import com.sun.enterprise.log.Log; 32 import com.sun.enterprise.util.i18n.StringManager; 33 34 import java.util.logging.*; 35 import com.sun.logging.*; 36 37 49 50 public class UserTransactionImpl implements UserTransaction, Serializable 51 { 52 53 static Logger _logger=LogDomains.getLogger(LogDomains.JTA_LOGGER); 54 private static StringManager sm = StringManager.getManager(UserTransactionImpl.class); 56 private static final boolean debug = false; 57 private transient J2EETransactionManager transactionManager; 58 private transient InvocationManager invocationManager; 59 private transient boolean initialized; 60 61 private transient UserTransaction userTx; 63 64 66 private boolean checkEjbAccess; 70 71 74 public UserTransactionImpl() 75 { 76 this(true); 77 } 78 79 83 public UserTransactionImpl(boolean doEjbAccessChecks) 84 { 85 init(); 86 checkEjbAccess = doEjbAccessChecks; 87 } 88 89 90 93 private void init() 94 { 95 initialized = true; 96 Switch theSwitch = Switch.getSwitch(); 97 transactionManager = theSwitch.getTransactionManager(); 98 invocationManager = theSwitch.getInvocationManager(); 99 if (transactionManager == null) { 100 userTx = new com.sun.jts.jta.UserTransactionImpl(); 102 } 103 } 104 105 private void checkUserTransactionMethodAccess(ComponentInvocation inv) 106 throws IllegalStateException , SystemException 107 { 108 if ( (inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION) 109 && checkEjbAccess ) { 110 Container ejbContainer = (Container) inv.container; 111 if( !ejbContainer.userTransactionMethodsAllowed(inv) ) { 112 throw new IllegalStateException (sm.getString("enterprise_distributedtx.operation_not_allowed")); 113 } 114 } else if (inv.getInvocationType() == 115 ComponentInvocation.APP_CLIENT_INVOCATION) { 116 throw new SystemException(sm.getString("enterprise_distributedtx.usertransaction_not_supported")); 117 } 118 } 119 120 public void begin() throws NotSupportedException, SystemException 121 { 122 if (!initialized) init(); 123 124 if (userTx != null) { 125 userTx.begin(); 126 return; 127 } 128 129 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 130 if (inv != null) { 131 checkUserTransactionMethodAccess(inv); 132 } else { 133 throw new InvocationException(); 134 } 135 136 transactionManager.begin(); 137 143 144 try { 145 if ( inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION ) 146 ((Container)inv.container).doAfterBegin(inv); 147 148 inv.setTransaction(transactionManager.getTransaction()); 150 transactionManager.enlistComponentResources(); 151 } catch ( RemoteException ex ) { 152 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_begin", ex); 153 SystemException sysEx = new SystemException(ex.getMessage()); 154 sysEx.initCause(ex); 155 throw sysEx; 156 } 157 } 158 159 public void commit() throws RollbackException, 160 HeuristicMixedException, HeuristicRollbackException, SecurityException , 161 IllegalStateException , SystemException 162 { 163 if (!initialized) init(); 164 165 if (userTx != null) { 166 userTx.commit(); 167 return; 168 } 169 170 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 171 if (inv != null) { 172 checkUserTransactionMethodAccess(inv); 173 } else { 174 throw new InvocationException(); 175 } 176 177 178 try { 179 transactionManager.delistComponentResources(false); transactionManager.commit(); 181 } catch ( RemoteException ex ) { 182 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_commit", ex); 183 throw new SystemException(); 184 } finally { 185 inv.setTransaction(null); 186 } 187 } 188 189 public void rollback() throws IllegalStateException , SecurityException , 190 SystemException 191 { 192 if (!initialized) init(); 193 194 if (userTx != null) { 195 userTx.rollback(); 196 return; 197 } 198 199 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 200 if (inv != null) { 201 checkUserTransactionMethodAccess(inv); 202 } else { 203 throw new InvocationException(); 204 } 205 206 207 try { 208 transactionManager.delistComponentResources(false); transactionManager.rollback(); 210 } catch ( RemoteException ex ) { 211 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_rollback", ex); 212 throw new SystemException(); 213 } finally { 214 inv.setTransaction(null); 215 } 216 } 217 218 public void setRollbackOnly() throws IllegalStateException , SystemException 219 { 220 if (!initialized) init(); 221 222 if (userTx != null) { 223 userTx.setRollbackOnly(); 224 return; 225 } 226 227 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 228 if (inv != null) { 229 checkUserTransactionMethodAccess(inv); 230 } else { 231 throw new InvocationException(); 232 } 233 234 transactionManager.setRollbackOnly(); 235 } 236 237 public int getStatus() throws SystemException 238 { 239 if (!initialized) init(); 240 241 if (userTx != null) { 242 return userTx.getStatus(); 243 } 244 245 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 246 if (inv != null) { 247 checkUserTransactionMethodAccess(inv); 248 } else { 249 throw new InvocationException(); 250 } 251 252 return transactionManager.getStatus(); 253 } 254 255 public void setTransactionTimeout(int seconds) throws SystemException 256 { 257 if (!initialized) init(); 258 259 if (userTx != null) { 260 userTx.setTransactionTimeout(seconds); 261 return; 262 } 263 264 ComponentInvocation inv = invocationManager.getCurrentInvocation(); 265 if (inv != null) { 266 checkUserTransactionMethodAccess(inv); 267 } else { 268 throw new InvocationException(); 269 } 270 271 272 if (seconds < 0) seconds = 0; 273 transactionManager.setTransactionTimeout(seconds); 275 } 276 } 277 | Popular Tags |