1 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 ; 41 import javax.ejb.EJBHome ; 42 import javax.ejb.EJBObject ; 43 import javax.ejb.Handle ; 44 import java.rmi.NoSuchObjectException ; 45 import java.rmi.RemoteException ; 46 import java.util.Hashtable ; 47 48 51 public class SameJVMClientContainer { 52 protected static final L10N L = new L10N(SameJVMClientContainer.class); 53 54 private static final EnvironmentLocal<Hashtable <String ,SameJVMClientContainer>> _jvmClient = 55 new EnvironmentLocal<Hashtable <String ,SameJVMClientContainer>>("caucho.jvm.client"); 56 57 String _serverId; 58 AbstractServer _server; 60 61 EJBHome _ejbHome; 63 64 Class _homeStubClass; 66 Class _remoteStubClass; 68 69 74 public SameJVMClientContainer(AbstractServer server, String serverId) 75 { 76 _serverId = serverId; 77 _server = server; 78 79 Class beanSkelClass = server.getBeanSkelClass(); 80 81 Class cl = server.getHomeStubClass(); 82 83 Thread thread = Thread.currentThread(); 84 ClassLoader loader = thread.getContextClassLoader(); 85 86 try { 87 if (cl != null) 88 _homeStubClass = Class.forName(cl.getName(), false, loader); 89 } catch (ClassNotFoundException 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 e) { 98 } 99 } 100 101 public static SameJVMClientContainer find(String serverId) 102 { 103 try { 104 Hashtable <String ,SameJVMClientContainer> map; 105 map = _jvmClient.getLevel(); 106 SameJVMClientContainer client = null; 107 108 if (map != null) 109 client = (SameJVMClientContainer) map.get(serverId); 110 111 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 <String ,SameJVMClientContainer>(); 119 map.put(serverId, client); 120 _jvmClient.set(map); 121 } 122 } 123 124 return client; 125 } catch (Exception e) { 126 throw EJBExceptionWrapper.createRuntime(e); 127 } 128 } 129 130 133 public Class getPrimaryKeyClass() 134 { 135 return _server.getPrimaryKeyClass(); 136 } 137 138 public EJBHome getEJBHome() 139 throws RemoteException 140 { 141 try { 142 return createHomeStub(); 143 } catch (Exception e) { 144 throw RemoteExceptionWrapper.create(e); 145 } 146 } 147 148 155 public EJBHome createHomeStub() 156 throws Exception 157 { 158 if (_homeStubClass == null && _server.getRemoteHomeClass() != null) { 159 Class cl = _server.getRemoteHomeClass(); 160 Class remoteHomeClass = null; 161 try { 162 if (cl != null) 163 remoteHomeClass = CauchoSystem.loadClass(cl.getName(), false, null); 164 } catch (ClassNotFoundException 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 (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 198 public EJBObject createObjectStub(Object primaryKey) 199 throws Exception 200 { 201 AbstractServer server = getServer(); 202 203 if (_remoteStubClass == null && _server.getRemoteObjectClass() != null) { 204 Class cl = _server.getRemoteObjectClass(); 205 Class remoteObjectClass = null; 206 try { 207 if (cl != null) 208 remoteObjectClass = CauchoSystem.loadClass(cl.getName(), false, null); 209 } catch (ClassNotFoundException 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 (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 e) { 243 throw EJBExceptionWrapper.createRuntime(e); 244 } 245 246 throw new EJBException ("can't find server " + handle); 247 } 248 249 250 253 public AbstractServer getServer() 254 throws RemoteException 255 { 256 AbstractServer jvmServer = EjbProtocolManager.getJVMServer(_serverId); 257 258 if (jvmServer != null) 259 return jvmServer; 260 else 261 throw new NoSuchObjectException (_serverId); 262 } 263 264 public EJBObject getObject(Handle handle) 265 { 266 return null; 267 } 268 269 272 public String toString() 273 { 274 return "SameJVMClientContainer[" + _serverId + "]"; 275 } 276 } 277 | Popular Tags |