tomcat(2)之Catalina.java

本文详细解析了Tomcat启动过程中的关键步骤,包括初始化目录、加载配置文件server.xml及服务器的初始化等。深入探讨了Bootstrap类及Catalina类的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个类位于org.apache.catalina.startup包中.
其实这个类是启动tomcat的真正的入口类.
从Bootstrap.java中看得出来,其实启动时只执行此类的load和start,并设置此对象的父加载器属性。
我们选来看此类的方法load有什么作。
--------------------------------------------------------
public void load(String args[]) {

try {
if (arguments(args))
load();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
注:从方法可以看出,它主要是通过传进来的参数看是否要不要执行load()这个无参的方法。arguments作用就指对不同的参数
进行属性附值操作,这里start参数是true,因此要执行load();
我们来看看load的源码:
-----------------------------------------------------------------------------------------------------------------------
public void load() {

long t1 = System.nanoTime(); //返回最准确的可用系统计时器的当前值,以毫微秒为单位。

initDirs(); //初始化tomcat的目录

// Before digester - it may be needed

initNaming();//初始化命名

// Create and execute our Digester
Digester digester = createStartDigester();

InputSource inputSource = null;
InputStream inputStream = null;
File file = null;
try {
file = configFile();
inputStream = new FileInputStream(file);
inputSource = new InputSource("file://" + file.getAbsolutePath()); //加载本置文件conf/server.xml
} catch (Exception e) {
;
}
if (inputStream == null) {
try {
inputStream = getClass().getClassLoader()
.getResourceAsStream(getConfigFile());
inputSource = new InputSource
(getClass().getClassLoader()
.getResource(getConfigFile()).toString());
} catch (Exception e) {
;
}
}

// This should be included in catalina.jar
// Alternative: don't bother with xml, just create it manually.
if( inputStream==null ) {
try {
inputStream = getClass().getClassLoader()
.getResourceAsStream("server-embed.xml");
inputSource = new InputSource
(getClass().getClassLoader()
.getResource("server-embed.xml").toString());
} catch (Exception e) {
;
}
}


if ((inputStream == null) && (file != null)) {
log.warn("Can't load server.xml from " + file.getAbsolutePath());
return;
}

try {
inputSource.setByteStream(inputStream);
digester.push(this);
digester.parse(inputSource);
inputStream.close();
} catch (Exception e) {
log.warn("Catalina.start using "
+ getConfigFile() + ": " , e);
return;
}

// Stream redirection
initStreams();

// Start the new server
if (server instanceof Lifecycle) {
try {
server.initialize();
} catch (LifecycleException e) {
log.error("Catalina.start", e);
}
}

long t2 = System.nanoTime();
if(log.isInfoEnabled())
log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");

}

我们来一行行的分析上面的为码:
1)long t1 = System.nanoTime();//返回最准确的可用系统计时器的当前值,以毫微秒为单位。
此时间主要是用于计算启动的时间,从最后看,只是用于记录进日中用到,
if(log.isInfoEnabled())
log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");

2)initDirs();
初始化目录,也就是把一些目录作为属性写入system属性中,主要是tomcat的一些系统目录,如:
System.setProperty("catalina.base",
catalinaHome);
System.setProperty("catalina.home", catalinaHome);
把这些tomcat目录信息写入系统环境变量用,用于后面程序要用到。

3)initNaming();
也是把一些相关属性写入环境变量中,不过对于作用,目前并不太清楚,如:
System.setProperty("catalina.useNaming", "false");
System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);

4)Digester digester = createStartDigester();
这个类主要作用就是把配置文件server.xml转换成对象,以便TOMCAT使用和调用。这个类的使用细节有点复杂,目前只知道
它的作用就是从server.xml中把配置信息加载,并用于初始化server实例,至于初始化的细节要进一步的研究。
Server是一个接口,位于包:org.apache.catalina
他的实现类有:StandardServer,StandardService 都位于包:org.apache.catalina.core
如下:
public final class StandardServer
implements Lifecycle, Server, MBeanRegistration
---------------------------------------------------------
public class StandardService
implements Lifecycle, Service, MBeanRegistration
可见它们都实现了Lifecycle, Service, MBeanRegistration 三个接口。三个字面意思应为生命周期,服务,Mbean注册器。


总结:此类的作用也就是继Bootstrap.java过来,在启动时执行了load()和start();
load()的作用如下:
1)返回最准确的可用系统计时器的当前值,以毫微秒为单位。
2)初始化tomcat目录。
3)设置一些环境参数,有关于命名的。
4)得到Digester实例,用于解释server.xml文档,并初化服务器,设置server实例。
5)设置日志输出和错误输出的地方.
6)server.initialize(); //服务器进行初始化.

start()的作用如下:
1)执行server中的start方法:
((Lifecycle) server).start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值