Tomcat源码阅读(三)Catalina.createStartDegester

来源:互联网 发布:淘宝店招如何全屏 编辑:程序博客网 时间:2024/06/15 06:49

在研究Catalina之前,首先转一下createStartDegester的解析说明,这是对Tomcat配置文件server.xml的解析并初始化到Tomcat中。本来想自己研究一番,再写阅读心得上来。但发现这个解析过程也是比较复杂,涉及东西挺多的,然后搜了一下其他研究Tomcat源码大神们的心得,发现这篇文章的确不错。详尽介绍了解析xml的过程,值得转载一番。感谢holly2k这位大神的杰作。


tomcat解析(八)Catalina.createStartDigester


在tomcat解析(四)中我们讲到了Catalina的load及start方法启动及准备整个tomcat服务器,而这两个方法最终又将该任务交由server的initialize及start方法处理,该变更将引用Server类的实例,但初始化时为空,因此我们需要该对象实例化过程,而该过程尽在Catalina.load方法的第三步骤里(可看tomcat解析四).首先我们需要了解一下其中的createStartDigester方法,该方法内容如下:

[java] view plaincopy
  1. /** 
  2.      * Create and configure the Digester we will be using for startup. 
  3.      */  
  4.     protected Digester createStartDigester() {  
  5.         long t1=System.currentTimeMillis();  
  6.         // Initialize the digester  
  7.         Digester digester = new Digester();  
  8.         digester.setValidating(false);  
  9.         digester.setRulesValidation(true);  
  10.         HashMap<Class, List<String>> fakeAttributes = new HashMap<Class, List<String>>();  
  11.         ArrayList<String> attrs = new ArrayList<String>();  
  12.         attrs.add("className");  
  13.         fakeAttributes.put(Object.class, attrs);  
  14.         digester.setFakeAttributes(fakeAttributes);  
  15.         digester.setClassLoader(StandardServer.class.getClassLoader());  
  16.         // Configure the actions we will be using  
  17.         digester.addObjectCreate("Server",  
  18.                                  "org.apache.catalina.core.StandardServer",  
  19.                                  "className");  
  20.         digester.addSetProperties("Server");  
  21.         digester.addSetNext("Server",  
  22.                             "setServer",  
  23.                             "org.apache.catalina.Server");  
  24.         digester.addObjectCreate("Server/GlobalNamingResources",  
  25.                                  "org.apache.catalina.deploy.NamingResources");  
  26.         digester.addSetProperties("Server/GlobalNamingResources");  
  27.         digester.addSetNext("Server/GlobalNamingResources",  
  28.                             "setGlobalNamingResources",  
  29.                             "org.apache.catalina.deploy.NamingResources");  
  30.         digester.addObjectCreate("Server/Listener",  
  31.                                  null// MUST be specified in the element  
  32.                                  "className");  
  33.         digester.addSetProperties("Server/Listener");  
  34.         digester.addSetNext("Server/Listener",  
  35.                             "addLifecycleListener",  
  36.                             "org.apache.catalina.LifecycleListener");  
  37.         digester.addObjectCreate("Server/Service",  
  38.                                  "org.apache.catalina.core.StandardService",  
  39.                                  "className");  
  40.         digester.addSetProperties("Server/Service");  
  41.         digester.addSetNext("Server/Service",  
  42.                             "addService",  
  43.                             "org.apache.catalina.Service");  
  44.         digester.addObjectCreate("Server/Service/Listener",  
  45.                                  null// MUST be specified in the element  
  46.                                  "className");  
  47.         digester.addSetProperties("Server/Service/Listener");  
  48.         digester.addSetNext("Server/Service/Listener",  
  49.                             "addLifecycleListener",  
  50.                             "org.apache.catalina.LifecycleListener");  
  51.         //Executor  
  52.         digester.addObjectCreate("Server/Service/Executor",  
  53.                          "org.apache.catalina.core.StandardThreadExecutor",  
  54.                          "className");  
  55.         digester.addSetProperties("Server/Service/Executor");  
  56.         digester.addSetNext("Server/Service/Executor",  
  57.                             "addExecutor",  
  58.                             "org.apache.catalina.Executor");  
  59.           
  60.         digester.addRule("Server/Service/Connector",  
  61.                          new ConnectorCreateRule());  
  62.         digester.addRule("Server/Service/Connector",   
  63.                          new SetAllPropertiesRule(new String[]{"executor"}));  
  64.         digester.addSetNext("Server/Service/Connector",  
  65.                             "addConnector",  
  66.                             "org.apache.catalina.connector.Connector");  
  67.           
  68.           
  69.         digester.addObjectCreate("Server/Service/Connector/Listener",  
  70.                                  null// MUST be specified in the element  
  71.                                  "className");  
  72.         digester.addSetProperties("Server/Service/Connector/Listener");  
  73.         digester.addSetNext("Server/Service/Connector/Listener",  
  74.                             "addLifecycleListener",  
  75.                             "org.apache.catalina.LifecycleListener");  
  76.         // Add RuleSets for nested elements  
  77.         digester.addRuleSet(new NamingRuleSet("Server/GlobalNamingResources/"));  
  78.         digester.addRuleSet(new EngineRuleSet("Server/Service/"));  
  79.         digester.addRuleSet(new HostRuleSet("Server/Service/Engine/"));  
  80.         digester.addRuleSet(new ContextRuleSet("Server/Service/Engine/Host/"));  
  81.         digester.addRuleSet(ClusterRuleSetFactory.getClusterRuleSet("Server/Service/Engine/Host/Cluster/"));  
  82.         digester.addRuleSet(new NamingRuleSet("Server/Service/Engine/Host/Context/"));  
  83.         // When the 'engine' is found, set the parentClassLoader.  
  84.         digester.addRule("Server/Service/Engine",  
  85.                          new SetParentClassLoaderRule(parentClassLoader));  
  86.         digester.addRuleSet(ClusterRuleSetFactory.getClusterRuleSet("Server/Service/Engine/Cluster/"));  
  87.         long t2=System.currentTimeMillis();  
  88.         if (log.isDebugEnabled())  
  89.             log.debug("Digester for server.xml created " + ( t2-t1 ));  
  90.         return (digester);  
  91.     }  

