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: CMTNotSupportedTransactionInterceptor.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 using the NOT_SUPPORTED attribute.
037: * @author Florent Benoit
038: */
039: public class CMTNotSupportedTransactionInterceptor extends
040: AbsTransactionInterceptor {
041:
042: /**
043: * Logger.
044: */
045: private Log logger = LogFactory
046: .getLog(CMTNotSupportedTransactionInterceptor.class);
047:
048: /**
049: * Constructor.<br>
050: * Acquire the transaction manager.
051: */
052: public CMTNotSupportedTransactionInterceptor() {
053: super ();
054: }
055:
056: /**
057: * Execute transaction as specified with the NOT_SUPPORTED attribute.
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.2.1</a>
064: */
065: @Override
066: public Object intercept(
067: final EasyBeansInvocationContext invocationContext)
068: throws Exception {
069: logger.debug("Calling Not Supported 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: * If a client calls with a transaction context, the container suspends
085: * the association of the transaction context with the current thread
086: * before invoking the enterprise bean?s business method.
087: */
088:
089: Transaction suspendedTransaction = null;
090: if (transaction != null) {
091: try {
092: logger.debug("Suspending transaction {0}", transaction);
093: suspendedTransaction = getTransactionManager()
094: .suspend();
095: } catch (SystemException se) {
096: throw new EJBException(
097: "Cannot call suspend() on the transaction manager.",
098: se);
099: }
100: }
101:
102: /*
103: * The suspended transaction context of the client is not passed to the
104: * resource managers or other enterprise bean objects that are invoked
105: * from the business method.
106: */
107: try {
108: return invocationContext.proceed();
109: } catch (Exception e) {
110: handleUnspecifiedTransactionContext(invocationContext, e);
111: // Shouldn't come here
112: return null;
113: } finally {
114:
115: /*
116: * The container resumes the suspended association when the business
117: * method has completed.
118: */
119: if (suspendedTransaction != null) {
120:
121: logger.debug("Resuming transaction {0}", transaction);
122:
123: try {
124: getTransactionManager()
125: .resume(suspendedTransaction);
126: } catch (InvalidTransactionException ite) {
127: throw new EJBException(
128: "Cannot call resume() on the given transaction. There is an invalid transaction",
129: ite);
130: } catch (IllegalStateException ise) {
131: throw new EJBException(
132: "Cannot call resume() on the given transaction. There is another associated transaction",
133: ise);
134: } catch (SystemException se) {
135: throw new EJBException(
136: "Cannot call resume() on the given transaction. Unexpected error condition",
137: se);
138: }
139: }
140: }
141: }
142:
143: }
|