What do you look for tomcat boot files: Bootstrap.java

Source: Internet
Author: User
Tags exit range thread throwable tomcat

What do you do with the Tomcat boot file in the previous chapter---Catalina.bat, said what process has gone in the Catalina.bat, and most importantly, we have come to the following command:












cmd_line_args= Action=start

One of the most important attributes is: Mainclass=org.apache.catalina.startup.bootstrap,bootstrap in Bootstrap.jar, let's take a look at the Bootstrap class diagram:

The function of variables or methods can also be seen from the literal name of each variable and method.

Obviously, when the program goes to bootstrap, the main method is called first, and the main method is to start the Tomcat's entry through the script, and we'll look at what is implemented in the Main method:

if (daemon = = null) {//Don ' t set daemon until init () has completed Bootstrap Bootstrap = new Bo
            Otstrap ();
            try {bootstrap.init ();
                catch (Throwable t) {handlethrowable (t);
                T.printstacktrace ();
            Return
        } daemon = bootstrap;  else {//when running as a service the call to stop is on a new//thread so make sure the
            Correct class loader is used to prevent//a range of class not found exceptions.
        Thread.CurrentThread (). Setcontextclassloader (Daemon.catalinaloader);
            The try {String command = "Start";
            if (Args.length > 0) {command = args[args.length-1];
                } if (Command.equals ("STARTD")) {args[args.length-1] = "Start";
                Daemon.load (args); Daemon.start ();
                else if (command.equals ("STOPD")) {args[args.length-1] = "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 if (command.equals ("Configtest")) {daemon.load (args);
                if (Null==daemon.getserver ()) {system.exit (1);
            } system.exit (0); else {log.warn ("Bootstrap:command \" "+ Command +" \ "does not exist.");} catch (Throwable t) {//Unwrap the Exception for clearer error reporting if (t instanceof invocat
            Iontargetexception && t.getcause ()!= null) {t = T.getcause (); } handlethrowabLe (t);
            T.printstacktrace ();
        System.exit (1); }

You can see that the main method basically implements two features:

(1) Initialization of a daemon variable.

(2) Load parameters, parse commands, and execute.

The following is the process of initializing daemon execution:

if (daemon = = null) {
            //Don ' t set daemon until init () has completed Bootstrap Bootstrap
            = new Bootstrap ();
            try {
                bootstrap.init ();
            } catch (Throwable t) {
                handlethrowable (t);
                T.printstacktrace ();
                return;
            }
            daemon = bootstrap;
        } else {
            //when running as a service the call to stop is on a new
            //thread so make sure the correct class L Oader is used to prevent
            /a range of class not found exceptions.
            Thread.CurrentThread (). Setcontextclassloader (Daemon.catalinaloader);
        

You can see the initialization of the bootstrap variable in the Bootstrap.init () method, and then return the result to daemon. Let's look at the implementation in the Init method:

public void Init () throws Exception {//Set Catalina path Setcatalinahome ();
    
        Setcatalinabase ();
    
        Initclassloaders ();
    
        Thread.CurrentThread (). Setcontextclassloader (Catalinaloader);
    
        Securityclassload.securityclassload (Catalinaloader); Load our startup class and call it process () method if (log.isdebugenabled ()) Log.debug ("Loading s
        Tartup 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 P
        Roperties ");
        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; }

The ClassLoader is initialized in the Init method, and the referenced Catalinadaemon variable is set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.