先大概讲一下该方法的内容:

1.先实例化一个Digester类的对象,调用其setValidating、setClassLoader等方法设置其某些属性,这里不是本文的重点,因此不准备细讲。

2.调用了其addObjectCreate、addSetProperties、addSetNext、addRule及addRuleSet等方法

还记得在之前我们讲到Digester类的startElement等方法时,有一点讲到该类根据标签路径名获取到一年Rule类的List后,分别调用其begin、body及end方法,那个Rule List从何而来呢,答案即将揭晓,我们这里将通过详细讲解Digester的addObjectCreate等方法来向你揭示这些Ruler的意义。因为下面这些方法均调用多次,这里将只举例说明其作用。

如:
 

[java] view plaincopy
  1. digester.addObjectCreate("Server",  
  2.                                  "org.apache.catalina.core.StandardServer",  
  3.                                  "className");  

  方法内容如下:

[java] view plaincopy
  1. /** 
  2.     * Add an "object create" rule for the specified parameters. 
  3.     * 
  4.     * @param pattern Element matching pattern 
  5.     * @param className Default Java class name to be created 
  6.     * @param attr<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>ibuteName Attribute name that optionally overrides 
  7.     *  the default Java class name to be created 
  8.     * @see ObjectCreateRule 
  9.     */  
  10.    public void addObjectCreate(String pattern, String className,  
  11.                                String attributeName) {  
  12.        addRule(pattern,  
  13.                new ObjectCreateRule(className, attributeName));  
  14.    }  
 

    在这里将调用addRule方法,该方法如下:
    

[java] view plaincopy
  1. /** 
  2.      * <p>Register a new Rule matching the specified pattern. 
  3.      * This method sets the <code>Digester</code> property on the rule.</p> 
  4.      * 
  5.      * @param pattern Element matching pattern 
  6.      * @param rule Rule to be registered 
  7.      */  
  8.     public void addRule(String pattern, Rule rule) {  
  9.         rule.setDigester(this);  
  10.         getRules().add(pattern, rule);  
  11.     }  
 

    先看getRules(),该方法如下:

