标签:tomcat源码
Catalina的作用是初始化各个组件,并开始启动各个组件。3,初始化命名空间(tomcat会使用JNDI技术,比如在server.xml中配置了数据库连接池的话,就使用了JNDI)。最后还包装了System.out和System.err。
这里面的重点就是Digester解析server.xml的过程,先来看看start方法:
public void start() { //这里剔除了一些判断,日志,服务器钩子函数,等代码 ...... if (getServer() == null) { load(); } ...... getServer().start(); if (await) { await(); stop(); } }
public void load() { //这个方法的篇幅过长,我踢掉一些对流程不重要的代码 Digester digester = createStartDigester(); InputSource inputSource = null; InputStream inputStream = null; File file = null; try { //获取sever.xml配置文件 file = configFile(); inputStream = new FileInputStream(file); inputSource = new InputSource(file.toURI().toURL().toString()); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(sm.getString("catalina.configFail", file), e); } } //将sever.xml对应的流传给digester,由digester解析它。 inputSource.setByteStream(inputStream); //这个方法很重要,它将自己也就是Catalina这个对象放到了digester对象里的一个栈里面,后面解析xml实例化server后 //会从栈里拿出Catalina对象调用它的setServer方法来设置Catalina.server属性。server里的service属性也是通过这种形式 //设置的。 digester.push(this); digester.parse(inputSource); //server实例化后,将设置server.catalina属性,这里Catalina和Server是双向关联的。 getServer().setCatalina(this); // 包装了System.out和System.err initStreams(); getServer().init(); }
/** * Create and configure the Digester we will be using for startup. */ protected Digester createStartDigester() { Digester digester = new Digester(); digester.addObjectCreate("Server", "org.apache.catalina.core.StandardServer", "className"); digester.addSetProperties("Server"); digester.addSetNext("Server", "setServer", "org.apache.catalina.Server"); digester.addObjectCreate("Server/GlobalNamingResources", "org.apache.catalina.deploy.NamingResources"); digester.addSetProperties("Server/GlobalNamingResources"); digester.addSetNext("Server/GlobalNamingResources", "setGlobalNamingResources", "org.apache.catalina.deploy.NamingResources"); digester.addObjectCreate("Server/Listener", null, // MUST be specified in the element "className"); digester.addSetProperties("Server/Listener"); digester.addSetNext("Server/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener"); digester.addObjectCreate("Server/Service", "org.apache.catalina.core.StandardService", "className"); digester.addSetProperties("Server/Service"); digester.addSetNext("Server/Service", "addService", "org.apache.catalina.Service"); digester.addObjectCreate("Server/Service/Listener", null, // MUST be specified in the element "className"); digester.addSetProperties("Server/Service/Listener"); digester.addSetNext("Server/Service/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener"); //Executor digester.addObjectCreate("Server/Service/Executor", "org.apache.catalina.core.StandardThreadExecutor", "className"); digester.addSetProperties("Server/Service/Executor"); digester.addSetNext("Server/Service/Executor", "addExecutor", "org.apache.catalina.Executor"); digester.addRule("Server/Service/Connector", new ConnectorCreateRule()); digester.addRule("Server/Service/Connector", new SetAllPropertiesRule(new String[]{"executor"})); digester.addSetNext("Server/Service/Connector", "addConnector", "org.apache.catalina.connector.Connector"); digester.addObjectCreate("Server/Service/Connector/Listener", null, // MUST be specified in the element "className"); digester.addSetProperties("Server/Service/Connector/Listener"); digester.addSetNext("Server/Service/Connector/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener"); // Add RuleSets for nested elements digester.addRuleSet(new NamingRuleSet("Server/GlobalNamingResources/")); digester.addRuleSet(new EngineRuleSet("Server/Service/")); digester.addRuleSet(new HostRuleSet("Server/Service/Engine/")); digester.addRuleSet(new ContextRuleSet("Server/Service/Engine/Host/")); addClusterRuleSet(digester, "Server/Service/Engine/Host/Cluster/"); digester.addRuleSet(new NamingRuleSet("Server/Service/Engine/Host/Context/")); // When the 'engine' is found, set the parentClassLoader. digester.addRule("Server/Service/Engine", new SetParentClassLoaderRule(parentClassLoader)); addClusterRuleSet(digester, "Server/Service/Engine/Cluster/"); return (digester); }
digester实例化Server时是实例化了它的子类StanderServer。
进入addObjectCreate方法:
public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName)); }
每调用一个add*方法都为pattern映射了一个Rule的子类,addSetProperties,addSetNext都为pattern映射了一个Rule的子类。
这个映射关系被存放在RulesBase.cache中:
public class RulesBase implements Rules { protected HashMap<String,List<Rule>> cache = new HashMap<String,List<Rule>>(); }
public abstract class Rule { public Digester getDigester() { return (this.digester); } public void begin(String namespace, String name, Attributes attributes) throws Exception { begin(attributes); } public void end(String namespace, String name) throws Exception { end(); } }
digester.addObjectCreate("Server", "org.apache.catalina.core.StandardServer", "className"); digester.addSetProperties("Server"); digester.addSetNext("Server", "setServer", "org.apache.catalina.Server");
public void begin(String namespace, String name, Attributes attributes) throws Exception { Class<?> clazz = digester.getClassLoader().loadClass(realClassName); Object instance = clazz.newInstance(); digester.push(instance); }
@Override public void end(String namespace, String name) throws Exception { Object child = digester.peek(0); Object parent = digester.peek(1); if (digester.log.isDebugEnabled()) { if (parent == null) { digester.log.debug("[SetNextRule]{" + digester.match + "} Call [NULL PARENT]." + methodName + "(" + child + ")"); } else { digester.log.debug("[SetNextRule]{" + digester.match + "} Call " + parent.getClass().getName() + "." + methodName + "(" + child + ")"); } } IntrospectionUtils.callMethod1(parent, methodName, child, paramType, digester.getClassLoader()); }在调用这个方法前,degister的成员变量stack已经push和pop好几个对象了,每次标签开始解析时创建对象push到stack里面,标签结束后从stack里pop出来。因为<Server>标签是顶层标签,所以server对象最先被创建并push到stack里面(Catalina一直在栈底),最后被pop出来,所以结束标签</server>解析时和开始一样,stack依然还是两个元素,server在栈顶,Catalina在栈底。 Object child = digester.peek(0);Object parent = digester.peek(1);分别取出了栈顶和栈底的元素。server.xml的解析不是很好叙述,它的关系有点复杂。要记住一点server.xml的每一级标签对应一个tomcat组件,被外层标签包裹的标签是外层标签的一个属性比如serveice是server的一个属性。xml在被解析时会根据当前的事件(开始,或结束)来调用对应节点的解析规则,创建对象,并通过栈来完成对象之间的关联关系。
标签:tomcat源码
原文地址:http://blog.csdn.net/ilovezhangxian/article/details/40827195