KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.session;
30
31 import com.caucho.ejb.AbstractContext;
32 import com.caucho.ejb.AbstractServer;
33 import com.caucho.ejb.EJBExceptionWrapper;
34 import com.caucho.ejb.EjbServerManager;
35 import com.caucho.ejb.protocol.AbstractHandle;
36 import com.caucho.naming.Jndi;
37 import com.caucho.util.Log;
38
39 import javax.ejb.EJBHome JavaDoc;
40 import javax.ejb.EJBLocalObject JavaDoc;
41 import javax.ejb.EJBObject JavaDoc;
42 import javax.ejb.FinderException JavaDoc;
43 import java.lang.reflect.Constructor JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Server home container for a stateless session bean
49  */

50 public class StatelessServer extends AbstractServer {
51   protected static Logger JavaDoc log = Log.open(StatelessServer.class);
52   
53   private AbstractStatelessContext _homeContext;
54   
55   private EJBObject JavaDoc _remoteObject;
56   private EJBLocalObject JavaDoc _localObject;
57   
58   /**
59    * Creates a new stateless server.
60    *
61    * @param urlPrefix the url prefix for any request to the server
62    * @param allowJVMCall allows fast calls to the same JVM (with serialization)
63    * @param config the session configuration from the ejb.xml
64    */

65   public StatelessServer(EjbServerManager ejbManager)
66   {
67     super(ejbManager);
68   }
69
70   /**
71    * Initialize the server
72    */

73   @Override JavaDoc
74   public void init()
75     throws Exception JavaDoc
76   {
77     Thread JavaDoc thread = Thread.currentThread();
78     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
79
80     try {
81       thread.setContextClassLoader(_loader);
82       
83       super.init();
84     
85       Jndi.rebindDeep("java:comp/env/ejb/sessionContext",
86               getStatelessContext());
87       
88       _localHome = getStatelessContext().createLocalHome();
89       _remoteHomeView = getStatelessContext().createRemoteHomeView();
90
91       try {
92     _localObject = getStatelessContext().getEJBLocalObject();
93       } catch (Throwable JavaDoc e) {
94       }
95
96       try {
97     _remoteObject = getStatelessContext().getEJBObject();
98       } catch (Throwable JavaDoc e) {
99       }
100       /*
101     if (_config.getLocalHomeClass() != null)
102     _localHome = _homeContext.createLocalHome();
103
104     if (_homeStubClass != null) {
105     _remoteHomeView = _homeContext.createRemoteHomeView();
106
107     if (_config.getJndiName() != null) {
108     Context ic = new InitialContext();
109     ic.rebind(_config.getJndiName(), this);
110     }
111     }
112       */

113       
114       log.config("initialized session bean: " + this);
115     } finally {
116       thread.setContextClassLoader(oldLoader);
117     }
118   }
119
120   @Override JavaDoc
121   public boolean isLocal()
122   {
123     return super.isLocal() || _localObject != null;
124   }
125
126   @Override JavaDoc
127   public boolean isRemote()
128   {
129     return super.isRemote() || _remoteObject != null;
130   }
131
132   /**
133    * Returns the ejb home.
134    */

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

144   @Override JavaDoc
145   public Object JavaDoc getRemoteObject()
146   {
147     Object JavaDoc home = getEJBHome();
148     
149     if (home != null)
150       return home;
151
152     if (_remoteObject != null)
153       return _remoteObject;
154     
155     return null;
156   }
157
158   /**
159    * Returns the EJBHome stub for the container
160    */

161   @Override JavaDoc
162   public Object JavaDoc getClientObject()
163   {
164     Object JavaDoc home = getClientLocalHome();
165     
166     if (home != null)
167       return home;
168
169     if (_localObject != null)
170       return _localObject;
171     
172     return null;
173   }
174
175   /**
176    * Finds the remote bean by its key.
177    *
178    * @param key the remote key
179    *
180    * @return the remote interface of the entity.
181    */

182   @Override JavaDoc
183   public EJBObject JavaDoc getEJBObject(Object JavaDoc key)
184     throws FinderException JavaDoc
185   {
186     return getStatelessContext().getEJBObject();
187   }
188
189   public AbstractContext getContext()
190   {
191     return getStatelessContext();
192   }
193
194   @Override JavaDoc
195   public AbstractContext getContext(Object JavaDoc key, boolean forceLoad)
196   {
197     return getStatelessContext();
198   }
199
200   private AbstractStatelessContext getStatelessContext()
201   {
202     synchronized (this) {
203       if (_homeContext == null) {
204     try {
205       Class JavaDoc []param = new Class JavaDoc[] { StatelessServer.class };
206       Constructor JavaDoc cons = _contextImplClass.getConstructor(param);
207
208       _homeContext = (AbstractStatelessContext) cons.newInstance(this);
209     } catch (Exception JavaDoc e) {
210       throw new EJBExceptionWrapper(e);
211     }
212       }
213     }
214
215     return _homeContext;
216   }
217
218   /**
219    * Creates a handle for a new session.
220    */

221   AbstractHandle createHandle(AbstractContext context)
222   {
223     String JavaDoc key = createSessionKey(context);
224     
225     return getHandleEncoder().createHandle(key);
226   }
227
228   /**
229    * Creates a handle for a new session.
230    */

231   /*
232   JVMObject createEJBObject(Object primaryKey)
233   {
234     try {
235       JVMObject obj = (JVMObject) _remoteStubClass.newInstance();
236       obj._init(this, createSessionKey(null));
237
238       return obj;
239     } catch (Exception e) {
240       throw new EJBExceptionWrapper(e);
241     }
242   }
243   */

244
245   /**
246    * Creates a handle for a new session.
247    */

248   String JavaDoc createSessionKey(AbstractContext context)
249   {
250     return "::ejb:stateless";
251   }
252   
253   /**
254    * Cleans up the entity server nicely.
255    */

256   @Override JavaDoc
257   public void destroy()
258   {
259     if (_homeContext != null) {
260       try {
261     _homeContext.destroy();
262       } catch (Throwable JavaDoc e) {
263     log.log(Level.WARNING, e.toString(), e);
264       }
265     }
266     
267     super.destroy();
268   }
269 }
270
Popular Tags