[java] view plaincopy
  1. /** 
  2.     * Return the <code>Rules</code> implementation object containing our 
  3.     * rules collection and associated matching policy.  If none has been 
  4.     * established, a default implementation will be created and returned. 
  5.     */  
  6.    public Rules getRules() {  
  7.        if (this.rules == null) {  
  8.            this.rules = new RulesBase();  
  9.            this.rules.setDigester(this);  
  10.        }  
  11.        return (this.rules);  
  12.    }  

我们可以看到该方法用于实例化一个RuleBase类对象,并且可以了解该方法以后每一次调用都只返回第一次生成的对象,因此是一个单例对象,在getRule方法返回后又调用了add方法,此时调用的将是RuleBase.add方法,该方法如下

    

[java] view plaincopy
  1. /** 
  2.      * Register a new Rule instance matching the specified pattern. 
  3.      * 
  4.      * @param pattern Nesting pattern to be matched for this Rule 
  5.      * @param rule Rule instance to be registered 
  6.      */  
  7.     public void add(String pattern, Rule rule) {  
  8.         // to help users who accidently add '/' to the end of their patterns  
  9.         int patternLength = pattern.length();  
  10.         if (patternLength>1 && pattern.endsWith("/")) {  
  11.             pattern = pattern.substring(0, patternLength-1);  
  12.         }  
  13.           
  14.           
  15.         List list = (List) cache.get(pattern);  
  16.         if (list == null) {  
  17.             list = new ArrayList();  
  18.             cache.put(pattern, list);  
  19.         }  
  20.         list.add(rule);  
  21.         rules.add(rule);  
  22.         if (this.digester != null) {  
  23.             rule.setDigester(this.digester);  
  24.         }  
  25.         if (this.namespaceURI != null) {  
  26.             rule.setNamespaceURI(this.namespaceURI);  
  27.         }  
  28.     }  
    

RuleBase类里挂有一个变量名为cache的HashMap,该Map将以pattern为key,各种继承Rule类对象组成的List为value.我们再看一下在Digester.startElement里有如下语句:

List rules = getRules().match(namespaceURI, match);

如上边的解释,getRule方法将返回单例的RuleBase对象,然后以标签路径名调用其match方法,该方法内容如下:

[java] view plaincopy
  1. /** 
  2.      * Return a List of all registered Rule instances that match the specified 
  3.      * nesting pattern, or a zero-length List if there are no matches.  If more 
  4.      * than one Rule instance matches, they <strong>must</strong> be returned 
  5.      * in the order originally registered through the <code>add()</code> 
  6.      * method. 
  7.      * 
  8.      * @param namespaceURI Namespace URI for which to select matching rules, 
  9.      *  or <code>null</code> to match regardless of namespace URI 
  10.      * @param pattern Nesting pattern to be matched 
  11.      */  
  12.     public List match(String namespaceURI, String pattern) {  
  13.         // List rulesList = (List) this.cache.get(pattern);  
  14.         List rulesList = lookup(namespaceURI, pattern);  
  15.         if ((rulesList == null) || (rulesList.size() < 1)) {  
  16.             // Find the longest key, ie more discriminant  
  17.             String longKey = "";  
  18.             Iterator keys = this.cache.keySet().iterator();  
  19.             while (keys.hasNext()) {  
  20.                 String key = (String) keys.next();  
  21.                 if (key.startsWith("*/")) {  
  22.                     if (pattern.equals(key.substring(2)) ||  
  23.                         pattern.endsWith(key.substring(1))) {  
  24.                         if (key.length() > longKey.length()) {  
  25.                             // rulesList = (List) this.cache.get(key);  
  26.                             rulesList = lookup(namespaceURI, key);  
  27.                             longKey = key;  
  28.                         }  
  29.                     }  
  30.                 }  
  31.             }  
  32.         }  
  33.         if (rulesList == null) {  
  34.             rulesList = new ArrayList();  
  35.         }  
  36.         return (rulesList);  
  37.     }  

该方法将主要以调用lookup方法来完成获取对应Rule List的工作,lookup方法如下:

