Tomcat8源码分析3

Easter79
• 阅读 474

1. 执行Bootstrap类的static代码块, 初始化Bootstrap的catalinaHomeFile属性和catalinaBaseFile属性, 默认情况下值都为tomcat的安装目录. 关于这两个属性, 说明如下.

==================================================
Advanced Configuration - Multiple Tomcat Instances
==================================================

In many circumstances, it is desirable to have a single copy of a Tomcat
binary distribution shared among multiple users on the same server.  To make
this possible, you can set the CATALINA_BASE environment variable to the
directory that contains the files for your 'personal' Tomcat instance.

When running with a separate CATALINA_HOME and CATALINA_BASE, the files
and directories are split as following:

In CATALINA_BASE:

 * bin  - Only the following files:

           * setenv.sh (*nix) or setenv.bat (Windows),
           * tomcat-juli.jar

          The setenv scripts were described above. The tomcat-juli library
          is documented in the Logging chapter in the User Guide.

 * conf - Server configuration files (including server.xml)

 * lib  - Libraries and classes, as explained below

 * logs - Log and output files

 * webapps - Automatically loaded web applications

 * work - Temporary working directories for web applications

 * temp - Directory used by the JVM for temporary files (java.io.tmpdir)


In CATALINA_HOME:

 * bin  - Startup and shutdown scripts

          The following files will be used only if they are absent in
          CATALINA_BASE/bin:

          setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar

 * lib  - Libraries and classes, as explained below

 * endorsed - Libraries that override standard "Endorsed Standards"
              libraries provided by JRE. See Classloading documentation
              in the User Guide for details.

              By default this "endorsed" directory is absent.

In the default configuration the JAR libraries and classes both in
CATALINA_BASE/lib and in CATALINA_HOME/lib will be added to the common
classpath, but the ones in CATALINA_BASE will be added first and thus will
be searched first.

The idea is that you may leave the standard Tomcat libraries in
CATALINA_HOME/lib and add other ones such as database drivers into
CATALINA_BASE/lib.

In general it is advised to never share libraries between web applications,
but put them into WEB-INF/lib directories inside the applications. See
Classloading documentation in the User Guide for details.


It might be useful to note that the values of CATALINA_HOME and
CATALINA_BASE can be referenced in the XML configuration files processed
by Tomcat as ${catalina.home} and ${catalina.base} respectively.

For example, the standard manager web application can be kept in
CATALINA_HOME/webapps/manager and loaded into CATALINA_BASE by using
the following trick:

 * Copy the CATALINA_HOME/webapps/manager/META-INF/context.xml
   file as CATALINA_BASE/conf/Catalina/localhost/manager.xml

 * Add docBase attribute as shown below.

The file will look like the following:

  <?xml version="1.0" encoding="UTF-8"?>
  <Context docBase="${catalina.home}/webapps/manager"
    antiResourceLocking="false" privileged="true" >
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1" />
  </Context>

See Deployer chapter in User Guide and Context and Host chapters in the
Configuration Reference for more information on contexts and web
application deployment.

    区分这两个属性主要是为了支持Tomcat的多实例功能.

    home属性主要包含:  bin(主要包含启动停止脚本), lib(公用jar包), endorsed(要覆盖JRE的jar).

    base属性主要包含 :  bin(只能包含setevn(设置JRE_HOME等环境变量),  tomcat-juli.jar(日志相关)文件), conf, logs, webapps, work, temp, lib(该tomcat实例下所有web应用共享的jar,tomcat加载时,会先加载这里的jar再加载home/lib下的jar).

2. 如果daemon属性为null, 就创建一个Bootstrap实例,并调用Bootstrap.init()方法初始化, 否则设置当前线程类加载器为daemon.catalinaLoader(另外线程调用Bootstrap.main('stop')方法).

3. Bootstrap.init()方法逻辑, 首先初始化三个类加载器initClassLoaders().

