1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.InetAddress ; 25 import java.util.HashMap ; 26 27 import org.apache.catalina.Authenticator; 28 import org.apache.catalina.Container; 29 import org.apache.catalina.Context; 30 import org.apache.catalina.Engine; 31 import org.apache.catalina.Host; 32 import org.apache.catalina.Lifecycle; 33 import org.apache.catalina.LifecycleException; 34 import org.apache.catalina.LifecycleListener; 35 import org.apache.catalina.Loader; 36 import org.apache.catalina.Realm; 37 import org.apache.catalina.Valve; 38 import org.apache.catalina.connector.Connector; 39 import org.apache.catalina.core.StandardContext; 40 import org.apache.catalina.core.StandardEngine; 41 import org.apache.catalina.core.StandardHost; 42 import org.apache.catalina.core.StandardService; 43 import org.apache.catalina.loader.WebappLoader; 44 import org.apache.catalina.security.SecurityConfig; 45 import org.apache.catalina.util.LifecycleSupport; 46 import org.apache.catalina.util.StringManager; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.apache.tomcat.util.IntrospectionUtils; 50 import org.apache.tomcat.util.log.SystemLogHandler; 51 52 53 103 104 public class Embedded extends StandardService implements Lifecycle { 105 private static Log log = LogFactory.getLog(Embedded.class); 106 107 109 110 113 public Embedded() { 114 115 this(null); 116 117 } 118 119 120 126 public Embedded(Realm realm) { 127 128 super(); 129 setRealm(realm); 130 setSecurityProtection(); 131 132 } 133 134 135 137 138 141 protected boolean useNaming = true; 142 143 144 147 protected boolean redirectStreams = true; 148 149 150 154 protected Engine engines[] = new Engine[0]; 155 156 157 160 protected HashMap authenticators; 161 162 163 166 protected static final String info = 167 "org.apache.catalina.startup.Embedded/1.0"; 168 169 170 173 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 174 175 176 180 protected Realm realm = null; 181 182 183 186 protected static StringManager sm = 187 StringManager.getManager(Constants.Package); 188 189 190 193 protected boolean started = false; 194 195 198 protected boolean await = false; 199 200 201 203 204 207 public boolean isUseNaming() { 208 209 return (this.useNaming); 210 211 } 212 213 214 219 public void setUseNaming(boolean useNaming) { 220 221 boolean oldUseNaming = this.useNaming; 222 this.useNaming = useNaming; 223 support.firePropertyChange("useNaming", new Boolean (oldUseNaming), 224 new Boolean (this.useNaming)); 225 226 } 227 228 229 232 public boolean isRedirectStreams() { 233 234 return (this.redirectStreams); 235 236 } 237 238 239 244 public void setRedirectStreams(boolean redirectStreams) { 245 246 boolean oldRedirectStreams = this.redirectStreams; 247 this.redirectStreams = redirectStreams; 248 support.firePropertyChange("redirectStreams", new Boolean (oldRedirectStreams), 249 new Boolean (this.redirectStreams)); 250 251 } 252 253 254 257 public Realm getRealm() { 258 259 return (this.realm); 260 261 } 262 263 264 269 public void setRealm(Realm realm) { 270 271 Realm oldRealm = this.realm; 272 this.realm = realm; 273 support.firePropertyChange("realm", oldRealm, this.realm); 274 275 } 276 277 public void setAwait(boolean b) { 278 await = b; 279 } 280 281 public boolean isAwait() { 282 return await; 283 } 284 285 public void setCatalinaHome( String s ) { 286 System.setProperty( "catalina.home", s); 287 } 288 289 public void setCatalinaBase( String s ) { 290 System.setProperty( "catalina.base", s); 291 } 292 293 public String getCatalinaHome() { 294 return System.getProperty("catalina.home"); 295 } 296 297 public String getCatalinaBase() { 298 return System.getProperty("catalina.base"); 299 } 300 301 302 304 312 public synchronized void addConnector(Connector connector) { 313 314 if( log.isDebugEnabled() ) { 315 log.debug("Adding connector (" + connector.getInfo() + ")"); 316 } 317 318 if (engines.length < 1) 320 throw new IllegalStateException 321 (sm.getString("embedded.noEngines")); 322 323 327 super.addConnector(connector); 328 } 329 330 331 336 public synchronized void addEngine(Engine engine) { 337 338 if( log.isDebugEnabled() ) 339 log.debug("Adding engine (" + engine.getInfo() + ")"); 340 341 Engine results[] = new Engine[engines.length + 1]; 343 for (int i = 0; i < engines.length; i++) 344 results[i] = engines[i]; 345 results[engines.length] = engine; 346 engines = results; 347 348 if (started && (engine instanceof Lifecycle)) { 350 try { 351 ((Lifecycle) engine).start(); 352 } catch (LifecycleException e) { 353 log.error("Engine.start", e); 354 } 355 } 356 357 this.container = engine; 358 } 359 360 361 371 public Connector createConnector(InetAddress address, int port, 372 boolean secure) { 373 return createConnector(address != null? address.toString() : null, 374 port, secure); 375 } 376 377 public Connector createConnector(String address, int port, 378 boolean secure) { 379 String protocol = "http"; 380 if (secure) { 381 protocol = "https"; 382 } 383 384 return createConnector(address, port, protocol); 385 } 386 387 388 public Connector createConnector(InetAddress address, int port, 389 String protocol) { 390 return createConnector(address != null? address.toString() : null, 391 port, protocol); 392 } 393 394 public Connector createConnector(String address, int port, 395 String protocol) { 396 397 Connector connector = null; 398 399 if (address != null) { 400 406 int index = address.indexOf('/'); 407 if (index != -1) { 408 address = address.substring(index + 1); 409 } 410 } 411 412 if (log.isDebugEnabled()) { 413 log.debug("Creating connector for address='" + 414 ((address == null) ? "ALL" : address) + 415 "' port='" + port + "' protocol='" + protocol + "'"); 416 } 417 418 try { 419 420 if (protocol.equals("ajp")) { 421 connector = new Connector("org.apache.jk.server.JkCoyoteHandler"); 422 } else if (protocol.equals("memory")) { 423 connector = new Connector("org.apache.coyote.memory.MemoryProtocolHandler"); 424 } else if (protocol.equals("http")) { 425 connector = new Connector(); 426 } else if (protocol.equals("https")) { 427 connector = new Connector(); 428 connector.setScheme("https"); 429 connector.setSecure(true); 430 } 432 433 if (address != null) { 434 IntrospectionUtils.setProperty(connector, "address", 435 "" + address); 436 } 437 IntrospectionUtils.setProperty(connector, "port", "" + port); 438 439 } catch (Exception e) { 440 log.error("Couldn't create connector."); 441 } 442 443 return (connector); 444 445 } 446 447 470 public Context createContext(String path, String docBase) { 471 472 if( log.isDebugEnabled() ) 473 log.debug("Creating context '" + path + "' with docBase '" + 474 docBase + "'"); 475 476 StandardContext context = new StandardContext(); 477 478 context.setDocBase(docBase); 479 context.setPath(path); 480 481 ContextConfig config = new ContextConfig(); 482 config.setCustomAuthenticators(authenticators); 483 ((Lifecycle) context).addLifecycleListener(config); 484 485 return (context); 486 487 } 488 489 490 495 public Engine createEngine() { 496 497 if( log.isDebugEnabled() ) 498 log.debug("Creating engine"); 499 500 StandardEngine engine = new StandardEngine(); 501 502 engine.setRealm(realm); 505 return (engine); 506 507 } 508 509 510 536 public Host createHost(String name, String appBase) { 537 538 if( log.isDebugEnabled() ) 539 log.debug("Creating host '" + name + "' with appBase '" + 540 appBase + "'"); 541 542 StandardHost host = new StandardHost(); 543 544 host.setAppBase(appBase); 545 host.setName(name); 546 547 return (host); 548 549 } 550 551 552 559 public Loader createLoader(ClassLoader parent) { 560 561 if( log.isDebugEnabled() ) 562 log.debug("Creating Loader with parent class loader '" + 563 parent + "'"); 564 565 WebappLoader loader = new WebappLoader(parent); 566 return (loader); 567 568 } 569 570 571 576 public String getInfo() { 577 578 return (info); 579 580 } 581 582 583 590 public synchronized void removeContext(Context context) { 591 592 if( log.isDebugEnabled() ) 593 log.debug("Removing context[" + context.getPath() + "]"); 594 595 boolean found = false; 597 for (int i = 0; i < engines.length; i++) { 598 Container hosts[] = engines[i].findChildren(); 599 for (int j = 0; j < hosts.length; j++) { 600 Container contexts[] = hosts[j].findChildren(); 601 for (int k = 0; k < contexts.length; k++) { 602 if (context == (Context) contexts[k]) { 603 found = true; 604 break; 605 } 606 } 607 if (found) 608 break; 609 } 610 if (found) 611 break; 612 } 613 if (!found) 614 return; 615 616 if( log.isDebugEnabled() ) 618 log.debug(" Removing this Context"); 619 context.getParent().removeChild(context); 620 621 } 622 623 624 631 public synchronized void removeEngine(Engine engine) { 632 633 if( log.isDebugEnabled() ) 634 log.debug("Removing engine (" + engine.getInfo() + ")"); 635 636 int j = -1; 638 for (int i = 0; i < engines.length; i++) { 639 if (engine == engines[i]) { 640 j = i; 641 break; 642 } 643 } 644 if (j < 0) 645 return; 646 647 if( log.isDebugEnabled() ) 649 log.debug(" Removing related Containers"); 650 while (true) { 651 int n = -1; 652 for (int i = 0; i < connectors.length; i++) { 653 if (connectors[i].getContainer() == (Container) engine) { 654 n = i; 655 break; 656 } 657 } 658 if (n < 0) 659 break; 660 removeConnector(connectors[n]); 661 } 662 663 if (engine instanceof Lifecycle) { 665 if( log.isDebugEnabled() ) 666 log.debug(" Stopping this Engine"); 667 try { 668 ((Lifecycle) engine).stop(); 669 } catch (LifecycleException e) { 670 log.error("Engine.stop", e); 671 } 672 } 673 674 if( log.isDebugEnabled() ) 676 log.debug(" Removing this Engine"); 677 int k = 0; 678 Engine results[] = new Engine[engines.length - 1]; 679 for (int i = 0; i < engines.length; i++) { 680 if (i != j) 681 results[k++] = engines[i]; 682 } 683 engines = results; 684 685 } 686 687 688 695 public synchronized void removeHost(Host host) { 696 697 if( log.isDebugEnabled() ) 698 log.debug("Removing host[" + host.getName() + "]"); 699 700 boolean found = false; 702 for (int i = 0; i < engines.length; i++) { 703 Container hosts[] = engines[i].findChildren(); 704 for (int j = 0; j < hosts.length; j++) { 705 if (host == (Host) hosts[j]) { 706 found = true; 707 break; 708 709 } 710 } 711 if (found) 712 break; 713 } 714 if (!found) 715 return; 716 717 if( log.isDebugEnabled() ) 719 log.debug(" Removing this Host"); 720 host.getParent().removeChild(host); 721 722 } 723 724 725 737 public void addAuthenticator(Authenticator authenticator, 738 String loginMethod) { 739 if (!(authenticator instanceof Valve)) { 740 throw new IllegalArgumentException ( 741 sm.getString("embedded.authenticatorNotInstanceOfValve")); 742 } 743 if (authenticators == null) { 744 synchronized (this) { 745 if (authenticators == null) { 746 authenticators = new HashMap (); 747 } 748 } 749 } 750 authenticators.put(loginMethod, authenticator); 751 } 752 753 754 756 757 762 public void addLifecycleListener(LifecycleListener listener) { 763 764 lifecycle.addLifecycleListener(listener); 765 766 } 767 768 769 773 public LifecycleListener[] findLifecycleListeners() { 774 775 return lifecycle.findLifecycleListeners(); 776 777 } 778 779 780 785 public void removeLifecycleListener(LifecycleListener listener) { 786 787 lifecycle.removeLifecycleListener(listener); 788 789 } 790 791 792 800 public void start() throws LifecycleException { 801 802 if( log.isInfoEnabled() ) 803 log.info("Starting tomcat server"); 804 805 initDirs(); 807 808 initNaming(); 810 811 if (started) 813 throw new LifecycleException 814 (sm.getString("embedded.alreadyStarted")); 815 lifecycle.fireLifecycleEvent(START_EVENT, null); 816 started = true; 817 initialized = true; 818 819 for (int i = 0; i < engines.length; i++) { 821 if (engines[i] instanceof Lifecycle) 822 ((Lifecycle) engines[i]).start(); 823 } 824 825 for (int i = 0; i < connectors.length; i++) { 827 connectors[i].initialize(); 828 if (connectors[i] instanceof Lifecycle) 829 ((Lifecycle) connectors[i]).start(); 830 } 831 832 } 833 834 835 843 public void stop() throws LifecycleException { 844 845 if( log.isDebugEnabled() ) 846 log.debug("Stopping embedded server"); 847 848 if (!started) 850 throw new LifecycleException 851 (sm.getString("embedded.notStarted")); 852 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 853 started = false; 854 855 for (int i = 0; i < connectors.length; i++) { 857 if (connectors[i] instanceof Lifecycle) 858 ((Lifecycle) connectors[i]).stop(); 859 } 860 861 for (int i = 0; i < engines.length; i++) { 863 if (engines[i] instanceof Lifecycle) 864 ((Lifecycle) engines[i]).stop(); 865 } 866 867 } 868 869 870 872 873 883 protected void initNaming() { 884 if (!useNaming) { 886 log.info( "Catalina naming disabled"); 887 System.setProperty("catalina.useNaming", "false"); 888 } else { 889 System.setProperty("catalina.useNaming", "true"); 890 String value = "org.apache.naming"; 891 String oldValue = 892 System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); 893 if (oldValue != null) { 894 value = value + ":" + oldValue; 895 } 896 System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value); 897 if( log.isDebugEnabled() ) 898 log.debug("Setting naming prefix=" + value); 899 value = System.getProperty 900 (javax.naming.Context.INITIAL_CONTEXT_FACTORY); 901 if (value == null) { 902 System.setProperty 903 (javax.naming.Context.INITIAL_CONTEXT_FACTORY, 904 "org.apache.naming.java.javaURLContextFactory"); 905 } else { 906 log.debug( "INITIAL_CONTEXT_FACTORY alread set " + value ); 907 } 908 } 909 } 910 911 912 protected void initDirs() { 913 914 String catalinaHome = System.getProperty("catalina.home"); 915 if (catalinaHome == null) { 916 String j2eeHome = System.getProperty("com.sun.enterprise.home"); 918 if (j2eeHome != null) { 919 catalinaHome=System.getProperty("com.sun.enterprise.home"); 920 } else if (System.getProperty("catalina.base") != null) { 921 catalinaHome = System.getProperty("catalina.base"); 922 } else { 923 catalinaHome = IntrospectionUtils.guessInstall 925 ("catalina.home", "catalina.base", "catalina.jar"); 926 if (catalinaHome == null) { 927 catalinaHome = IntrospectionUtils.guessInstall 928 ("tomcat.install", "catalina.home", "tomcat.jar"); 929 } 930 } 931 } 932 if(catalinaHome==null) { 934 catalinaHome=System.getProperty("user.dir"); 935 } 936 if (catalinaHome != null) { 937 File home = new File (catalinaHome); 938 if (!home.isAbsolute()) { 939 try { 940 catalinaHome = home.getCanonicalPath(); 941 } catch (IOException e) { 942 catalinaHome = home.getAbsolutePath(); 943 } 944 } 945 System.setProperty("catalina.home", catalinaHome); 946 } 947 948 if (System.getProperty("catalina.base") == null) { 949 System.setProperty("catalina.base", 950 catalinaHome); 951 } else { 952 String catalinaBase = System.getProperty("catalina.base"); 953 File base = new File (catalinaBase); 954 if (!base.isAbsolute()) { 955 try { 956 catalinaBase = base.getCanonicalPath(); 957 } catch (IOException e) { 958 catalinaBase = base.getAbsolutePath(); 959 } 960 } 961 System.setProperty("catalina.base", catalinaBase); 962 } 963 964 String temp = System.getProperty("java.io.tmpdir"); 965 if (temp == null || (!(new File (temp)).exists()) 966 || (!(new File (temp)).isDirectory())) { 967 log.error(sm.getString("embedded.notmp", temp)); 968 } 969 970 } 971 972 973 protected void initStreams() { 974 if (redirectStreams) { 975 SystemLogHandler systemlog = new SystemLogHandler(System.out); 977 System.setOut(systemlog); 978 System.setErr(systemlog); 979 } 980 } 981 982 983 985 988 protected void setSecurityProtection(){ 989 SecurityConfig securityConfig = SecurityConfig.newInstance(); 990 securityConfig.setPackageDefinition(); 991 securityConfig.setPackageAccess(); 992 } 993 994 } 995 | Popular Tags |