KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JStatefulContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@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: JStatefulContext.java,v 1.15 2005/07/13 06:29:45 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.ejb.RemoveException JavaDoc;
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.SessionSynchronization JavaDoc;
34 import javax.ejb.TimerService JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.transaction.Status JavaDoc;
38 import javax.transaction.Synchronization JavaDoc;
39 import javax.xml.rpc.handler.MessageContext JavaDoc;
40
41 import org.objectweb.jonas.naming.NamingManager;
42 import org.objectweb.jonas_lib.naming.ContainerNaming;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * This class extends JSessionContext in case of Stateful Session Bean.
47  * @author Philippe Coq, Philippe Durieux
48  */

49 public class JStatefulContext extends JSessionContext implements Synchronization JavaDoc {
50
51     /**
52      * the bean instance implements SessionSynchronization interface
53      * @serial
54      */

55     boolean synchro = false;
56
57     boolean timedout = false;
58
59     // ------------------------------------------------------------------
60
// constructors
61
// ------------------------------------------------------------------
62

63     /**
64      * Constructs a JStatefulContext
65      * @param bf - the Session Factory
66      * @param sb - the Enterprise Bean instance
67      * @param sync - True if implements SessionSymchronization.
68      */

69     public JStatefulContext(JSessionFactory bf, SessionBean JavaDoc sb, boolean sync) {
70         super(bf, sb);
71         if (TraceEjb.isDebugIc()) {
72             TraceEjb.interp.log(BasicLevel.DEBUG, "");
73         }
74         synchro = sync;
75         // Register this Context in java:comp (used by JTimerHandle)
76
try {
77             ContainerNaming naming = NamingManager.getInstance();
78             Context JavaDoc c = naming.getComponentContext();
79             c.rebind("MY_SF_CONTEXT", this);
80         } catch (NamingException JavaDoc ne) {
81             TraceEjb.logger.log(BasicLevel.ERROR, "Cannot bind EJBContext", ne);
82         }
83     }
84     
85     /**
86      * set instance. Used for passivation/activation.
87      * @param sb
88      */

89     public void setInstance(SessionBean JavaDoc sb) {
90         instance = sb;
91     }
92
93     // ------------------------------------------------------------------
94
// EJBContext implementation
95
// ------------------------------------------------------------------
96

97     /**
98      * Get access to the EJB Timer Service.
99      * @return the EJB Timer Service
100      * @throws IllegalStateException Thrown if the instance is not allowed to
101      * use this method
102      */

103     public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc {
104         throw new IllegalStateException JavaDoc("getTimerService Not Allowed on Stateful Session Beans");
105     }
106
107     /**
108      * Obtain a reference to the JAX-RPC MessageContext.
109      * @return The MessageContext for this web service invocation.
110      * @throws java.lang.IllegalStateException - the instance is in a state that
111      * does not allow access to this method.
112      */

113     public MessageContext JavaDoc getMessageContext() throws java.lang.IllegalStateException JavaDoc {
114         throw new IllegalStateException JavaDoc("getMessageContext Not Allowed on Stateful Session Beans");
115     }
116
117     // -------------------------------------------------------------------
118
// Synchronization implementation
119
// -------------------------------------------------------------------
120

121     /**
122      * This beforeCompletion method is called by the transaction manager prior
123      * to the start of the transaction completion process. This method executes
124      * in the transaction context of the calling thread.
125      */

126     public void beforeCompletion() {
127         if (TraceEjb.isDebugTx()) {
128             TraceEjb.tx.log(BasicLevel.DEBUG, "");
129         }
130
131         if (synchro) {
132             // Set classloader for getting the right component context
133
ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
134             Thread.currentThread().setContextClassLoader(bf.myClassLoader());
135             Context JavaDoc bnctx = bf.setComponentContext();
136             try {
137                 SessionSynchronization JavaDoc ss = (SessionSynchronization JavaDoc) getInstance();
138                 ss.beforeCompletion();
139             } catch (RemoteException JavaDoc e) {
140                 TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
141             } finally {
142                 bf.resetComponentContext(bnctx);
143                 Thread.currentThread().setContextClassLoader(old);
144             }
145         }
146     }
147
148     /**
149      * The afterCompletion method is called by the transaction manager after the
150      * transaction is committed or rolled back. This method executes without a
151      * transaction context.
152      * @param status The status of the transaction completion.
153      */

154     public void afterCompletion(int status) {
155         if (TraceEjb.isDebugTx()) {
156             TraceEjb.tx.log(BasicLevel.DEBUG, "");
157         }
158
159         if (synchro) {
160             // Set classloader for getting the right component context
161
ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
162             Thread.currentThread().setContextClassLoader(bf.myClassLoader());
163             Context JavaDoc bnctx = bf.setComponentContext();
164             try {
165                 SessionSynchronization JavaDoc ss = (SessionSynchronization JavaDoc) getInstance();
166                 setState(3);
167                 ss.afterCompletion(status == Status.STATUS_COMMITTED);
168                 setState(2);
169             } catch (RemoteException JavaDoc e) {
170                 TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
171             } finally {
172                 bf.resetComponentContext(bnctx);
173                 Thread.currentThread().setContextClassLoader(old);
174             }
175         }
176
177         // Let the StatefulSwitch now that transaction is over and that it can
178
// now dispose of this Context for another transaction.
179
// no need to be in the correct component context ?
180
JStatefulSwitch jss = (JStatefulSwitch) bs;
181         jss.txCompleted();
182
183         // The timeout has expired during the transaction. We can now remove
184
// this session.
185
if (timedout) {
186             TraceEjb.logger.log(BasicLevel.WARN, "timeout expired during the transaction");
187             timedout = false;
188             // TODO
189
// ((JStatefulSession)ejbObject).forceSessionRemove();
190
}
191     }
192
193     // ------------------------------------------------------------------
194
// Other methods
195
// ------------------------------------------------------------------
196

197     /**
198      * set this instance as removed
199      */

200     public void setRemoved() throws RemoteException JavaDoc, RemoveException JavaDoc {
201         if (TraceEjb.isDebugIc()) {
202             TraceEjb.interp.log(BasicLevel.DEBUG, "");
203         }
204
205         // Cannot remove a stateful session inside a transaction
206
JStatefulSwitch jss = (JStatefulSwitch) bs;
207         if (jss.isInTransaction()) {
208             throw new RemoveException JavaDoc("Cannot remove a statefull session inside a transaction");
209         }
210
211         // Call ejbRemove on instance
212
// Assume that if this method is transacted (container transaction
213
// only), this
214
// transaction will be committed by the container (always true normally)
215
SessionBean JavaDoc sb = (SessionBean JavaDoc) instance;
216         sb.ejbRemove();
217
218         // Set a flag to finish remove at postInvoke.
219
// getEJBObject should work in ejbRemove.
220
// => we do this only after ejbRemove call.
221
ismarkedremoved = true;
222     }
223
224     /**
225      * Set the connection list for this instance.
226      */

227     public void setConnectionList(List JavaDoc conlist) {
228         JStatefulSwitch jss = (JStatefulSwitch) bs;
229         jss.setConnectionList(conlist);
230     }
231
232 }
233
Popular Tags