[java] view plaincopy
  1. /** 
  2.      * Return a List of Rule instances for the specified pattern that also 
  3.      * match the specified namespace URI (if any).  If there are no such 
  4.      * rules, return <code>null</code>. 
  5.      * 
  6.      * @param namespaceURI Namespace URI to match, or <code>null</code> to 
  7.      *  select matching rules regardless of namespace URI 
  8.      * @param pattern Pattern to be matched 
  9.      */  
  10.     protected List lookup(String namespaceURI, String pattern) {  
  11.         // Optimize when no namespace URI is specified  
  12.         List list = (List) this.cache.get(pattern);  
  13.         if (list == null) {  
  14.             return (null);  
  15.         }  
  16.         if ((namespaceURI == null) || (namespaceURI.length() == 0)) {  
  17.             return (list);  
  18.         }  
  19.         // Select only Rules that match on the specified namespace URI  
  20.         ArrayList results = new ArrayList();  
  21.         Iterator items = list.iterator();  
  22.         while (items.hasNext()) {  
  23.             Rule item = (Rule) items.next();  
  24.             if ((namespaceURI.equals(item.getNamespaceURI())) ||  
  25.                     (item.getNamespaceURI() == null)) {  
  26.                 results.add(item);  
  27.             }  
  28.         }  
  29.         return (results);  
  30.     }  

我们可以看到,该方法将以标签路径名pattern为key值,从cache中取出对应的Rule List,如果我们看一下Catalina.createStartDigester各方法调用时的传入的pattern值,你会发现是到server.xml一一匹配的,据此我们可大概地理解到这个框架的实现方式:

1.实例化Digester

2.以要解析的xml的结构为准,调用Digester类各add方法加入对应的Rule.

3.调用Digester.parse方法解析xml文件,则解析每一个标签时对每一个解析动作将触发对应Rule的begin、body及end方法。

尽管理解了该框架,但我们仍然需要看一下在Catalina.createStartDigester方法加入了哪些Rule来确定在解析该文件时服务器做了多少事及这些事对整个服务器的启动有何意义。下面我们先看一下其中几个比较重要的调用,如

1.addObjectCreate,有如下的调用

[java] view plaincopy
  1. digester.addObjectCreate("Server",  
  2.                                  "org.apache.catalina.core.StandardServer",  
  3.                                  "className");  
 

所加入的Rule实现类为ObjectCreateRule,该类有begin方法如下

    

[java] view plaincopy
  1. /** 
  2.      * Process the beginning of this element. 
  3.      * 
  4.      * @param attributes The attribute list of this element 
  5.      */  
  6.     public void begin(Attributes attributes) throws Exception {  
  7.         // Identify the name of the class to instantiate  
  8.         String realClassName = className;  
  9.         if (attributeName != null) {  
  10.             String value = attributes.getValue(attributeName);  
  11.             if (value != null) {  
  12.                 realClassName = value;  
  13.             }  
  14.         }  
  15.         if (digester.log.isDebugEnabled()) {  
  16.             digester.log.debug("[ObjectCreateRule]{" + digester.match +  
  17.                     "}New " + realClassName);  
  18.         }  
  19.         // Instantiate the new object and push it on the context stack  
  20.         Class clazz = digester.getClassLoader().loadClass(realClassName);  
  21.         Object instance = clazz.newInstance();  
  22.         digester.push(instance);  
  23.     }  
 

    可以看到该方法内容为实例化一个类,类名可为实例化该类时初始化的className对象或在调用其begin方法时传入的Attribute对象(该对象用以表示标签的属性)里className对应的类名,实例化类后又调用digester.push()方法,我们在tomcat解析四中已看到过该方法,该方法会在digester.parse之前调用,参数为已实例化的Catalina对象。

该类又有end方法如下:

[java] view plaincopy
  1. /** 
  2.      * Process the end of this element. 
  3.      */  
  4.     public void end() throws Exception {  
  5.         Object top = digester.pop();  
  6.         if (digester.log.isDebugEnabled()) {  
  7.             digester.log.debug("[ObjectCreateRule]{" + digester.match +  
  8.                     "} Pop " + top.getClass().getName());  
  9.         }  
  10.     }  
 

可以看到在解析Server的开始标签时将产生一个org.apache.catalina.core.StandardServer实例,并将之放到栈中,在结束标签时又将它从栈中取,之所以放于栈中是因为后续还要对该对象进行很多处理,如下面将讲到的

2.addSetProperties

