KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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: JStatelessSwitch.java,v 1.19 2005/04/28 16:52:59 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.rmi.NoSuchObjectException JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30
31 import javax.ejb.RemoveException JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33 import javax.xml.rpc.handler.MessageContext JavaDoc;
34
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 /**
38  * JStatelessSwitch is the implementation of JSessionSwitch dedicated to the
39  * Stateless Session Bean.
40  * @author Philippe Durieux
41  */

42 public class JStatelessSwitch extends JSessionSwitch {
43
44     /**
45      * Stateless context
46      */

47     private JStatelessContext bctx = null;
48
49     /**
50      * Service Endpoint
51      */

52     private JServiceEndpoint se = null;
53
54     /**
55      * constructor.
56      * @param bf The Bean Factory
57      * @throws RemoteException if the constructor fails
58      */

59     public JStatelessSwitch(JStatelessFactory bf) throws RemoteException JavaDoc {
60         super(bf);
61         if (TraceEjb.isDebugIc()) {
62             TraceEjb.interp.log(BasicLevel.DEBUG, "");
63         }
64
65         // Create ServiceEndpoint if defined in the bean descriptor
66
if (bf.getSEHome() != null) {
67             se = bf.getSEHome().createServiceEndpointObject();
68             se.setSessionSwitch(this);
69         }
70     }
71
72     // ===============================================================
73
// TimerEventListener implementation
74
// ===============================================================
75

76     /**
77      * The session timeout has expired
78      * @param arg Not Used.
79      */

80     public synchronized void timeoutExpired(Object JavaDoc arg) {
81         if (TraceEjb.logger.isLoggable(BasicLevel.WARN)) {
82             TraceEjb.logger.log(BasicLevel.WARN, "stateless session '" + bf.getEJBName() + "' : timeout expired");
83         }
84         mytimer = null;
85         if (bctx != null) {
86             try {
87                 bctx.setRemoved();
88                 ((JStatelessFactory) bf).releaseJContext(bctx);
89                 bctx = null;
90             } catch (RemoteException JavaDoc e) {
91                 TraceEjb.logger.log(BasicLevel.WARN, "timeout expired", e);
92             } catch (RemoveException JavaDoc e) {
93                 TraceEjb.logger.log(BasicLevel.WARN, "timeout expired", e);
94             }
95         }
96         noLongerUsed();
97     }
98
99     // ===============================================================
100
// other public methods
101
// ===============================================================
102

103     /**
104      * @return The ServiceEndpoint
105      */

106     public JServiceEndpoint getServiceEndpoint() {
107         return se;
108     }
109
110     /**
111      * @return The JAX-RPC MessageContext
112      */

113     public MessageContext JavaDoc getMsgContext() {
114         if (se == null) {
115             TraceEjb.logger.log(BasicLevel.ERROR, "No ServiceEndpoint for this bean");
116             return null;
117         }
118         return se.getMessageContext();
119     }
120
121     /**
122      * get an initialized Bean Context
123      * @param tx Current transaction (not used)
124      * @return the Session Context
125      * @throws RemoteException if context cannot be obtained
126      */

127     public JSessionContext getICtx(Transaction JavaDoc tx) throws RemoteException JavaDoc {
128         if (TraceEjb.isDebugIc()) {
129             TraceEjb.interp.log(BasicLevel.DEBUG, "");
130         }
131         bctx = (JStatelessContext) ((JStatelessFactory) bf).getJContext(this);
132         return bctx;
133     }
134
135     /**
136      * release the bean context. Assumes that only 1 Context is managed at a
137      * time. Contexts are release at each request, in case of stateless session.
138      * @param tx Current transaction (not used)
139      */

140     public void releaseICtx(Transaction JavaDoc tx) {
141         if (TraceEjb.isDebugIc()) {
142             TraceEjb.interp.log(BasicLevel.DEBUG, "");
143         }
144
145         if (bctx == null) {
146             return;
147         }
148         if (bctx.isMarkedRemoved()) {
149             stopTimer();
150             noLongerUsed();
151         }
152         ((JStatelessFactory) bf).releaseJContext(bctx);
153         bctx = null;
154     }
155
156     /**
157      * Discard a context/instance at end of request. A problem occured on this
158      * instance and it must be discarded.
159      * @param tx - transaction associated to this context
160      */

161     public void discardICtx(Transaction JavaDoc tx) {
162         if (TraceEjb.isDebugIc()) {
163             TraceEjb.interp.log(BasicLevel.DEBUG, "");
164         }
165
166         // In case getICtx failed, bctx may be null.
167
if (bctx == null) {
168             return;
169         }
170
171         stopTimer();
172         noLongerUsed();
173
174         ((JStatelessFactory) bf).releaseJContext(bctx);
175         bctx = null;
176
177     }
178
179     /**
180      * This Session is no longer used.
181      */

182     public void noLongerUsed() {
183         if (TraceEjb.isDebugIc()) {
184             TraceEjb.interp.log(BasicLevel.DEBUG, "");
185         }
186
187         // Unexport the EJBObject from the Orb
188
if (remote != null) {
189             try {
190                 remote.unexportObject();
191             } catch (NoSuchObjectException JavaDoc e) {
192                 TraceEjb.logger.log(BasicLevel.ERROR, "exception", e);
193             }
194         }
195
196         // return the SessionSwitch in the pool.
197
// will be reused for another Session.
198
bf.removeEJB(this);
199     }
200
201     /**
202      * This is not used for stateless
203      * @param mc Not Used.
204      */

205     public void setMustCommit(boolean mc) {
206     }
207
208     /**
209      * This is not used for stateless
210      */

211     public void pushConnectionList() {
212     }
213
214     /**
215      * This is not used for stateless
216      */

217     public void popConnectionList() {
218     }
219
220     /**
221      * This is not used for stateless
222      * @param tx the transaction object
223      */

224     public void enlistConnections(Transaction JavaDoc tx) {
225     }
226     /**
227      * This is not used for stateless
228      * @param tx the transaction object
229      */

230     public void delistConnections(Transaction JavaDoc tx) {
231     }
232     /**
233      * This do nothing for stateless
234      */

235     public void saveBeanTx() {
236     }
237 }
238
Popular Tags