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: CMTMandatoryTransactionInterceptor.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.ejb.EJBTransactionRequiredException;
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 MANDATORY attribute.
037: * @author Florent Benoit
038: */
039: public class CMTMandatoryTransactionInterceptor extends
040: AbsTransactionInterceptor {
041:
042: /**
043: * Logger.
044: */
045: private Log logger = LogFactory
046: .getLog(CMTMandatoryTransactionInterceptor.class);
047:
048: /**
049: * Constructor.<br>
050: * Acquire the transaction manager.
051: */
052: public CMTMandatoryTransactionInterceptor() {
053: super ();
054: }
055:
056: /**
057: * Execute transaction as specified with the MANDATORY 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.5</a>
064: */
065: @Override
066: public Object intercept(
067: final EasyBeansInvocationContext invocationContext)
068: throws Exception {
069: logger.debug("Calling Mandatory 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 the client calls without a transaction context, the container
085: * throws the javax.ejb.EJBTransactionRequiredException[ 54]. If the EJB
086: * 2.1 client view is used, the container throws the
087: * javax.transaction.TransactionRequiredException exception if the
088: * client is a remote client, or the
089: * javax.ejb.TransactionRequiredLocalException if the client is a local
090: * client.
091: */
092: if (transaction == null) {
093: logger
094: .warn("Mandatory as transaction attribute and not in transaction");
095: throw new EJBTransactionRequiredException(
096: "Client should call with a transaction context in MANDATORY mode.");
097: }
098:
099: // Transaction not started by this interceptor, nothing to do after the
100: // proceed.
101: try {
102: return invocationContext.proceed();
103: } catch (Exception e) {
104: // Chapter 14.3.1
105: // Runs in the context of the client
106: handleContextClientTransaction(invocationContext, e);
107:
108: // Shouldn't come here
109: return null;
110: }
111: }
112:
113: }
|