1 28 29 package com.caucho.ejb.hessian; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.ejb.AbstractServer; 33 import com.caucho.ejb.EJBExceptionWrapper; 34 import com.caucho.ejb.protocol.EjbProtocolManager; 35 import com.caucho.ejb.protocol.HandleEncoder; 36 import com.caucho.hessian.io.HessianRemoteResolver; 37 import com.caucho.loader.EnvironmentLocal; 38 import com.caucho.server.util.CauchoSystem; 39 import com.caucho.util.L10N; 40 import com.caucho.vfs.Path; 41 import com.caucho.vfs.Vfs; 42 43 import javax.ejb.EJBHome ; 44 import javax.ejb.EJBObject ; 45 import java.io.IOException ; 46 import java.util.Hashtable ; 47 import java.util.Map ; 48 49 53 class HessianClientContainer implements HessianRemoteResolver { 54 protected static L10N L = new L10N(HessianClientContainer.class); 55 56 private static EnvironmentLocal<Map <String ,HessianClientContainer>> _hessianClient = 57 new EnvironmentLocal<Map <String ,HessianClientContainer>>("caucho.hessian.client"); 58 59 private String _serverId; 60 private HessianHandleEncoder _handleEncoder; 61 EJBHome _ejbHome; 63 64 Class _homeClass; 65 Class _remoteClass; 66 Class _homeStubClass; 68 Class _remoteStubClass; 70 Class _primaryKeyClass; 72 73 private String _basicAuth; 74 75 80 HessianClientContainer(String serverId) 81 throws ConfigException 82 { 83 _serverId = serverId; 84 85 _remoteStubClass = getRemoteStubClass(); 86 _homeStubClass = getHomeStubClass(); 87 } 88 89 static HessianClientContainer find(String serverId) 90 { 91 try { 92 Map <String ,HessianClientContainer> map = _hessianClient.getLevel(); 93 HessianClientContainer client = null; 94 95 if (map != null) 96 client = map.get(serverId); 97 98 if (client == null) { 100 client = new HessianClientContainer(serverId); 101 if (map == null) 102 map = new Hashtable <String ,HessianClientContainer>(); 103 map.put(serverId, client); 104 _hessianClient.set(map); 105 } 106 107 return client; 108 } catch (Exception e) { 109 throw EJBExceptionWrapper.createRuntime(e); 110 } 111 } 112 113 118 protected EJBHome getHomeStub() 119 throws ConfigException 120 { 121 try { 122 HomeStub homeStub = (HomeStub) _homeStubClass.newInstance(); 123 124 homeStub._init(_serverId, this); 125 126 return homeStub; 127 } catch (IllegalAccessException e) { 128 throw new ConfigException(e); 129 } catch (InstantiationException e) { 130 throw new ConfigException(e); 131 } 132 } 133 134 141 protected EJBObject createObjectStub(String url) 142 throws ConfigException 143 { 144 try { 145 ObjectStub objStub = (ObjectStub) _remoteStubClass.newInstance(); 146 objStub._init(url, this); 147 return objStub; 148 } catch (IllegalAccessException e) { 149 throw new ConfigException(e); 150 } catch (InstantiationException e) { 151 throw new ConfigException(e); 152 } 153 } 154 155 HessianHomeHandle getHomeHandle() 156 { 157 return new HessianHomeHandle(_ejbHome, _serverId); 158 } 159 160 HessianHandle createHandle(String url) 161 { 162 return new HessianHandle(url); 163 } 164 165 public HandleEncoder getHandleEncoder() 166 { 167 try { 168 if (_handleEncoder == null) 169 _handleEncoder = new HessianHandleEncoder(null, _serverId, 170 getPrimaryKeyClass()); 171 172 return _handleEncoder; 173 } catch (Exception e) { 174 throw EJBExceptionWrapper.createRuntime(e); 175 } 176 } 177 178 181 Class getHomeStubClass() 182 throws ConfigException 183 { 184 if (_homeStubClass != null) 185 return _homeStubClass; 186 187 synchronized (this) { 188 if (_homeStubClass != null) 189 return _homeStubClass; 190 191 StubGenerator gen = new StubGenerator(); 192 193 _homeStubClass = gen.createHomeStub(getHomeClass()); 194 } 195 196 return _homeStubClass; 197 } 198 199 202 Class getRemoteStubClass() 203 throws ConfigException 204 { 205 if (_remoteStubClass != null) 206 return _remoteStubClass; 207 208 synchronized (this) { 209 if (_remoteStubClass != null) 210 return _remoteStubClass; 211 212 Class remoteClass = getRemoteClass(); 213 if (remoteClass == null) 214 return null; 215 216 StubGenerator gen = new StubGenerator(); 217 218 _remoteStubClass = gen.createObjectStub(remoteClass); 219 } 220 221 return _remoteStubClass; 222 } 223 224 228 Class getHomeClass() 229 throws ConfigException 230 { 231 if (_homeClass != null) 232 return _homeClass; 233 234 try { 235 synchronized (this) { 236 if (_homeClass != null) 237 return _homeClass; 238 239 String className = getHomeClassName(); 240 241 _homeClass = CauchoSystem.loadClass(className, false, null); 242 } 243 } catch (ClassNotFoundException e) { 244 throw new ConfigException(e); 245 } 246 247 return _homeClass; 248 } 249 250 253 String getHomeClassName() 254 throws ConfigException 255 { 256 AbstractServer server = EjbProtocolManager.getJVMServer(_serverId); 257 258 if (server != null) { 259 Class cl = server.getRemoteHomeClass(); 260 if (cl != null) 261 return cl.getName(); 262 else 263 throw new ConfigException(L.l("`{0}' has no remote interface.", 264 _serverId)); 265 } 266 267 try { 268 Path path = Vfs.lookup(_serverId); 269 270 return (String ) MetaStub.call(path, "_hessian_getAttribute", "home-class"); 271 } catch (Throwable e) { 272 throw new ConfigException(e); 273 } 274 } 275 276 280 Class getRemoteClass() 281 throws ConfigException 282 { 283 if (_remoteClass != null) 284 return _remoteClass; 285 286 try { 287 synchronized (this) { 288 if (_remoteClass != null) 289 return _remoteClass; 290 291 String className = getRemoteClassName(); 292 293 if (className == null || className.equals("null")) 294 return null; 295 296 _remoteClass = CauchoSystem.loadClass(className, false, null); 297 } 298 } catch (ClassNotFoundException e) { 299 throw new ConfigException(e); 300 } 301 302 return _remoteClass; 303 } 304 305 308 String getRemoteClassName() 309 throws ConfigException 310 { 311 AbstractServer server = EjbProtocolManager.getJVMServer(_serverId); 312 313 if (server != null) { 314 Class cl = server.getRemoteObjectClass(); 315 if (cl != null) 316 return cl.getName(); 317 else 318 throw new ConfigException(L.l("`{0}' has no remote interface.", 319 _serverId)); 320 } 321 322 try { 323 Path path = Vfs.lookup(_serverId); 324 325 return (String ) MetaStub.call(path, "_hessian_getAttribute", 326 "remote-class"); 327 } catch (Throwable e) { 328 throw new ConfigException(e); 329 } 330 } 331 332 336 Class getPrimaryKeyClass() 337 throws ConfigException 338 { 339 if (_primaryKeyClass != null) 340 return _primaryKeyClass; 341 342 try { 343 synchronized (this) { 344 if (_primaryKeyClass != null) 345 return _primaryKeyClass; 346 347 String className = getPrimaryKeyClassName(); 348 349 _primaryKeyClass = CauchoSystem.loadClass(className, false, null); 350 } 351 } catch (ClassNotFoundException e) { 352 throw new ConfigException(e); 353 } 354 355 return _primaryKeyClass; 356 } 357 358 361 String getPrimaryKeyClassName() 362 throws ConfigException 363 { 364 AbstractServer server = EjbProtocolManager.getJVMServer(_serverId); 365 366 if (server != null) { 367 Class cl = server.getPrimaryKeyClass(); 368 if (cl != null) 369 return cl.getName(); 370 else 371 throw new ConfigException(L.l("`{0}' has no remote interface.", 372 _serverId)); 373 } 374 375 try { 376 Path path = Vfs.lookup(_serverId); 377 378 return (String ) MetaStub.call(path, "_hessian_getAttribute", "primary-key-class"); 379 } catch (Throwable e) { 380 throw new ConfigException(e); 381 } 382 } 383 384 387 public Object lookup(String type, String url) 388 throws IOException 389 { 390 try { 391 Class api = CauchoSystem.loadClass(type); 392 393 return create(api, url); 394 } catch (Exception e) { 395 throw new IOException (String.valueOf(e)); 396 } 397 } 398 399 413 public Object create(Class api, String url) 414 throws Exception 415 { 416 StubGenerator gen = new StubGenerator(); 417 418 Class cl = gen.createStub(api); 419 420 HessianStub stub = (HessianStub) cl.newInstance(); 421 422 stub._init(url, this); 423 424 return stub; 425 } 426 427 430 String getBasicAuthentication() 431 { 432 return _basicAuth; 433 } 434 435 438 void setBasicAuthentication(String auth) 439 { 440 if (auth != null) 441 _basicAuth = "Basic " + auth; 442 else 443 _basicAuth = auth; 444 } 445 446 449 public String toString() 450 { 451 return "HessianClientContainer[" + _serverId + "]"; 452 } 453 } 454 | Popular Tags |