这个方法将加入的Rule实现类为SetPropertiesRule,这里先不细讲该类啦
3.addSetNext,如
该方法将添加的实现类为SetNextRule类,该类没有begin方法,因此在解析开始标签的时候没有对应的动作,但该类有end()方法,该方法是在解析结束标签的事件方法endElement()里调用的,SetNextRule类的end()方法如下:

   

[java] view plaincopy
  1. /** 
  2.     * Process the end of this element. 
  3.     */  
  4.    public void end() throws Exception {  
  5.        // Identify the objects to be used  
  6.        Object child = digester.peek(0);  
  7.        Object parent = digester.peek(1);  
  8.        if (digester.log.isDebugEnabled()) {  
  9.            if (parent == null) {  
  10.                digester.log.debug("[SetNextRule]{" + digester.match +  
  11.                        "} Call [NULL PARENT]." +  
  12.                        methodName + "(" + child + ")");  
  13.            } else {  
  14.                digester.log.debug("[SetNextRule]{" + digester.match +  
  15.                        "} Call " + parent.getClass().getName() + "." +  
  16.                        methodName + "(" + child + ")");  
  17.            }  
  18.        }  
  19.        // Call the specified method  
  20.        IntrospectionUtils.callMethod1(parent, methodName,  
  21.                child, paramType, digester.getClassLoader());  
  22.                  
  23.    }  
 

    该方法内容为:取出stack顶部的对象,再取出其前一个对象,然后调用前一个对象的方法,方法名为methodName,比如有如下的调用(事实是有的)

[java] view plaincopy
  1. digester.addSetNext("Server",  
  2.                             "setServer",  
  3.                             "org.apache.catalina.Server");  

这段代码将在解析Server标签的时候触发,此时在栈中的对象有两个,一个是最先放入的Catalina对象,而另一个是在addSetNext调用前加入的ObjectCreateRule类所实例化的org.apache.catalina.core.StandardServer实例,因此此时调用Catalina.setServer方法,以StandardServer实例为参数。这里是一个非常巧妙的设计,通过相邻的标签来生成对应的对象,并且让上一个对象作为父对象持有另一对象,因此在加入每一个RULE的时候都必须精妙地设计加入的顺序.

4.addRule,比如:
 这个直接对某一个pattern加入对应的Rule,无也讲

5.addRuleSet,如
 

[java] view plaincopy
  1. digester.addRuleSet(new NamingRuleSet("Server/GlobalNamingResources/"));  
 

 addRuleSet代码如下:

[java] view plaincopy
  1. /** 
  2.      * Register a set of Rule instances defined in a RuleSet. 
  3.      * 
  4.      * @param ruleSet The RuleSet instance to configure from 
  5.      */  
  6.     public void addRuleSet(RuleSet ruleSet) {  
  7.         String oldNamespaceURI = getRuleNamespaceURI();  
  8.         String newNamespaceURI = ruleSet.getNamespaceURI();  
  9.         if (log.isDebugEnabled()) {  
  10.             if (newNamespaceURI == null) {  
  11.                 log.debug("addRuleSet() with no namespace URI");  
  12.             } else {  
  13.                 log.debug("addRuleSet() with namespace URI " + newNamespaceURI);  
  14.             }  
  15.         }  
  16.         setRuleNamespaceURI(newNamespaceURI);  
  17.         ruleSet.addRuleInstances(this);  
  18.         setRuleNamespaceURI(oldNamespaceURI);  
  19.     }  
 

可以看到又调用了RuleSet实现类的addRuleInstances方法,以EngineRuleSet为例,其addRuleInstances方法如下:

   

