1 28 29 package com.caucho.ejb.burlap; 30 31 import com.caucho.burlap.io.BurlapRemoteResolver; 32 import com.caucho.config.ConfigException; 33 import com.caucho.ejb.AbstractServer; 34 import com.caucho.ejb.EJBExceptionWrapper; 35 import com.caucho.ejb.protocol.EjbProtocolManager; 36 import com.caucho.ejb.protocol.HandleEncoder; 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 48 52 class BurlapClientContainer implements BurlapRemoteResolver { 53 protected static L10N L = new L10N(BurlapClientContainer.class); 54 55 private static EnvironmentLocal burlapClient = 56 new EnvironmentLocal("caucho.burlap.client"); 57 58 private String serverId; 59 private BurlapHandleEncoder handleEncoder; 60 EJBHome ejbHome; 62 63 Class homeClass; 64 Class remoteClass; 65 Class homeStubClass; 67 Class remoteStubClass; 69 Class primaryKeyClass; 71 72 private String _basicAuth; 73 74 79 BurlapClientContainer(String serverId) 80 throws ConfigException 81 { 82 this.serverId = serverId; 83 84 remoteStubClass = getRemoteStubClass(); 85 homeStubClass = getHomeStubClass(); 86 } 87 88 static BurlapClientContainer find(String serverId) 89 { 90 try { 91 Hashtable map = (Hashtable ) burlapClient.getLevel(); 92 BurlapClientContainer client = null; 93 94 if (map != null) 95 client = (BurlapClientContainer) map.get(serverId); 96 97 if (client == null) { 99 client = new BurlapClientContainer(serverId); 100 if (map == null) 101 map = new Hashtable (); 102 map.put(serverId, client); 103 burlapClient.set(map); 104 } 105 106 return client; 107 } catch (Exception e) { 108 throw EJBExceptionWrapper.createRuntime(e); 109 } 110 } 111 112 117 protected EJBHome getHomeStub() 118 throws ConfigException 119 { 120 try { 121 HomeStub homeStub = (HomeStub) homeStubClass.newInstance(); 122 123 homeStub._init(serverId, this); 124 125 return homeStub; 126 } catch (IllegalAccessException e) { 127 throw new ConfigException(e); 128 } catch (InstantiationException e) { 129 throw new ConfigException(e); 130 } 131 } 132 133 140 protected EJBObject createObjectStub(String url) 141 throws ConfigException 142 { 143 try { 144 ObjectStub objStub = (ObjectStub) remoteStubClass.newInstance(); 145 objStub._init(url, this); 146 return objStub; 147 } catch (IllegalAccessException e) { 148 throw new ConfigException(e); 149 } catch (InstantiationException e) { 150 throw new ConfigException(e); 151 } 152 } 153 154 BurlapHomeHandle getHomeHandle() 155 { 156 return new BurlapHomeHandle(ejbHome, serverId); 157 } 158 159 BurlapHandle createHandle(String url) 160 { 161 return new BurlapHandle(url); 162 } 163 164 public HandleEncoder getHandleEncoder() 165 { 166 try { 167 if (handleEncoder == null) 168 handleEncoder = new BurlapHandleEncoder(null, serverId, 169 getPrimaryKeyClass()); 170 171 return handleEncoder; 172 } catch (Exception e) { 173 throw EJBExceptionWrapper.createRuntime(e); 174 } 175 } 176 177 180 Class getHomeStubClass() 181 throws ConfigException 182 { 183 if (homeStubClass != null) 184 return homeStubClass; 185 186 synchronized (this) { 187 if (homeStubClass != null) 188 return homeStubClass; 189 190 StubGenerator gen = new StubGenerator(); 191 192 homeStubClass = gen.createHomeStub(getHomeClass()); 193 } 194 195 return homeStubClass; 196 } 197 198 201 Class getRemoteStubClass() 202 throws ConfigException 203 { 204 if (remoteStubClass != null) 205 return remoteStubClass; 206 207 synchronized (this) { 208 if (remoteStubClass != null) 209 return remoteStubClass; 210 211 Class remoteClass = getRemoteClass(); 212 if (remoteClass == null) 213 return null; 214 215 StubGenerator gen = new StubGenerator(); 216 217 remoteStubClass = gen.createObjectStub(remoteClass); 218 } 219 220 return remoteStubClass; 221 } 222 223 227 Class getHomeClass() 228 throws ConfigException 229 { 230 if (homeClass != null) 231 return homeClass; 232 233 try { 234 synchronized (this) { 235 if (homeClass != null) 236 return homeClass; 237 238 String className = getHomeClassName(); 239 240 homeClass = CauchoSystem.loadClass(className, false, null); 241 } 242 } catch (ClassNotFoundException e) { 243 throw new ConfigException(e); 244 } 245 246 return homeClass; 247 } 248 249 252 String getHomeClassName() 253 throws ConfigException 254 { 255 AbstractServer server = EjbProtocolManager.getJVMServer(serverId); 256 257 if (server != null) { 258 Class cl = server.getRemoteHomeClass(); 259 if (cl != null) 260 return cl.getName(); 261 else 262 throw new ConfigException(L.l("`{0}' has no remote interface.", 263 serverId)); 264 } 265 266 try { 267 Path path = Vfs.lookup(serverId); 268 269 return (String ) MetaStub.call(path, "_burlap_getAttribute", "java.home.class"); 270 } catch (Throwable e) { 271 throw new ConfigException(e); 272 } 273 } 274 275 279 Class getRemoteClass() 280 throws ConfigException 281 { 282 if (remoteClass != null) 283 return remoteClass; 284 285 try { 286 synchronized (this) { 287 if (remoteClass != null) 288 return remoteClass; 289 290 String className = getRemoteClassName(); 291 292 if (className == null || className.equals("null")) 293 return null; 294 295 remoteClass = CauchoSystem.loadClass(className, false, null); 296 } 297 } catch (ClassNotFoundException e) { 298 throw new ConfigException(e); 299 } 300 301 return remoteClass; 302 } 303 304 307 String getRemoteClassName() 308 throws ConfigException 309 { 310 AbstractServer server = EjbProtocolManager.getJVMServer(serverId); 311 312 if (server != null) { 313 Class cl = server.getRemoteObjectClass(); 314 if (cl != null) 315 return cl.getName(); 316 else 317 throw new ConfigException(L.l("`{0}' has no remote interface.", 318 serverId)); 319 } 320 321 try { 322 Path path = Vfs.lookup(serverId); 323 324 return (String ) MetaStub.call(path, "_burlap_getAttribute", 325 "java.object.class"); 326 } catch (Throwable e) { 327 throw new ConfigException(e); 328 } 329 } 330 331 335 Class getPrimaryKeyClass() 336 throws ConfigException 337 { 338 if (primaryKeyClass != null) 339 return primaryKeyClass; 340 341 try { 342 synchronized (this) { 343 if (primaryKeyClass != null) 344 return primaryKeyClass; 345 346 String className = getPrimaryKeyClassName(); 347 348 primaryKeyClass = CauchoSystem.loadClass(className, false, null); 349 } 350 } catch (ClassNotFoundException e) { 351 throw new ConfigException(e); 352 } 353 354 return primaryKeyClass; 355 } 356 357 360 String getPrimaryKeyClassName() 361 throws ConfigException 362 { 363 AbstractServer server = EjbProtocolManager.getJVMServer(serverId); 364 365 if (server != null) { 366 Class cl = server.getPrimaryKeyClass(); 367 if (cl != null) 368 return cl.getName(); 369 else 370 throw new ConfigException(L.l("`{0}' has no remote interface.", 371 serverId)); 372 } 373 374 try { 375 Path path = Vfs.lookup(serverId); 376 377 return (String ) MetaStub.call(path, "_burlap_getAttribute", "primary-key-class"); 378 } catch (Throwable e) { 379 throw new ConfigException(e); 380 } 381 } 382 383 386 public Object lookup(String type, String url) 387 throws IOException 388 { 389 try { 390 Class api = CauchoSystem.loadClass(type); 391 392 return create(api, url); 393 } catch (Exception e) { 394 throw new IOException (String.valueOf(e)); 395 } 396 } 397 398 412 public Object create(Class api, String url) 413 throws Exception 414 { 415 StubGenerator gen = new StubGenerator(); 416 gen.setClassDir(CauchoSystem.getWorkPath()); 417 418 Class cl = gen.createStub(api); 419 420 BurlapStub stub = (BurlapStub) 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 "BurlapClientContainer[" + serverId + "]"; 452 } 453 } 454 | Popular Tags |