1 25 26 package org.objectweb.easybeans.transaction.interceptors; 27 28 import static javax.transaction.Status.STATUS_MARKED_ROLLBACK ; 29 30 import java.lang.reflect.Method ; 31 import java.util.Map ; 32 33 import javax.ejb.ApplicationException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EJBTransactionRolledbackException ; 36 import javax.transaction.HeuristicMixedException ; 37 import javax.transaction.HeuristicRollbackException ; 38 import javax.transaction.RollbackException ; 39 import javax.transaction.SystemException ; 40 import javax.transaction.Transaction ; 41 import javax.transaction.TransactionManager ; 42 43 import org.objectweb.easybeans.api.EasyBeansInterceptor; 44 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 45 import org.objectweb.easybeans.api.Factory; 46 import org.objectweb.easybeans.api.bean.EasyBeansSFSB; 47 import org.objectweb.easybeans.api.pool.Pool; 48 import org.objectweb.easybeans.api.pool.PoolException; 49 import org.objectweb.easybeans.log.JLog; 50 import org.objectweb.easybeans.log.JLogFactory; 51 import org.objectweb.easybeans.transaction.JTransactionManager; 52 53 58 public abstract class AbsTransactionInterceptor implements EasyBeansInterceptor { 59 60 63 private JLog logger = JLogFactory.getLog(AbsTransactionInterceptor.class); 64 65 68 private TransactionManager transactionManager = null; 69 70 74 public AbsTransactionInterceptor() { 75 this.transactionManager = JTransactionManager.getTransactionManager(); 76 } 77 78 85 public abstract Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception ; 86 87 91 public TransactionManager getTransactionManager() { 92 return transactionManager; 93 } 94 95 104 protected ApplicationException getApplicationException(final EasyBeansInvocationContext invocationContext, 105 final Exception e) { 106 Map <String , ApplicationException > applicationExceptions = invocationContext.getFactory().getBeanInfo() 107 .getApplicationExceptions(); 108 ApplicationException appException = applicationExceptions.get(e.getClass().getName()); 109 if (appException != null) { 110 return appException; 111 } 112 if (e instanceof RuntimeException ) { 114 return null; 115 } 116 Method method = invocationContext.getMethod(); 118 if (method != null) { 119 Class [] exceptions = method.getExceptionTypes(); 120 if (exceptions != null) { 121 for (Class clazz : exceptions) { 122 if (clazz.isInstance(e) && !(e instanceof RuntimeException )) { 124 return applicationExceptions.get("DEFAULT"); 127 } 128 } 129 } 130 } 131 return null; 133 134 } 135 136 142 @SuppressWarnings ("unchecked") 143 protected void discard(final EasyBeansInvocationContext invocationContext) throws PoolException { 144 145 Object target = invocationContext.getTarget(); 146 147 if (target instanceof EasyBeansSFSB) { 149 Factory factory = invocationContext.getFactory(); 151 152 Pool<EasyBeansSFSB, Long > pool = factory.getPool(); 154 155 EasyBeansSFSB bean = (EasyBeansSFSB) invocationContext.getTarget(); 157 158 pool.discard(bean); 160 } else { 161 logger.debug("Instance not discarded as it is not a stateful bean"); 162 } 163 164 } 165 166 169 protected void markTransactionRollback() { 170 Transaction transaction; 172 try { 173 transaction = getTransactionManager().getTransaction(); 174 } catch (SystemException se) { 175 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 176 } 177 if (transaction != null) { 178 try { 179 transactionManager.setRollbackOnly(); 180 } catch (IllegalStateException e) { 181 logger.warn("Cannot mark transaction as rollbackOnly", e); 182 } catch (SystemException e) { 183 logger.warn("Cannot mark transaction as rollbackOnly", e); 184 } 185 } 186 } 187 188 192 protected boolean isMarkedRollbackOnly() { 193 try { 194 return (STATUS_MARKED_ROLLBACK == transactionManager.getStatus()); 195 } catch (SystemException e) { 196 logger.warn("Cannot get transaction status", e); 197 return false; 198 } 199 } 200 201 204 protected void rollback() { 205 try { 206 transactionManager.rollback(); 207 } catch (IllegalStateException e) { 208 logger.warn("Cannot rollback the transaction", e); 209 } catch (SecurityException e) { 210 logger.warn("Cannot rollback the transaction", e); 211 } catch (SystemException e) { 212 logger.warn("Cannot rollback the transaction", e); 213 } 214 } 215 216 219 protected void commit() { 220 try { 221 transactionManager.commit(); 222 } catch (IllegalStateException e) { 223 logger.warn("Cannot commit the transaction", e); 224 } catch (SecurityException e) { 225 logger.warn("Cannot commit the transaction", e); 226 } catch (HeuristicMixedException e) { 227 logger.warn("Cannot commit the transaction", e); 228 } catch (HeuristicRollbackException e) { 229 logger.warn("Cannot commit the transaction", e); 230 } catch (RollbackException e) { 231 logger.warn("Cannot commit the transaction", e); 232 } catch (SystemException e) { 233 logger.warn("Cannot commit the transaction", e); 234 } 235 } 236 237 244 protected void handleBeanManagedException(final EasyBeansInvocationContext invocationContext, final Exception e) 245 throws Exception { 246 ApplicationException applicationException = getApplicationException(invocationContext, e); 247 248 if (applicationException != null) { 250 throw e; 252 } 253 254 256 logger.error("Bean Managed Transaction : Exception (not application exception) in business method", e); 258 259 markTransactionRollback(); 262 263 try { 265 discard(invocationContext); 266 } catch (PoolException pe) { 267 throw new EJBException ("Cannot discard the bean", pe); 268 } 269 270 throw new EJBException ("Bean Managed Transaction : Business exception which is not an application exception", e); 272 } 273 274 281 protected void handleUnspecifiedTransactionContext(final EasyBeansInvocationContext invocationContext, 282 final Exception e) throws Exception { 283 284 ApplicationException applicationException = getApplicationException(invocationContext, e); 285 286 if (applicationException != null) { 288 throw e; 290 } 291 292 294 logger.error("Exception (not application exception) in business method", e); 296 297 try { 299 discard(invocationContext); 300 } catch (PoolException pe) { 301 throw new EJBException ("Cannot discard the bean", pe); 302 } 303 304 throw new EJBException ("Business exception which is not an application exception", e); 306 307 } 308 309 316 protected void handleContextClientTransaction(final EasyBeansInvocationContext invocationContext, final Exception e) 317 throws Exception { 318 ApplicationException applicationException = getApplicationException(invocationContext, e); 319 320 if (applicationException != null) { 322 326 327 if (applicationException.rollback()) { 329 markTransactionRollback(); 330 } 331 332 throw e; 334 } 335 336 338 logger.error("Exception (not application exception) in business method", e); 340 341 markTransactionRollback(); 343 344 try { 346 discard(invocationContext); 347 } catch (PoolException pe) { 348 throw new EJBException ("Cannot discard the bean", pe); 349 } 350 351 EJBTransactionRolledbackException transactionException = new EJBTransactionRolledbackException ( 354 "System exception, The transaction has been marked for rollback only"); 355 transactionException.initCause(e); 356 throw transactionException; 357 } 358 359 368 protected void handleContextContainerTransaction(final EasyBeansInvocationContext invocationContext, 369 final Exception e) throws Exception { 370 371 ApplicationException applicationException = getApplicationException(invocationContext, e); 372 373 if (applicationException != null) { 375 379 if (isMarkedRollbackOnly()) { 380 rollback(); 381 throw e; 382 } 383 384 390 if (applicationException.rollback()) { 391 rollback(); 393 } else { 394 commit(); 395 } 396 throw e; 397 } 398 logger.error("Exception (not application exception) in business method", e); 401 402 rollback(); 404 405 try { 407 discard(invocationContext); 408 } catch (PoolException pe) { 409 throw new EJBException ("Cannot discard the bean", pe); 410 } 411 412 throw new EJBException ("Exception in a business interface with REQUIRED TX attribute", e); 414 415 } 416 } 417 | Popular Tags |