lable  

找源码切入口先从main函数和启动参数入手
从配置项考验看出我们的入口在Bootstrap.java上

Main class:
org.apache.catalina.startup.Bootstrap
VM options: 
-Dcatalina.home=/Users/username/Desktop/java/tomcat8 
-Dcatalina.base=/Users/username/Desktop/java/tomcat8 
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager # 指定日志配置类
-Djava.util.logging.config.file=/Users/username/Desktop/java/tomcat8/conf/logging.properties # 指定日志配置文件
-Dfile.encoding=UTF8 # 指定源文件使用的字符编码
-Duser.language=en # 指定语言
-Duser.region=US # 指定区域

在Bootstrap.java的静态代码块可以看到,catalina.home其实是可省的,如果不填catalina.home那么将尝试以我们平常运行tomcat(运行目录下的bootstrap.jar)的形式启动,如果没有bootstrap.jar则会以当前目录作为catalina.home也就是说catalina.home其实也是可省的,如果不填catalina.base那么将用catalina.home作为默认配置,也就是说catalina.base配置也是可省的。

// Bootstrap.java
static {
    // Will always be non-null
    String userDir = System.getProperty("user.dir");

    // Home first 读取home目录
    String home = System.getProperty(Constants.CATALINA_HOME_PROP);
    File homeFile = null;

    if (home != null) {
        File f = new File(home);
        try {
            homeFile = f.getCanonicalFile();
        } catch (IOException ioe) {
            homeFile = f.getAbsoluteFile();
        }
    }

    // home没有配置的情况下就是我们平时所用的
    if (homeFile == null) {
        // First fall-back. See if current directory is a bin directory
        // in a normal Tomcat install
        File bootstrapJar = new File(userDir, "bootstrap.jar");

        if (bootstrapJar.exists()) {
            File f = new File(userDir, "..");
            try {
                homeFile = f.getCanonicalFile();
            } catch (IOException ioe) {
                homeFile = f.getAbsoluteFile();
            }
        }
    }

    if (homeFile == null) {
        // Second fall-back. Use current directory
        // home目录和bootstrap.jar都找不到了使用当前目录作为home目录
        File f = new File(userDir);
        try {
            homeFile = f.getCanonicalFile();
        } catch (IOException ioe) {
            homeFile = f.getAbsoluteFile();
        }
    }

    catalinaHomeFile = homeFile;
    System.setProperty(
            Constants.CATALINA_HOME_PROP, catalinaHomeFile.getPath());

    // Then base 如果base目录为空那么base目录就沿用home目录
    String base = System.getProperty(Constants.CATALINA_BASE_PROP);
    if (base == null) {
        catalinaBaseFile = catalinaHomeFile;
    } else {
        File baseFile = new File(base);
        try {
            baseFile = baseFile.getCanonicalFile();
        } catch (IOException ioe) {
            baseFile = baseFile.getAbsoluteFile();
        }
        catalinaBaseFile = baseFile;
    }
    System.setProperty(
            Constants.CATALINA_BASE_PROP, catalinaBaseFile.getPath());
}

tomcat对默认logger包装了一层实现了自己的日志类并保留了拓展,所以我们需要指定LoggerManager和配置路径

// Catalina.java
private static final Log log = LogFactory.getLog(Catalina.class);

事实上我们更倾向于通过配置项指定一个目录将我们产生的文件和源码隔离开所以我们可以在项目根目录下创建home目录,将home和base配置到home目录下,并把启动必要的目录和文件都拷贝进home目录
配置项:

-Dcatalina.home=/Users/username/Desktop/java/tomcat8/home
-Dcatalina.base=/Users/username/Desktop/java/tomcat8/home
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=/Users/username/Desktop/java/tomcat8/home/conf/logging.properties
-Dfile.encoding=UTF8
-Duser.language=en
-Duser.region=US

home目录:

home
├── conf
└── webapps
posted on 2022-02-03 20:57  lable  阅读(59)  评论(0)    收藏  举报