Engine、Host及Context的解析过程

1、Engine解析过程

  1.1创建Engine实例

//EngineRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Engine","org.apache.catalina.core.StandardEngine","className");
digester.addSetProperties(prfix+"Engine");
digester.addRule(prefix+"Engine",new LifecycleListenerRule("org.apache.catalina.startup.EngineConfig","engineConfigClass"));
digester.addSetNext(prefix+"Engine","setContainer","org.apache.catalina.Engine");

  创建Engine实例,并将其通过setContainer()方法添加到Service实例,Catalina默认实现为org.apache.catalina.core.StandardEngine。同时,还为Engine添加了一个生命周期监听器EngineConfig。注意,此类是在创建时默认添加的,并非由server.xml配置实现。该监听器用于打印Engine启动和停止日志。

  1.2为Engine添加集群配置

//EngineRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Engine/Cluster",null,"className");
digester.addSetProperties(prefix+"Engine/Cluster");
digester.addSetNext(prefix+"Engine/Cluster","setCluster","org.apache.catalina.Cluster");

  具体集群实现类由className属性指定。

  1.3为Engine添加生命周期监听器

//EngineRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Engine/Listener",null,"className");
digester.addSetProperties([refix+"Engine/Listener");
digester.addSerNext(prefix+"Engine/Listener"."addLifecycleListener","org.apache.catalina.LifecycleListener");

  与EngineConfig不同,此部分监听器由server.xml配置。默认情况下,Catalina未指定Engine监听器。

  1.4为Engine添加安全配置

//EngineRuleSet.addRuleInstances
digester.addObjectCreate(new RealmRUleSet(prefix+"Engine/"));
digester.addObjectCreate(prefix+"Engine/Value",null,"className");
digester.addSetProperties(prefix+"Engine/Value/");
digester.addSetNext(prefix+"Engine/Value","addValue","org.apache.catalina.Value");

  为Engine添加安全配置以及拦截器Value,具体的拦截器类由className属性指定。

2、Host解析过程

  2.1创建Host实例

//HostRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Host","org.apache.catalina.core.StandardHost","className");
digester.addSetProperties(preifx+"Host");
digester.addRule(prefix+"Host",new CoypParentClassLoaderRule());
digester.addRule(prefix+"Host", new LifecycleListenerRule(),"org.apache.catalina.startup.HostConfig","hostConfigClass");
digester.addSetNext(prefix+"Host","addChild","org.apache.catalina.Container");
digester.addCallMethod(prefix+"Host/Alias",addAlias,0);

  创建Host实例,并将其通过addChild()方法添加到Engine上,Catalina默认实现org,apache.catalina.core.StandardHost。同时,还为Host添加了一个生命周期监听器HostConfig,同样,该监听器由Catalina默认添加,而不是由server.xlm配置。

  2.2为Host添加集群

//HostRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Host/Cluster",null,"className");
digester.addSetProperties(prefix+"Host/Cluster");
digester.addSetNext(prefix+"Host/Cluster","setCluster","org.apache.catalina.Cluster");

  2.3为Host添加生命周期管理

//HostRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Host/Listener",null,"className");
digester.addSetProperties(prefix+"Host/Listener");
digester.addSetNext(prefix+"Host/Listener","addLifecycleListener","org.apache.catalina.LifecycleListener");

  与HostConfig不同,此部分监听器由server.xml配置。默认情况下,Catalina未指定Host监听器。

  2.4为Host添加安全配置

//HostRuleSet.addRuleInstances
digester.addRuleSet(new RealmRuleSet(prefix+"Host/"));
digester.addObjectCreate(prefix+"Host/Value",null,"className");
digester.addSetProperties(prefix+"HostValue");
digester.addSetNext(prefix+"Host/Value","addValue","org.apache.catalina.Value");

  为Host添加安全配置以及拦截器Value,具体的拦截器类由className属性指定。

3、Context解析过程

   Catalina的Context配置并非来源一处,这里仅指server.xml中配置。多数情况下,并不需要在server.xml中配置Context,而是由HostConfig自动扫描部署目录,以context.xml文件为基础进行解析创建,如果通过IDE启动Tomcat并部署应用,其Context配置将会被动态更新到server.xml中。

  3.1Context实例化

//ContextRuleSet.addRuleInstances
if(create){
	digester.addObjectCreate(prefix+"Context","org.apache.catalina.core.StandardContext","className");
}else{
	digester.addRule(prefix+"Context",new SetContextPropertiesRule());
}
if(create){
	digester.addRule(prefix+"Context",new LifecycleListenerRule("org.apache.catalina.startup.ContextConfig","configClass"));
	digester.adddSetNext(prefix+"Context","addChild","org.apache.catalina.Container");
}	

  Context的解析会根据create属性的不同而有所区别,主要是由于Context来源于多处。通过server.xml配置Context时,create为true,因此需要创建Context实例;而通过HostConfig自动创建Context时,create为false,此时需要解析子节点即可。Catalina提供的Context实现类为org.apache.catalina.core.StandardContext。Catalina在创建Context实例的同时,还添加了一个生命周期监听器ContextConfig,用于详细配置Context,如解析web.xml等。

  3.2为Context添加生命周期监听器

//ContextRuleSet.RuleInstances
digester.addObjectCreate(prefix+"Context/Listener",null,"className");
digester.addSetProperties(prefix+"Context/Listener");
digester.addSetNext(prefix+"Context/Listener","addLifecycleListener","org.apache.catalina.LifecycleListener");

  具体的监听器类由className属性指定。

  3.3为Context指定类加载器

//ContextRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/Loader","org.apache.catalina.loader.WebappLoader","className");
digester.addSetProperties(prefix+"Cotext/Loader");
digester.addSetNext(prefix+"Context/Loader","setLoader","org.apache.catalina.Loader");

  默认为org.apache.catalina.loader.WebappLoader,可以通过className属性指定自己的实现类。

  3.4为Context添加会话管理器

//ContextRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/Manger","org.apache.catalina.session.StandardManager","className");
digester.addSetProperties(prefix+"Context/Manager");
digester.addSetNext(prefix+"Context/Manager","setManager","org.apache.catalina.Manager");
digester.addObjectXCreate(prefix+"Context/Manager/Store",null,"className");
digester.addSetProperties(prefix+"Context/Manager/Store");
digester.addSetNext(prefix+"Context/Manager/Store","setStore","org.apache.catalina.Store");
digester.addObjectCreate(prefix+"Context/Manager/SessionIdGenerator","org.apache.catalina.util.StandardSessionIdGenerator","className");
digester.addSetProperties(prefix+"Context/Manager/SessionIdGenerator");
digester.addSetNext(prefix+"Context/Manager/SessionIdGenerator","setSessionIdGenerator","org.apache.catalina.SessionIdGenerator");

  默认实现为org.apache.catalina.session.StandardManager,同时为管理器指定会话存储方式和会话标识生成器。

  3.5为Context添加初始化参数

//ContextRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/Parameter","org.apache.catalina.deploy.ApplicationParameter");
digester.addSetProperties(prefix+"Context/Parameter");
digester.addSetNext(prefix+"Context/Parameter","addApplicationParameter","org.apache.catalina.deploy.ApplicationParameter");

  通过该配置,为Context添加初始化参数。可以在context.xml配置文件中添加初始化参数,以实现在所有Web应用中复用,而不必每个Web应用重复配置。当然,只有在Web应用确实允许与Tomcat紧耦合的情况下,否则会导致适用性很差。

  3.6为Context添加安全配置以及Web资源配置

//ContextRuleSet.addRuleInstances
digester.addRuleSet(new RealmRuleSet(prefix+"Context/"));
digester.addObjectCreate(prefix+"Contetx/Resources","org.apache.catalina.webresources.StandardRoot","className");
digester.addSetProperties(prefix+"Cotext/Resources");
digester.addSetNext(prefix+"Context/Resources","setResouces","org.apache.catalina.WebResouceRoot");
digester.addObjectCreate(prefix+"Context/Resources/PreResources",null,"className");
digester.addSetProperties(prefix+"Context/Resources/PreResouces");
digester.addSetNext(prefix+"ContextResouces/PreResources","addPreResources","org.apache.catalina.WebResouceSet");
digester.addObjectCreate(prefix+"Context/Resources/JarResources",null,"className");
digester.addSetProperties(prefix+"Context/Resources/JarResources");
digester.addSetNext(prefix+"Context/Resources/JarResources","addJarResources","org.apache.catalina.WebResourceSet");
digester.addObjectCreate(prefix+"Context/Resources/PostResources",null,"className");
digester.addProperties(prefix+"Context/Resources/PostResources");
digester.addSetNext(prefix+"COntext/Resources/PostResources","addPostResources","org.apache.catalina.WebResourceSet");

  Tomcat 8 新增了PreResources、JarResources、PostResouces这3种资源的配置。

  3.7为Context添加资源链接

//ContetxRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/ResourceLink","org.apache.catalina.deploy.ContextResourceLink");
degister.addSetProperties(prefix+"Context/ResourceLink");
digester.addRule(prefix+"Context/ResourceLink",new SetNextNamingRule("addResourceLink","org.apache.catalina.deploy.ContextResourceLink"));

  为Context添加资源链接ContextResourceLink,用于J2EE命名服务。

  3.8为Context添加Value

//ContextRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/Value",null,"className");
digester.addSetProperties(prefix+"Context/Value");
digester.addSetNext(prefix+"Context/Value","addValue","org.apache.catalina.Value");

  为Context添加拦截器Value,具体的拦截器类由className属性指定。

  3.9为Context添加守护资源配置

//ContextRuleSet.addRuleInstances
digester.addCallMethod(prefix+"Context/WatchedResource","addWatchedResource",0);
digester.addCallMethod(prefix+"Context/WrapperLifecycle","addWrapperLifecycle",0);
digester.addCallMethod(prefix+"Context/WrapperListener","addWrapperListener",0);
digester.addObjectCreate(prefix+"Context/JarScanner","org.apache.tomcat.util.scan.StandardJarScanner","className");
digester.addSetProperties(prefix+"Context/JarScanner");
digester.addSetNext(prefix+"Context/JarScanner","setJarScanner","org.apache.tomcat.JarScanner");
digester.addObjectCreate(prefix+"Context/JarScanner/JarScanFilter","org.apache.tomcat.util.scan.StandardJarScanFilter","className");
digester.addSetProperties(prefix+"Context/JarScanner/JarScanFilter");
digester.addSetNext(prefix+"Context/JarScanner/JarScanFilter","setJarScanFilter","org.apache.tomcat.Jar.ScanFilter");

  WatchedResource标签用于为Context添加监视资源,当这些资源发生变更时,Web应用将会被重新加载,默认为WEB-INF/web.xml。
  WrapperLifecycle标签用于为Context添加一个生命周期监听器,此类的实例并非添加到Context上,而是添加到Context包含的Wrapper上。
  WrapperListener标签用于为Context添加一个容器监听器类(ContainerListener),此类的实例也添加到Wrapper上。
  JarScanner标签用于为Context添加一个Jar扫描器,Catalina默认实现为org.apache.tomcat.util.scan.StandardJarScanner。JarScanner扫描Web应用和类加载器层级的Jar包,主要用于TLD扫描和web-fragment.xml扫描。通过JarScanFilter标签,还可以为JarScanner指定一个过滤器,只有符合条件的Jar包才会被处理,默认为org.apache.tomcat.util.scan.StandardJarScanFilter。

  3.10为Context添加Cookie处理器

//ContextRuleSet.addRuleInstances
digester.addObjectCreate(prefix+"Context/CookieProcessor","org.apache.tomcat.util.http.Rfc6265CookieProcessor","className");
digester.addProperties(prefix+"Context/CookiePrecessor");
sigester.addSetNext(prefix+"Context/CookieProcessor","setCookieProcessor","org.apache.tomcat.util.http.CookieProcessor"):

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页