commonLoader: 根据common.loader属性的配置(catalina.properties), 创建对应的类加载器, 默认情况下顺序加载 ${catalina.base}/lib, ${catalina.base}/lib/*.jar, ${catalina.home}/lib, ${catalina.home}/lib/*.jar 四个目录下的class和jar.

catalinaLoader:  根据server.loader属性的配置, 创建对应的类加载器,其父类加载其为commonLoader, 默认server.loader属性为空, 则直接使用commonLoader.

sharedLoader:根据shared.loader属性配置,创建对应的类加载器,其父类加载其为commonLoader, 默认shared.loader属性为空, 则直接使用commonLoader.

4. 初始化完类加载器后, 设置当前线程上下文类加载器为 catalinaLoader 类加载器, 既commonLoader类加载器. Thread.currentThread().setContextClassLoader(catalinaLoader)

5. 如果当前有SecurityManager, 则提前加载一些类, 防止出现AccessControlException异常. SecurityClassLoad.securityClassLoad(catalinaLoader)

6. 使用catalinaLoader加载org.apache.catalina.startup.Catalina类, 创建实例(Catalina构造方法会配置安全属性,如果有安全管理器), 并反射调用setParentClassLoader(sharedLoader), 设置Catalina实例的parentClassLoader属性为sharedLoader类加载器.

7. 设置daemon为新创建的实例.

8. 进入start命令逻辑. 反射调用Catalina.setAwait(true), 主要是为了启动完成后, 阻塞main线程, 监听某一端口, 等待shutdown命令到来(Catalina.start(), Catalina.await()). 如果不设置, 则main线程执行完, 直接退出.

9. 反射调用Catalina.load(arg[])方法, 可以通过启动参数设置server.xml文件位置, 设置是否启用naming, 然后调用Catalina.load().

10. Catalina.load()逻辑, 首先初始化temp目录, 然后初始化naming需要的一些系统属性(org.apache.naming.java.javaURLContextFactory),  然后获取server.xml配置文件, 创建Digester实例, 开始解析server.xml配置文件.

11. Digester具体的解析方式见 xxxxxxxxxxxxxxxxxxxxxxxxx.

12. 双休关联StandardServer和Catalina实例, 并设置server的catalinaHome和catalinaBase属性.

13. 包装System.out和in流为SystemLogHandler.

14. 启动server, getServer().init().

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java8新特性
Stream将List转换为Map,使用Collectors.toMap方法进行转换背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象1、指定keyvalue,value是对象中的某个属性值。 Map<Integer,StringuserMap1userList.str
Easter79 Easter79
2年前
static & final
static被static修饰的方法,是类的方法,被static修饰的属性,是类的属性。static如果放在属性上,publicclassPerson1{   publicstatic Stringaaa"中国人";      publicStr
Wesley13 Wesley13
2年前
Java(3)
面向对象编程思想:抽象 继承 多态 封装对象:万事万物皆对象. 类:把具有相同属性和方法的对象抽象出来形成的几何体!类是对象的抽象,对象是类的具体!如何描述一个类1.属性(属性不初始化时也有初始值,会自动初始化,整形为0 小数型为0.0  boolea
Wesley13 Wesley13
2年前
Java中static、final、static final的区别
说明:不一定准确,但是最快理解。final:final可以修饰:属性,方法,类,局部变量(方法中的变量)final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变。final修饰的属性跟具体对象有关,在运行期初始化的final属性,不同对象可以有不同的值。final修饰的属性表明是一个常数(创建后不能被修改)。
Stella981 Stella981
2年前
Angular7教程
1\.本节说明本节以及后面的内容我们将会通过搭建一个简单的博客程序来对angular进行介绍,项目使用前端框架是bootstrap.版本v3.3.7,另外需要安装jquery.关于bootstrap,jquery的安装方法第一篇中有讲解,不再赘述。本节内容由于搭建页面框架的关系,主要是bootstrap的内容,与angular关系不太
Stella981 Stella981
2年前
IntelliJ IDEA 12 与 Tomcat7 配置
系统配置:win764bit,IntelliJIDEA12,tomcat7先说一下与Tomcat相关的两个配置:1、配置默认端口在tomcat安装目录的conf目录下的server.xml文件中,以下内容中的port属性指定了默认端口:<ConnectorconnectionTi
Wesley13 Wesley13
2年前
CSS基础_CSS样式的几种书写形式
1.内嵌式将CSS代码集中写在HTML文档的head头部标签中,并且用style标签定义,其基本语法格式如下:<head<styletype"text/CSS"标签名/选择器{属性1:属性值1;属性2:属性值2;属性3:属性值3;
Stella981 Stella981
2年前
Kafka多线程消费消息创建线程池并封装成Jar包
XML及属性配置1.业务处理(kiafka.worker.xml)<?xmlversion"1.0"encoding"UTF8"?<root<topics<!name:要消费的topic;<!worker:执行消费逻辑的worker,配置值为执行的类
Easter79 Easter79
2年前
Spring两种依赖注入方式的比较
我们知道,Spring对象属性的注入方式有两种:设值注入和构造注入。先看代码:  假设有个类为People,该对象包含三个属性,name和school还有age,这些属性都有各自的setter和getter方法,还有一个包含这三个属性的构造方法。如果用spring来管理这个对象,那么有以下两种方式为People设置属性:  1.设值注入:
小万哥 小万哥
2个月前
Java 构造函数与修饰符详解:初始化对象与控制权限
Java构造函数Java构造函数是一种特殊的类方法,用于在创建对象时初始化对象的属性。它与类名相同,并且没有返回值类型。构造函数的作用:为对象的属性设置初始值执行必要的初始化操作提供创建对象的多种方式构造函数的类型:默认构造函数:无参数的构造函数,如果用户
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k