[java] view plaincopy
  1. /** 
  2.     * <p>Add the set of Rule instances defined in this RuleSet to the 
  3.     * specified <code>Digester</code> instance, associating them with 
  4.     * our namespace URI (if any).  This method should only be called 
  5.     * by a Digester instance.</p> 
  6.     * 
  7.     * @param digester Digester instance to which the new Rule instances 
  8.     *  should be added. 
  9.     */  
  10.    public void addRuleInstances(Digester digester) {  
  11.        digester.addObjectCreate(prefix + "Ejb",  
  12.                                 "org.apache.catalina.deploy.ContextEjb");  
  13.        digester.addRule(prefix + "Ejb"new SetAllPropertiesRule());  
  14.        digester.addRule(prefix + "Ejb",  
  15.                new SetNextNamingRule("addEjb",  
  16.                            "org.apache.catalina.deploy.ContextEjb"));  
  17.        digester.addObjectCreate(prefix + "Environment",  
  18.                                 "org.apache.catalina.deploy.ContextEnvironment");  
  19.        digester.addSetProperties(prefix + "Environment");  
  20.        digester.addRule(prefix + "Environment",  
  21.                            new SetNextNamingRule("addEnvironment",  
  22.                            "org.apache.catalina.deploy.ContextEnvironment"));  
  23.        digester.addObjectCreate(prefix + "LocalEjb",  
  24.                                 "org.apache.catalina.deploy.ContextLocalEjb");  
  25.        digester.addRule(prefix + "LocalEjb"new SetAllPropertiesRule());  
  26.        digester.addRule(prefix + "LocalEjb",  
  27.                new SetNextNamingRule("addLocalEjb",  
  28.                            "org.apache.catalina.deploy.ContextLocalEjb"));  
  29.        digester.addObjectCreate(prefix + "Resource",  
  30.                                 "org.apache.catalina.deploy.ContextResource");  
  31.        digester.addRule(prefix + "Resource"new SetAllPropertiesRule());  
  32.        digester.addRule(prefix + "Resource",  
  33.                new SetNextNamingRule("addResource",  
  34.                            "org.apache.catalina.deploy.ContextResource"));  
  35.        digester.addObjectCreate(prefix + "ResourceEnvRef",  
  36.            "org.apache.catalina.deploy.ContextResourceEnvRef");  
  37.        digester.addRule(prefix + "ResourceEnvRef"new SetAllPropertiesRule());  
  38.        digester.addRule(prefix + "ResourceEnvRef",  
  39.                new SetNextNamingRule("addResourceEnvRef",  
  40.                            "org.apache.catalina.deploy.ContextResourceEnvRef"));  
  41.        digester.addObjectCreate(prefix + "ServiceRef",  
  42.            "org.apache.catalina.deploy.ContextService");  
  43.        digester.addRule(prefix + "ServiceRef"new SetAllPropertiesRule());  
  44.        digester.addRule(prefix + "ServiceRef",  
  45.                new SetNextNamingRule("addService",  
  46.                            "org.apache.catalina.deploy.ContextService"));  
  47.        digester.addObjectCreate(prefix + "Transaction",  
  48.            "org.apache.catalina.deploy.ContextTransaction");  
  49.        digester.addRule(prefix + "Transaction"new SetAllPropertiesRule());  
  50.        digester.addRule(prefix + "Transaction",  
  51.                new SetNextNamingRule("setTransaction",  
  52.                            "org.apache.catalina.deploy.ContextTransaction"));  
  53.    }  
 

可以得知addRuleSet方法主要是用于为某一想同前辍的标签加入一批的Rule实现类
我们没有篇幅来说明tomcat做的每一件事,因此这里我将影响到后续服务器启动的一些Rule拿出来讲一下。

1.对<Server>标签

[java] view plaincopy
  1. // Configure the actions we will be using  
  2.         digester.addObjectCreate("Server",  
  3.                                  "org.apache.catalina.core.StandardServer",  
  4.                                  "className");  
  5.         digester.addSetProperties("Server");  
  6.         digester.addSetNext("Server",  
  7.                             "setServer",  
  8.                             "org.apache.catalina.Server");  
 

实例化了一个org.apache.catalina.core.StandardServer,并以该对象为参数调用Catalina类的setServer方法,在这里我们可以回想一下在tomcat解析四里我们说的server变量所引用的对象是如何得到的呢?该对象有一个设置方法,而该方法即是setServer,因此后续整个服务器的启动重任将落于刚刚新生的org.apache.catalina.core.StandardServer对象

2.对Service标签

[java] view plaincopy
  1. digester.addObjectCreate("Server/Service",  
  2.                                  "org.apache.catalina.core.StandardService",  
  3.                                  "className");  
  4.         digester.addSetProperties("Server/Service");  
  5.         digester.addSetNext("Server/Service",  
  6.                             "addService",  
  7.                             "org.apache.catalina.Service");  
 

