KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > protocol > SameJVMClientContainer


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.protocol;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.ejb.EJBExceptionWrapper;
33 import com.caucho.ejb.RemoteExceptionWrapper;
34 import com.caucho.ejb.gen.JVMHomeStubGenerator;
35 import com.caucho.ejb.gen.JVMObjectStubGenerator;
36 import com.caucho.loader.EnvironmentLocal;
37 import com.caucho.server.util.CauchoSystem;
38 import com.caucho.util.L10N;
39
40 import javax.ejb.EJBException JavaDoc;
41 import javax.ejb.EJBHome JavaDoc;
42 import javax.ejb.EJBObject JavaDoc;
43 import javax.ejb.Handle JavaDoc;
44 import java.rmi.NoSuchObjectException JavaDoc;
45 import java.rmi.RemoteException JavaDoc;
46 import java.util.Hashtable JavaDoc;
47
48 /**
49  * Container for EJB clients in the same JVM, but not the same class loader.
50  */

51 public class SameJVMClientContainer {
52   protected static final L10N L = new L10N(SameJVMClientContainer.class);
53   
54   private static final EnvironmentLocal<Hashtable JavaDoc<String JavaDoc,SameJVMClientContainer>> _jvmClient =
55     new EnvironmentLocal<Hashtable JavaDoc<String JavaDoc,SameJVMClientContainer>>("caucho.jvm.client");
56   
57   String JavaDoc _serverId;
58   // The server container in the JVM
59
AbstractServer _server;
60   
61   // the home stub
62
EJBHome JavaDoc _ejbHome;
63   
64   // the home stub class
65
Class JavaDoc _homeStubClass;
66   // the remote stub class
67
Class JavaDoc _remoteStubClass;
68
69   /**
70    * Creates a client container for same-JVM connections.
71    *
72    * @param serverId the server id
73    */

74   public SameJVMClientContainer(AbstractServer server, String JavaDoc serverId)
75   {
76     _serverId = serverId;
77     _server = server;
78
79     Class JavaDoc beanSkelClass = server.getBeanSkelClass();
80
81     Class JavaDoc cl = server.getHomeStubClass();
82
83     Thread JavaDoc thread = Thread.currentThread();
84     ClassLoader JavaDoc loader = thread.getContextClassLoader();
85
86     try {
87       if (cl != null)
88         _homeStubClass = Class.forName(cl.getName(), false, loader);
89     } catch (ClassNotFoundException JavaDoc e) {
90     }
91     
92     cl = server.getRemoteStubClass();
93
94     try {
95       if (cl != null)
96         _remoteStubClass = Class.forName(cl.getName(), false, loader);
97     } catch (ClassNotFoundException JavaDoc e) {
98     }
99   }
100
101   public static SameJVMClientContainer find(String JavaDoc serverId)
102   {
103     try {
104       Hashtable JavaDoc<String JavaDoc,SameJVMClientContainer> map;
105       map = _jvmClient.getLevel();
106       SameJVMClientContainer client = null;
107
108       if (map != null)
109         client = (SameJVMClientContainer) map.get(serverId);
110
111       // sync doesn't matter since it's okay to load a dup
112
if (client == null) {
113         AbstractServer server = EjbProtocolManager.getJVMServer(serverId);
114
115         if (server != null) {
116           client = new SameJVMClientContainer(server, serverId);
117           if (map == null)
118             map = new Hashtable JavaDoc<String JavaDoc,SameJVMClientContainer>();
119           map.put(serverId, client);
120           _jvmClient.set(map);
121         }
122       }
123       
124       return client;
125     } catch (Exception JavaDoc e) {
126       throw EJBExceptionWrapper.createRuntime(e);
127     }
128   }
129
130   /**
131    * Returns the object key from a handle.
132    */

133   public Class JavaDoc getPrimaryKeyClass()
134   {
135     return _server.getPrimaryKeyClass();
136   }
137
138   public EJBHome JavaDoc getEJBHome()
139     throws RemoteException JavaDoc
140   {
141     try {
142       return createHomeStub();
143     } catch (Exception JavaDoc e) {
144       throw RemoteExceptionWrapper.create(e);
145     }
146   }
147
148   /**
149    * Finds the stub corresponding to the given URL.
150    *
151    * @param handle the handle for the bean's home
152    *
153    * @return the bean's home stub
154    */

155   public EJBHome JavaDoc createHomeStub()
156     throws Exception JavaDoc
157   {
158     if (_homeStubClass == null && _server.getRemoteHomeClass() != null) {
159       Class JavaDoc cl = _server.getRemoteHomeClass();
160       Class JavaDoc remoteHomeClass = null;
161       try {
162         if (cl != null)
163           remoteHomeClass = CauchoSystem.loadClass(cl.getName(), false, null);
164       } catch (ClassNotFoundException JavaDoc e) {
165       }
166
167       if (remoteHomeClass == null) {
168       }
169       else if (remoteHomeClass.equals(_server.getRemoteHomeClass())) {
170         _homeStubClass = _server.getHomeStubClass();
171       }
172       else {
173         JVMHomeStubGenerator gen;
174         gen = new JVMHomeStubGenerator(remoteHomeClass, true);
175         gen.setClassDir(CauchoSystem.getWorkPath());
176         _homeStubClass = gen.generateStub();
177       }
178     }
179     
180     if (_homeStubClass == null)
181       throw new EJBException JavaDoc(L.l("`{0}' is a local bean with no remote interface. Local beans must be looked up with a local context.",
182                                  _serverId));
183
184     JVMHome homeStub = (JVMHome) _homeStubClass.newInstance();
185
186     homeStub._init(_server);
187
188     return homeStub;
189   }
190
191   /**
192    * Finds the stub for the remote object for the given Handle.
193    *
194    * @param handle the handle for the remote object
195    *
196    * @return the bean's remote stub
197    */

198   public EJBObject JavaDoc createObjectStub(Object JavaDoc primaryKey)
199     throws Exception JavaDoc
200   {
201     AbstractServer server = getServer();
202     
203     if (_remoteStubClass == null && _server.getRemoteObjectClass() != null) {
204       Class JavaDoc cl = _server.getRemoteObjectClass();
205       Class JavaDoc remoteObjectClass = null;
206       try {
207         if (cl != null)
208           remoteObjectClass = CauchoSystem.loadClass(cl.getName(), false, null);
209       } catch (ClassNotFoundException JavaDoc e) {
210       }
211
212       if (remoteObjectClass == null) {
213       } else if (remoteObjectClass.equals(_server.getRemoteObjectClass())) {
214         _remoteStubClass = server.getRemoteStubClass();
215       }
216       else {
217         JVMObjectStubGenerator gen;
218         gen = new JVMObjectStubGenerator(remoteObjectClass, true);
219         gen.setClassDir(CauchoSystem.getWorkPath());
220         _remoteStubClass = gen.generateStub();
221       }
222     }
223     
224     if (_remoteStubClass == null)
225       throw new EJBException JavaDoc(L.l("`{0}' is a local bean with no remote interface. Local beans must be looked up with a local context.",
226                                  _serverId));
227
228     JVMObject objStub = (JVMObject) _remoteStubClass.newInstance();
229
230     objStub._init(server, primaryKey);
231     
232     return objStub;
233   }
234
235   public HandleEncoder getHandleEncoder(AbstractHandle handle)
236   {
237     try {
238       AbstractServer server = getServer();
239
240       if (server != null)
241         return server.getHandleEncoder();
242     } catch (Exception JavaDoc e) {
243       throw EJBExceptionWrapper.createRuntime(e);
244     }
245
246     throw new EJBException JavaDoc("can't find server " + handle);
247   }
248   
249
250   /**
251    * Returns the current server.
252    */

253   public AbstractServer getServer()
254     throws RemoteException JavaDoc
255   {
256     AbstractServer jvmServer = EjbProtocolManager.getJVMServer(_serverId);
257
258     if (jvmServer != null)
259       return jvmServer;
260     else
261       throw new NoSuchObjectException JavaDoc(_serverId);
262   }
263
264   public EJBObject JavaDoc getObject(Handle JavaDoc handle)
265   {
266     return null;
267   }
268
269   /**
270    * Returns a printable version of the client container
271    */

272   public String JavaDoc toString()
273   {
274     return "SameJVMClientContainer[" + _serverId + "]";
275   }
276 }
277
Popular Tags