tomcat源码分析

1.tomcat 启动类 org.apache.catalina.startup.Bootstrap  start

public void init() throws Exception {
setCatalinaHome();//设置启动路径
setCatalinaBase();//设置启动路径
initClassLoaders();//
Thread.currentThread().setContextClassLoader(catalinaLoader);
SecurityClassLoad.securityClassLoad(catalinaLoader);
if (log.isDebugEnabled())
log.debug("Loading startup class");
Class startupClass = catalinaLoader
.loadClass("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.newInstance();
if (log.isDebugEnabled())
log.debug("Setting startup class properties");
String methodName = "setParentClassLoader";
Class paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.ClassLoader");
Object paramValues[] = new Object[1];
paramValues[0] = sharedLoader;
Method method = startupInstance.getClass().getMethod(methodName,
paramTypes);
method.invoke(startupInstance, paramValues);
catalinaDaemon = startupInstance;
}
    public void start() throws Exception {
if (catalinaDaemon == null)
init();
Method method = catalinaDaemon.getClass().getMethod("start",
(Class[]) null);
method.invoke(catalinaDaemon, (Object[]) null);//通过反射启动org.apache.catalina.startup.catalina start
}
public void start() {
if (getServer() == null)
load();//启动catalina
if (getServer() == null) {
log.fatal("Cannot start server. Server instance is not configured.");
return;
}
long t1 = System.nanoTime();
if (getServer() instanceof Lifecycle)
try {
((Lifecycle) getServer()).start();
} catch (LifecycleException e) {
log.error("Catalina.start: ", e);
}
long t2 = System.nanoTime();
if (log.isInfoEnabled())
log.info((new StringBuilder()).append("Server startup in ")
.append((t2 - t1) / 1000000L).append(" ms").toString());
try {
if (useShutdownHook) {
if (shutdownHook == null)
shutdownHook = new CatalinaShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
LogManager logManager = LogManager.getLogManager();
if (logManager instanceof ClassLoaderLogManager)
((ClassLoaderLogManager) logManager)
.setUseShutdownHook(false);
}
} catch (Throwable t) {
}
if (await) {
await();
stop();
}
}
 
public void load() {
long t1 = System.nanoTime();
initDirs();//初始化目录
initNaming();//初始化命名空间
Digester digester = createStartDigester();//创建初始类
InputSource inputSource = null;
InputStream inputStream = null;
File file = null;
try {
file = configFile();//configFile = "conf/server.xml";
inputStream = new FileInputStream(file);
inputSource = new InputSource((new StringBuilder())
.append("file://").append(file.getAbsolutePath())
.toString());
} catch (Exception e) {
}
if (inputStream == null)
try {
inputStream = getClass().getClassLoader().getResourceAsStream(
getConfigFile());
inputSource = new InputSource(getClass().getClassLoader()
.getResource(getConfigFile()).toString());
} catch (Exception e) {
}
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((new StringBuilder())
.append("Can't load server.xml from ")
.append(file.getAbsolutePath()).toString());
if (file.exists() && !file.canRead())
log.warn("Permissions incorrect, read permission is not allowed on the file.");
return;
}
try {
inputSource.setByteStream(inputStream);
digester.push(this);
digester.parse(inputSource)//解析配置文件加载
inputStream.close();
} catch (Exception e) {
log.warn((new StringBuilder()).append("Catalina.start using ")
.append(getConfigFile()).append(": ").toString(), e);
return;
}
initStreams();
if (getServer() instanceof Lifecycle)//catalina 继承Embedded standardservice implements liftsycle
try {
getServer().initialize();//实例化server
} catch (LifecycleException e) {
if (Boolean
.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"))
throw new Error(e);
log.error("Catalina.start", e);
}
long t2 = System.nanoTime();
if (log.isInfoEnabled())
log.info((new StringBuilder())
.append("Initialization processed in ")
.append((t2 - t1) / 1000000L).append(" ms").toString());
}
    protected Digester createStartDigester() {//用来解析server.xml来注册和创建类
long t1 = System.currentTimeMillis();
Digester digester = new Digester();
digester.setValidating(false);
digester.setRulesValidation(true);
HashMap fakeAttributes = new HashMap();
ArrayList attrs = new ArrayList();
attrs.add("className");
fakeAttributes.put(java / lang / Object, attrs);
digester.setFakeAttributes(fakeAttributes);
digester.setClassLoader(org / apache / catalina / core
/ StandardServer.getClassLoader());
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, "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, "className");
digester.addSetProperties("Server/Service/Listener");
digester.addSetNext("Server/Service/Listener", "addLifecycleListener",
"org.apache.catalina.LifecycleListener");
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,
"className");
digester.addSetProperties("Server/Service/Connector/Listener");
digester.addSetNext("Server/Service/Connector/Listener",
"addLifecycleListener", "org.apache.catalina.LifecycleListener");
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/"));
digester.addRuleSet(ClusterRuleSetFactory
.getClusterRuleSet("Server/Service/Engine/Host/Cluster/"));
digester.addRuleSet(new NamingRuleSet(
"Server/Service/Engine/Host/Context/"));
digester.addRule("Server/Service/Engine", new SetParentClassLoaderRule(
parentClassLoader));
digester.addRuleSet(ClusterRuleSetFactory
.getClusterRuleSet("Server/Service/Engine/Cluster/"));
long t2 = System.currentTimeMillis();
if (log.isDebugEnabled())
log.debug((new StringBuilder())
.append("Digester for server.xml created ").append(t2 - t1)
.toString());
return digester;
}
 

你可能感兴趣的:(java,tomcat,源码,源码分析)