KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > transaction > SessionSynchronizationListener


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: SessionSynchronizationListener.java 572 2006-06-04 21:23:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.transaction;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.SessionSynchronization JavaDoc;
32 import javax.transaction.Synchronization JavaDoc;
33 import static javax.transaction.Status.STATUS_COMMITTED JavaDoc;
34
35 /**
36  * This listener will be notified by the transaction manager and will call
37  * methods on the bean.
38  * @author Florent Benoit
39  */

40 public class SessionSynchronizationListener implements Synchronization JavaDoc {
41
42     /**
43      * Bean on which synchonization will be done.
44      */

45     private SessionSynchronization JavaDoc synchronizedBean;
46
47     /**
48      * Creates a listener which will act on the given bean.
49      * @param synchronizedBean bean on which call synchronization methods.
50      */

51     public SessionSynchronizationListener(final SessionSynchronization JavaDoc synchronizedBean) {
52         this.synchronizedBean = synchronizedBean;
53     }
54
55     /**
56      * 4.3.11 Interceptors for Session Beans.<br>
57      * For stateful session beans that implement the SessionSynchronization
58      * interface, afterBegin occurs before any AroundInvoke method invocation,
59      * and beforeCompletion after all AroundInvoke invocations are finished.<br>
60      * The beforeCompletion method is called by the transaction manager prior to
61      * the start of the two-phase transaction commit process. This call is
62      * executed with the transaction context of the transaction that is being
63      * committed.
64      */

65     public void beforeCompletion() {
66         try {
67             synchronizedBean.beforeCompletion();
68         } catch (EJBException JavaDoc e) {
69             throw e;
70         } catch (RemoteException JavaDoc e) {
71             throw new EJBException JavaDoc("Error in beforeCompletion()", e);
72         }
73     }
74
75     /**
76      * This method is called by the transaction manager after the transaction is
77      * committed or rolled back.
78      * @param status The status of the transaction completion.
79      */

80     public void afterCompletion(final int status) {
81         try {
82             synchronizedBean.afterCompletion(status == STATUS_COMMITTED);
83         } catch (EJBException JavaDoc e) {
84             throw e;
85         } catch (RemoteException JavaDoc e) {
86             throw new EJBException JavaDoc("Error in afterCompletion()", e);
87         } finally {
88             ready = true;
89         }
90
91     }
92
93     /**
94      * This listener is ready to receive event from the transaction manager.
95      */

96     private boolean ready = true;
97
98
99     /**
100      * Gets the ready state of this listener.
101      * @return true/false
102      */

103     public boolean isReady() {
104         return ready;
105     }
106
107     /**
108      * Sets the ready state to false as the transaction is in progress.
109      */

110     public void inTX() {
111         ready = false;
112     }
113
114
115
116 }
117
Popular Tags