一路跋山涉水,这是最后一章了。
关于tomcat的启动,有两个类,一个是Catalina类,一个是Bootstrap类。
理论上,两个类能够和到一起,可是为了支持多种执行模式,又把他们分开了。
为了让用户更方便的启动tomcat,还有批处理文件startup.bat(最终见到最顶层的模块了!!)
先说Catalina
它里面有一个Server组件(不懂的看第14章)。
同一时候在它的start方法中包括一个Digester对象(不懂的看第15章)用来解析conf/下的Server.xml(这个xml装配起Catalina类里面的那个Server组件)。
同一时候Catalina本身也有main方法,在执行main方法的时候须要加上启动參数。
可选start,stop,debug等等
我们看看process方法
public void process(String args[]) {
setCatalinaHome(); //设定Home与Base的值为user.dir
setCatalinaBase();
if (arguments(args)) //省略try catch
execute(); //检查args的格式
}
protected void execute() throws Exception {
if (starting)
start();
else if (stopping)
stop();
}
我们主要看怎样启动。
protected void start() { //省略了大量非核心代码 及try catch
...
// Create and execute our Digester
Digester digester = createStartDigester(); //这个要是不懂看15章
File file = configFile(); //指定文件为conf/server.xml
InputSource is =new InputSource("file://" + file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file);
is.setByteStream(fis);
digester.push(this); //把catalina自己放了进去
//catalina里有server组件
digester.parse(is);
fis.close();
...
// Start the new server
if (server instanceof Lifecycle) {
server.initialize();
// Wait for the server to be told to shut down
server.await(); //上面的英文再给大家解释一下吧
//除非收到SHUTDOWM的指令 就一直堵塞在这里
//这要是不懂 回去看第二三章
}
...
// Shut down the server
if (server instanceof Lifecycle) {
((Lifecycle) server).stop();
}
}
这里有一个问题
server.initialize();
server从哪里来的?
别急看createStartDigester方法。
protected Digester createStartDigester() {
// Initialize the digester
Digester digester = new Digester();
if (debug)
digester.setDebug(999);
digester.setValidating(false);
// Configure the actions we will be using
digester.addObjectCreate("Server",
"org.apache.catalina.core.StandardServer",
"className");
digester.addSetProperties("Server");
//解释一下以下
//在解析server.xml时 假设碰到Server这个模式
//就调用栈底元素的方法名为setServer參数为org.apache.catalina.Server的方法
//把Server自己(就是上面addObjectCreate产生的那个)注入栈底的那个元素
//那么栈底是什么呢?
//digester.push(this); 看看这行代码还有Catalian的setServer方法
digester.addSetNext("Server",
"setServer",
"org.apache.catalina.Server");
.....
return digester
}
如今说说Bootstrap
Bootstrap的main方法会实例化一个Catalina对象,并调用它的process方法。
在main方法中,首先会创建三个类载入器。
为什么?
就是为了不让应用程序中的类使用WEB-INF/Class和WEB-INF/lib之外的类。
详细怎么做的看第八章。
public static void main(String args[]) {// 省略非核心代码及try catch
// Construct the class loaders we will need
ClassLoader commonLoader = null;
ClassLoader catalinaLoader = null;
ClassLoader sharedLoader = null;
...
// Load our startup class and call its process() method
// Instantiate a startup class instance
if (debug >= 1)
log("Loading startup class");
Class startupClass =
catalinaLoader.loadClass
("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.newInstance(); //载入Catalina类
// Call the process() method
if (debug >= 1)
log("Calling startup class process() method");
methodName = "process";
paramTypes = new Class[1];
paramTypes[0] = args.getClass();
paramValues = new Object[1];
paramValues[0] = args;
method =
startupInstance.getClass().getMethod(methodName, paramTypes);
method.invoke(startupInstance, paramValues); //调用Catalina的process方法
}
关于startup.bat的知识,我们下一节再谈... 哎另一节呀