001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: BMTTransactionInterceptor.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.transaction.interceptors;
025:
026: import javax.ejb.EJBException;
027: import javax.transaction.InvalidTransactionException;
028: import javax.transaction.SystemException;
029: import javax.transaction.Transaction;
030:
031: import org.ow2.easybeans.api.EasyBeansInvocationContext;
032: import org.ow2.util.log.Log;
033: import org.ow2.util.log.LogFactory;
034:
035: /**
036: * Defines an interceptor for method that are in Bean managed mode and then in Bean Managed Transaction.
037: * @author Florent Benoit
038: */
039: public class BMTTransactionInterceptor extends
040: AbsTransactionInterceptor {
041:
042: /**
043: * Logger.
044: */
045: private Log logger = LogFactory
046: .getLog(BMTTransactionInterceptor.class);
047:
048: /**
049: * Constructor.<br>
050: * Acquire the transaction manager.
051: */
052: public BMTTransactionInterceptor() {
053: super ();
054: }
055:
056: /**
057: * Execute transaction as specified for BMT.
058: * @param invocationContext context with useful attributes on the current
059: * invocation
060: * @return result of the next invocation (to chain interceptors)
061: * @throws Exception if interceptor fails
062: * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
063: * specification ?12.6.1</a>
064: */
065: @Override
066: public Object intercept(
067: final EasyBeansInvocationContext invocationContext)
068: throws Exception {
069: logger.debug("Calling BMT TX interceptor");
070:
071: // Get current transaction
072: Transaction transaction;
073: try {
074: transaction = getTransactionManager().getTransaction();
075: } catch (SystemException se) {
076: throw new EJBException(
077: "Cannot get the current transaction on transaction manager.",
078: se);
079: }
080:
081: logger.debug("Transaction found = {0}", transaction);
082:
083: /*
084: * When a client invokes a business method via one of the enterprise
085: * bean?s client view interfaces, the container suspends any transaction
086: * that may be associated with the client request. If there is a
087: * transaction associated with the instance (this would happen if a
088: * stateful session bean instance started the transaction in some
089: * previous business method), the container associates the method
090: * execution with this transaction. If there are interceptor methods
091: * associated with the bean instances, these actions are taken before
092: * the interceptor methods are invoked.
093: */
094:
095: Transaction suspendedTransaction = null;
096: if (transaction != null) {
097: try {
098: logger.debug("Suspending transaction {0}", transaction);
099: suspendedTransaction = getTransactionManager()
100: .suspend();
101: } catch (SystemException se) {
102: throw new EJBException(
103: "Cannot call suspend() on the transaction manager.",
104: se);
105: }
106: }
107:
108: try {
109: return invocationContext.proceed();
110: } finally {
111:
112: /*
113: * The container resumes the suspended association when the business
114: * method has completed.
115: */
116: if (suspendedTransaction != null) {
117:
118: logger.debug("Resuming transaction {0}", transaction);
119:
120: try {
121: getTransactionManager()
122: .resume(suspendedTransaction);
123: } catch (InvalidTransactionException ite) {
124: throw new EJBException(
125: "Cannot call resume() on the given transaction. There is an invalid transaction",
126: ite);
127: } catch (IllegalStateException ise) {
128: throw new EJBException(
129: "Cannot call resume() on the given transaction. There is another associated transaction",
130: ise);
131: } catch (SystemException se) {
132: throw new EJBException(
133: "Cannot call resume() on the given transaction. Unexpected error condition",
134: se);
135: }
136: }
137: }
138: }
139:
140: }
|