1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Valve;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.catalina.mbeans.MBeanUtils;
import org.apache.catalina.valves.ValveBase;
import org.apache.tomcat.util.ExceptionUtils;
/**
* Standard implementation of the <b>Host</b> interface. Each
* child container must be a Context implementation to process the
* requests directed to a particular web application.
*
* @author Craig R. McClanahan
* @author Remy Maucherat
*/
public class StandardHost extends ContainerBase implements Host {
private static final org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( StandardHost.class );
// ----------------------------------------------------------- Constructors
/**
* Create a new StandardHost component with the default basic Valve.
*/
public StandardHost() {
super();
pipeline.setBasic(new StandardHostValve());
}
// ----------------------------------------------------- Instance Variables
/**
* The set of aliases for this Host.
*/
private String[] aliases = new String[0];
private final Object aliasesLock = new Object();
/**
* The application root for this Host.
*/
private String appBase = "webapps";
/**
* The XML root for this Host.
*/
private String xmlBase = null;
/**
* The auto deploy flag for this Host.
*/
private boolean autoDeploy = true;
/**
* The Java class name of the default context configuration class
* for deployed web applications.
*/
private String configClass =
"org.apache.catalina.startup.ContextConfig";
/**
* The Java class name of the default Context implementation class for
* deployed web applications.
*/
private String contextClass =
"org.apache.catalina.core.StandardContext";
/**
* The deploy on startup flag for this Host.
*/
private boolean deployOnStartup = true;
/**
* deploy Context XML config files property.
*/
private boolean deployXML = !Globals.IS_SECURITY_ENABLED;
/**
* Should XML files be copied to
* $CATALINA_BASE/conf/<engine>/<host> by default when
* a web application is deployed?
*/
private boolean copyXML = false;
/**
* The Java class name of the default error reporter implementation class
* for deployed web applications.
*/
private String errorReportValveClass =
"org.apache.catalina.valves.ErrorReportValve";
/**
* The descriptive information string for this implementation.
*/
private static final String info =
"org.apache.catalina.core.StandardHost/1.0";
/**
* Unpack WARs property.
*/
private boolean unpackWARs = true;
/**
* Work Directory base for applications.
*/
private String workDir = null;
/**
* Should we create directories upon startup for appBase and xmlBase
*/
private boolean createDirs = true;
/**
* Track the class loaders for the child web applications so memory leaks
* can be detected.
*/
private Map<ClassLoader, String> childClassLoaders =
new WeakHashMap<ClassLoader, String>();
/**
* Any file or directory in {@link #appBase} that this pattern matches will
* be ignored by the automatic deployment process (both
* {@link #deployOnStartup} and {@link #autoDeploy}).
*/
private Pattern deployIgnore = null;
private boolean undeployOldVersions = false;
private boolean failCtxIfServletStartFails = false;
// ------------------------------------------------------------- Properties
@Override
public boolean getUndeployOldVersions() {
return undeployOldVersions;
}
@Override
public void setUndeployOldVersions(boolean undeployOldVersions) {
this.undeployOldVersions = undeployOldVersions;
}
@Override
public ExecutorService getStartStopExecutor() {
return startStopExecutor;
}
/**
* Return the application root for this Host. This can be an absolute
* pathname, a relative pathname, or a URL.
*/
@Override
public String getAppBase() {
return (this.appBase);
}
/**
* Set the application root for this Host. This can be an absolute
* pathname, a relative pathname, or a URL.
*
* @param appBase The new application root
*/
@Override
public void setAppBase(String appBase) {
String oldAppBase = this.appBase;
this.appBase = appBase;
support.firePropertyChange("appBase", oldAppBase, this.appBase);
}
/**
* Return the XML root for this Host. This can be an absolute
* pathname, a relative pathname, or a URL.
* If null, defaults to
* ${catalina.base}/conf/<engine name>/<host name> directory
*/
@Override
public String getXmlBase() {
return (this.xmlBase);
}
/**
* Set the Xml root for this Host. This can be an absolute
* pathname, a relative pathname, or a URL.
* If null, defaults to
* ${catalina.base}/conf/<engine name>/<host name> directory
*
* @param xmlBase The new XML root
*/
@Override
public void setXmlBase(String xmlBase) {
String oldXmlBase = this.xmlBase;
this.xmlBase = xmlBase;
support.firePropertyChange("xmlBase", oldXmlBase, this.xmlBase);
}
/**
* Returns true if the Host will attempt to create directories for appBase and xmlBase
* unless they already exist.
*/
@Override
public boolean getCreateDirs() {
return createDirs;
}
/**
* Set to true if the Host should attempt to create directories for xmlBase and appBase upon startup
* @param createDirs
*/
@Override
public void setCreateDirs(boolean createDirs) {
this.createDirs = createDirs;
}
/**
* Return the value of the auto deploy flag. If true, it indicates that
* this host's child webapps will be dynamically deployed.
*/
@Override
public boolean getAutoDeploy() {
return (this.autoDeploy);
}
/**
* Set the auto deploy flag value for this host.
*
* @param autoDeploy The new auto deploy flag
*/
@Override
public void setAutoDeploy(boolean autoDeploy) {
boolean oldAutoDeploy = this.autoDeploy;
this.autoDeploy = autoDeploy;
support.firePropertyChange("autoDeploy", oldAutoDeploy,
this.autoDeploy);
}
/**
* Return the Java class name of the context configuration class
* for new web applications.
*/
@Override
public String getConfigClass() {
return (this.configClass);
}
/**
* Set the Java class name of the context configuration class
* for new web applications.
*
* @param configClass The new context configuration class
*/
@Override
public void setConfigClass(String configClass) {
String oldConfigClass = this.configClass;
this.configClass = configClass;
support.firePropertyChange("configClass",
oldConfigClass, this.configClass);
}
/**
* Return the Java class name of the Context implementation class
* for new web applications.
*/
public String getContextClass() {
return (this.contextClass);
}
/**
* Set the Java class name of the Context implementation class
* for new web applications.
*
* @param contextClass The new context implementation class
*/
public void setContextClass(String contextClass) {
String oldContextClass = this.contextClass;
this.contextClass = contextClass;
support.firePropertyChange("contextClass",
oldContextClass, this.contextClass);
}
/**
* Return the value of the deploy on startup flag. If true, it indicates
* that this host's child webapps should be discovered and automatically
* deployed at startup time.
*/
@Override
public boolean getDeployOnStartup() {
return (this.deployOnStartup);
}
/**
* Set the deploy on startup flag value for this host.
*
* @param deployOnStartup The new deploy on startup flag
*/
@Override
public void setDeployOnStartup(boolean deployOnStartup) {
boolean oldDeployOnStartup = this.deployOnStartup;
this.deployOnStartup = deployOnStartup;
support.firePropertyChange("deployOnStartup", oldDeployOnStartup,
this.deployOnStartup);
}
/**
* Deploy XML Context config files flag accessor.
*/
public boolean isDeployXML() {
return (deployXML);
}
/**
* Deploy XML Context config files flag mutator.
*/
public void setDeployXML(boolean deployXML) {
this.deployXML = deployXML;
}
/**
* Return the copy XML config file flag for this component.
*/
public boolean isCopyXML() {
return (this.copyXML);
}
/**
* Set the copy XML config file flag for this component.
*
* @param copyXML The new copy XML flag
*/
public void setCopyXML(boolean copyXML) {
this.copyXML= copyXML;
}
/**
* Return the Java class name of the error report valve class
* for new web applications.
*/
public String getErrorReportValveClass() {
return (this.errorReportValveClass);
}
/**
* Set the Java class name of the error report valve class
* for new web applications.
*
* @param errorReportValveClass The new error report valve class
*/
public void setErrorReportValveClass(String errorReportValveClass) {
String oldErrorReportValveClassClass = this.errorReportValveClass;
this.errorReportValveClass = errorReportValveClass;
support.firePropertyChange("errorReportValveClass",
oldErrorReportValveClassClass,
this.errorReportValveClass);
}
/**
* Return the canonical, fully qualified, name of the virtual host
* this Container represents.
*/
@Override
public String getName() {
return (name);
}
/**
* Set the canonical, fully qualified, name of the virtual host
* this Container represents.
*
* @param name Virtual host name
*
* @exception IllegalArgumentException if name is null
*/
@Override
public void setName(String name) {
if (name == null)
throw new IllegalArgumentException
(sm.getString("standardHost.nullName"));
name = name.toLowerCase(Locale.ENGLISH); // Internally all names are lower case
String oldName = this.name;
this.name = name;
support.firePropertyChange("name", oldName, this.name);
}
/**
* Unpack WARs flag accessor.
*/
public boolean isUnpackWARs() {
return (unpackWARs);
}
/**
* Unpack WARs flag mutator.
*/
public void setUnpackWARs(boolean unpackWARs) {
this.unpackWARs = unpackWARs;
}
/**
* Host work directory base.
*/
public String getWorkDir() {
return (workDir);
}
/**
* Host work directory base.
*/
public void setWorkDir(String workDir) {
this.workDir = workDir;
}
/**
* Return the regular expression that defines the files and directories in
* the host's {@link #appBase} that will be ignored by the automatic
* deployment process.
*/
@Override
public String getDeployIgnore() {
if (deployIgnore == null) {
return null;
}
return this.deployIgnore.toString();
}
/**
* Return the compiled regular expression that defines the files and
* directories in the host's {@link #appBase} that will be ignored by the
* automatic deployment process.
*/
@Override
public Pattern getDeployIgnorePattern() {
return this.deployIgnore;
}
/**
* Set the regular expression that defines the files and directories in
* the host's {@link #appBase} that will be ignored by the automatic
* deployment process.
*/
@Override
public void setDeployIgnore(String deployIgnore) {
String oldDeployIgnore;
if (this.deployIgnore == null) {
oldDeployIgnore = null;
} else {
oldDeployIgnore = this.deployIgnore.toString();
}
if (deployIgnore == null) {
this.deployIgnore = null;
} else {
this.deployIgnore = Pattern.compile(deployIgnore);
}
support.firePropertyChange("deployIgnore",
oldDeployIgnore,
deployIgnore);
}
public boolean isFailCtxIfServletStartFails() {
return failCtxIfServletStartFails;
}
public void setFailCtxIfServletStartFails(
boolean failCtxIfServletStartFails) {
boolean oldFailCtxIfServletStartFails = this.failCtxIfServletStartFails;
this.failCtxIfServletStartFails = failCtxIfServletStartFails;
support.firePropertyChange("failCtxIfServletStartFails",
oldFailCtxIfServletStartFails,
failCtxIfServletStartFails);
}
// --------------------------------------------------------- Public Methods
/**
* Add an alias name that should be mapped to this same Host.
*
* @param alias The alias to be added
*/
@Override
public void addAlias(String alias) {
alias = alias.toLowerCase(Locale.ENGLISH);
synchronized (aliasesLock) {
// Skip duplicate aliases
for (int i = 0; i < aliases.length; i++) {
if (aliases[i].equals(alias))
return;
}
// Add this alias to the list
String newAliases[] = new String[aliases.length + 1];
for (int i = 0; i < aliases.length; i++)
newAliases[i] = aliases[i];
newAliases[aliases.length] = alias;
aliases = newAliases;
}
// Inform interested listeners
fireContainerEvent(ADD_ALIAS_EVENT, alias);
}
/**
* Add a child Container, only if the proposed child is an implementation
* of Context.
*
* @param child Child container to be added
*/
@Override
public void addChild(Container child) {
child.addLifecycleListener(new MemoryLeakTrackingListener());
if (!(child instanceof Context))
throw new IllegalArgumentException
(sm.getString("standardHost.notContext"));
super.addChild(child);
}
/**
* Used to ensure the regardless of {@link Context} implementation, a record
* is kept of the class loader used every time a context starts.
*/
private class MemoryLeakTrackingListener implements LifecycleListener {
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
if (event.getSource() instanceof Context) {
Context context = ((Context) event.getSource());
childClassLoaders.put(context.getLoader().getClassLoader(),
context.getServletContext().getContextPath());
}
}
}
}
/**
* Attempt to identify the contexts that have a class loader memory leak.
* This is usually triggered on context reload. Note: This method attempts
* to force a full garbage collection. This should be used with extreme
* caution on a production system.
*/
public String[] findReloadedContextMemoryLeaks() {
System.gc();
List<String> result = new ArrayList<String>();
for (Map.Entry<ClassLoader, String> entry :
childClassLoaders.entrySet()) {
ClassLoader cl = entry.getKey();
if (cl instanceof WebappClassLoader) {
if (!((WebappClassLoader) cl).isStarted()) {
result.add(entry.getValue());
}
}
}
return result.toArray(new String[result.size()]);
}
/**
* Return the set of alias names for this Host. If none are defined,
* a zero length array is returned.
*/
@Override
public String[] findAliases() {
synchronized (aliasesLock) {
return (this.aliases);
}
}
/**
* Return descriptive information about this Container implementation and
* the corresponding version number, in the format
* <code><description>/<version></code>.
*/
@Override
public String getInfo() {
return (info);
}
/**
* Remove the specified alias name from the aliases for this Host.
*
* @param alias Alias name to be removed
*/
@Override
public void removeAlias(String alias) {
alias = alias.toLowerCase(Locale.ENGLISH);
synchronized (aliasesLock) {
// Make sure this alias is currently present
int n = -1;
for (int i = 0; i < aliases.length; i++) {
if (aliases[i].equals(alias)) {
n = i;
break;
}
}
if (n < 0)
return;
// Remove the specified alias
int j = 0;
String results[] = new String[aliases.length - 1];
for (int i = 0; i < aliases.length; i++) {
if (i != n)
results[j++] = aliases[i];
}
aliases = results;
}
// Inform interested listeners
fireContainerEvent(REMOVE_ALIAS_EVENT, alias);
}
/**
* Return a String representation of this component.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (getParent() != null) {
sb.append(getParent().toString());
sb.append(".");
}
sb.append("StandardHost[");
sb.append(getName());
sb.append("]");
return (sb.toString());
}
/**
* Start this component and implement the requirements
* of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
@Override
protected synchronized void startInternal() throws LifecycleException {
// Set error report valve
String errorValve = getErrorReportValveClass();
if ((errorValve != null) && (!errorValve.equals(""))) {
try {
boolean found = false;
Valve[] valves = getPipeline().getValves();
for (Valve valve : valves) {
if (errorValve.equals(valve.getClass().getName())) {
found = true;
break;
}
}
if(!found) {
Valve valve =
(Valve) Class.forName(errorValve).newInstance();
getPipeline().addValve(valve);
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString(
"standardHost.invalidErrorReportValveClass",
errorValve), t);
}
}
super.startInternal();
}
// -------------------- JMX --------------------
/**
* Return the MBean Names of the Valves associated with this Host
*
* @exception Exception if an MBean cannot be created or registered
*/
public String [] getValveNames()
throws Exception
{
Valve [] valves = this.getPipeline().getValves();
String [] mbeanNames = new String[valves.length];
for (int i = 0; i < valves.length; i++) {
if( valves[i] == null ) continue;
if( ((ValveBase)valves[i]).getObjectName() == null ) continue;
mbeanNames[i] = ((ValveBase)valves[i]).getObjectName().toString();
}
return mbeanNames;
}
public String[] getAliases() {
synchronized (aliasesLock) {
return aliases;
}
}
@Override
protected String getObjectNameKeyProperties() {
StringBuilder keyProperties = new StringBuilder("type=Host");
keyProperties.append(MBeanUtils.getContainerKeyProperties(this));
return keyProperties.toString();
}
}
|