24 March 2016

TL;DR

Tomcat版本为8.0.28

  1. org.apache.catalina.startup.BootStrap
    1. 初始化catalina.home
      1. 获取参数catalina.home,判断相应目录是否存在,若不存在则
      2. 查看当前工作目录是否存在bootstrap.jar文件,若存在,则以父目录作为catalina.home的值,否则
      3. 使用当前目录作为catalina.home的值
    2. 初始化catalina.base
      1. 获取参数catalina.base,若值不为空,则根据其值设置相应的变量,否则
      2. 设置为catalina.home的值
    3. 调用init方法,初始化类加载器
      1. 加载catalina.properties
      2. 创建common类加载器(commonLoader)
        1. 查询common.loader属性的值,拆解其中的类路径,根据创建Repository实例组
        2. 遍历Repository实例组,去重,创建URLClassLoader实例
      3. 创建server类加载器(catalinaLoader)
        1. 查询server.loader属性的值,拆解其中的类路径,根据创建Repository实例组
        2. 遍历Repository实例组,去重,创建URLClassLoader实例
      4. 创建shared类加载器(sharedLoader)
        1. 查询shared.loader属性的值,拆解其中的类路径,根据创建Repository实例组
        2. 遍历Repository实例组,去重,创建URLClassLoader实例
      5. 设置线程上下文类加载器为catalinaLoader
      6. 使用catalinaLoader加载基础类
        1. loadCorePackage
          • org.apache.catalina.core.AccessLogAdapter
          • org.apache.catalina.core.ApplicationContextFacade$1
          • org.apache.catalina.core.ApplicationDispatcher$PrivilegedForward
          • org.apache.catalina.core.ApplicationDispatcher$PrivilegedInclude
          • org.apache.catalina.core.AsyncContextImpl
          • org.apache.catalina.core.AsyncContextImpl$DebugException
          • org.apache.catalina.core.AsyncContextImpl$1
          • org.apache.catalina.core.AsyncListenerWrapper
          • org.apache.catalina.core.ContainerBase$PrivilegedAddChild
          • org.apache.catalina.core.DefaultInstanceManager$1
          • org.apache.catalina.core.DefaultInstanceManager$2
          • org.apache.catalina.core.DefaultInstanceManager$3
          • org.apache.catalina.core.DefaultInstanceManager$AnnotationCacheEntry
          • org.apache.catalina.core.DefaultInstanceManager$AnnotationCacheEntryType
          • org.apache.catalina.core.ApplicationHttpRequest$AttributeNamesEnumerator
        2. loadCoyotePackage
          • org.apache.coyote.http11.AbstractOutputBuffer$1
          • org.apache.coyote.http11.Constants
          • org.apache.coyote.Constants
        3. loadLoaderPackage
          • org.apache.catalina.loader.WebappClassLoaderBase$PrivilegedFindResourceByName
        4. loadRealmPackage
          • org.apache.catalina.realm.LockOutRealm$LockRecord
        5. loadServletsPackage
          • org.apache.catalina.servlets.DefaultServlet
        6. loadSessionPackage
          • org.apache.catalina.session.StandardSession
          • org.apache.catalina.session.StandardSession$1
          • org.apache.catalina.session.StandardManager$PrivilegedDoUnload
        7. loadUtilPackage
          • org.apache.catalina.util.ParameterMap
          • org.apache.catalina.util.RequestUtil
        8. loadValvesPackage
          • org.apache.catalina.valves.AbstractAccessLogValve$3
        9. loadJavaxPackage
          • javax.servlet.http.Cookie
        10. loadConnectorPackage
          • org.apache.catalina.connector.RequestFacade$GetAttributePrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetParameterMapPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetRequestDispatcherPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetParameterPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetParameterNamesPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetCharacterEncodingPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetHeadersPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetHeaderNamesPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetCookiesPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetLocalePrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetLocalesPrivilegedAction
          • org.apache.catalina.connector.ResponseFacade$SetContentTypePrivilegedAction
          • org.apache.catalina.connector.ResponseFacade$DateHeaderPrivilegedAction
          • org.apache.catalina.connector.RequestFacade$GetSessionPrivilegedAction
          • org.apache.catalina.connector.ResponseFacade$1
          • org.apache.catalina.connector.OutputBuffer$1
          • org.apache.catalina.connector.CoyoteInputStream$1
          • org.apache.catalina.connector.CoyoteInputStream$2
          • org.apache.catalina.connector.CoyoteInputStream$3
          • org.apache.catalina.connector.CoyoteInputStream$4
          • org.apache.catalina.connector.CoyoteInputStream$5
          • org.apache.catalina.connector.InputBuffer$1
          • org.apache.catalina.connector.Response$1
          • org.apache.catalina.connector.Response$2
          • org.apache.catalina.connector.Response$3
        11. loadTomcatPackage
          • org.apache.tomcat.util.buf.HexUtils
          • org.apache.tomcat.util.buf.StringCache
          • org.apache.tomcat.util.buf.StringCache$ByteEntry
          • org.apache.tomcat.util.buf.StringCache$CharEntry
          • org.apache.tomcat.util.http.FastHttpDateFormat
          • org.apache.tomcat.util.http.HttpMessages
          • org.apache.tomcat.util.http.parser.HttpParser
          • org.apache.tomcat.util.http.parser.MediaType
          • org.apache.tomcat.util.http.parser.MediaTypeCache
          • org.apache.tomcat.util.http.parser.SkipResult
          • org.apache.tomcat.util.net.Constants
          • org.apache.tomcat.util.net.DispatchType
          • org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller$1
          • org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller$2
          • org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller$3
          • org.apache.tomcat.util.security.PrivilegedGetTccl
          • org.apache.tomcat.util.security.PrivilegedSetTccl
      7. 通过反射创建org.apache.catalina.startup.Catalina实例(startupInstance)
      8. 通过反射,以sharedLoader为实参,调用org.apache.catalina.startup.Catalina实例(startupInstance)的setParentClassLoader方法
      9. catalinaDaemon属性设置为org.apache.catalina.startup.Catalina实例(startupInstance)
    4. 加载配置,调用org.apache.catalina.startup.Catalina#load方法完成
  2. org.apache.catalina.startup.Catalina
    1. 使用Digester库解析server.xml文件
      1. 创建org.apache.tomcat.util.digester.Digester实例
      2. 加载各种Rule,根据server.xml文件的内容层次结构,设定遇到哪些标签时应该创建哪些实例
        • Server -> org.apache.catalina.core.StandardServer
          • Server/GlobalNamingResources -> org.apache.catalina.deploy.NamingResourcesImpl
            • Server/GlobalNamingResources/Ejb -> org.apache.tomcat.util.descriptor.web.ContextEjb
            • Server/GlobalNamingResources/Environment -> org.apache.tomcat.util.descriptor.web.ContextEnvironment
            • Server/GlobalNamingResources/LocalEjb -> org.apache.tomcat.util.descriptor.web.ContextLocalEjb
            • Server/GlobalNamingResources/Resource -> org.apache.tomcat.util.descriptor.web.ContextResource
            • Server/GlobalNamingResources/ResourceEnvRef -> org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef
            • Server/GlobalNamingResources/ServiceRef -> org.apache.tomcat.util.descriptor.web.ContextService
            • Server/GlobalNamingResources/Transaction -> org.apache.tomcat.util.descriptor.web.ContextTransaction
          • Server/Listener -> org.apache.catalina.LifecycleListener,监听器的具体类型由className属性决定
          • Server/Service -> org.apache.catalina.core.StandardService
            • Server/Service/Listener -> org.apache.catalina.LifecycleListener,监听器的具体类型由className属性决定
            • Server/Service/Executor -> org.apache.catalina.core.StandardThreadExecutor
            • Server/Service/Connector -> org.apache.catalina.connector.Connector
              • 根据不同协议(protocol属性的值)创建协议处理器
                • APR available
                  • HTTP/1.1 -> org.apache.coyote.http11.Http11AprProtocol
                  • AJP/1.3 -> org.apache.coyote.ajp.AjpAprProtocol
                  • 直接以protocol的值作为连接器实现类的类名
                  • protocol is null -> org.apache.coyote.http11.Http11AprProtocol
                • APR not available
                  • HTTP/1.1 -> org.apache.coyote.http11.Http11NioProtocol
                  • AJP/1.3 -> org.apache.coyote.ajp.AjpNioProtocol
                  • 直接以protocol的值作为连接器实现类的类名
              • Server/Service/Connector/Listener -> org.apache.catalina.LifecycleListener,监听器的具体类型由className属性决定
            • Server/Service/Engine -> org.apache.catalina.core.StandardEngine
              • Server/Service/Engine/Cluster -> org.apache.catalina.Cluster,根据属性名确定使用哪种集群管理器
                • Server/Service/Engine/Cluster/Manager -> org.apache.catalina.ha.ClusterManager,具体类型由className属性决定
                  • Server/Service/Engine/Cluster/Manager/SessionIdGenerator -> org.apache.catalina.util.StandardSessionIdGeneratorJSESSIONID生成器
                • Server/Service/Engine/Cluster/Channel -> org.apache.catalina.tribes.Channel,具体类型由className属性决定
                  • Server/Service/Engine/Cluster/Channel/Membership -> org.apache.catalina.tribes.MembershipService,具体类型由className属性决定
                  • Server/Service/Engine/Cluster/Channel/Sender -> org.apache.catalina.tribes.ChannelSender,具体类型由className属性决定
                    • Server/Service/Engine/Cluster/Channel/Sender/Transport -> org.apache.catalina.tribes.transport.MultiPointSender,具体类型由className属性决定
                  • Server/Service/Engine/Cluster/Channel/Receiver -> org.apache.catalina.tribes.ChannelReceiver,具体类型由className属性决定
                  • Server/Service/Engine/Cluster/Channel/Interceptor -> org.apache.catalina.tribes.ChannelInterceptor,具体类型由className属性决定
                    • Server/Service/Engine/Cluster/Channel/Interceptor/Member -> org.apache.catalina.tribes.Member,具体类型由className属性决定
                • Server/Service/Engine/Cluster/Valve -> org.apache.catalina.Valve,具体类型由className属性决定
                • Server/Service/Engine/Cluster/Deployer -> org.apache.catalina.ha.ClusterDeployer,具体类型由className属性决定
                • Server/Service/Engine/Cluster/Listener -> org.apache.catalina.LifecycleListener,具体类型由className属性决定
                • Server/Service/Engine/Cluster/ClusterListener -> org.apache.catalina.ha.ClusterListener,具体类型由className属性决定
              • Server/Service/Engine/Listener -> org.apache.catalina.LifecycleListener,监听器的具体类型由className属性决定
              • Server/Service/Engine/Valve -> org.apache.catalina.Valve,具体类型由className属性决定
              • Server/Service/Engine/Host -> 创建主机
                • Server/Service/Engine/Host/Context -> org.apache.catalina.core.StandardContext
                  • Server/Service/Engine/Host/Context/Listener -> org.apache.catalina.LifecycleListener, 监听器的具体类型由className属性决定
                  • Server/Service/Engine/Host/Context/Loader -> org.apache.catalina.loader.WebappLoader, 监听器的具体类型由className属性决定
                  • Server/Service/Engine/Host/Context/Manager -> org.apache.catalina.session.StandardManager
                    • Server/Service/Engine/Host/Context/Manager/Store -> org.apache.catalina.Store, 具体存储类型由className属性决定
                    • Server/Service/Engine/Host/Context/Manager/SessionIdGenerator -> org.apache.catalina.util.StandardSessionIdGeneratorJSESSIONID生成器
                  • Server/Service/Engine/Host/Context/Parameter -> org.apache.tomcat.util.descriptor.web.ApplicationParameter
                  • Server/Service/Engine/Host/Context/Resources -> org.apache.catalina.webresources.StandardRoot
                    • Server/Service/Engine/Host/Context/Resources/PreResources -> org.apache.catalina.WebResourceSet,具体类型由className属性决定
                    • Server/Service/Engine/Host/Context/Resources/JarResources -> org.apache.catalina.WebResourceSet,具体类型由className属性决定
                    • Server/Service/Engine/Host/Context/Resources/PostResources -> org.apache.catalina.WebResourceSet,具体类型由className属性决定
                  • Server/Service/Engine/Host/Context/ResourceLink -> org.apache.tomcat.util.descriptor.web.ContextResourceLink
                  • Server/Service/Engine/Host/Context/Valve -> org.apache.catalina.Valve,具体类型由className属性决定
                  • Server/Service/Engine/Host/Context/JarScanner -> org.apache.tomcat.util.scan.StandardJarScanner
                    • Server/Service/Engine/Host/Context/JarScanner/JarScanFilter -> org.apache.tomcat.util.scan.StandardJarScanFilter
                  • Server/Service/Engine/Host/Context/CookieProcessor -> org.apache.tomcat.util.http.LegacyCookieProcessor
                  • Server/Service/Engine/Host/Context/Ejb -> org.apache.tomcat.util.descriptor.web.ContextEjb
                  • Server/Service/Engine/Host/Context/Environment -> org.apache.tomcat.util.descriptor.web.ContextEnvironment
                  • Server/Service/Engine/Host/Context/LocalEjb -> org.apache.tomcat.util.descriptor.web.ContextLocalEjb
                  • Server/Service/Engine/Host/Context/Resource -> org.apache.tomcat.util.descriptor.web.ContextResource
                  • Server/Service/Engine/Host/Context/ResourceEnvRef -> org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef
                  • Server/Service/Engine/Host/Context/ServiceRef -> org.apache.tomcat.util.descriptor.web.ContextService
                  • Server/Service/Engine/Host/Context/Transaction -> org.apache.tomcat.util.descriptor.web.ContextTransaction
                • Server/Service/Engine/Host/Cluster -> 主机集群
                  • Server/Service/Engine/Host/Cluster/Manager -> org.apache.catalina.ha.ClusterManager,具体类型由className属性决定
                    • Server/Service/Engine/Host/Cluster/Manager/SessionIdGenerator -> org.apache.catalina.util.StandardSessionIdGeneratorJSESSIONID生成器
                  • Server/Service/Engine/Host/Cluster/Channel -> org.apache.catalina.tribes.Channel,具体类型由className属性决定
                    • Server/Service/Engine/Host/Cluster/Channel/Membership -> org.apache.catalina.tribes.MembershipService,具体类型由className属性决定
                    • Server/Service/Engine/Host/Cluster/Channel/Sender -> org.apache.catalina.tribes.ChannelSender,具体类型由className属性决定
                      • Server/Service/Engine/Host/Cluster/Channel/Sender/Transport -> org.apache.catalina.tribes.transport.MultiPointSender,具体类型由className属性决定
                    • Server/Service/Engine/Host/Cluster/Channel/Receiver -> org.apache.catalina.tribes.ChannelReceiver,具体类型由className属性决定
                    • Server/Service/Engine/Host/Cluster/Channel/Interceptor -> org.apache.catalina.tribes.ChannelInterceptor,具体类型由className属性决定
                      • Server/Service/Engine/Host/Cluster/Channel/Interceptor/Member -> org.apache.catalina.tribes.Member,具体类型由className属性决定
                  • Server/Service/Engine/Host/Cluster/Valve -> org.apache.catalina.Valve,具体类型由className属性决定
                  • Server/Service/Engine/Host/Cluster/Deployer -> org.apache.catalina.ha.ClusterDeployer,具体类型由className属性决定
                  • Server/Service/Engine/Host/Cluster/Listener -> org.apache.catalina.LifecycleListener,具体类型由className属性决定
                  • Server/Service/Engine/Host/Cluster/ClusterListener -> org.apache.catalina.ha.ClusterListener,具体类型由className属性决定
    2. 设置catalinaHomecatalinaBase
    3. 重定向标准输出和标准错误到系统日志(SystemLogHandler
    4. 初始化serverorg.apache.catalina.core.StandardServer
      1. 注册MBean(type=StringCachetype=MBeanFactory)
      2. 初始化globalNamingResources(org.apache.catalina.deploy.NamingResourcesImpl)
        • 注册MBean
      3. 初始化services(org.apache.catalina.Service)
        1. 初始化container
          • org.apache.catalina.core.StandardEngine
          • org.apache.catalina.core.StandardHost
          • org.apache.catalina.core.StandardContext
          • org.apache.catalina.core.StandardWrapper
        2. 初始化executor(org.apache.catalina.core.StandardThreadExecutor)
          • 注册MBean
        3. 初始化mapperListener(org.apache.catalina.mapper.MapperListener)
          • 注册MBean
        4. 初始化connectors
          • 注册MBean
          • 创建org.apache.catalina.connector.CoyoteAdapter实例,用于处理接收到的请求
          • 初始化protocolHandler
            • org.apache.coyote.ajp.AjpAprProtocol
            • org.apache.coyote.ajp.AjpNio2Protocol
            • org.apache.coyote.ajp.AjpNioProtocol
            • org.apache.coyote.ajp.AjpProtocol
            • org.apache.coyote.http11.Http11Nio2Protocol
            • org.apache.coyote.http11.Http11NioProtocol
            • org.apache.coyote.http11.Http11Protocol
            • org.apache.coyote.http11.Http11AprProtocol