`
flywangfei
  • 浏览: 4540 次
社区版块
存档分类
最新评论

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;
}
 
1
28
分享到:
评论

相关推荐

    tomcat 源码分析系列文档

    【标签】"tomcat源码分析"表明整个资料集专注于Tomcat的源代码级探索,适合于开发者或运维人员深入了解服务器的底层实现。 【文件名称列表】中的每个文档都对应一个特定主题: 1. "Tomcat处理HTTP请求源码分析.doc...

    TOMCAT源码分析(启动框架)

    【TOMCAT源码分析(启动框架)】 Tomcat是一款广泛应用的开源Java Servlet容器,它实现了Java Servlet和JavaServer Pages(JSP)规范,为Web应用程序提供了运行环境。本篇文章将深入探讨Tomcat的系统框架及其启动流程...

    Tomcat源码分析

    【标题】"Tomcat源码分析" 在深入探讨Tomcat源码之前,首先需要了解Tomcat是什么。Tomcat是一款开源的、基于Java的Web应用服务器,由Apache软件基金会开发。它实现了Java Servlet和JavaServer Pages(JSP)规范,...

    Tomcat源码分析1

    《Tomcat源码分析1——服务启动与架构详解》 Tomcat,作为一款广泛应用的开源Java Servlet容器,其内部架构和启动流程对于深入理解和优化Web应用程序至关重要。本文将重点解析Tomcat启动时的关键步骤和核心组件,...

    tomcat源码分析1

    《深入理解Tomcat源码分析1:Connector配置详解》 Tomcat,作为广泛使用的Java Servlet容器,其核心组件之一就是Connector,它负责处理Web服务器与客户端之间的通信。本篇文章将详细探讨Tomcat Connector的种类、...

    Tomcat源码分析_v4 完整版1

    【Tomcat源码分析_v4 完整版1】这篇文档主要探讨了Tomcat源码中的部分核心概念,包括XML解析框架Digester的工作原理及其在Java对象映射中的应用。以下是对这些知识点的详细说明: **Java对象创建与管理:** 在...

    TOMCAT源码分析(启动框架).pdf

    ### TOMCAT源码分析——启动框架详解 #### 一、前言 TOMCAT作为一款广泛使用的开源Java Servlet容器,其内部实现复杂且强大。本文旨在深入剖析TOMCAT的启动框架及其整体架构,帮助读者更好地理解其工作原理。...

    tomcat源码分析图谱

    tomcat的基础脚本分析 tomcat的源码启动分析 tomcat的web应用启动分析 tomcat的socket分析 tomcat的cocket与容器对接时序分析

    「Tomcat源码剖析」.pdf

    Tomcat源码剖析 : 整体架构 层层分析 源码解析 架构分析 (Http服务器功能:Socket通信(TCP/IP)、解析Http报文 Servlet容器功能:有很多Servlet(自带系统级Servlet+自定义Servlet),Servlet处理具体的业务逻辑...

    tomcat6源码分析

    《Tomcat6源码分析——深入理解Web服务器的运行机制》 Tomcat6作为Apache软件基金会的Jakarta项目的一部分,是一款广泛使用的Java Servlet容器,它实现了Java Servlet和JavaServer Pages(JSP)规范,为开发和部署...

    tomcat架构的源码分析

    ### Tomcat架构的源码分析 #### 一、Tomcat的架构概述 Tomcat作为一款广泛使用的开源Java Servlet容器,其内部架构设计简洁而高效。本文档将对Tomcat的架构进行详细介绍,并从源码层面深入分析其核心组成部分。...

    tomcat源码

    Apache Tomcat源码分析 Apache Tomcat是一款广泛应用的开源Java Servlet容器,它是Java EE Web应用程序的标准实现。Tomcat源码的深入理解对于Java Web开发者来说是至关重要的,它可以帮助我们了解HTTP服务器的工作...

Global site tag (gtag.js) - Google Analytics