[Tomcat startup class analysis]

1. Introduction to startup class


 2. Code Analysis

/**

     * Main method, used for testing only.

     *

     * @param args Command line arguments to be processed

     */

    public static void main(String args[]) {

 

        if (daemon == null) {

            daemon = new Bootstrap();

            try {

                daemon.init();

            } catch (Throwable t) {

                t.printStackTrace();

                return;

            }

        }

 

        try {

            String command = "start";

            if (args.length > 0) {

                command = args[args.length - 1];

            }

 

            if (command.equals("startd")) {

                args[0] = "start";

                daemon.load(args);

                daemon.start();

            } else if (command.equals("stopd")) {

                args[0] = "stop";

                daemon.stop();

            } else if (command.equals("start")) {

                daemon.setAwait(true);

               daemon.load(args);

                daemon.start();

            } else if (command.equals("stop")) {

               daemon.stopServer(args);

            } else {

                log.warn("Bootstrap: command \"" + command + "\" does not exist.");

            }

        } catch (Throwable t) {

            t.printStackTrace();

        }

 

    }



 

/**

     * Initialize daemon.

     */

    public void init()

        throws Exception

    {

 

        // Set Catalina path

        setCatalinaHome();

        setCatalinaBase();

 

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

 

    }

 

Summary of the init method:

The init method does a total of 6 main things

The system variable CATALINA_HOME is set.

The system variable CATALINA_BASE is set.

Created three class loaders commonloader, catalinaLoader, sharedLoader.

Sets the class loader of the currently started thread to catalinaLoader.

Create an instance of the class Catalina and call the setParentClassLoader method to set the parent class loader of the Catalina class to catalinaLoader.

Assign the Catalina class instance created in Article 5 to the catalinaDaemon variable.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326760080&siteId=291194637