实例化了一个org.apache.catalina.core.StandardService对象,放入栈里,并以自身为参数调用栈里上一个对象的addService方法,通过server.xml我们可以了解到<service>标签是位于<server>标签之内的,因此此时StandardService的上一个对象为StandardServer对象(该对象要到结束标签时才会被取出来,这又是一个很巧妙的设计,让xml文件里有上下级关系的两个对象实例化后又存在上下级关系,或者是互持有的关系)

3.对Engine标签有:

[java] view plaincopy
  1. digester.addRuleSet(new EngineRuleSet("Server/Service/"));  
 

将会加下以下动作:

[java] view plaincopy
  1. /** 
  2.      * <p>Add the set of Rule instances defined in this RuleSet to the 
  3.      * specified <code>Digester</code> instance, associating them with 
  4.      * our namespace URI (if any).  This method should only be called 
  5.      * by a Digester instance.</p> 
  6.      * 
  7.      * @param digester Digester instance to which the new Rule instances 
  8.      *  should be added. 
  9.      */  
  10.     public void addRuleInstances(Digester digester) {  
  11.           
  12.         digester.addObjectCreate(prefix + "Engine",  
  13.                                  "org.apache.catalina.core.StandardEngine",  
  14.                                  "className");  
  15.         digester.addSetProperties(prefix + "Engine");  
  16.         digester.addRule(prefix + "Engine",  
  17.                          new LifecycleListenerRule  
  18.                          ("org.apache.catalina.startup.EngineConfig",  
  19.                           "engineConfigClass"));  
  20.         digester.addSetNext(prefix + "Engine",  
  21.                             "setContainer",  
  22.                             "org.apache.catalina.Container");  
  23.         //Cluster configuration start  
  24.         digester.addObjectCreate(prefix + "Engine/Cluster",  
  25.                                  null// MUST be specified in the element  
  26.                                  "className");  
  27.         digester.addSetProperties(prefix + "Engine/Cluster");  
  28.         digester.addSetNext(prefix + "Engine/Cluster",  
  29.                             "setCluster",  
  30.                             "org.apache.catalina.Cluster");  
  31.         //Cluster configuration end  
  32.         digester.addObjectCreate(prefix + "Engine/Listener",  
  33.                                  null// MUST be specified in the element  
  34.                                  "className");  
  35.         digester.addSetProperties(prefix + "Engine/Listener");  
  36.         digester.addSetNext(prefix + "Engine/Listener",  
  37.                             "addLifecycleListener",  
  38.                             "org.apache.catalina.LifecycleListener");  
  39.         digester.addRuleSet(new RealmRuleSet(prefix + "Engine/"));  
  40.         digester.addObjectCreate(prefix + "Engine/Valve",  
  41.                                  null// MUST be specified in the element  
  42.                                  "className");  
  43.         digester.addSetProperties(prefix + "Engine/Valve");  
  44.         digester.addSetNext(prefix + "Engine/Valve",  
  45.                             "addValve",  
  46.                             "org.apache.catalina.Valve");  
  47.     }  
 

内容有:实例化org.apache.catalina.core.StandardEngine对象,实例化org.apache.catalina.startup.EngineConfig对象,以该对象为参数,调用StandardEngine的addLifecycleListener方法,以StandardEngine对象为参数,调用StandardService.setContainer方法

4.对Host标签

内容有:内容有:实例化org.apache.catalina.core.StandardHost对象,实例化org.apache.catalina.startup.HostConfig对象,以该对象为参数,调用StandardHost的addLifecycleListener方法,以StandardHost对象为参数,调用StandardEngine.addChild方法

5.对Context标签

内容有:内容有:实例化org.apache.catalina.core.StandardContext对象,实例化org.apache.catalina.startup.ContextConfig对象,以该对象为参数,调用StandardContext的addLifecycleListener方法,以StandardContext对象为参数,调用StandardHost.addChild方法

 

方法总结:自上而下地实例了一些tomcat启动及处理客户请求的处理类,其中有StandardHost(表示着一个http访问的主机)、HostConfig、StandardContext(java web中的虚拟目录)及ContextConfig等,我们后续将会介绍各个主要的启动类

转载:http://blog.csdn.net/holly2k/article/details/5258849

0 0
原创粉丝点击