KickJava   Java API By Example, From Geeks To Geeks.

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


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: JSessionFactory.java,v 1.15 2005/06/16 11:50:03 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.transaction.SystemException JavaDoc;
34
35 import org.objectweb.jonas_ejb.deployment.api.SessionDesc;
36
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * This class is a factory for a Session Bean. It is responsible for - managing
41  * Home and LocalHome. - keeping the JNDI context for this component
42  * (java:comp/env)
43  * @author Philippe Durieux
44  */

45 public abstract class JSessionFactory extends JFactory {
46
47     // Timeout for this session
48
int timeout = 0;
49
50     protected JSessionHome home = null;
51
52     protected JSessionLocalHome localhome = null;
53
54     protected boolean isSynchro = false;
55
56     protected boolean isStateful; // set after constructor call
57

58     // Pool of free JSessionSwitch objects
59
protected ArrayList JavaDoc sessionList = new ArrayList JavaDoc();
60
61     /**
62      * constructor
63      * @param dd The Session Deployment Descriptor
64      * @param cont The Container where the bean is defined.
65      */

66     public JSessionFactory(SessionDesc dd, JContainer cont) {
67         super(dd, cont);
68         if (TraceEjb.isDebugIc()) {
69             TraceEjb.interp.log(BasicLevel.DEBUG, "");
70         }
71
72         txbeanmanaged = dd.isBeanManagedTransaction();
73         timeout = dd.getSessionTimeout();
74
75         // Create the Home if defined in DD
76
Class JavaDoc homeclass = null;
77         String JavaDoc clname = dd.getFullWrpHomeName();
78         if (clname != null) {
79             try {
80                 homeclass = cont.getClassLoader().loadClass(clname);
81             } catch (ClassNotFoundException JavaDoc e) {
82                 throw new EJBException JavaDoc(ejbname + " Cannot load " + clname, e);
83             }
84             if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded");
85             try {
86                 // new JSessionHome(dd, this)
87
int nbp = 2;
88                 Class JavaDoc[] ptype = new Class JavaDoc[nbp];
89                 Object JavaDoc[] pobj = new Object JavaDoc[nbp];
90                 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class;
91                 pobj[0] = (Object JavaDoc) dd;
92                 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class;
93                 pobj[1] = (Object JavaDoc) this;
94                 home = (JSessionHome) homeclass.getConstructor(ptype).newInstance(pobj);
95             } catch (Exception JavaDoc e) {
96                 throw new EJBException JavaDoc(ejbname + " Cannot create home ", e);
97             }
98             // register it in JNDI
99
try {
100                 home.register();
101             } catch (Exception JavaDoc e) {
102                 throw new EJBException JavaDoc(ejbname + " Cannot register home ", e);
103             }
104         }
105
106         // Create the LocalHome if defined in DD
107
clname = dd.getFullWrpLocalHomeName();
108         if (clname != null) {
109             try {
110                 homeclass = cont.getClassLoader().loadClass(clname);
111             } catch (ClassNotFoundException JavaDoc e) {
112                 throw new EJBException JavaDoc(ejbname + " Cannot load " + clname, e);
113             }
114             if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded");
115             try {
116                 // new JSessionLocalHome(dd, this)
117
int nbp = 2;
118                 Class JavaDoc[] ptype = new Class JavaDoc[nbp];
119                 Object JavaDoc[] pobj = new Object JavaDoc[nbp];
120                 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class;
121                 pobj[0] = (Object JavaDoc) dd;
122                 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class;
123                 pobj[1] = (Object JavaDoc) this;
124                 localhome = (JSessionLocalHome) homeclass.getConstructor(ptype).newInstance(pobj);
125             } catch (Exception JavaDoc e) {
126                 throw new EJBException JavaDoc(ejbname + " Cannot create localhome ", e);
127             }
128             // register it in JNDI
129
try {
130                 localhome.register();
131             } catch (Exception JavaDoc e) {
132                 throw new EJBException JavaDoc(ejbname + " Cannot register localhome ", e);
133             }
134         }
135
136     }
137
138     // ---------------------------------------------------------------
139
// BeanFactory implementation
140
// ---------------------------------------------------------------
141

142     /**
143      * stop this EJB. Mainly unregister it in JNDI.
144      */

145     public void stop() {
146         if (TraceEjb.isDebugIc()) {
147             TraceEjb.interp.log(BasicLevel.DEBUG, "");
148         }
149         try {
150             if (home != null) {
151                 home.unregister();
152             }
153             if (localhome != null) {
154                 localhome.unregister();
155             }
156         } catch (NamingException JavaDoc e) {
157         }
158     }
159
160     /**
161      * synchronize bean instances if needed
162      */

163     public void sync() {
164     }
165
166     /**
167      * @return the home if exist
168      */

169     public JHome getHome() {
170         return home;
171     }
172
173     /**
174      * @return the local home if exist
175      */

176     public JLocalHome getLocalHome() {
177         return localhome;
178     }
179
180     // ---------------------------------------------------------------
181
// JSessionSwitch pool management
182
// ---------------------------------------------------------------
183

184     /**
185      * Create a new Session Find one in the pool, or create a new object.
186      * @return a SessionSwitch
187      */

188     public synchronized JSessionSwitch createEJB() throws RemoteException JavaDoc {
189
190         if (TraceEjb.isDebugIc()) {
191             TraceEjb.interp.log(BasicLevel.DEBUG, "");
192         }
193
194         JSessionSwitch bs = null;
195         if (sessionList.size() > 0) {
196             bs = (JSessionSwitch) sessionList.remove(0);
197             // must re-export the remote object in the Orb since EJBObjects
198
// should be un exported when put in the pool.
199
JSessionRemote remote = bs.getRemote();
200             if (remote != null) {
201                 if (remote.exportObject() == false) {
202                     TraceEjb.logger.log(BasicLevel.ERROR, "bad JSessionSwitch found in pool.");
203                     return null;
204                 }
205             }
206         } else {
207             // This depend on subclass (stateless or stateful)
208
// no need to export the EJBObject because it is basically a
209
// RemoteObject
210
bs = createNewSession();
211         }
212
213         // Start a timer for this new session
214
if (timeout > 0) {
215             bs.startTimer(timeout * 1000);
216         }
217         return bs;
218     }
219
220     /**
221      * remove a Session. This may be called also on timeout. put it back in the
222      * pool for later use.
223      * @param bs The Bean Session Switch to put back in the pool.
224      */

225     public synchronized void removeEJB(JSessionSwitch bs) {
226
227         if (TraceEjb.isDebugIc()) {
228             TraceEjb.interp.log(BasicLevel.DEBUG, "");
229         }
230
231         sessionList.add(bs);
232     }
233
234     // ---------------------------------------------------------------
235
// other public methods
236
// ---------------------------------------------------------------
237

238     /**
239      * Session beans can be container managed or bean managed transaction
240      * Session home don't check transactional context.
241      * @param rctx The Request Context
242      */

243     public void checkTransaction(RequestCtx rctx) {
244         if (txbeanmanaged) {
245             try {
246                 rctx.clientTx = tm.suspend();
247                 if (TraceEjb.isDebugTx()) {
248                     TraceEjb.tx.log(BasicLevel.DEBUG, "suspending tx:" + rctx.clientTx);
249                 }
250             } catch (SystemException JavaDoc e) {
251                 throw new EJBException JavaDoc("cannot suspend transaction", e);
252             }
253         } else {
254             checkTransactionContainer(rctx);
255         }
256     }
257
258     /**
259      * @return True if this Session implements SessionSynchronization
260      */

261     public boolean isSessionSynchro() {
262         return isSynchro;
263     }
264
265     /**
266      * @return the current timeout value in seconds for Jmx
267      */

268     public int getTimeout() {
269         return timeout;
270     }
271
272     /**
273      * set the current timeout value for Jmx
274      * @param timeout in seconds
275      */

276     public void setTimeout(int t) {
277         if (TraceEjb.isDebugTx()) {
278             TraceEjb.tx.log(BasicLevel.DEBUG, "");
279         }
280         timeout = t;
281     }
282
283     /**
284      * @return true if this Session is Stateful. Will be used internally by
285      * EJBObject or EJBLocalObject because they are common to both
286      * types.
287      */

288     public boolean isStateful() {
289         return isStateful;
290     }
291
292     /**
293      * @return a JSessionContext
294      */

295     public abstract JSessionContext getJContext(JSessionSwitch ss);
296
297     // ---------------------------------------------------------------
298
// private methods
299
// ---------------------------------------------------------------
300

301     abstract protected JSessionSwitch createNewSession() throws RemoteException JavaDoc;
302 }
303
Popular Tags