Tomcat8源码分析3

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

1================================================== 2Advanced Configuration - Multiple Tomcat Instances 3================================================== 4 5In many circumstances, it is desirable to have a single copy of a Tomcat 6binary distribution shared among multiple users on the same server.  To make 7this possible, you can set the CATALINA_BASE environment variable to the 8directory that contains the files for your 'personal' Tomcat instance. 9 10When running with a separate CATALINA_HOME and CATALINA_BASE, the files 11and directories are split as following: 12 13In CATALINA_BASE: 14 15 * bin  - Only the following files: 16 17           * setenv.sh (*nix) or setenv.bat (Windows), 18           * tomcat-juli.jar 19 20          The setenv scripts were described above. The tomcat-juli library 21          is documented in the Logging chapter in the User Guide. 22 23 * conf - Server configuration files (including server.xml) 24 25 * lib  - Libraries and classes, as explained below 26 27 * logs - Log and output files 28 29 * webapps - Automatically loaded web applications 30 31 * work - Temporary working directories for web applications 32 33 * temp - Directory used by the JVM for temporary files (java.io.tmpdir) 34 35 36In CATALINA_HOME: 37 38 * bin  - Startup and shutdown scripts 39 40          The following files will be used only if they are absent in 41          CATALINA_BASE/bin: 42 43          setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar 44 45 * lib  - Libraries and classes, as explained below 46 47 * endorsed - Libraries that override standard "Endorsed Standards" 48              libraries provided by JRE. See Classloading documentation 49              in the User Guide for details. 50 51              By default this "endorsed" directory is absent. 52 53In the default configuration the JAR libraries and classes both in 54CATALINA_BASE/lib and in CATALINA_HOME/lib will be added to the common 55classpath, but the ones in CATALINA_BASE will be added first and thus will 56be searched first. 57 58The idea is that you may leave the standard Tomcat libraries in 59CATALINA_HOME/lib and add other ones such as database drivers into 60CATALINA_BASE/lib. 61 62In general it is advised to never share libraries between web applications, 63but put them into WEB-INF/lib directories inside the applications. See 64Classloading documentation in the User Guide for details. 65 66 67It might be useful to note that the values of CATALINA_HOME and 68CATALINA_BASE can be referenced in the XML configuration files processed 69by Tomcat as ${catalina.home} and ${catalina.base} respectively. 70 71For example, the standard manager web application can be kept in 72CATALINA_HOME/webapps/manager and loaded into CATALINA_BASE by using 73the following trick: 74 75 * Copy the CATALINA_HOME/webapps/manager/META-INF/context.xml 76   file as CATALINA_BASE/conf/Catalina/localhost/manager.xml 77 78 * Add docBase attribute as shown below. 79 80The file will look like the following: 81 82  <?xml version="1.0" encoding="UTF-8"?> 83  <Context docBase="${catalina.home}/webapps/manager" 84    antiResourceLocking="false" privileged="true" > 85    <Valve className="org.apache.catalina.valves.RemoteAddrValve" 86         allow="127\.0\.0\.1" /> 87  </Context> 88 89See Deployer chapter in User Guide and Context and Host chapters in the 90Configuration Reference for more information on contexts and web 91application 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().

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java8新特性

Stream将List转换为Map,使用Collectors.toMap方法进行转换背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象1、指定keyvalue,value是对象中的某个属性值。 Map<Integer,StringuserMap1userList.str

static & final

static被static修饰的方法,是类的方法,被static修饰的属性,是类的属性。static如果放在属性上,publicclassPerson1{   publicstatic Stringaaa"中国人";      publicStr

Java(3)

面向对象编程思想:抽象 继承 多态 封装对象:万事万物皆对象. 类:把具有相同属性和方法的对象抽象出来形成的几何体!类是对象的抽象,对象是类的具体!如何描述一个类1.属性(属性不初始化时也有初始值,会自动初始化,整形为0 小数型为0.0  boolea

Java中static、final、static final的区别

说明:不一定准确,但是最快理解。final:final可以修饰:属性,方法,类,局部变量(方法中的变量)final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变。final修饰的属性跟具体对象有关,在运行期初始化的final属性,不同对象可以有不同的值。final修饰的属性表明是一个常数(创建后不能被修改)。

Angular7教程

1\.本节说明本节以及后面的内容我们将会通过搭建一个简单的博客程序来对angular进行介绍,项目使用前端框架是bootstrap.版本v3.3.7,另外需要安装jquery.关于bootstrap,jquery的安装方法第一篇中有讲解,不再赘述。本节内容由于搭建页面框架的关系,主要是bootstrap的内容,与angular关系不太