All the way, this is the last chapter.
About Tomcat startup, there are two classes, one is the Catalina class, and the other is the Bootstrap class.
Theoretically, two classes can be combined, but in order to support a variety of operating modes, they are separated.
In order to make it easier for users to start Tomcat, there are batch files Startup.bat (finally see the topmost module!!)
Catalina First, Catalina.
It has a server component (not a look at the 14th chapter). At the same time, it contains a Digester object in its Start method (which does not understand the 15th chapter) used to parse the Server.xml under conf/(the XML assembles the server component within the Catalina Class).
At the same time, Catalina itself has the main method, the main method when running the need to add a startup parameter. Optional Start,stop,debug, etc.
Let's look at the process method
public void process (String args[]) { setcatalinahome (); Set the home and base value to User.dir setcatalinabase (); if (arguments (args)) //omit Try Catch execute (); Check the format of args } protected void execute () throws Exception { if (starting) start (); else if (stopping) stop (); }
We mainly look at how to start.
protected void Start () {//omitted a large number of non-core code and try catch ...//Create and execute our Digester digester digest ER = Createstartdigester (); If this is not understood, see Chapter 15 File File = ConfigFile (); Specify the file as Conf/server.xml InputSource is =new inputsource ("file://" + File.getabsolutepath ()); FileInputStream fis = new FileInputStream (file); Is.setbytestream (FIS); Digester.push (this); Put Catalina himself in the//catalina. Server Component Digester.parse (IS); Fis.close (); ...//Start the new server if (server instanceof Lifecycle) {server.initialize (); Wait for the server to is told to shut down Server.await (); I'll explain it to you in English.//Unless you receive SHUTDOWM's instructions, it's stuck here//If you don't know how to go back to chapter 23rd} ... Shut down the server if (server instanceof Lifecycle) {(Lifecycle) server). Stop (); } }
Here's a question.
Server.initialize ();
Where did the server come from?
Don't rush to see the Createstartdigester method.
Protected Digester Createstartdigester () { //Initialize the digester digester digester = new Digester (); if (Debug) digester.setdebug (999); Digester.setvalidating (false); Configure The actions we'll be using digester.addobjectcreate ("Server", " Org.apache.catalina.core.StandardServer ", " ClassName "); Digester.addsetproperties ("Server"); Explain the following //in parsing server.xml if you encounter the server this mode //Call the bottom element of the method named Setserver parameter is Org.apache.catalina.Server method //What is the bottom of the stack when the server itself (which is the one above addobjectcreate) is injected into the bottom of the stack ? Digester.push (this); Look at this line of code and the Catalian Setserver method digester.addsetnext ("Server", "Setserver", " Org.apache.catalina.Server "); ..... Return Digester}
Bootstrap class now talk about Bootstrap
The main method of Bootstrap instantiates a Catalina object and invokes its process method.
In the main method, three class loaders are created first.
Why? Just to not let classes in your application use classes outside of Web-inf/class and Web-inf/lib.
Specifically how to do the look at the eighth chapter.
public static void Main (String args[]) {//Omit non-core code and try Catch//Construct the class loaders we'll need C Lassloader commonloader = null; ClassLoader catalinaloader = null; ClassLoader sharedloader = null; ...//Load our startup class and call its process () method//Instantiate a startup class instance if (debug >= 1) log ("Loading startup Class"); Class Startupclass = Catalinaloader.loadclass ("Org.apache.catalina.startup.Catalina"); Object startupinstance = Startupclass.newinstance (); Load Catalina class//Call the process () method if (debug >= 1) log ("Calling startup cl () method "); MethodName = "process"; Paramtypes = new Class[1]; Paramtypes[0] = Args.getclass (); Paramvalues = new Object[1]; Paramvalues[0] = args; METHOD = Startupinstance.getclass (). GetMethod (MethodName, paramtypes); Method.invoke (Startupinstance, paramvalues); Call Catalina's Process method}
About Startup.bat's knowledge, we'll talk about it in the next section ... Hey, there's another section.
How Tomcat works reading notes 17 starting Tomcat on