KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > session > SessionServer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.session;
31
32 import com.caucho.ejb.AbstractContext;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.ejb.EJBExceptionWrapper;
35 import com.caucho.ejb.EjbServerManager;
36 import com.caucho.ejb.protocol.AbstractHandle;
37 import com.caucho.ejb.protocol.JVMObject;
38 import com.caucho.naming.Jndi;
39 import com.caucho.util.Log;
40 import com.caucho.util.LruCache;
41
42 import javax.ejb.EJBHome JavaDoc;
43 import javax.ejb.EJBLocalHome JavaDoc;
44 import javax.ejb.FinderException JavaDoc;
45 import javax.ejb.SessionBean JavaDoc;
46 import java.lang.reflect.Constructor JavaDoc;
47 import java.rmi.RemoteException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 /**
54  * Server container for a session bean.
55  */

56 public class SessionServer extends AbstractServer {
57   protected final static Logger JavaDoc log = Log.open(SessionServer.class);
58
59   private StatefulContext _homeContext;
60
61   private LruCache<Object JavaDoc,AbstractSessionContext> _sessions
62     = new LruCache<Object JavaDoc,AbstractSessionContext>(1024);
63
64   private boolean _isClosed;
65
66   public SessionServer(EjbServerManager manager)
67   {
68     super(manager);
69   }
70
71   /**
72    * Initialize the server
73    */

74   @Override JavaDoc
75   public void init()
76     throws Exception JavaDoc
77   {
78     Thread JavaDoc thread = Thread.currentThread();
79     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
80
81     try {
82       thread.setContextClassLoader(_loader);
83     
84       super.init();
85
86       // XXX: from TCK, s/b local or remote?
87
String JavaDoc prefix = getServerManager().getLocalJndiPrefix();
88       if (prefix != null)
89     Jndi.rebindDeep(prefix + "/sessionContext", getSessionContext());
90       
91       prefix = getServerManager().getRemoteJndiPrefix();
92       if (prefix != null)
93     Jndi.rebindDeep(prefix + "/sessionContext", getSessionContext());
94       
95       _localHome = getSessionContext().createLocalHome();
96       _remoteHomeView = getSessionContext().createRemoteHomeView();
97       
98       log.config("initialized session bean: " + this);
99     } finally {
100       thread.setContextClassLoader(oldLoader);
101     }
102   }
103
104   /**
105    * Returns the object key from a handle.
106    */

107   @Override JavaDoc
108   public Class JavaDoc getPrimaryKeyClass()
109   {
110     return null;
111   }
112
113   /**
114    * Returns the EJBLocalHome stub for the container
115    */

116   @Override JavaDoc
117   public EJBLocalHome JavaDoc getEJBLocalHome()
118   {
119     return _localHome;
120   }
121
122   /**
123    * Returns the EJBHome stub for the container
124    */

125   @Override JavaDoc
126   public EJBHome JavaDoc getEJBHome()
127     throws RemoteException JavaDoc
128   {
129     return _remoteHomeView;
130   }
131
132   /**
133    * Returns the home object for jndi.
134    */

135   @Override JavaDoc
136   public Object JavaDoc getHomeObject()
137   {
138     return _remoteHomeView;
139   }
140
141   /**
142    * Returns the EJBRemote stub for the container
143    */

144   @Override JavaDoc
145   public Object JavaDoc getRemoteObject()
146   {
147     if (_remoteHomeView != null)
148       return _remoteHomeView;
149     else
150       return _homeContext._caucho_newRemoteInstance();
151   }
152
153   /**
154    * Returns the EJBHome stub for the container
155    */

156   @Override JavaDoc
157   public Object JavaDoc getClientObject()
158   {
159     return new StatefulJndiFactory(this);
160   }
161
162   /**
163    * Returns a new instance.
164    */

165   Object JavaDoc newInstance()
166   {
167     return _homeContext._caucho_newInstance();
168   }
169
170   /**
171    * Creates the local stub for the object in the context.
172    */

173   SessionObject getEJBLocalObject(SessionBean JavaDoc bean)
174   {
175     try {
176       SessionObject obj = null;
177
178       /*
179       obj = (SessionObject) bean.getLocal();
180       obj._setObject(bean);
181       */

182       if (obj == null)
183         throw new IllegalStateException JavaDoc("bean has no local interface");
184
185       return obj;
186     } catch (Exception JavaDoc e) {
187       throw new EJBExceptionWrapper(e);
188     }
189   }
190
191   /**
192    * Creates a handle for a new session.
193    */

194   JVMObject createEJBObject(Object JavaDoc primaryKey)
195   {
196     try {
197       JVMObject obj = (JVMObject) _remoteStubClass.newInstance();
198       obj._init(this, primaryKey);
199
200       return obj;
201     } catch (Exception JavaDoc e) {
202       throw new EJBExceptionWrapper(e);
203     }
204   }
205
206   /**
207    * Creates a handle for a new session.
208    */

209   AbstractHandle createHandle(AbstractContext context)
210   {
211     String JavaDoc key = ((AbstractSessionContext) context).getPrimaryKey();
212     
213     return getHandleEncoder().createHandle(key);
214   }
215
216   /**
217    * Creates a handle for a new session.
218    */

219   public String JavaDoc createSessionKey(AbstractSessionContext context)
220   {
221     String JavaDoc key = getHandleEncoder().createRandomStringKey();
222     
223     _sessions.put(key, context);
224     
225     return key;
226   }
227   
228   /**
229    * Finds the remote bean by its key.
230    *
231    * @param key the remote key
232    *
233    * @return the remote interface of the entity.
234    */

235   @Override JavaDoc
236   public AbstractContext getContext(Object JavaDoc key, boolean forceLoad)
237     throws FinderException JavaDoc
238   {
239     if (key == null)
240       return null;
241     
242     AbstractSessionContext cxt = _sessions.get(key);
243     if (cxt == null)
244       throw new FinderException JavaDoc("no matching object:" + key);
245
246     return cxt;
247   }
248
249   private AbstractSessionContext getSessionContext()
250   {
251     synchronized (this) {
252       if (_homeContext == null) {
253     try {
254       Class JavaDoc []param = new Class JavaDoc[] { SessionServer.class };
255       Constructor JavaDoc cons = _contextImplClass.getConstructor(param);
256
257       _homeContext = (StatefulContext) cons.newInstance(this);
258     } catch (Exception JavaDoc e) {
259       throw new EJBExceptionWrapper(e);
260     }
261       }
262     }
263
264     return _homeContext;
265   }
266
267   public void addSession(AbstractSessionContext context)
268   {
269     createSessionKey(context);
270   }
271
272   /**
273    * Remove an object by its handle.
274    */

275   @Override JavaDoc
276   public void remove(AbstractHandle handle)
277   {
278     _sessions.remove(handle.getObjectId());
279     // _ejbManager.remove(handle);
280
}
281   
282   /**
283    * Cleans up the entity server nicely.
284    */

285   @Override JavaDoc
286   public void destroy()
287   {
288     synchronized (this) {
289       if (_isClosed)
290         return;
291
292       _isClosed = true;
293     }
294
295     ArrayList JavaDoc<AbstractSessionContext> values;
296     values = new ArrayList JavaDoc<AbstractSessionContext>();
297
298     Iterator JavaDoc<AbstractSessionContext> iter = _sessions.values();
299     while (iter.hasNext()) {
300       values.add(iter.next());
301     }
302
303     _sessions = null;
304     
305     log.fine("closing session server " + this);
306
307     for (AbstractSessionContext cxt : values) {
308       try {
309     cxt.destroy();
310       } catch (Throwable JavaDoc e) {
311     log.log(Level.WARNING, e.toString(), e);
312       }
313     }
314
315     super.destroy();
316   }
317 }
318
Popular Tags