| 1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 33 import javax.management.MBeanRegistration ; 34 import javax.management.MBeanServer ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import javax.naming.directory.DirContext ; 38 import javax.servlet.ServletException ; 39 40 import org.apache.catalina.Cluster; 41 import org.apache.catalina.Container; 42 import org.apache.catalina.ContainerEvent; 43 import org.apache.catalina.ContainerListener; 44 import org.apache.catalina.Lifecycle; 45 import org.apache.catalina.LifecycleException; 46 import org.apache.catalina.LifecycleListener; 47 import org.apache.catalina.Loader; 48 import org.apache.catalina.Manager; 49 import org.apache.catalina.Pipeline; 50 import org.apache.catalina.Realm; 51 import org.apache.catalina.Valve; 52 import org.apache.catalina.connector.Request; 53 import org.apache.catalina.connector.Response; 54 import org.apache.catalina.util.LifecycleSupport; 55 import org.apache.catalina.util.StringManager; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.naming.resources.ProxyDirContext; 59 import org.apache.tomcat.util.modeler.Registry; 60 61 62 121 122 public abstract class ContainerBase 123 implements Container, Lifecycle, Pipeline, MBeanRegistration , Serializable { 124 125 private static org.apache.commons.logging.Log log= 126 org.apache.commons.logging.LogFactory.getLog( ContainerBase.class ); 127 128 134 protected class PrivilegedAddChild 135 implements PrivilegedAction { 136 137 private Container child; 138 139 PrivilegedAddChild(Container child) { 140 this.child = child; 141 } 142 143 public Object run() { 144 addChildInternal(child); 145 return null; 146 } 147 148 } 149 150 151 153 154 157 protected HashMap children = new HashMap (); 158 159 160 163 protected int backgroundProcessorDelay = -1; 164 165 166 169 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 170 171 172 175 protected ArrayList listeners = new ArrayList (); 176 177 178 181 protected Loader loader = null; 182 183 184 187 protected Log logger = null; 188 189 190 193 protected String logName = null; 194 195 196 199 protected Manager manager = null; 200 201 202 205 protected Cluster cluster = null; 206 207 208 211 protected String name = null; 212 213 214 217 protected Container parent = null; 218 219 220 223 protected ClassLoader parentClassLoader = null; 224 225 226 229 protected Pipeline pipeline = new StandardPipeline(this); 230 231 232 235 protected Realm realm = null; 236 237 238 241 protected DirContext resources = null; 242 243 244 247 protected static StringManager sm = 248 StringManager.getManager(Constants.Package); 249 250 251 254 protected boolean started = false; 255 256 protected boolean initialized=false; 257 258 261 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 262 263 264 267 private Thread thread = null; 268 269 270 273 private boolean threadDone = false; 274 275 276 278 279 288 public int getBackgroundProcessorDelay() { 289 return backgroundProcessorDelay; 290 } 291 292 293 300 public void setBackgroundProcessorDelay(int delay) { 301 backgroundProcessorDelay = delay; 302 } 303 304 305 310 public String getInfo() { 311 return this.getClass().getName(); 312 } 313 314 315 320 public Loader getLoader() { 321 322 if (loader != null) 323 return (loader); 324 if (parent != null) 325 return (parent.getLoader()); 326 return (null); 327 328 } 329 330 331 336 public synchronized void setLoader(Loader loader) { 337 338 Loader oldLoader = this.loader; 340 if (oldLoader == loader) 341 return; 342 this.loader = loader; 343 344 if (started && (oldLoader != null) && 346 (oldLoader instanceof Lifecycle)) { 347 try { 348 ((Lifecycle) oldLoader).stop(); 349 } catch (LifecycleException e) { 350 log.error("ContainerBase.setLoader: stop: ", e); 351 } 352 } 353 354 if (loader != null) 356 loader.setContainer(this); 357 if (started && (loader != null) && 358 (loader instanceof Lifecycle)) { 359 try { 360 ((Lifecycle) loader).start(); 361 } catch (LifecycleException e) { 362 log.error("ContainerBase.setLoader: start: ", e); 363 } 364 } 365 366 support.firePropertyChange("loader", oldLoader, this.loader); 368 369 } 370 371 372 377 public Log getLogger() { 378 379 if (logger != null) 380 return (logger); 381 logger = LogFactory.getLog(logName()); 382 return (logger); 383 384 } 385 386 387 392 public Manager getManager() { 393 394 if (manager != null) 395 return (manager); 396 if (parent != null) 397 return (parent.getManager()); 398 return (null); 399 400 } 401 402 403 408 public synchronized void setManager(Manager manager) { 409 410 Manager oldManager = this.manager; 412 if (oldManager == manager) 413 return; 414 this.manager = manager; 415 416 if (started && (oldManager != null) && 418 (oldManager instanceof Lifecycle)) { 419 try { 420 ((Lifecycle) oldManager).stop(); 421 } catch (LifecycleException e) { 422 log.error("ContainerBase.setManager: stop: ", e); 423 } 424 } 425 426 if (manager != null) 428 manager.setContainer(this); 429 if (started && (manager != null) && 430 (manager instanceof Lifecycle)) { 431 try { 432 ((Lifecycle) manager).start(); 433 } catch (LifecycleException e) { 434 log.error("ContainerBase.setManager: start: ", e); 435 } 436 } 437 438 support.firePropertyChange("manager", oldManager, this.manager); 440 441 } 442 443 444 447 public Object getMappingObject() { 448 return this; 449 } 450 451 452 457 public Cluster getCluster() { 458 if (cluster != null) 459 return (cluster); 460 461 if (parent != null) 462 return (parent.getCluster()); 463 464 return (null); 465 } 466 467 468 473 public synchronized void setCluster(Cluster cluster) { 474 Cluster oldCluster = this.cluster; 476 if (oldCluster == cluster) 477 return; 478 this.cluster = cluster; 479 480 if (started && (oldCluster != null) && 482 (oldCluster instanceof Lifecycle)) { 483 try { 484 ((Lifecycle) oldCluster).stop(); 485 } catch (LifecycleException e) { 486 log.error("ContainerBase.setCluster: stop: ", e); 487 } 488 } 489 490 if (cluster != null) 492 cluster.setContainer(this); 493 494 if (started && (cluster != null) && 495 (cluster instanceof Lifecycle)) { 496 try { 497 ((Lifecycle) cluster).start(); 498 } catch (LifecycleException e) { 499 log.error("ContainerBase.setCluster: start: ", e); 500 } 501 } 502 503 support.firePropertyChange("cluster", oldCluster, this.cluster); 505 } 506 507 508 513 public String getName() { 514 515 return (name); 516 517 } 518 519 520 531 public void setName(String name) { 532 533 String oldName = this.name; 534 this.name = name; 535 support.firePropertyChange("name", oldName, this.name); 536 } 537 538 539 543 public Container getParent() { 544 545 return (parent); 546 547 } 548 549 550 561 public void setParent(Container container) { 562 563 Container oldParent = this.parent; 564 this.parent = container; 565 support.firePropertyChange("parent", oldParent, this.parent); 566 567 } 568 569 570 575 public ClassLoader getParentClassLoader() { 576 if (parentClassLoader != null) 577 return (parentClassLoader); 578 if (parent != null) { 579 return (parent.getParentClassLoader()); 580 } 581 return (ClassLoader.getSystemClassLoader()); 582 583 } 584 585 586 595 public void setParentClassLoader(ClassLoader parent) { 596 ClassLoader oldParentClassLoader = this.parentClassLoader; 597 this.parentClassLoader = parent; 598 support.firePropertyChange("parentClassLoader", oldParentClassLoader, 599 this.parentClassLoader); 600 601 } 602 603 604 608 public Pipeline getPipeline() { 609 610 return (this.pipeline); 611 612 } 613 614 615 620 public Realm getRealm() { 621 622 if (realm != null) 623 return (realm); 624 if (parent != null) 625 return (parent.getRealm()); 626 return (null); 627 628 } 629 630 631 636 public synchronized void setRealm(Realm realm) { 637 638 Realm oldRealm = this.realm; 640 if (oldRealm == realm) 641 return; 642 this.realm = realm; 643 644 if (started && (oldRealm != null) && 646 (oldRealm instanceof Lifecycle)) { 647 try { 648 ((Lifecycle) oldRealm).stop(); 649 } catch (LifecycleException e) { 650 log.error("ContainerBase.setRealm: stop: ", e); 651 } 652 } 653 654 if (realm != null) 656 realm.setContainer(this); 657 if (started && (realm != null) && 658 (realm instanceof Lifecycle)) { 659 try { 660 ((Lifecycle) realm).start(); 661 } catch (LifecycleException e) { 662 log.error("ContainerBase.setRealm: start: ", e); 663 } 664 } 665 666 support.firePropertyChange("realm", oldRealm, this.realm); 668 669 } 670 671 672 678 public DirContext getResources() { 679 if (resources != null) 680 return (resources); 681 if (parent != null) 682 return (parent.getResources()); 683 return (null); 684 685 } 686 687 688 694 public synchronized void setResources(DirContext resources) { 695 699 DirContext oldResources = this.resources; 701 if (oldResources == resources) 702 return; 703 Hashtable env = new Hashtable (); 704 if (getParent() != null) 705 env.put(ProxyDirContext.HOST, getParent().getName()); 706 env.put(ProxyDirContext.CONTEXT, getName()); 707 this.resources = new ProxyDirContext(env, resources); 708 support.firePropertyChange("resources", oldResources, this.resources); 710 711 } 712 713 714 716 717 734 public void addChild(Container child) { 735 if (System.getSecurityManager() != null) { 736 PrivilegedAction dp = 737 new PrivilegedAddChild(child); 738 AccessController.doPrivileged(dp); 739 } else { 740 addChildInternal(child); 741 } 742 } 743 744 private void addChildInternal(Container child) { 745 746 if( log.isDebugEnabled() ) 747 log.debug("Add child " + child + " " + this); 748 synchronized(children) { 749 if (children.get(child.getName()) != null) 750 throw new IllegalArgumentException ("addChild: Child name '" + 751 child.getName() + 752 "' is not unique"); 753 child.setParent(this); children.put(child.getName(), child); 755 756 if (started && (child instanceof Lifecycle)) { 758 boolean success = false; 759 try { 760 ((Lifecycle) child).start(); 761 success = true; 762 } catch (LifecycleException e) { 763 log.error("ContainerBase.addChild: start: ", e); 764 throw new IllegalStateException  765 ("ContainerBase.addChild: start: " + e); 766 } finally { 767 if (!success) { 768 children.remove(child.getName()); 769 } 770 } 771 } 772 773 fireContainerEvent(ADD_CHILD_EVENT, child); 774 } 775 776 } 777 778 779 784 public void addContainerListener(ContainerListener listener) { 785 786 synchronized (listeners) { 787 listeners.add(listener); 788 } 789 790 } 791 792 793 798 public void addPropertyChangeListener(PropertyChangeListener listener) { 799 800 support.addPropertyChangeListener(listener); 801 802 } 803 804 805 811 public Container findChild(String name) { 812 813 if (name == null) 814 return (null); 815 synchronized (children) { return ((Container) children.get(name)); 817 } 818 819 } 820 821 822 826 public Container[] findChildren() { 827 828 synchronized (children) { 829 Container results[] = new Container[children.size()]; 830 return ((Container[]) children.values().toArray(results)); 831 } 832 833 } 834 835 836 841 public ContainerListener[] findContainerListeners() { 842 843 synchronized (listeners) { 844 ContainerListener[] results = 845 new ContainerListener[listeners.size()]; 846 return ((ContainerListener[]) listeners.toArray(results)); 847 } 848 849 } 850 851 852 867 public void invoke(Request request, Response response) 868 throws IOException , ServletException { 869 870 pipeline.getFirst().invoke(request, response); 871 872 } 873 874 875 881 public void removeChild(Container child) { 882 883 synchronized(children) { 884 if (children.get(child.getName()) == null) 885 return; 886 children.remove(child.getName()); 887 } 888 889 if (started && (child instanceof Lifecycle)) { 890 try { 891 if( child instanceof ContainerBase ) { 892 if( ((ContainerBase)child).started ) { 893 ((Lifecycle) child).stop(); 894 } 895 } else { 896 ((Lifecycle) child).stop(); 897 } 898 } catch (LifecycleException e) { 899 log.error("ContainerBase.removeChild: stop: ", e); 900 } 901 } 902 903 fireContainerEvent(REMOVE_CHILD_EVENT, child); 904 905 907 } 908 909 910 915 public void removeContainerListener(ContainerListener listener) { 916 917 synchronized (listeners) { 918 listeners.remove(listener); 919 } 920 921 } 922 923 924 929 public void removePropertyChangeListener(PropertyChangeListener listener) { 930 931 support.removePropertyChangeListener(listener); 932 933 } 934 935 936 938 939 944 public void addLifecycleListener(LifecycleListener listener) { 945 946 lifecycle.addLifecycleListener(listener); 947 948 } 949 950 951 955 public LifecycleListener[] findLifecycleListeners() { 956 957 return lifecycle.findLifecycleListeners(); 958 959 } 960 961 962 967 public void removeLifecycleListener(LifecycleListener listener) { 968 969 lifecycle.removeLifecycleListener(listener); 970 971 } 972 973 974 980 public synchronized void start() throws |