Tomcat中xml的解析器Digester 第三篇(共三篇)
这篇研究Tomcat自己实现的Rule, 具体分析LifecycleListenerRule和典型的Rule调用。
1。Tomcat实现的Rule:
大部分都在org.apache.catalina.startup包下, 可见这些Rule在启动的时候占举足轻重的作用。
- 分析LifecycleListenerRule。 LifecycleListener是tomcat自定义的容器事件框架,所以它在容器中有广泛的用途和可以非常灵活的配置。
关键的begin方法:
1 利用className创建一个实例; 2 向下转型至LifecycleListener 3 将lifecycleListener利用addLifecycleListener放到栈顶的对象中。
/**
* Handle the beginning of an XML element.
*
* @param attributes The attributes of this element
*
* @exception Exception if a processing error occurs
*/
public void begin(String namespace, String name, Attributes attributes)
throws Exception {
// Instantiate a new LifecyleListener implementation object
String className = listenerClass;
if (attributeName != null) {
String value = attributes.getValue(attributeName);
if (value != null)
className = value;
}
Class clazz = Class.forName(className);
LifecycleListener listener =
(LifecycleListener) clazz.newInstance();
// Add this LifecycleListener to our associated component
Lifecycle lifecycle = (Lifecycle) digester.peek();
lifecycle.addLifecycleListener(listener);
}
3 org.apache.catalina.startup.Catalina中的使用 Catalina在load的时候调用createStartDigester组建Digester, 定义Catalina对应的配置XML和对象初始化的映射关系。
通过分析Catalina的 createStartDigester, 可以清楚地看到配置文件XML中各个属性对应的对象实现中的属性, 能更加深刻的理解Tomcat的配置。