KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > transaction > interceptors > CMTMandatoryTransactionInterceptor


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: CMTMandatoryTransactionInterceptor.java 942 2006-07-26 09:17:13Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.transaction.interceptors;
27
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.EJBTransactionRequiredException JavaDoc;
30 import javax.transaction.SystemException JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32
33 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36
37 /**
38  * Defines an interceptor for method using the MANDATORY attribute.
39  * @author Florent Benoit
40  */

41 public class CMTMandatoryTransactionInterceptor extends AbsTransactionInterceptor {
42
43     /**
44      * Logger.
45      */

46     private JLog logger = JLogFactory.getLog(CMTMandatoryTransactionInterceptor.class);
47
48     /**
49      * Constructor.<br>
50      * Acquire the transaction manager.
51      */

52     public CMTMandatoryTransactionInterceptor() {
53         super();
54     }
55
56     /**
57      * Execute transaction as specified with the MANDATORY attribute.
58      * @param invocationContext context with useful attributes on the current
59      * invocation
60      * @return result of the next invocation (to chain interceptors)
61      * @throws Exception if interceptor fails
62      * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
63      * specification ?12.6.2.5</a>
64      */

65     @Override JavaDoc
66     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
67         logger.debug("Calling Mandatory TX interceptor");
68
69         // Get current transaction
70
Transaction JavaDoc transaction;
71         try {
72             transaction = getTransactionManager().getTransaction();
73         } catch (SystemException JavaDoc se) {
74             throw new EJBException JavaDoc("Cannot get the current transaction on transaction manager.", se);
75         }
76
77         logger.debug("Transaction found = {0}", transaction);
78
79         /*
80          * If the client calls without a transaction context, the container
81          * throws the javax.ejb.EJBTransactionRequiredException[ 54]. If the EJB
82          * 2.1 client view is used, the container throws the
83          * javax.transaction.TransactionRequiredException exception if the
84          * client is a remote client, or the
85          * javax.ejb.TransactionRequiredLocalException if the client is a local
86          * client.
87          */

88         if (transaction == null) {
89             logger.warn("Mandatory as transaction attribute and not in transaction");
90             throw new EJBTransactionRequiredException JavaDoc("Client should call with a transaction context in MANDATORY mode.");
91         }
92
93         // Transaction not started by this interceptor, nothing to do after the
94
// proceed.
95
try {
96             return invocationContext.proceed();
97         } catch (Exception JavaDoc e) {
98             // Chapter 14.3.1
99
// Runs in the context of the client
100
handleContextClientTransaction(invocationContext, e);
101
102             // Shouldn't come here
103
return null;
104         }
105     }
106
107 }
108
Popular Tags