1 29 30 package com.caucho.server.e_app; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.ejb.EJBServer; 34 import com.caucho.java.WorkDir; 35 import com.caucho.lifecycle.Lifecycle; 36 import com.caucho.loader.Environment; 37 import com.caucho.loader.EnvironmentBean; 38 import com.caucho.loader.EnvironmentClassLoader; 39 import com.caucho.log.Log; 40 import com.caucho.server.deploy.EnvironmentDeployInstance; 41 import com.caucho.server.webapp.WebAppContainer; 42 import com.caucho.server.webapp.WebAppController; 43 import com.caucho.util.L10N; 44 import com.caucho.vfs.Depend; 45 import com.caucho.vfs.JarPath; 46 import com.caucho.vfs.Path; 47 import com.caucho.vfs.Vfs; 48 49 import javax.annotation.PostConstruct; 50 import java.util.ArrayList ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 57 public class EnterpriseApplication 58 implements EnvironmentBean, EnvironmentDeployInstance 59 { 60 static final L10N L = new L10N(EnterpriseApplication.class); 61 static final Logger log = Log.open(EnterpriseApplication.class); 62 63 67 68 private EnvironmentClassLoader _loader; 69 70 private String _name; 71 72 private String _ejbServerJndiName = "java:comp/env/cmp"; 73 74 private Path _rootDir; 75 76 private Path _earPath; 77 78 private String _prefix = ""; 79 80 private EarDeployController _controller; 81 82 private Path _webappsPath; 83 84 private ApplicationConfig _config; 85 86 private WebAppContainer _container; 87 88 private boolean _hasModule; 89 90 92 private ArrayList <Path> _ejbPaths 93 = new ArrayList <Path>(); 94 95 private ArrayList <WebAppController> _webApps 96 = new ArrayList <WebAppController>(); 97 98 private Throwable _configException; 99 100 private final Lifecycle _lifecycle; 101 102 105 EnterpriseApplication(WebAppContainer container, 106 EarDeployController controller, String name) 107 { 108 _container = container; 109 110 _controller = controller; 111 _name = name; 112 113 ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); 114 115 _loader = new EnvironmentClassLoader(container.getClassLoader()); 116 _loader.setId("EnterpriseApplication[" + name + "]"); 117 118 _webappsPath = _controller.getRootDirectory().lookup("webapps"); 119 WorkDir.setLocalWorkDir(_controller.getRootDirectory().lookup("META-INF/work"), 120 _loader); 121 122 _lifecycle = new Lifecycle(log, toString(), Level.INFO); 123 124 if (controller.getArchivePath() != null) 125 Environment.addDependency(new Depend(controller.getArchivePath()), _loader); 126 } 127 128 131 public void setName(String name) 132 { 133 _name = name; 134 _loader.setId("EnterpriseApplication[" + name + "]"); 135 } 136 137 140 public String getName() 141 { 142 return _name; 143 } 144 145 148 public void setEjbServerJndiName(String name) 149 { 150 _ejbServerJndiName = name; 151 } 152 153 156 public void setRootDirectory(Path rootDir) 157 { 158 _rootDir = rootDir; 159 } 160 161 164 public Path getRootDirectory() 165 { 166 return _rootDir; 167 } 168 169 172 public EnvironmentClassLoader getClassLoader() 173 { 174 return _loader; 175 } 176 177 180 public void setEnvironmentClassLoader(EnvironmentClassLoader loader) 181 { 182 _loader = loader; 183 } 184 185 188 public void setEarPath(Path earPath) 189 { 190 _earPath = earPath; 191 } 192 193 196 public void setWebapps(Path webappsPath) 197 { 198 _webappsPath = webappsPath; 199 } 200 201 204 public void setPrefix(String prefix) 205 { 206 _prefix = prefix; 207 } 208 209 212 public void setId(String id) 213 { 214 } 215 216 219 public void setVersion(String version) 220 { 221 } 222 223 226 public void setSchemaLocation(String schema) 227 { 228 } 229 230 233 public void setDisplayName(String name) 234 { 235 } 236 237 240 public void setDescription(String description) 241 { 242 } 243 244 247 public void setIcon(Icon icon) 248 { 249 } 250 251 254 public Module createModule() 255 { 256 _hasModule = true; 257 258 return new Module(); 259 } 260 261 264 public void addSecurityRole(SecurityRole role) 265 { 266 } 267 268 271 public boolean isModified() 272 { 273 return _loader.isModified(); 274 } 275 276 279 public boolean isModifiedNow() 280 { 281 return _loader.isModifiedNow(); 282 } 283 284 287 public boolean isDeployError() 288 { 289 return _configException != null; 290 } 291 292 295 public boolean isDeployIdle() 296 { 297 return false; 298 } 299 300 303 public void setConfigException(Throwable e) 304 { 305 _configException = e; 306 307 for (WebAppController controller : _webApps) { 308 controller.setConfigException(e); 309 } 310 } 311 312 315 public Throwable getConfigException() 316 { 317 return _configException; 318 } 319 320 323 @PostConstruct 324 public void init() 325 throws Exception 326 { 327 if (! _lifecycle.toInit()) 328 return; 329 330 log.fine(this + " initializing"); 331 332 Vfs.setPwd(_rootDir, _loader); 333 334 fillDefaultModules(); 336 337 if (_ejbPaths.size() != 0) { 338 EJBServer ejbServer = EJBServer.getLocal(); 339 340 if (ejbServer == null) 341 throw new ConfigException(L.l("Expected configured <ejb-server> in " + 342 Thread.currentThread().getContextClassLoader())); 343 344 for (Path path : _ejbPaths) 345 ejbServer.addEJBJar(path); 346 347 Path ejbJar = _rootDir.lookup("META-INF/ejb-jar.xml"); 348 if (ejbJar.canRead()) { 349 ejbServer.addEJBDescriptor("META-INF/ejb-jar.xml"); 350 } 351 352 ejbServer.initEJBs(); 353 } 354 355 if (_container != null) 357 _container.clearCache(); 358 } 359 360 private void fillDefaultModules() 361 { 362 try { 363 if (_rootDir.lookup("lib").isDirectory()) { 364 Path lib = _rootDir.lookup("lib"); 365 366 for (String file : lib.list()) { 367 if (file.endsWith(".jar")) { 368 _loader.addJar(lib.lookup(file)); 369 } 370 } 371 } 372 373 for (String file : _rootDir.list()) { 374 if (file.endsWith(".jar")) { 375 Path path = _rootDir.lookup(file); 376 Path jar = JarPath.create(path); 377 378 if (jar.lookup("META-INF/application-client.xml").canRead()) { 379 } 381 else if (jar.lookup("META-INF/ejb-jar.xml").canRead()) { 382 _ejbPaths.add(path); 383 384 _loader.addJar(path); 385 _loader.addJarManifestClassPath(path); 386 } 387 else { 388 _ejbPaths.add(path); 389 390 _loader.addJar(path); 391 } 392 } 393 else if (file.endsWith(".war")) { 394 Module module = createModule(); 395 WebModule web = new WebModule(); 396 web.setWebURI(file); 397 web.setContextRoot(file.substring(0, file.length() - 4)); 398 399 module.addWeb(web); 400 } 401 } 402 } catch (RuntimeException e) { 403 throw e; 404 } catch (Exception e) { 405 throw new ConfigException(e); 406 } 407 } 408 409 412 public void start() 413 { 414 if (! _lifecycle.toStarting()) 415 return; 416 417 Thread thread = Thread.currentThread(); 418 ClassLoader oldLoader = thread.getContextClassLoader(); 419 420 try { 421 thread.setContextClassLoader(getClassLoader()); 422 423 getClassLoader().start(); 424 425 436 437 for (WebAppController webApp : _webApps) { 438 _container.getWebAppGenerator().update(webApp.getContextPath()); 439 } 440 } finally { 441 _lifecycle.toActive(); 442 443 thread.setContextClassLoader(oldLoader); 444 } 445 } 446 447 450 public WebAppController findWebAppEntry(String name) 451 { 452 for (int i = 0; i < _webApps.size(); i++) { 453 WebAppController controller = _webApps.get(i); 454 455 if (controller.isNameMatch(name)) 456 return controller; 457 } 458 459 return null; 460 } 461 462 private void addDepend(Path path) 463 { 464 _loader.addDependency(new com.caucho.vfs.Depend(path)); 465 } 466 467 470 public ArrayList <WebAppController> getApplications() 471 { 472 return _webApps; 473 } 474 475 478 public void stop() 479 { 480 if (! _lifecycle.toStopping()) 481 return; 482 483 Thread thread = Thread.currentThread(); 484 ClassLoader oldLoader = thread.getContextClassLoader(); 485 486 try { 487 thread.setContextClassLoader(_loader); 488 489 491 _loader.stop(); 492 493 } finally { 495 _lifecycle.toStop(); 496 497 thread.setContextClassLoader(oldLoader); 498 } 499 } 500 501 504 public void destroy() 505 { 506 stop(); 507 508 if (! _lifecycle.toDestroy()) 509 return; 510 511 Thread thread = Thread.currentThread(); 512 ClassLoader oldLoader = thread.getContextClassLoader(); 513 514 try { 515 thread.setContextClassLoader(getClassLoader()); 516 517 log.fine(this + " destroying"); 518 519 ArrayList <WebAppController> webApps = _webApps; 520 _webApps = null; 521 522 if (webApps != null) { 523 for (WebAppController webApp : webApps) { 524 _container.getWebAppGenerator().update(webApp.getContextPath()); 525 } 526 } 527 } finally { 528 thread.setContextClassLoader(oldLoader); 529 530 _loader.destroy(); 531 532 log.fine(this + " destroyed"); 533 } 534 } 535 536 public String toString() 537 { 538 return "EnterpriseApplication[" + getName() + "]"; 539 } 540 541 public class Module { 542 545 public void setId(String id) 546 { 547 } 548 549 552 public void addWeb(WebModule web) 553 throws Exception 554 { 555 String webUri = web.getWebURI(); 556 String contextUrl = web.getContextRoot(); 557 Path path = _rootDir.lookup(webUri); 558 559 if (contextUrl == null) 560 contextUrl = webUri; 561 562 WebAppController controller = null; 563 if (webUri.endsWith(".war")) { 564 String name = webUri.substring(0, webUri.length() - 4); 566 int p = name.lastIndexOf('/'); 567 if (p > 0) 568 name = name.substring(p + 1); 569 570 if (contextUrl.equals("")) 572 contextUrl = "/" + name; 573 574 if (contextUrl.endsWith(".war")) 575 contextUrl = contextUrl.substring(0, contextUrl.length() - 4); 576 577 Path expandPath = _webappsPath; 578 expandPath.mkdirs(); 579 580 controller = new WebAppController(contextUrl, 581 expandPath.lookup(name), 582 _container); 583 584 controller.setArchivePath(path); 585 } else { 586 if (contextUrl.equals("")) { 588 String name = webUri; 589 int p = name.lastIndexOf('/'); 590 if (p > 0) 591 name = name.substring(p + 1); 592 contextUrl = "/" + name; 593 } 594 595 if (contextUrl.endsWith(".war")) 597 contextUrl = contextUrl.substring(0, contextUrl.length() - 4); 598 599 controller = new WebAppController(contextUrl, path, _container); 600 } 601 602 controller.setDynamicDeploy(true); 603 if (_configException != null) 604 controller.setConfigException(_configException); 605 606 controller.setManifestClassLoader(_loader); 607 608 if (findWebAppEntry(controller.getContextPath()) == null) 610 _webApps.add(controller); 611 else 612 controller = findWebAppEntry(controller.getContextPath()); 613 614 if (web.getWebApp() != null) 615 controller.addConfigDefault(web.getWebApp()); 616 } 617 618 621 public void addEjb(Path path) 622 throws Exception 623 { 624 _ejbPaths.add(path); 625 626 _loader.addJar(path); 627 _loader.addJarManifestClassPath(path); 629 } 630 631 634 public void addJava(Path path) 635 throws ConfigException 636 { 637 if (! path.canRead()) 638 throw new ConfigException(L.l("<java> module {0} must be a valid path.", 639 path)); 640 } 641 642 645 public void addConnector(String path) 646 { 647 } 648 649 652 public void addAltDD(String path) 653 { 654 } 655 } 656 } 657 | Popular Tags |