Clover coverage report -
Coverage timestamp: Thu Jun 22 2006 14:24:50 CEST
file stats: LOC: 156   Methods: 2
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CMTRequiredTransactionInterceptor.java 0% 0% 0% 0%
coverage
 1    /**
 2    * EasyBeans
 3    * Copyright (C) 2006 Bull S.A.S.
 4    * Contact: easybeans@objectweb.org
 5    *
 6    * This library is free software; you can redistribute it and/or
 7    * modify it under the terms of the GNU Lesser General Public
 8    * License as published by the Free Software Foundation; either
 9    * version 2.1 of the License, or any later version.
 10    *
 11    * This library is distributed in the hope that it will be useful,
 12    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14    * Lesser General Public License for more details.
 15    *
 16    * You should have received a copy of the GNU Lesser General Public
 17    * License along with this library; if not, write to the Free Software
 18    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 19    * USA
 20    *
 21    * --------------------------------------------------------------------------
 22    * $Id: CMTRequiredTransactionInterceptor.java 9 2006-02-19 18:53:32Z benoitf $
 23    * --------------------------------------------------------------------------
 24    */
 25   
 26    package org.objectweb.easybeans.transaction.interceptors;
 27   
 28    import static javax.transaction.Status.STATUS_ACTIVE;
 29    import static javax.transaction.Status.STATUS_MARKED_ROLLBACK;
 30   
 31    import javax.ejb.EJBException;
 32    import javax.ejb.TransactionRolledbackLocalException;
 33    import javax.transaction.NotSupportedException;
 34    import javax.transaction.RollbackException;
 35    import javax.transaction.SystemException;
 36    import javax.transaction.Transaction;
 37   
 38    import org.objectweb.easybeans.api.EasyBeansInvocationContext;
 39    import org.objectweb.easybeans.log.JLog;
 40    import org.objectweb.easybeans.log.JLogFactory;
 41   
 42    /**
 43    * Defines an interceptor for method using the REQUIRED attribute.
 44    * @author Florent Benoit
 45    */
 46    public class CMTRequiredTransactionInterceptor extends AbsTransactionInterceptor {
 47   
 48    /**
 49    * Logger.
 50    */
 51    private JLog logger = JLogFactory.getLog(CMTRequiredTransactionInterceptor.class);
 52   
 53   
 54    /**
 55    * Constructor.<br>
 56    * Acquire the transaction manager.
 57    */
 58  0 public CMTRequiredTransactionInterceptor() {
 59  0 super();
 60    }
 61   
 62    /**
 63    * Execute transaction as specified with the REQUIRED attribute.
 64    * @param invocationContext context with useful attributes on the current
 65    * invocation
 66    * @return result of the next invocation (to chain interceptors)
 67    * @throws Exception if interceptor fails
 68    * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
 69    * specification ?12.6.2.2</a>
 70    */
 71  0 @Override
 72    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
 73  0 logger.debug("Calling Required TX interceptor");
 74   
 75    // Get current transaction
 76  0 Transaction transaction;
 77  0 try {
 78  0 transaction = getTransactionManager().getTransaction();
 79    } catch (SystemException se) {
 80  0 throw new EJBException("Cannot get the current transaction on transaction manager.", se);
 81    }
 82   
 83  0 logger.debug("Transaction found = {0}", transaction);
 84   
 85   
 86    /*
 87    * If the client invokes the enterprise bean's method while the client
 88    * is not associated with a transaction context, the container
 89    * automatically starts a new transaction before delegating a method
 90    * call to the enterprise bean business method.
 91    */
 92  0 boolean startedTransaction = false;
 93  0 if (transaction == null) {
 94  0 try {
 95  0 getTransactionManager().begin();
 96  0 startedTransaction = true;
 97    } catch (NotSupportedException nse) {
 98  0 throw new EJBException("Transaction Manager implementation does not support nested transactions.", nse);
 99    } catch (SystemException se) {
 100  0 throw new EJBException("Cannot call begin() on the transaction manager.", se);
 101    }
 102    }
 103    // else
 104    /*
 105    * If a client invokes the enterprise bean's method while the client is
 106    * associated with a transaction context, the container invokes the
 107    * enterprise bean's method in the client's transaction context.
 108    */
 109   
 110  0 try {
 111  0 return invocationContext.proceed();
 112    } finally {
 113   
 114    // only do some operations if transaction has been started before
 115    // invoking the method.
 116  0 if (startedTransaction) {
 117   
 118    // sanity check.
 119  0 Transaction transactionAfter = null;
 120  0 try {
 121  0 transactionAfter = getTransactionManager().getTransaction();
 122    } catch (SystemException se) {
 123  0 throw new EJBException("Cannot get the current transaction on transaction manager.", se);
 124    }
 125   
 126  0 if (transactionAfter == null) {
 127  0 throw new RuntimeException("Transaction disappeared.");
 128    }
 129   
 130    /*
 131    * The container attempts to commit the transaction when the
 132    * business method has completed. The container performs the
 133    * commit protocol before the method result is sent to the
 134    * client.
 135    */
 136  0 try {
 137  0 switch (getTransactionManager().getStatus()) {
 138  0 case STATUS_ACTIVE:
 139  0 getTransactionManager().commit();
 140  0 break;
 141  0 case STATUS_MARKED_ROLLBACK:
 142  0 getTransactionManager().rollback();
 143  0 break;
 144  0 default:
 145  0 throw new RuntimeException("Unexpected transaction status" + getTransactionManager().getStatus());
 146    }
 147    } catch (RollbackException e) {
 148  0 throw new TransactionRolledbackLocalException("Could not commit transaction", e);
 149    } catch (Exception e) {
 150  0 throw new EJBException("Container exception", e);
 151    }
 152    }
 153    }
 154    }
 155   
 156    }