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: CMTNeverTransactionInterceptor.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.SystemException;
028: import javax.transaction.Transaction;
029:
030: import org.ow2.easybeans.api.EasyBeansInvocationContext;
031: import org.ow2.util.log.Log;
032: import org.ow2.util.log.LogFactory;
033:
034: /**
035: * Defines an interceptor for method using the NEVER attribute.
036: * @author Florent Benoit
037: */
038: public class CMTNeverTransactionInterceptor extends
039: AbsTransactionInterceptor {
040:
041: /**
042: * Logger.
043: */
044: private Log logger = LogFactory
045: .getLog(CMTNeverTransactionInterceptor.class);
046:
047: /**
048: * Constructor.<br>
049: * Acquire the transaction manager.
050: */
051: public CMTNeverTransactionInterceptor() {
052: super ();
053: }
054:
055: /**
056: * Execute transaction as specified with the NEVER attribute.
057: * @param invocationContext context with useful attributes on the current
058: * invocation
059: * @return result of the next invocation (to chain interceptors)
060: * @throws Exception if interceptor fails
061: * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
062: * specification ?12.6.2.6</a>
063: */
064: @Override
065: public Object intercept(
066: final EasyBeansInvocationContext invocationContext)
067: throws Exception {
068: logger.debug("Calling Never TX interceptor");
069:
070: // Get current transaction
071: Transaction transaction;
072: try {
073: transaction = getTransactionManager().getTransaction();
074: } catch (SystemException se) {
075: throw new EJBException(
076: "Cannot get the current transaction on transaction manager.",
077: se);
078: }
079:
080: logger.debug("Transaction found = {0}", transaction);
081:
082: /*
083: * If the client calls with a transaction context, the container throws
084: * the javax.ejb.EJBException[ 55]. If the EJB 2.1 client view is used,
085: * the container throws the java.rmi.RemoteException exception if the
086: * client is a remote client, or the javax.ejb.EJBException if the
087: * client is a local client.
088: */
089: if (transaction != null) {
090: logger
091: .warn("Mandatory as transaction attribute but within a transaction.");
092: throw new EJBException(
093: "Client should not call within a transaction context by using NEVER mode.");
094: }
095: // else
096: /*
097: * If the client calls without a transaction context, the container
098: * performs the same steps as described in the NOT_SUPPORTED case.
099: */
100: try {
101: return invocationContext.proceed();
102: } catch (Exception e) {
103: handleUnspecifiedTransactionContext(invocationContext, e);
104: // Shouldn't come here
105: return null;
106: }
107: }
108:
109: }
|