Tomcat-02-tomcat start process 01

Time:2019-11-14

Tomcat start process 01

Analyze Tomcat startup process through debug

1. Tomcat startup entry

Tomcat-02-tomcat start process 01

2. Initialize Catalina object

1. Initialize content: reflect Catalina of instance and add classloader

Initialization entry

Public static void main (string args []) {// args parameters are start, stop, etc

        if (daemon == null) {
            // Don't set daemon until init() has completed
            Bootstrap bootstrap = new Bootstrap();
            try {
                Bootstrap. Init(); // initialize Catalina object, refer to catalinadaemon

Bootstrap.init initializes a Catalina object and assigns a value to its classloader

Classloader member variable

    /**
     * The shared extensions class loader for this server.
     */
    protected ClassLoader parentClassLoader =Catalina.class.getClassLoader();

2. Initialize Catalina’s classloader

//Three related classloaders of Tomcat
    ClassLoader commonLoader = null;
    ClassLoader catalinaLoader = null;
    ClassLoader sharedLoader = null;


    // -------------------------------------------------------- Private Methods
    private void initClassLoaders() {
        try {
            commonLoader = createClassLoader("common", null);
            if( commonLoader == null ) {
                // no config file, default to this loader - we might be in a 'single' env.
                commonLoader=this.getClass().getClassLoader();
            }
            catalinaLoader = createClassLoader("server", commonLoader);
            sharedLoader = createClassLoader("shared", commonLoader);

The initclassloaders method above will read the ${tomcat_home} / conf / catalina.properties file and read the jar package configuration of the loader

Tomcat-02-tomcat start process 01

Note that Tomcat specifies common.loader, catalina.loader and shared.loader in the catalina.properties configuration file, but the configuration of the latter two is empty. It is said that shared.loader is shared public on the Internet and has no meaning of configuration.
From the above initclassloaders, it can be seen that when using createclassloader (“”, “”) to create the latter two loaders, commonloader is passed in. In this way, if the configuration is empty, catalinaloader is actually commonloader

Note: Tomcat uses org.apache.catalina.startup.catalina properties to encapsulate the Tomcat / conf / catalina.properties file. The way to read the configuration file is worth learning. The code is as follows:

    private static void loadProperties() {

        InputStream is = null;
        Throwable error = null;

        try {
            String configUrl = System.getProperty("catalina.config");
            if (configUrl != null) {
                is = (new URL(configUrl)).openStream();
            }
        } catch (Throwable t) {
            handleThrowable(t);
        }

        if (is == null) {
            try {
                File home = new File(Bootstrap.getCatalinaBase());
                File conf = new File(home, "conf");
                File propsFile = new File(conf, "catalina.properties");

What to learn: as you can see, the first step is to determine whether catalina.config specifies the configuration path of catalina.conf. Without the – D parameter, the configuration under Tomcat / conf will be used. This is worth learning.

3. Create Catalina object by reflection and set classloader

 public void init() throws Exception {

        initClassLoaders();

        Thread.currentThread().setContextClassLoader(catalinaLoader);

        SecurityClassLoad.securityClassLoad(catalinaLoader);

        // Load our startup class and call its process() method
        if (log.isDebugEnabled())
            log.debug("Loading startup class");
        **Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");**
        Object startupInstance = startupClass.getConstructor().newInstance();

        // Set the shared extensions class loader
        if (log.isDebugEnabled())
            log.debug("Setting startup class properties");
        **String methodName = "setParentClassLoader";**
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = Class.forName("java.lang.ClassLoader");
        Object paramValues[] = new Object[1];
        paramValues[0] = sharedLoader;
        Method method =
            startupInstance.getClass().getMethod(methodName, paramTypes);
        method.invoke(startupInstance, paramValues);

        catalinaDaemon = startupInstance;

    }

3. Set Catalina object to catalinadaemon member variable of bootstrap

Private object catalinadaemon = null; // catalinadaemon is Catalina object. The parentclassloader property of this object is sharedclassloader, that is, commonclassloader

We can see that the declaration of catalinadaemon is of object type, which reduces the coupling between Tomcat and Catalina, and does not provide Catalina dependency when compiling bootstrap.

Recommended Today

The method of obtaining the resolution of display by pyqt5

The code is as follows import sys from PyQt5.QtWidgets import QApplication, QWidget class Example(QWidget): def __init__(self): super().__init__() self.initUI() #Interface drawing to initui method def initUI(self): self.desktop = QApplication.desktop() #Get display resolution size self.screenRect = self.desktop.screenGeometry() self.height = self.screenRect.height() self.width = self.screenRect.width() print(self.height) print(self.width) #Show window self.show() if __name__ == ‘__main__’: #Create applications and objects app […]