• [tomcat7源码学习]初始化之catalina.home和catalina.base(转)


    我们在代码中为了获取某个配置文件路径下的文件经常会这么写

    String tomcatPath = System.getProperty("catalina.home") + "/webapps/axis2/WEB-INF/conf/";
    tomcatPath = tomcatPath.replace("/", File.separator); //使用此方法是为了区分unix系统与windows,
    //File.separator UNIX中为/,而WINDOWS下为

    而在检查环境变量的时候可能会发现,系统中并不存在catalina.home或者说并不存在CATALINA_HOME环境变量,那么我们在运行时成功的得到了CATALINA_HOME路径,

    并且如果多个tomcat实例存在,那么可以得到各自tomcat实例下安装路径对应的路径。

    这是因为System.getProperty严格意义上来讲指AppClassLoader得到的property,即程序运行时的环境变量。

    例如在TOMCAT运行时中,TOMCAT启动之初便会设置环境变量catalina.home。

    所以可以得到tomcat实例下对应的路径。

    参考:

    [tomcat7源码学习]初始化之catalina.home和catalina.base

    初始化之catalina.home和catalina.base

    首先先看一下这两个参数在tomcat源码中的注释(参考类Globals):

    /**
     * Name of the system property containing
     * the tomcat product installation path
     */
    public static final String CATALINA_HOME_PROP = "catalina.home";
    
    
    /**
     * Name of the system property containing
     * the tomcat instance installation path
     */
    public static final String CATALINA_BASE_PROP = "catalina.base";

    就两个差异,一个是product,一个是instance。

    再看一下RUNNING.txt中的说明

    ==================================================
    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.

    大概意思就是

    • CATALINA_BASE:是实例配置位置,也就是一个tomcat可以配置多个实例,实例里面有自己的配置
    • CATALINA_HOME:是tomcat安装位置

    tomcat内部设置

    1.从入口org.apache.catalina.startup.main方法开始跟踪,

    Bootstrap bootstrap = new Bootstrap();
            try {
                bootstrap.init();
            } catch (Throwable t) {
                handleThrowable(t);
                t.printStackTrace();
                return;
            }
            daemon = bootstrap;

    这是tomcat7第一次启动时最先执行的代码。

    2.进入init,就是一堆初始化了,最先执行的

    // Set Catalina path
        setCatalinaHome();
        setCatalinaBase();

    3.进入setCatalinaHome()

    if (System.getProperty(Globals.CATALINA_HOME_PROP) != null)
                return;
    File bootstrapJar =
        new File(System.getProperty("user.dir"), "bootstrap.jar");
    if (bootstrapJar.exists()) {
        try {
            System.setProperty
                (Globals.CATALINA_HOME_PROP,
                 (new File(System.getProperty("user.dir"), ".."))
                 .getCanonicalPath());
        } catch (Exception e) {
            // Ignore
            System.setProperty(Globals.CATALINA_HOME_PROP,
                               System.getProperty("user.dir"));
        }
    } else {
        System.setProperty(Globals.CATALINA_HOME_PROP,
                           System.getProperty("user.dir"));
    }

    它的步骤是:

    1.从system中取,如果有就直接返回。

    2.如果bootstrap.jar在当前工作目录,就取上一级,返回。

    3.如果bootstrap.jar没有在当前工作目录,那么就设置CATALINA_HOME为当前工作目录

    4.进入setCatalinaBase()

    if (System.getProperty(Globals.CATALINA_BASE_PROP) != null)
        return;
    if (System.getProperty(Globals.CATALINA_HOME_PROP) != null)
        System.setProperty(Globals.CATALINA_BASE_PROP,
                           System.getProperty(Globals.CATALINA_HOME_PROP));
    else
        System.setProperty(Globals.CATALINA_BASE_PROP,
                           System.getProperty("user.dir"));

    它的步骤是:

    1.从system中取,如果有就直接返回。

    2.如果已经设置了CATALINA_HOME,就设置CATALINA_BASECATALINA_HOME,返回。

    3.设置CATALINA_BASE为当前工作目录

    总结

    1.如果我们安装的tomcat就只有一个实例,就只需要配置CATALINA_HOME

    2.有时候项目太多,它们相互之间的一些配置又有些不同,我们就可以考虑用多个实例,而不必去复制多个tomcat了。实例配置了之后,在启动时只需要指定CATALINA_BASE就可以了

  • 相关阅读:
    对象的强、软、弱和虚引用
    spark运行模式之一:Spark的local模式安装部署
    Spark Tungsten in-heap / off-heap 内存管理机制--待整理
    sparkContext之一:sparkContext的初始化分析
    mysql分区表之四:分区表性能
    服务的升级和降级
    怎样编写高质量的 Java 代码
    Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)
    Spark Streaming之六:Transformations 普通的转换操作
    Spark Streaming之五:Window窗体相关操作
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/6127721.html
Copyright © 2020-2023  润新知