1 23 24 package com.sun.enterprise.web; 25 26 import com.sun.enterprise.web.stats.PWCRequestStatsImpl; 27 import java.io.File ; 28 import java.text.MessageFormat ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.ResourceBundle ; 32 import java.util.Set ; 33 import java.util.Vector ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 import org.apache.catalina.Container; 38 import org.apache.catalina.LifecycleException; 39 import org.apache.catalina.core.StandardHost; 40 41 import com.sun.enterprise.config.ConfigException; 42 import com.sun.enterprise.config.ConfigContext; 43 import com.sun.enterprise.config.serverbeans.Applications; 44 import com.sun.enterprise.config.serverbeans.J2eeApplication; 45 import com.sun.enterprise.config.serverbeans.HttpService; 46 import com.sun.enterprise.config.serverbeans.Server; 47 import com.sun.enterprise.config.serverbeans.ApplicationRef; 48 import com.sun.enterprise.config.serverbeans.WebModule; 50 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 51 import com.sun.enterprise.instance.WebModulesManager; 52 import com.sun.enterprise.server.ApplicationRegistry; 53 import com.sun.enterprise.util.StringUtils; 54 import com.sun.enterprise.util.io.FileUtils; 55 56 import com.sun.enterprise.server.ApplicationServer; 57 58 import com.sun.enterprise.Switch; 59 import com.sun.enterprise.deployment.Application; 60 import com.sun.enterprise.deployment.WebBundleDescriptor; 61 import com.sun.enterprise.deployment.util.DOLLoadingContextFactory; 62 import com.sun.logging.LogDomains; 63 64 68 69 public class VirtualServer extends StandardHost { 70 71 73 77 public VirtualServer() { 78 super(); 79 80 setPipeline(new VirtualServerPipeline(this)); 81 82 if (_logger == null) { 85 _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 86 _rb = _logger.getResourceBundle(); 87 } 88 _debug = _logger.isLoggable(Level.FINE); 89 } 90 91 93 96 private String _id = null; 97 98 101 protected static Logger _logger = null; 102 103 106 protected static ResourceBundle _rb = null; 107 108 116 protected boolean _debug = false; 117 118 121 private static final String _info = 122 "com.sun.enterprise.web.VirtualServer/1.0"; 123 124 127 private com.sun.enterprise.config.serverbeans.VirtualServer vsBean; 128 129 132 private MimeMap mimeMap; 133 134 140 private boolean allowLinking = true; 141 142 145 private String defaultWebXmlLocation; 146 147 148 private String [] cacheControls; 149 150 151 private boolean isActive; 153 154 155 158 private PWCRequestStatsImpl pwcRequestStatsImpl; 159 161 164 public String getID() { 165 return _id; 166 } 167 168 173 public void setID(String id) { 174 _id = id; 175 } 176 177 180 public boolean isActive() { 181 return isActive; 182 } 183 184 189 public void setIsActive(boolean isActive) { 190 this.isActive = isActive; 191 } 192 193 200 public String getDefaultWebXmlLocation() { 201 return defaultWebXmlLocation; 202 } 203 204 211 public void setDefaultWebXmlLocation(String defaultWebXmlLocation) { 212 this.defaultWebXmlLocation = defaultWebXmlLocation; 213 } 214 215 222 public boolean getAllowLinking() { 223 return allowLinking; 224 } 225 226 236 public void setAllowLinking(boolean allowLinking) { 237 this.allowLinking = allowLinking; 238 } 239 240 243 public com.sun.enterprise.config.serverbeans.VirtualServer getBean(){ 244 return vsBean; 245 } 246 247 250 public void setBean(com.sun.enterprise.config.serverbeans.VirtualServer vsBean){ 251 this.vsBean = vsBean; 252 } 253 254 257 public MimeMap getMimeMap(){ 258 return mimeMap; 259 } 260 261 264 public void setMimeMap(MimeMap mimeMap){ 265 this.mimeMap = mimeMap; 266 } 267 268 274 public String [] getCacheControls() { 275 return cacheControls; 276 } 277 278 284 public void setCacheControls(String [] cacheControls) { 285 this.cacheControls = cacheControls; 286 } 287 288 289 291 296 public String getInfo() { 297 return _info; 298 } 299 300 301 303 310 public synchronized void stop() throws LifecycleException { 311 312 Switch sw = Switch.getSwitch(); 315 Container children[] = findChildren(); 316 if (children != null) { 317 for (int i = 0; i < children.length; i++) { 318 sw.removeDescriptorFor(children[i]); 319 } 320 } 321 super.stop(); 322 } 323 324 326 333 protected List getWebModules(Server serverBean, String modulesRoot) { 334 335 List modules = new Vector (); 336 337 Applications appsBean = null; 340 try { 341 appsBean = ServerBeansFactory.getApplicationsBean(serverBean.getConfigContext()); 342 } catch (ConfigException e) { 343 String msg = _rb.getString("vs.appsConfigError"); 344 Object [] params = { getID() }; 345 msg = MessageFormat.format(msg, params); 346 _logger.log(Level.SEVERE, msg, e); 347 } 348 349 if (appsBean != null) { 350 WebModule[] wmBeans = appsBean.getWebModule(); 351 if (wmBeans != null && wmBeans.length > 0) { 352 for (int i = 0; i < wmBeans.length; i++) { 353 WebModule wm = wmBeans[i]; 354 if (isActive(wm)) { 355 ApplicationRef ref = 358 serverBean.getApplicationRefByRef(wm.getName()); 359 if (ref == null) { 360 continue; 361 } 362 363 String location = wm.getLocation(); 364 File moduleBase = new File (location); 368 if (!moduleBase.isAbsolute()) { 369 location = modulesRoot+File.separator+location; 370 wm.setLocation(location); 371 } 372 WebModuleConfig wmInfo = loadWebModuleConfig(wm); 373 if (wmInfo != null) 374 modules.add(wmInfo); 375 } else { 376 if (_debug) { 377 _logger.finer("Web Module [" + wm.getName() + 378 "] is not applicable for virtual " + 379 " server [" + getID() + "]"); 380 } 381 } 382 } 383 } 384 } 385 return modules; 386 } 387 388 408 protected WebModuleConfig getUserDefaultWebModuleConfig(Server serverBean) { 409 410 WebModuleConfig wmInfo = null; 411 Applications appsBean = null; 412 try{ 413 appsBean = ServerBeansFactory.getApplicationsBean(serverBean.getConfigContext()); 414 } catch (ConfigException e) { 415 String msg = _rb.getString("vs.appsConfigError"); 416 Object [] params = { getID() }; 417 msg = MessageFormat.format(msg, params); 418 _logger.log(Level.SEVERE, msg, e); 419 } 420 421 String wmID = getDefaultWebModuleID(); 422 if (wmID != null) { 423 wmInfo = findWebModuleInJ2eeApp(appsBean, wmID); 426 427 if (wmInfo == null) { 429 WebModule wm = appsBean.getWebModuleByName(wmID); 430 if (wm != null) { 431 if (isActive(wm, false)) { 432 WebModule wmCopy = new WebModule(); 438 wmCopy.setName(wm.getName()); 439 wmCopy.setLocation(wm.getLocation()); 440 wmCopy.setEnabled(wm.isEnabled()); 441 wmCopy.setContextRoot(wm.getContextRoot()); 442 wmInfo = loadWebModuleConfig(wmCopy); 444 wmInfo.setVirtualServers(_id); 445 wmCopy.setName(Constants.DEFAULT_WEB_MODULE_PREFIX + 446 wmCopy.getName()); 447 wmCopy.setContextRoot(""); 448 wmCopy.setObjectType(wm.getObjectType()); 449 } else { 450 Object [] params = { wmID, getID() }; 451 _logger.log(Level.SEVERE, "vs.defaultWebModuleDisabled", 452 params); 453 wm = null; 454 } 455 } 456 } else { 457 WebModule wm = wmInfo.getBean(); 458 wmInfo.setVirtualServers(_id); 459 wm.setName(Constants.DEFAULT_WEB_MODULE_PREFIX + wm.getName()); 460 wm.setContextRoot(""); 461 } 462 463 if (wmInfo == null) { 464 Object [] params = { wmID, getID() }; 465 _logger.log(Level.SEVERE, "vs.defaultWebModuleNotFound", 466 params); 467 } 468 } 469 return wmInfo; 470 } 471 472 483 protected WebModuleConfig createSystemDefaultWebModuleIfNecessary() { 484 485 WebModuleConfig wmInfo = null; 486 String docroot = getAppBase(); 491 if ((findChild("") == null) && (docroot != null)) { 492 wmInfo = new WebModuleConfig(); 493 WebModule wm = new WebModule(); 494 wm.setName(Constants.DEFAULT_WEB_MODULE_NAME); 495 wm.setContextRoot(""); 496 wm.setLocation(docroot); 497 wmInfo.setBean(wm); 498 499 wmInfo.setDescriptor(DOLLoadingContextFactory.getDefaultWebBundleDescriptor()); 500 501 WebBundleDescriptor wbd = wmInfo.getDescriptor(); 502 if ( wbd.getApplication() == null ) { 503 Application application = new Application(); 504 application.setVirtual(true); 505 application.setName(Constants.DEFAULT_WEB_MODULE_NAME); 506 wbd.setApplication(application); 507 } 508 509 } 510 return wmInfo; 511 } 512 513 514 522 protected WebModuleConfig loadWebModuleConfig(WebModule wm) { 523 524 WebModuleConfig wmInfo = new WebModuleConfig(); 525 wmInfo.setBean(wm); 526 String wmID = wm.getName(); 527 String location = wm.getLocation(); 528 try { 529 WebModulesManager webModulesManager = new WebModulesManager( 530 ApplicationServer.getServerContext().getInstanceEnvironment()); 531 Application app = webModulesManager.getDescriptor(wmID, location); 532 WebBundleDescriptor wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor(); 533 wmInfo.setDescriptor(wbd); 534 } catch (ConfigException ce) { 535 wmInfo = null; 536 String msg = _rb.getString("vs.moduleConfigError"); 537 Object [] params = { wmID, getID() }; 538 msg = MessageFormat.format(msg, params); 539 _logger.log(Level.SEVERE, msg, ce); 540 } 541 return wmInfo; 542 } 543 544 548 private boolean isActive(WebModule wm) { 549 return isActive(wm, true); 550 } 551 552 578 protected boolean isActive(WebModule wm, boolean matchVSID) { 579 580 String vsID = getID(); 581 582 boolean active = ((vsID != null) && (vsID.length() > 0)); 583 active &= (wm != null); 584 585 if (active) { 586 active &= wm.isEnabled(); 588 589 String vsIDs = getVirtualServers(wm.getName()); 598 599 if (getID().equals(WebContainer.ADMIN_VS) && matchVSID 606 && ((vsIDs == null) || (vsIDs.length() == 0 ))) { 607 return false; 608 } 609 610 611 if ((vsIDs != null) && matchVSID) { 612 List vsList = StringUtils.parseStringList(vsIDs, " ,"); 613 if (vsList != null) 614 active &= vsList.contains(vsID.trim()); 615 else 616 active &= true; 617 } else 618 active &= true; 619 } 620 return active; 621 } 622 623 630 protected String getDefaultWebModuleID() { 631 String wmID = null; 632 633 if (vsBean != null) { 634 wmID = vsBean.getDefaultWebModule(); 635 if (wmID != null && _debug) { 636 Object [] params = { wmID, _id }; 637 _logger.log(Level.FINE, "vs.defaultWebModule", params); 638 } 639 } else { 640 _logger.log(Level.SEVERE, "vs.configError", _id); 641 } 642 643 return wmID; 644 } 645 646 656 protected WebModuleConfig findWebModuleInJ2eeApp(Applications appsBean, 657 String id) { 658 WebModuleConfig wmInfo = null; 659 660 int length = id.length(); 661 int separatorIndex = id.indexOf(Constants.NAME_SEPARATOR); 663 if (separatorIndex == -1) { 664 separatorIndex = id.indexOf('#'); 666 } 667 if (separatorIndex != -1) { 668 String appID = id.substring(0, separatorIndex); 669 String moduleID = id.substring(separatorIndex + 1); 670 J2eeApplication j2eeApp = appsBean.getJ2eeApplicationByName(appID); 671 if ((j2eeApp != null) && j2eeApp.isEnabled()) { 672 String location = j2eeApp.getLocation(); 673 String moduleDir = FileUtils.makeFriendlyFilename(moduleID); 674 ApplicationRegistry registry = 675 ApplicationRegistry.getInstance(); 676 ClassLoader appLoader = 677 registry.getClassLoaderForApplication(appID); 678 if (appLoader != null) { 679 Application appDesc = registry.getApplication(appLoader); 680 if (appDesc != null) { 681 Set wbds = appDesc.getWebBundleDescriptors(); 682 WebBundleDescriptor wbd = null; 683 for (Iterator itr = wbds.iterator(); itr.hasNext(); ) { 684 wbd = (WebBundleDescriptor) itr.next(); 685 String webUri = wbd.getModuleDescriptor().getArchiveUri(); 686 if (moduleID.equals(webUri)) { 687 StringBuffer dir = new StringBuffer (location); 688 dir.append(File.separator); 689 dir.append(moduleDir); 690 WebModule wm = new WebModule(); 691 wm.setName(moduleID); 692 wm.setContextRoot(wbd.getContextRoot()); 693 wm.setLocation(dir.toString()); 694 wm.setEnabled(true); 695 String vsList = getVirtualServers(j2eeApp.getName()); 696 wmInfo = new WebModuleConfig(); 697 wmInfo.setBean(wm); 698 wmInfo.setDescriptor(wbd); 699 wmInfo.setParentLoader(appLoader); 700 wmInfo.setVirtualServers(vsList); 701 break; 702 } 703 } 704 } 705 } 706 } else { 707 Object [] params = { id, getID() }; 708 _logger.log(Level.SEVERE, "vs.defaultWebModuleDisabled", 709 params); 710 } 711 } 712 return wmInfo; 713 } 714 715 724 private String getVirtualServers(String appName) { 725 String ret = null; 726 try { 727 ConfigContext ctx = 728 ApplicationServer.getServerContext().getConfigContext(); 729 ret = ServerBeansFactory 730 .getVirtualServersByAppName(ctx, appName); 731 } catch (ConfigException ce) { 732 _logger.log(Level.FINE, "Cannot get virtual server for " + appName, ce); 733 } 734 735 return ret; 736 } 737 738 739 742 public void clearAliases(){ 743 aliases = new String [0]; 744 } 745 746 747 void setIsDisabled(boolean isDisabled) { 748 ((VirtualServerPipeline) getPipeline()).setIsDisabled(isDisabled); 749 } 750 751 752 void setIsOff(boolean isOff) { 753 ((VirtualServerPipeline) getPipeline()).setIsOff(isOff); 754 } 755 756 757 766 void addRedirect(String from, String url, String urlPrefix, 767 boolean escape) { 768 769 ((VirtualServerPipeline) getPipeline()).addRedirect( 770 from, url, urlPrefix, escape); 771 } 772 773 774 777 public void setPWCRequestStatsImpl(PWCRequestStatsImpl pwcRequestStatsImpl){ 778 this.pwcRequestStatsImpl = pwcRequestStatsImpl; 779 } 780 781 782 785 public PWCRequestStatsImpl getPWCRequestStatsImpl(){ 786 return pwcRequestStatsImpl; 787 } 788 } 789 | Popular Tags |