KickJava   Java API By Example, From Geeks To Geeks.

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


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: ListenerSessionSynchronizationInterceptor.java 450 2006-05-12 15:30:25Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.transaction.interceptors;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.SessionSynchronization JavaDoc;
34 import javax.ejb.TransactionRolledbackLocalException JavaDoc;
35 import javax.transaction.RollbackException JavaDoc;
36 import javax.transaction.SystemException JavaDoc;
37
38 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
39 import org.objectweb.easybeans.log.JLog;
40 import org.objectweb.easybeans.log.JLogFactory;
41 import org.objectweb.easybeans.transaction.SessionSynchronizationListener;
42
43 /**
44  * This interceptor will add on the current transaction an object which will
45  * listen the transaction synchronization and call methods on a bean.
46  * @author Florent Benoit
47  */

48 public class ListenerSessionSynchronizationInterceptor extends AbsTransactionInterceptor {
49
50     /**
51      * Logger.
52      */

53     private JLog logger = JLogFactory.getLog(ListenerSessionSynchronizationInterceptor.class);
54
55     /**
56      * Listener which will receive event of the transaction manager.
57      */

58     private Map JavaDoc<Object JavaDoc, SessionSynchronizationListener> listeners = new WeakHashMap JavaDoc<Object JavaDoc, SessionSynchronizationListener>();
59
60     /**
61      * Adds a listener object receiving calls from the transaction manager.
62      * @param invocationContext context with useful attributes on the current
63      * invocation
64      * @return result of the next invocation (to chain interceptors).
65      * @throws Exception if interceptor fails
66      * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
67      * specification ?12.6.2.2</a>
68      */

69     @Override JavaDoc
70     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
71         logger.debug("Calling ListenerSessionSynchronizationInterceptor interceptor");
72         if (getTransactionManager().getTransaction() != null) {
73             addSynchronization(invocationContext);
74         } else {
75             logger.warn("No transaction but the bean is implementing session synchonization interface.");
76         }
77         return invocationContext.proceed();
78     }
79
80     /**
81      * Add a synchronization listener to the transaction manager in order to be
82      * notified and send actions on the bean. It should be done only once until
83      * transaction is completed.
84      * @param invocationContext the context on the current invocation.
85      */

86     private void addSynchronization(final EasyBeansInvocationContext invocationContext) {
87         Object JavaDoc o = invocationContext.getTarget();
88         if (!(o instanceof SessionSynchronization JavaDoc)) {
89             throw new IllegalArgumentException JavaDoc("This interceptor should not have been added on this "
90                     + "bean which doesn't implement SessionSynchronization interface.");
91         }
92         SessionSynchronization JavaDoc bean = (SessionSynchronization JavaDoc) o;
93
94         /**
95          * 4.3.11 Interceptors for Session Beans.<br>
96          * For stateful session beans that implement the SessionSynchronization
97          * interface, afterBegin occurs before any AroundInvoke method
98          * invocation, and beforeCompletion after all AroundInvoke invocations
99          * are finished.<br>
100          * The beforeCompletion method is called by the transaction manager
101          * prior to the start of the two-phase transaction commit process. This
102          * call is executed with the transaction context of the transaction that
103          * is being committed.
104          */

105
106         // TODO: THE COMPLETED information should be retrieved on the bean
107
// context, not on the session listener.
108
SessionSynchronizationListener sessionSynchronizationListener = listeners.get(bean);
109         if (sessionSynchronizationListener == null) {
110             sessionSynchronizationListener = new SessionSynchronizationListener(bean);
111             listeners.put(bean, sessionSynchronizationListener);
112         }
113
114         // add only once until
115
if (sessionSynchronizationListener.isReady()) {
116
117             try {
118                 getTransactionManager().getTransaction().registerSynchronization(sessionSynchronizationListener);
119             } catch (IllegalStateException JavaDoc e) {
120                 throw new EJBException JavaDoc("Cannot register the synchronization", e);
121             } catch (RollbackException JavaDoc e) {
122                 throw new TransactionRolledbackLocalException JavaDoc("Session rolled back");
123             } catch (SystemException JavaDoc e) {
124                 throw new EJBException JavaDoc("Cannot register the synchronization", e);
125             }
126
127             // now call the after begin method on the synchronized bean
128
try {
129                 bean.afterBegin();
130             } catch (EJBException JavaDoc e) {
131                 throw e;
132             } catch (RemoteException JavaDoc e) {
133                 throw new EJBException JavaDoc("Cannot call afterBefin method", e);
134             }
135             sessionSynchronizationListener.inTX();
136         }
137
138     }
139 }
140
Popular Tags