1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 31 import javax.servlet.ServletContext ; 32 33 import org.apache.catalina.Authenticator; 34 import org.apache.catalina.Container; 35 import org.apache.catalina.Context; 36 import org.apache.catalina.Engine; 37 import org.apache.catalina.Globals; 38 import org.apache.catalina.Host; 39 import org.apache.catalina.Lifecycle; 40 import org.apache.catalina.LifecycleEvent; 41 import org.apache.catalina.LifecycleListener; 42 import org.apache.catalina.Pipeline; 43 import org.apache.catalina.Valve; 44 import org.apache.catalina.Wrapper; 45 import org.apache.catalina.core.ContainerBase; 46 import org.apache.catalina.core.StandardContext; 47 import org.apache.catalina.core.StandardEngine; 48 import org.apache.catalina.core.StandardHost; 49 import org.apache.catalina.deploy.ErrorPage; 50 import org.apache.catalina.deploy.FilterDef; 51 import org.apache.catalina.deploy.FilterMap; 52 import org.apache.catalina.deploy.LoginConfig; 53 import org.apache.catalina.deploy.SecurityConstraint; 54 import org.apache.catalina.util.StringManager; 55 import org.apache.tomcat.util.digester.Digester; 56 import org.apache.tomcat.util.digester.RuleSet; 57 import org.xml.sax.ErrorHandler ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.SAXParseException ; 60 61 69 70 public class ContextConfig 71 implements LifecycleListener { 72 73 protected static org.apache.commons.logging.Log log= 74 org.apache.commons.logging.LogFactory.getLog( ContextConfig.class ); 75 76 78 79 82 protected Map customAuthenticators; 83 84 85 90 protected static Properties authenticators = null; 91 92 93 96 protected Context context = null; 97 98 99 102 protected String defaultContextXml = null; 103 104 105 108 protected String defaultWebXml = null; 109 110 111 114 protected boolean ok = false; 115 116 117 120 protected SAXParseException parseException = null; 121 122 123 126 protected String originalDocBase = null; 127 128 129 132 protected static final StringManager sm = 133 StringManager.getManager(Constants.Package); 134 135 136 140 protected static Digester contextDigester = null; 141 142 143 147 protected static Digester webDigester = null; 148 149 150 153 protected static WebRuleSet webRuleSet = new WebRuleSet(); 154 155 158 protected static boolean xmlValidation = false; 159 160 161 164 protected static boolean xmlNamespaceAware = false; 165 166 167 170 protected static long deploymentCount = 0L; 171 172 173 protected static final LoginConfig DUMMY_LOGIN_CONFIG = 174 new LoginConfig("NONE", null, null, null); 175 176 177 179 180 183 public String getDefaultWebXml() { 184 if( defaultWebXml == null ) { 185 defaultWebXml=Constants.DefaultWebXml; 186 } 187 188 return (this.defaultWebXml); 189 190 } 191 192 193 198 public void setDefaultWebXml(String path) { 199 200 this.defaultWebXml = path; 201 202 } 203 204 205 208 public String getDefaultContextXml() { 209 if( defaultContextXml == null ) { 210 defaultContextXml=Constants.DefaultContextXml; 211 } 212 213 return (this.defaultContextXml); 214 215 } 216 217 218 223 public void setDefaultContextXml(String path) { 224 225 this.defaultContextXml = path; 226 227 } 228 229 230 236 public void setCustomAuthenticators(Map customAuthenticators) { 237 this.customAuthenticators = customAuthenticators; 238 } 239 240 241 243 244 249 public void lifecycleEvent(LifecycleEvent event) { 250 251 try { 253 context = (Context ) event.getLifecycle(); 254 } catch (ClassCastException e) { 255 log.error(sm.getString("contextConfig.cce", event.getLifecycle()), e); 256 return; 257 } 258 259 if (event.getType().equals(Lifecycle.START_EVENT)) { 261 start(); 262 } else if (event.getType().equals(StandardContext.BEFORE_START_EVENT)) { 263 beforeStart(); 264 } else if (event.getType().equals(StandardContext.AFTER_START_EVENT)) { 265 if (originalDocBase != null) { 267 String docBase = context.getDocBase(); 268 context.setDocBase(originalDocBase); 269 originalDocBase = docBase; 270 } 271 } else if (event.getType().equals(Lifecycle.STOP_EVENT)) { 272 if (originalDocBase != null) { 273 String docBase = context.getDocBase(); 274 context.setDocBase(originalDocBase); 275 originalDocBase = docBase; 276 } 277 stop(); 278 } else if (event.getType().equals(Lifecycle.INIT_EVENT)) { 279 init(); 280 } else if (event.getType().equals(Lifecycle.DESTROY_EVENT)) { 281 destroy(); 282 } 283 284 } 285 286 287 289 290 293 protected void applicationAnnotationsConfig() { 294 295 long t1=System.currentTimeMillis(); 296 297 WebAnnotationSet.loadApplicationAnnotations(context); 298 299 long t2=System.currentTimeMillis(); 300 if (context instanceof StandardContext) { 301 ((StandardContext) context).setStartupTime(t2-t1+ 302 ((StandardContext) context).getStartupTime()); 303 } 304 } 305 306 307 310 protected void applicationWebConfig() { 311 312 String altDDName = null; 313 314 InputStream stream = null; 316 ServletContext servletContext = context.getServletContext(); 317 if (servletContext != null) { 318 altDDName = (String )servletContext.getAttribute( 319 Globals.ALT_DD_ATTR); 320 if (altDDName != null) { 321 try { 322 stream = new FileInputStream (altDDName); 323 } catch (FileNotFoundException e) { 324 log.error(sm.getString("contextConfig.altDDNotFound", 325 altDDName)); 326 } 327 } 328 else { 329 stream = servletContext.getResourceAsStream 330 (Constants.ApplicationWebXml); 331 } 332 } 333 if (stream == null) { 334 if (log.isDebugEnabled()) { 335 log.debug(sm.getString("contextConfig.applicationMissing") + " " + context); 336 } 337 return; 338 } 339 340 long t1=System.currentTimeMillis(); 341 342 if (webDigester == null){ 343 webDigester = createWebDigester(); 344 } 345 346 URL url=null; 347 synchronized (webDigester) { 349 try { 350 if (altDDName != null) { 351 url = new File (altDDName).toURL(); 352 } else { 353 url = servletContext.getResource( 354 Constants.ApplicationWebXml); 355 } 356 if( url!=null ) { 357 InputSource is = new InputSource (url.toExternalForm()); 358 is.setByteStream(stream); 359 if (context instanceof StandardContext) { 360 ((StandardContext) context).setReplaceWelcomeFiles(true); 361 } 362 webDigester.push(context); 363 webDigester.setErrorHandler(new ContextErrorHandler()); 364 365 if(log.isDebugEnabled()) { 366 log.debug("Parsing application web.xml file at " + url.toExternalForm()); 367 } 368 369 webDigester.parse(is); 370 371 if (parseException != null) { 372 ok = false; 373 } 374 } else { 375 log.info("No web.xml, using defaults " + context ); 376 } 377 } catch (SAXParseException e) { 378 log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e); 379 log.error(sm.getString("contextConfig.applicationPosition", 380 "" + e.getLineNumber(), 381 "" + e.getColumnNumber())); 382 ok = false; 383 } catch (Exception e) { 384 log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e); 385 ok = false; 386 } finally { 387 webDigester.reset(); 388 parseException = null; 389 try { 390 if (stream != null) { 391 stream.close(); 392 } 393 } catch (IOException e) { 394 log.error(sm.getString("contextConfig.applicationClose"), e); 395 } 396 } 397 } 398 webRuleSet.recycle(); 399 400 long t2=System.currentTimeMillis(); 401 if (context instanceof StandardContext) { 402 ((StandardContext) context).setStartupTime(t2-t1); 403 } 404 } 405 406 407 411 protected synchronized void authenticatorConfig() { 412 413 SecurityConstraint constraints[] = context.findConstraints(); 415 if ((constraints == null) || (constraints.length == 0)) 416 return; 417 LoginConfig loginConfig = context.getLoginConfig(); 418 if (loginConfig == null) { 419 loginConfig = DUMMY_LOGIN_CONFIG; 420 context.setLoginConfig(loginConfig); 421 } 422 423 if (context instanceof Authenticator) 425 return; 426 if (context instanceof ContainerBase) { 427 Pipeline pipeline = ((ContainerBase) context).getPipeline(); 428 if (pipeline != null) { 429 Valve basic = pipeline.getBasic(); 430 if ((basic != null) && (basic instanceof Authenticator)) 431 return; 432 Valve valves[] = pipeline.getValves(); 433 for (int i = 0; i < valves.length; i++) { 434 if (valves[i] instanceof Authenticator) 435 return; 436 } 437 } 438 } else { 439 return; } 441 442 if (context.getRealm() == null) { 444 log.error(sm.getString("contextConfig.missingRealm")); 445 ok = false; 446 return; 447 } 448 449 454 Valve authenticator = null; 455 if (customAuthenticators != null) { 456 authenticator = (Valve) 457 customAuthenticators.get(loginConfig.getAuthMethod()); 458 } 459 if (authenticator == null) { 460 if (authenticators == null) { 462 try { 463 InputStream is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties"); 464 if( is!=null ) { 465 authenticators = new Properties (); 466 authenticators.load(is); 467 } else { 468 log.error(sm.getString( 469 "contextConfig.authenticatorResources")); 470 ok=false; 471 return; 472 } 473 } catch (IOException e) { 474 log.error(sm.getString( 475 "contextConfig.authenticatorResources"), e); 476 ok = false; 477 return; 478 } 479 } 480 481 String authenticatorName = null; 483 authenticatorName = 484 authenticators.getProperty(loginConfig.getAuthMethod()); 485 if (authenticatorName == null) { 486 log.error(sm.getString("contextConfig.authenticatorMissing", 487 loginConfig.getAuthMethod())); 488 ok = false; 489 return; 490 } 491 492 try { 494 Class authenticatorClass = Class.forName(authenticatorName); 495 authenticator = (Valve) authenticatorClass.newInstance(); 496 } catch (Throwable t) { 497 log.error(sm.getString( 498 "contextConfig.authenticatorInstantiate", 499 authenticatorName), 500 t); 501 ok = false; 502 } 503 } 504 505 if (authenticator != null && context instanceof ContainerBase) { 506 Pipeline pipeline = ((ContainerBase) context).getPipeline(); 507 if (pipeline != null) { 508 ((ContainerBase) context).addValve(authenticator); 509 if (log.isDebugEnabled()) { 510 log.debug(sm.getString( 511 "contextConfig.authenticatorConfigured", 512 loginConfig.getAuthMethod())); 513 } 514 } 515 } 516 517 } 518 519 520 524 protected static Digester createWebDigester() { 525 Digester webDigester = 526 createWebXmlDigester(xmlNamespaceAware, xmlValidation); 527 return webDigester; 528 } 529 530 531 535 public static Digester createWebXmlDigester(boolean namespaceAware, 536 boolean validation) { 537 538 Digester webDigester = DigesterFactory.newDigester(xmlValidation, 539 xmlNamespaceAware, 540 webRuleSet); 541 return webDigester; 542 } 543 544 545 549 protected Digester createContextDigester() { 550 Digester digester = new Digester(); 551 digester.setValidating(false); 552 RuleSet contextRuleSet = new ContextRuleSet("", false); 553 digester.addRuleSet(contextRuleSet); 554 RuleSet namingRuleSet = new NamingRuleSet("Context/"); 555 digester.addRuleSet(namingRuleSet); 556 return digester; 557 } 558 559 560 protected String getBaseDir() { 561 Container engineC=context.getParent().getParent(); 562 if( engineC instanceof StandardEngine ) { 563 return ((StandardEngine)engineC).getBaseDir(); 564 } 565 return System.getProperty("catalina.base"); 566 } 567 568 573 protected void defaultWebConfig() { 574 long t1=System.currentTimeMillis(); 575 576 if( defaultWebXml==null && context instanceof StandardContext ) { 578 defaultWebXml=((StandardContext)context).getDefaultWebXml(); 579 } 580 if( defaultWebXml==null ) getDefaultWebXml(); 582 583 File file = new File (this.defaultWebXml); 584 if (!file.isAbsolute()) { 585 file = new File (getBaseDir(), 586 this.defaultWebXml); 587 } 588 589 InputStream stream = null; 590 InputSource source = null; 591 592 try { 593 if ( ! file.exists() ) { 594 stream = getClass().getClassLoader() 596 .getResourceAsStream(defaultWebXml); 597 if( stream != null ) { 598 source = new InputSource 599 (getClass().getClassLoader() 600 .getResource(defaultWebXml).toString()); 601 } 602 if( stream== null ) { 603 stream = getClass().getClassLoader() 605 .getResourceAsStream("web-embed.xml"); 606 if( stream != null ) { 607 source = new InputSource 608 (getClass().getClassLoader() 609 .getResource("web-embed.xml").toString()); 610 } 611 } 612 613 if( stream== null ) { 614 log.info("No default web.xml"); 615 } 616 } else { 617 source = 618 new InputSource ("file://" + file.getAbsolutePath()); 619 stream = new FileInputStream (file); 620 context.addWatchedResource(file.getAbsolutePath()); 621 } 622 } catch (Exception e) { 623 log.error(sm.getString("contextConfig.defaultMissing") 624 + " " + defaultWebXml + " " + file , e); 625 } 626 627 if (webDigester == null){ 628 webDigester = createWebDigester(); 629 } 630 631 if (stream != null) { 632 processDefaultWebConfig(webDigester, stream, source); 633 webRuleSet.recycle(); 634 } 635 636 long t2=System.currentTimeMillis(); 637 if( (t2-t1) > 200 ) 638 log.debug("Processed default web.xml " + file + " " + ( t2-t1)); 639 640 stream = null; 641 source = null; 642 643 String resourceName = getHostConfigPath(Constants.HostWebXml); 644 file = new File (getConfigBase(), resourceName); 645 646 try { 647 if ( ! file.exists() ) { 648 stream = getClass().getClassLoader() 650 .getResourceAsStream(resourceName); 651 if( stream != null ) { 652 source = new InputSource 653 (getClass().getClassLoader() 654 .getResource(resourceName).toString()); 655 } 656 } else { 657 source = 658 new InputSource ("file://" + file.getAbsolutePath()); 659 stream = new FileInputStream (file); 660 } 661 } catch (Exception e) { 662 log.error(sm.getString("contextConfig.defaultMissing") 663 + " " + resourceName + " " + file , e); 664 } 665 666 if (stream != null) { 667 processDefaultWebConfig(webDigester, stream, source); 668 webRuleSet.recycle(); 669 } 670 671 } 672 673 674 677 protected void processDefaultWebConfig(Digester digester, InputStream stream, 678 InputSource source) { 679 680 if (log.isDebugEnabled()) 681 log.debug("Processing context [" + context.getName() 682 + "] web configuration resource " + source.getSystemId()); 683 684 synchronized (digester) { 686 try { 687 source.setByteStream(stream); 688 689 if (context instanceof StandardContext) 690 ((StandardContext) context).setReplaceWelcomeFiles(true); 691 digester.setClassLoader(this.getClass().getClassLoader()); 692 digester.setUseContextClassLoader(false); 693 digester.push(context); 694 digester.setErrorHandler(new ContextErrorHandler()); 695 digester.parse(source); 696 if (parseException != null) { 697 ok = false; 698 } 699 } catch (SAXParseException e) { 700 log.error(sm.getString("contextConfig.defaultParse"), e); 701 log.error(sm.getString("contextConfig.defaultPosition", 702 "" + e.getLineNumber(), 703 "" + e.getColumnNumber())); 704 ok = false; 705 } catch (Exception e) { 706 log.error(sm.getString("contextConfig.defaultParse"), e); 707 ok = false; 708 } finally { 709 digester.reset(); 710 parseException = null; 711 try { 712 if (stream != null) { 713 stream.close(); 714 } 715 } catch (IOException e) { 716 log.error(sm.getString("contextConfig.defaultClose"), e); 717 } 718 } 719 } 720 } 721 722 723 726 protected void contextConfig() { 727 728 if( defaultContextXml==null && context instanceof StandardContext ) { 730 defaultContextXml = ((StandardContext)context).getDefaultContextXml(); 731 } 732 if( defaultContextXml==null ) getDefaultContextXml(); 734 735 if (!context.getOverride()) { 736 processContextConfig(new File (getBaseDir()), defaultContextXml); 737 processContextConfig(getConfigBase(), getHostConfigPath(Constants.HostContextXml)); 738 } 739 if (context.getConfigFile() != null) 740 processContextConfig(new File (context.getConfigFile()), null); 741 742 } 743 744 745 748 protected void processContextConfig(File baseDir, String resourceName) { 749 750 if (log.isDebugEnabled()) 751 log.debug("Processing context [" + context.getName() 752 + "] configuration file " + baseDir + " " + resourceName); 753 754 InputSource source = null; 755 InputStream stream = null; 756 757 File file = baseDir; 758 if (resourceName != null) { 759 file = new File (baseDir, resourceName); 760 } 761 762 try { 763 if ( !file.exists() ) { 764 if (resourceName != null) { 765 stream = getClass().getClassLoader() 767 .getResourceAsStream(resourceName); 768 if( stream != null ) { 769 source = new InputSource 770 (getClass().getClassLoader() 771 .getResource(resourceName).toString()); 772 } 773 } 774 } else { 775 source = 776 new InputSource ("file://" + file.getAbsolutePath()); 777 stream = new FileInputStream (file); 778 context.addWatchedResource(file.getAbsolutePath()); 781 } 782 } catch (Exception e) { 783 log.error(sm.getString("contextConfig.defaultMissing") 784 + " " + resourceName + " " + file , e); 785 } 786 787 if (source == null) 788 return; 789 if (contextDigester == null){ 790 contextDigester = createContextDigester(); 791 } 792 synchronized (contextDigester) { 793 try { 794 source.setByteStream(stream); 795 contextDigester.setClassLoader(this.getClass().getClassLoader()); 796 contextDigester.setUseContextClassLoader(false); 797 contextDigester.push(context.getParent()); 798 contextDigester.push(context); 799 contextDigester.setErrorHandler(new ContextErrorHandler()); 800 contextDigester.parse(source); 801 if (parseException != null) { 802 ok = false; 803 } 804 if (log.isDebugEnabled()) 805 log.debug("Successfully processed context [" + context.getName() 806 + "] configuration file " + baseDir + " " + resourceName); 807 } catch (SAXParseException e) { 808 log.error(sm.getString("contextConfig.defaultParse"), e); 809 log.error(sm.getString("contextConfig.defaultPosition", 810 "" + e.getLineNumber(), 811 "" + e.getColumnNumber())); 812 ok = false; 813 } catch (Exception e) { 814 log.error(sm.getString("contextConfig.defaultParse"), e); 815 ok = false; 816 } finally { 817 contextDigester.reset(); 818 parseException = null; 819 try { 820 if (stream != null) { 821 stream.close(); 822 } 823 } catch (IOException e) { 824 log.error(sm.getString("contextConfig.defaultClose"), e); 825 } 826 } 827 } 828 } 829 830 831 834 protected void fixDocBase() 835 throws IOException { 836 837 Host host = (Host) context.getParent(); 838 String appBase = host.getAppBase(); 839 840 boolean unpackWARs = true; 841 if (host instanceof StandardHost) { 842 unpackWARs = ((StandardHost) host).isUnpackWARs() 843 && ((StandardContext) context).getUnpackWAR(); 844 } 845 846 File canonicalAppBase = new File (appBase); 847 if (canonicalAppBase.isAbsolute()) { 848 canonicalAppBase = canonicalAppBase.getCanonicalFile(); 849 } else { 850 canonicalAppBase = 851 new File (System.getProperty("catalina.base"), appBase) 852 .getCanonicalFile(); 853 } 854 855 String docBase = context.getDocBase(); 856 if (docBase == null) { 857 String path = context.getPath(); 859 if (path == null) { 860 return; 861 } 862 if (path.equals("")) { 863 docBase = "ROOT"; 864 } else { 865 if (path.startsWith("/")) { 866 docBase = path.substring(1); 867 } else { 868 docBase = path; 869 } 870 } 871 } 872 873 File file = new File (docBase); 874 if (!file.isAbsolute()) { 875 docBase = (new File (canonicalAppBase, docBase)).getPath(); 876 } else { 877 docBase = file.getCanonicalPath(); 878 } 879 file = new File (docBase); 880 String origDocBase = docBase; 881 882 if (docBase.toLowerCase().endsWith(".war") && !file.isDirectory() && unpackWARs) { 883 URL war = new URL ("jar:" + (new File (docBase)).toURL() + "!/"); 884 String contextPath = context.getPath(); 885 if (contextPath.equals("")) { 886 contextPath = "ROOT"; 887 } 888 docBase = ExpandWar.expand(host, war, contextPath); 889 file = new File (docBase); 890 docBase = file.getCanonicalPath(); 891 if (context instanceof StandardContext) { 892 ((StandardContext) context).setOriginalDocBase(origDocBase); 893 } 894 } else { 895 File docDir = new File (docBase); 896 if (!docDir.exists()) { 897 File warFile = new File (docBase + ".war"); 898 if (warFile.exists()) { 899 if (unpackWARs) { 900 URL war = new URL ("jar:" + warFile.toURL() + "!/"); 901 docBase = ExpandWar.expand(host, war, context.getPath()); 902 file = new File (docBase); 903 docBase = file.getCanonicalPath(); 904 } else { 905 docBase = warFile.getCanonicalPath(); 906 } 907 } 908 if (context instanceof StandardContext) { 909 ((StandardContext) context).setOriginalDocBase(origDocBase); 910 } 911 } 912 } 913 914 if (docBase.startsWith(canonicalAppBase.getPath())) { 915 docBase = docBase.substring(canonicalAppBase.getPath().length()); 916 docBase = docBase.replace(File.separatorChar, '/'); 917 if (docBase.startsWith("/")) { 918 docBase = docBase.substring(1); 919 } 920 } else { 921 docBase = docBase.replace(File.separatorChar, '/'); 922 } 923 924 context.setDocBase(docBase); 925 926 } 927 928 929 protected void antiLocking() 930 throws IOException { 931 932 if ((context instanceof StandardContext) 933 && ((StandardContext) context).getAntiResourceLocking()) { 934 935 Host host = (Host) context.getParent(); 936 String appBase = host.getAppBase(); 937 String docBase = context.getDocBase(); 938 if (docBase == null) 939 return; 940 if (originalDocBase == null) { 941 originalDocBase = docBase; 942 } else { 943 docBase = originalDocBase; 944 } 945 File docBaseFile = new File (docBase); 946 if (!docBaseFile.isAbsolute()) { 947 File file = new File (appBase); 948 if (!file.isAbsolute()) { 949 file = new File (System.getProperty("catalina.base"), appBase); 950 } 951 docBaseFile = new File (file, docBase); 952 } 953 954 String path = context.getPath(); 955 if (path == null) { 956 return; 957 } 958 if (path.equals("")) { 959 docBase = "ROOT"; 960 } else { 961 if (path.startsWith("/")) { 962 docBase = path.substring(1); 963 } else { 964 docBase = path; 965 } 966 } 967 968 File file = null; 969 if (docBase.toLowerCase().endsWith(".war")) { 970 file = new File (System.getProperty("java.io.tmpdir"), 971 deploymentCount++ + "-" + docBase + ".war"); 972 } else { 973 file = new File (System.getProperty("java.io.tmpdir"), 974 deploymentCount++ + "-" + docBase); 975 } 976 977 if (log.isDebugEnabled()) 978 log.debug("Anti locking context[" + context.getPath() 979 + "] setting docBase to " + file); 980 981 ExpandWar.delete(file); 983 if (ExpandWar.copy(docBaseFile, file)) { 984 context.setDocBase(file.getAbsolutePath()); 985 } 986 987 } 988 989 } 990 991 992 995 protected void init() { 996 998 if (log.isDebugEnabled()) 999 log.debug(sm.getString("contextConfig.init")); 1000 context.setConfigured(false); 1001 ok = true; 1002 1003 contextConfig(); 1004 1005 try { 1006 fixDocBase(); 1007 } catch (IOException e) { 1008 log.error(sm.getString("contextConfig.fixDocBase"), e); 1009 } 1010 1011 } 1012 1013 1014 1017 protected synchronized void beforeStart() { 1018 1019 try { 1020 antiLocking(); 1021 } catch (IOException e) { 1022 log.error(sm.getString("contextConfig.antiLocking"), e); 1023 } 1024 1025 } 1026 1027 1028 1031 protected synchronized void start() { 1032 1034 if (log.isDebugEnabled()) 1035 log.debug(sm.getString("contextConfig.start")); 1036 1037 Container container = context.getParent(); 1039 if( !context.getOverride() ) { 1040 if( container instanceof Host ) { 1041 xmlValidation = context.getXmlValidation(); 1044 if (!xmlValidation) { 1045 xmlValidation = ((Host)container).getXmlValidation(); 1046 } 1047 1048 xmlNamespaceAware = context.getXmlNamespaceAware(); 1049 if (!xmlNamespaceAware){ 1050 xmlNamespaceAware 1051 = ((Host)container).getXmlNamespaceAware(); 1052 } 1053 1054 container = container.getParent(); 1055 } 1056 } 1057 1058 defaultWebConfig(); 1060 applicationWebConfig(); 1061 if (!context.getIgnoreAnnotations()) { 1062 applicationAnnotationsConfig(); 1063 } 1064 if (ok) { 1065 validateSecurityRoles(); 1066 } 1067 1068 if (ok) 1070 authenticatorConfig(); 1071 1072 if ((log.isDebugEnabled()) && (context instanceof ContainerBase)) { 1074 log.debug("Pipeline Configuration:"); 1075 Pipeline pipeline = ((ContainerBase) context).getPipeline(); 1076 Valve valves[] = null; 1077 if (pipeline != null) 1078 valves = pipeline.getValves(); 1079 if (valves != null) { 1080 for (int i = 0; i < valves.length; i++) { 1081 log.debug(" " + valves[i].getInfo()); 1082 } 1083 } 1084 log.debug("======================"); 1085 } 1086 1087 if (ok) 1089 context.setConfigured(true); 1090 else { 1091 log.error(sm.getString("contextConfig.unavailable")); 1092 context.setConfigured(false); 1093 } 1094 1095 } 1096 1097 1098 1101 protected synchronized void stop() { 1102 1103 if (log.isDebugEnabled()) 1104 log.debug(sm.getString("contextConfig.stop")); 1105 1106 int i; 1107 1108 Container[] children = context.findChildren(); 1110 for (i = 0; i < children.length; i++) { 1111 context.removeChild(children[i]); 1112 } 1113 1114 1123 1124 SecurityConstraint[] securityConstraints = context.findConstraints(); 1126 for (i = 0; i < securityConstraints.length; i++) { 1127 context.removeConstraint(securityConstraints[i]); 1128 } 1129 1130 1137 1138 1145 1146 ErrorPage[] errorPages = context.findErrorPages(); 1148 for (i = 0; i < errorPages.length; i++) { 1149 context.removeErrorPage(errorPages[i]); 1150 } 1151 1152 FilterDef[] filterDefs = context.findFilterDefs(); 1154 for (i = 0; i < filterDefs.length; i++) { 1155 context.removeFilterDef(filterDefs[i]); 1156 } 1157 1158 FilterMap[] filterMaps = context.findFilterMaps(); 1160 for (i = 0; i < filterMaps.length; i++) { 1161 context.removeFilterMap(filterMaps[i]); 1162 } 1163 1164 1171 1172 String [] mimeMappings = context.findMimeMappings(); 1174 for (i = 0; i < mimeMappings.length; i++) { 1175 context.removeMimeMapping(mimeMappings[i]); 1176 } 1177 1178 String [] parameters = context.findParameters(); 1180 for (i = 0; i < parameters.length; i++) { 1181 context.removeParameter(parameters[i]); 1182 } 1183 1184 1191 1192 1200 1201 1208 1209 String [] securityRoles = context.findSecurityRoles(); 1211 for (i = 0; i < securityRoles.length; i++) { 1212 context.removeSecurityRole(securityRoles[i]); 1213 } 1214 1215 String [] servletMappings = context.findServletMappings(); 1217 for (i = 0; i < servletMappings.length; i++) { 1218 context.removeServletMapping(servletMappings[i]); 1219 } 1220 1221 1223 String [] taglibs = context.findTaglibs(); 1225 for (i = 0; i < taglibs.length; i++) { 1226 context.removeTaglib(taglibs[i]); 1227 } 1228 1229 String [] welcomeFiles = context.findWelcomeFiles(); 1231 for (i = 0; i < welcomeFiles.length; i++) { 1232 context.removeWelcomeFile(welcomeFiles[i]); 1233 } 1234 1235 String [] wrapperLifecycles = context.findWrapperLifecycles(); 1237 for (i = 0; i < wrapperLifecycles.length; i++) { 1238 context.removeWrapperLifecycle(wrapperLifecycles[i]); 1239 } 1240 1241 String [] wrapperListeners = context.findWrapperListeners(); 1243 for (i = 0; i < wrapperListeners.length; i++) { 1244 context.removeWrapperListener(wrapperListeners[i]); 1245 } 1246 1247 Host host = (Host) context.getParent(); 1249 String appBase = host.getAppBase(); 1250 String docBase = context.getDocBase(); 1251 if ((docBase != null) && (originalDocBase != null)) { 1252 File docBaseFile = new File (docBase); 1253 if (!docBaseFile.isAbsolute()) { 1254 docBaseFile = new File (appBase, docBase); 1255 } 1256 ExpandWar.delete(docBaseFile); 1257 } 1258 1259 ok = true; 1260 1261 } 1262 1263 1264 1267 protected synchronized void destroy() { 1268 if (log.isDebugEnabled()) 1270 log.debug(sm.getString("contextConfig.destroy")); 1271 1272 String workDir = ((StandardContext) context).getWorkPath(); 1274 if (workDir != null) 1275 ExpandWar.delete(new File (workDir)); 1276 } 1277 1278 1279 1286 protected void validateSecurityRoles() { 1287 1288 SecurityConstraint constraints[] = context.findConstraints(); 1290 for (int i = 0; i < constraints.length; i++) { 1291 String roles[] = constraints[i].findAuthRoles(); 1292 for (int j = 0; j < roles.length; j++) { 1293 if (!"*".equals(roles[j]) && 1294 !context.findSecurityRole(roles[j])) { 1295 log.info(sm.getString("contextConfig.role.auth", roles[j])); 1296 context.addSecurityRole(roles[j]); 1297 } 1298 } 1299 } 1300 1301 Container wrappers[] = context.findChildren(); 1303 for (int i = 0; i < wrappers.length; i++) { 1304 Wrapper wrapper = (Wrapper) wrappers[i]; 1305 String runAs = wrapper.getRunAs(); 1306 if ((runAs != null) && !context.findSecurityRole(runAs)) { 1307 log.info(sm.getString("contextConfig.role.runas", runAs)); 1308 context.addSecurityRole(runAs); 1309 } 1310 String names[] = wrapper.findSecurityReferences(); 1311 for (int j = 0; j < names.length; j++) { 1312 String link = wrapper.findSecurityReference(names[j]); 1313 if ((link != null) && !context.findSecurityRole(link)) { 1314 log.info(sm.getString("contextConfig.role.link", link)); 1315 context.addSecurityRole(link); 1316 } 1317 } 1318 } 1319 1320 } 1321 1322 1323 1326 protected File getConfigBase() { 1327 File configBase = 1328 new File (System.getProperty("catalina.base"), "conf"); 1329 if (!configBase.exists()) { 1330 return null; 1331 } else { 1332 return configBase; 1333 } 1334 } 1335 1336 1337 protected String getHostConfigPath(String resourceName) { 1338 StringBuffer result = new StringBuffer (); 1339 Container container = context; 1340 Container host = null; 1341 Container engine = null; 1342 while (container != null) { 1343 if (container instanceof Host) 1344 host = container; 1345 if (container instanceof Engine) 1346 engine = container; 1347 container = container.getParent(); 1348 } 1349 if (engine != null) { 1350 result.append(engine.getName()).append('/'); 1351 } 1352 if (host != null) { 1353 result.append(host.getName()).append('/'); 1354 } 1355 result.append(resourceName); 1356 return result.toString(); 1357 } 1358 1359 1360 protected class ContextErrorHandler 1361 implements ErrorHandler { 1362 1363 public void error(SAXParseException exception) { 1364 parseException = exception; 1365 } 1366 1367 public void fatalError(SAXParseException exception) { 1368 parseException = exception; 1369 } 1370 1371 public void warning(SAXParseException exception) { 1372 parseException = exception; 1373 } 1374 1375 } 1376 1377 1378} 1379 | Popular Tags |