[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就可以了

你可能感兴趣的:(tomcat,tomcat7)