1 29 30 package com.caucho.server.e_app; 31 32 import com.caucho.config.Config; 33 import com.caucho.config.ConfigException; 34 import com.caucho.config.types.EjbRef; 35 import com.caucho.ejb.AbstractStubLoader; 36 import com.caucho.ejb.EJBClientInterface; 37 import com.caucho.java.WorkDir; 38 import com.caucho.lifecycle.Lifecycle; 39 import com.caucho.loader.Environment; 40 import com.caucho.loader.EnvironmentBean; 41 import com.caucho.loader.EnvironmentClassLoader; 42 import com.caucho.log.Log; 43 import com.caucho.naming.Jndi; 44 import com.caucho.server.deploy.DeployInstance; 45 import com.caucho.util.Alarm; 46 import com.caucho.util.L10N; 47 import com.caucho.vfs.Depend; 48 import com.caucho.vfs.JarPath; 49 import com.caucho.vfs.Path; 50 import com.caucho.vfs.Vfs; 51 52 import javax.annotation.PostConstruct; 53 import javax.naming.InitialContext ; 54 import javax.rmi.PortableRemoteObject ; 55 import java.lang.reflect.Method ; 56 import java.util.ArrayList ; 57 import java.util.HashMap ; 58 import java.util.Hashtable ; 59 import java.util.jar.Manifest ; 60 import java.util.logging.Level ; 61 import java.util.logging.Logger ; 62 63 66 public class EntAppClient implements DeployInstance, EnvironmentBean { 67 68 69 static final L10N L = new L10N(EntAppClient.class); 70 static final Logger log = Log.open(EntAppClient.class); 71 72 private EnvironmentClassLoader _loader; 73 74 private String _name; 75 76 private Path _rootDir; 77 78 private Path _archivePath; 79 80 private String _mainClass; 81 82 private String _prefix = ""; 83 84 private AppClientDeployController _entry; 85 86 private AbstractStubLoader _stubLoader; 87 88 private ApplicationConfig _config; 89 90 private JarPath _clientJar; 91 92 private EJBClientInterface _ejbClient; 93 94 private AppClientConfig _appClientConfig; 95 96 private ArrayList <EjbLink> _links = new ArrayList <EjbLink>(); 97 98 private HashMap <String ,EjbRef> _ejbRefMap = new HashMap <String ,EjbRef>(); 99 100 private Throwable _configException; 101 102 private Alarm _alarm; 103 private final Lifecycle _lifecycle; 104 105 private static void log(String message) 106 { 107 System.out.println(EntAppClient.class.getSimpleName() + ": " + message); 108 } 109 110 113 EntAppClient(AppClientDeployController entry, String name) 114 { 115 _entry = entry; 116 _name = name; 117 118 ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); 119 120 _loader = new EnvironmentClassLoader(parentLoader); 121 _loader.setId("EntAppClient[" + name + "]"); 123 124 _lifecycle = new Lifecycle(log, toString(), Level.INFO); 125 126 if (entry.getArchivePath() != null) 127 Environment.addDependency(new Depend(entry.getArchivePath()), _loader); 128 129 } 131 132 135 public void setName(String name) 136 { 137 _name = name; 138 _loader.setId("EntAppClient[" + name + "]"); 139 } 140 141 144 public String getName() 145 { 146 return _name; 147 } 148 149 152 public EJBClientInterface getEJBClient() 153 throws ClassNotFoundException , 154 InstantiationException , 155 IllegalAccessException 156 { 157 if (_ejbClient == null) { 158 Class cl = Class.forName("com.caucho.iiop.IiopClient"); 159 _ejbClient = (EJBClientInterface) cl.newInstance(); 160 } 161 162 return _ejbClient; 163 } 164 165 166 169 public void setRootDirectory(Path rootDir) 170 { 171 _rootDir = rootDir; 172 } 173 174 177 public Path getRootDirectory() 178 { 179 return _rootDir; 180 } 181 182 185 public void setArchivePath(Path archivePath) 186 { 187 _archivePath = archivePath; 188 } 189 190 193 public void setDescription(String description) 194 { 195 } 196 197 200 public void setDisplayName(String displayName) 201 { 202 } 203 204 207 public Module createModule() 208 { 209 return new Module(); 210 } 211 212 215 public void addEjbRef(EjbRef ejbRef) 216 { 217 _ejbRefMap.put(ejbRef.getEjbRefName(), ejbRef); 218 219 addIiopStub(ejbRef.getHome()); 220 addIiopStub(ejbRef.getRemote()); 221 } 222 223 private void addIiopStub(Class stubClass) 224 { 225 if (stubClass == null) 226 return; 227 228 try { 229 if (_stubLoader == null) { 230 Class iiopClass = Class.forName("com.caucho.iiop.IiopStubLoader"); 231 _stubLoader = (AbstractStubLoader) iiopClass.newInstance(); 232 _stubLoader.setPath(WorkDir.getLocalWorkDir()); 233 _loader.addLoader(_stubLoader); 234 } 235 236 _stubLoader.addStubClass(stubClass.getName()); 237 } catch (Throwable e) { 238 e.printStackTrace(); 239 240 log.info(e.toString()); 241 } 242 } 243 244 247 Class getEjbHome(String ejbName) 248 throws ConfigException 249 { 250 EjbRef ref = _ejbRefMap.get(ejbName); 251 252 if (ref != null) 253 return ref.getHome(); 254 else 255 return null; 256 } 257 258 261 public void setMainClass(String mainClass) 262 { 263 _mainClass = mainClass; 264 } 265 266 269 public EnvironmentClassLoader getClassLoader() 270 { 271 return _loader; 272 } 273 274 277 public void setEnvironmentClassLoader(EnvironmentClassLoader loader) 278 { 279 _loader = loader; 280 } 281 282 285 public void setConfigException(Throwable e) 286 { 287 _configException = e; 288 } 289 290 293 public Throwable getConfigException() 294 { 295 return _configException; 296 } 297 298 301 public EjbLink createEjbLink() 302 { 303 EjbLink link = new EjbLink(); 304 305 _links.add(link); 306 307 return link; 308 } 309 310 313 public SecurityRole createSecurityRole() 314 { 315 return new SecurityRole(); 316 } 317 318 321 public void setSchemaLocation(String s) 322 { 323 } 324 325 328 public void setVersion(String s) 329 { 330 } 331 332 335 @PostConstruct 336 public void init() 337 { 338 log("INIT: " + _links); 339 if (! _lifecycle.toInit()) 340 return; 341 342 Thread thread = Thread.currentThread(); 343 ClassLoader oldLoader = thread.getContextClassLoader(); 344 345 try { 346 thread.setContextClassLoader(_loader); 347 348 Path rootDir = getRootDirectory(); 349 Vfs.setPwd(rootDir); 350 351 Path workDir = getRootDirectory().lookup("META-INF/work"); 352 _loader.addJar(workDir); 353 354 WorkDir.setLocalWorkDir(workDir); 355 356 for (EjbLink link : _links) { 357 link.deploy(); 358 } 359 360 } catch (Throwable e) { 362 e.printStackTrace(); 363 log.log(Level.WARNING, e.toString(), e); 364 365 _configException = e; 366 } finally { 367 thread.setContextClassLoader(oldLoader); 368 } 369 } 370 371 private void configResinBinding() 372 throws Exception 373 { 374 Path rootDir = getRootDirectory(); 375 376 Path xml = rootDir.lookup("META-INF/resin-client.xml"); 377 378 if (! xml.canRead()) 379 return; 380 381 383 385 new Config().configure(this, xml); 386 } 387 388 391 public boolean isModified() 392 { 393 return false; 394 } 395 396 399 public boolean isModifiedNow() 400 { 401 return false; 402 } 403 404 407 public boolean isDeployError() 408 { 409 return _configException != null; 410 } 411 412 415 public boolean isDeployIdle() 416 { 417 return false; 418 } 419 420 423 public void start() 424 { 425 init(); 426 427 if (! _lifecycle.toActive()) 428 return; 429 } 430 431 434 public void main(String []args) 435 throws Throwable 436 { 437 if (_mainClass == null) 438 throw new IllegalStateException (L.l("main() method require a main class")); 439 440 main(_mainClass, args); 441 } 442 443 446 public void main(String mainClassName, String []args) 447 throws Throwable 448 { 449 start(); 450 451 log("MAIN: " + mainClassName); 452 log("C: " + System.getProperty("java.class.path")); 453 454 if (_configException != null) 455 throw _configException; 456 457 if (! _lifecycle.isActive()) 458 throw new IllegalStateException (L.l("{0} is not active.", 459 this)); 460 461 Thread thread = Thread.currentThread(); 462 ClassLoader oldLoader = thread.getContextClassLoader(); 463 464 try { 465 thread.setContextClassLoader(_loader); 466 467 Class mainClass = Class.forName(mainClassName, false, _loader); 468 469 log("MAIN:"); 470 Method main = mainClass.getMethod("main", 471 new Class [] { String [].class }); 472 473 try { 474 Class cl = Class.forName("com.sun.ts.lib.implementation.sun.common.SunRIURL", false, _loader); 475 log("CL: " + cl); 476 } catch (Throwable e) { 477 e.printStackTrace(); 478 } 479 480 main.invoke(null, new Object [] { args}); 481 } finally { 482 thread.setContextClassLoader(oldLoader); 483 } 484 } 485 486 489 public void destroy() 490 { 491 if (! _lifecycle.toDestroy()) 492 return; 493 } 494 495 public String toString() 496 { 497 return "EntAppClient[" + getName() + "]"; 498 } 499 500 public class EjbLink { 501 private String _ejbName; 502 private String _jndiName; 503 private Class _api; 504 505 public void setEjbName(String ejbName) 506 throws ConfigException 507 { 508 _ejbName = ejbName; 509 } 510 511 public void setJndiName(String jndiName) 512 { 513 _jndiName = jndiName; 514 } 515 516 public void deploy() 517 throws Exception 518 { 519 log("LINK: " + _jndiName + " " + _ejbName); 520 String orbHost = System.getProperty("org.omg.CORBA.ORBInitialHost"); 521 String orbPort = System.getProperty("org.omg.CORBA.ORBInitialPort"); 522 523 Hashtable env = new Hashtable (); 524 env.put("java.naming.factory.initial", 525 "com.sun.jndi.cosnaming.CNCtxFactory"); 526 env.put("java.naming.provider.url", "iiop://" + orbHost + ":" + orbPort); 527 javax.naming.Context ic = new InitialContext (env); 528 529 Object ior = ic.lookup(_jndiName); 530 531 _api = getEjbHome(_ejbName); 532 533 if (_api == null) 534 throw new ConfigException(L.l("'{0}' is an unknown ejb name.", 535 _ejbName)); 536 537 Object value = PortableRemoteObject.narrow(ior, _api); 538 539 log("VALUE: " + value + " " + value.getClass() + " " + _api); 540 Jndi.rebindDeepShort(_ejbName, value); 541 } 542 } 543 544 public class SecurityRole { 545 public void setId(String id) 546 { 547 } 548 549 public void addDescription(String description) 550 { 551 } 552 553 public void setRoleName(String roleName) 554 { 555 } 556 557 public void setRoleLink(String roleLink) 558 { 559 } 560 } 561 562 public class Module { 563 566 public void setId(String id) 567 { 568 } 569 570 573 public void addWeb(WebModule web) 574 throws Exception 575 { 576 } 577 578 581 public void addEjb(Path path) 582 throws Exception 583 { 584 getClassLoader().addJar(path); 585 586 getEJBClient().addEJBJar(path); 587 588 getEJBClient().initEJBs(); 589 } 590 591 594 public void addJava(Path path) 595 throws Exception 596 { 597 if (! path.canRead()) 598 throw new ConfigException(L.l("<java> module {0} must be a valid path.", 599 path)); 600 601 602 getClassLoader().addJar(path); 603 604 _clientJar = JarPath.create(path); 605 606 Manifest manifest = _clientJar.getManifest(); 607 String mainClass = manifest.getMainAttributes().getValue("Main-Class"); 608 609 setMainClass(mainClass); 610 611 Path appClient = _clientJar.lookup("META-INF/application-client.xml"); 612 613 if (appClient.canRead()) 614 new Config().configureBean(EntAppClient.this, appClient); 615 } 616 617 620 public void addConnector(String path) 621 { 622 } 623 624 627 public void addAltDD(String path) 628 { 629 } 630 } 631 } 632 | Popular Tags |