Tomcat Source Code Analysis (Startup Framework)
Preface:
This article is some experience after I read Tomcat source. Mainly to explain the Tomcat system framework, as well as the startup process. If there is a mistake, please criticize the advice!
Suggest:
After all, Tomcat's framework is more complicated, and the order is understood from text, it is not so easy to master Tomcat's framework. Therefore, we have practical, practice, and practice. It is recommended to download a Tomcat source code, debug pass, and then track its startup process. If you don't understand, come and see this article and see if you can get help. I believe that this effect and the speed of learning will be a lot!
1. Tomcat's overall frame structure
The basic framework of Tomcat is divided into 4 levels.
Top Level Elements:
Server
Service
CONNECTOR
HTTP
AJP
Container
Engine
Host
Context
Component
Manager
Logger
Loader
Pipeline
Valve
...
The top floor standing in the frame is Server and Service
Server: In fact, it is a Backgroud program. The use of Server in Tomcat is started and listening to server events (such as restart, shutdown, etc. In Tomcat standard configuration file: server.xml, we can see "
Service: In Tomcat, Service refers to a solution for a problem. Usually we will use Tomcat-Standalone mode for the Tomcat-Standalone mode by default. The service in this manner is given to us to provide the services of the JSP and Servlet, and it is also available to resolve the service of static text.
CONNECTOR: Tomcat is handled in the container, and where to get the input information again?
The Connector is the exclusive. He will pass the data passed from the socket, package into a request, and pass to the container.
Usually we will use two CONNECTORs, a kind of HTTP Connectoer to deliver HTTP needs. Another name called AJP, when we integrate Apache and Tomcat, Apache and Tomcat are interacting through this protocol. (Saying the integration of Apache and Tomcat, usually our purpose is to let Tomcat to resolve dynamic JSP or servlets in order to get Apache.
Container: When the HTTP Connector passes the need to the top Container: Engin, our sight should be moved to the Container level.
At the Container layer, we contain 3 containers: Engin, Host, Context.
Engin: Received the demand passing from Service, after processing, returns the result to the service (Service is interacting with ENGIN through the Connector).
Host: ENGIN does not handle itself after receiving the requirements of Service, but is handed over to the appropriate Host to handle it.
Host is herein the meaning of the virtual host, usually we will only use one host, or both "localhost". Context: Host received the demand passing from Host, it won't handle itself, but is handed over to the appropriate context.
For example:
http://127.0.0.1:8080/foo/index.jsp>
<
http://127.0.1: 8080 / bar / index.jsp>
The former is handed over to the context to handle it, the latter is handed over to BAR this context.
Obvious! Context meaning is actually a web app meaning.
We usually do such a configuration in Server.xml
This Context container is where we do what we do.
Compenent: Next, we continue to talk about what Component is dry.
We have to understand the relationship between the container and components.
The demand is passed to the container, and it is passed to the next container processing when appropriate.
The container is still in full contained components, and we can understand to provide a wide range of value-added services.
Manager: After a container is installed in the Manager component, this container supports session management, in fact, SESSION management inside Tomcat is depends on the Manager Component installed in Context.
Logger: After the Logger component is installed in a container, the thing that happens in this container is recorded by the component! We usually see Catalina_log.time.txt and localhost.time.txt and localhost_examples_log.time.txt. This is because we have the three containers of Engin, Host, and Context (Examples), which is also installed by default, and called standard :)
Loader: Loader This component usually only uses our Context container, and Loader is used to start Context and manage ClassLoader to manage this Context.
PIPLINE: Pipeline is such a thing, when a container determines how to give the need to the child container from the superior, he puts this demand into the pipeline of the container. The demand is a distant call to intercept the valves inside the pipe in the pipeline. For example, two valves have been put in the pipe. The first valve is called "access_allow_vavle", that is, when the demand flows, it will see which IP is coming over, if this IP is already in the blacklist, Sure, kill! The second valve is called "defaul_access_valve" it will do a routine check, if passing, OK, passes the demand to the status of the current container. In this way, the demand is transmitted, flowing, and finally arrived at the destination in each container.
Valve: It is the valve mentioned above.
Tomcat is probably so something, we can simply understand the Tomcat framework, which is a structure in which the container is also included in the container.
2. Tomcat startup process
This article says how to start Tomcat, since we substantially understand Tomcat's frame structure, then we can guess Tomcat's startup, will start the parent container first, then start the sub-container in it. When you start each container, you will start the components on him. When all the components are started, all containers are started, Tomcat itself is started. Transfer to the stage, we can also guess that Tomcat's startup will be divided into two parts, the first step is to assemble. The second step is to start working.
Assembly work is to install a child container for the parent container, and install the work of the components for each container. We will use Digester mode for this place, as for Digester mode, what is the use, how to work. Please refer to <
http://software.ccidnet.com/pub/Article/c322_a31671_p2.html>
The startup work is after assembly work, once the assembly is successful, we only need to ignite the top one of the wires, and the entire Tomcat will be activated. This is like we have to open a car that has already been equipped. We just need to put the key into the keyhole, twist, the car's engine will start, the air conditioner will start, the safety device will take effect. In this way, the car will start. (This process is indeed unconscious with Tomcat's startup process, let us doubt Tomcat's designer is developed in GE as Java).
2.1 Some interesting names:
Catalina
Tomcat
Bootstrap
Engin
Host
Context
They mean very interesting:
Catalina: Remote Bomber
Tomcat: Panda bomber - a bomber (this reminds me of the panda mobile phone that makes the Chinese people to be proud, is it called Tomcat ???, reminds me of another advertisement: Wave guide - mobile phone Fighter, Boeing - fighters in the passenger plane)
Bootstap: Boot
Engin: Engine
Host: Host, Territory
Context: content, goal, context
... After many years, modern humans have been extracted. The post-modern creatures found these words to zero zero falling in one. A self-thought intelligence guy translated these things:
Under the bootstrap of the ground, a bombing frame is vacated, far from the panda bomber (Tomcat), close or panda bomber! With excellent engine technology (Engin), this panda bomber flew over the territory of the enemy (Host), and the CONTEXT cast a nuclear warhead destroyed the earth, the wave ~ modern creature is so fart ~
In summary, this has never been associated with GE is also involved in the production of military equipment?
Against the US imperialism! Against the hegemony! Heaven is long! for freedom!
2.2 History is so amazing! Tomcat starts from org.apache.catalina.startup.bootstrap this class!
Tway things in bootstrap:
1. Specify three types of ClassLoader:
CommonLoader: Common / Classes, Common / Lib, Common / Endorsed
CatalinaLoader: Server / Classes, Server / Lib, CommonLoader
SharedLoader: Shared / Classes, Shared / Lib, CommonLoader2. Boot Catalina's startup.
Use the reflection technology to call org.apache.catalina.startup.catalina's Process method and pass the parameters.
2.3 Catalina.java
Catalina has completed several important tasks:
1. Use Digester technology to assemble Tomcat vessels and components.
1.1 The main content of assembly work is to install each major piece. What kind of servcie is there in Server. How many context will Host will accommodate. Which components will be used in Contexts and more.
1.2 At the same time, in the step of assembly work, the configuration of MBeans is completed. Here, I simply describe what MBean is, what is used.
The objects we have generated, managed by themselves, and the sky! But if we have created an object, I want someone to take the management, what should I do? I want to tell others what we have, and what methods can be found! JMX technology provides us with a means. There are three main things in JMX. MBean, Agent, Connector.
MBean: It is used to map our objects. Perhaps MBean is the object we created, maybe not, but with it, you can quote our object.
Agent: You can find MBean by it.
Connector: Connect the way of the Agent. Can be http, or the RMI, you can also pass the socket directly.
Things happening during the Tomcat assembly: The initialization of the GlobalResourceSlifeCyleClelicistener class is triggered:
Protected static registry registry = mbeanutils.createRegistry (); will run
MBeanUtils.createRegistry () will create MBeans. OK based on /org/apache/catalina/mbeans/mbeans-descriptors.xml, the outside world has a path access to each component in Tomcat. (A bit like backmen)
2. Do initialize the server for TOP Level. In fact, do two Connector, usually configured to service, (http, ajp)
3. Start from the container of Server, ignite the entire Tomcat.
4. Do a hook program for Server, detect when Server Shutdown is closed, and close the various containers of Tomcat.
5. Monitor 8005 port, if you send "Shutdown" (default, strings) come over, close 8005Serversocket.
2.4 Start each container
Server
Trigger the Server container before starting (Before_Start), start (start), starts 3 events (after_start), and run the corresponding event processor.
Start Server's Subjects: Servcie.
2. Service
Sub-container launching Service: Engin
Launch Connector
3. Engin
This level of Engin, as well as the following levels, Tomcat uses a comparison start-up mode.
First, run each container own task
Subsequently, the event is triggered.
Immediately, set the label, indicating that the container has been launched, launching each component in the container: Loader, Logger, Manager, etc.
Then, start the mapping component. (Note 1)
Tightly, promoter the sub-container.
Next, start the pipe of the container (PIPline)
Then, trigger the event in the startup
Finally, the event is triggered.
ENGIN is roughly doing this, Host will do this, and Context is roughly doing this. Obviously, we need to use the code multiplexed here. Tomcat uses an abstract class to handle it when processing this problem. ContainerBase. Finally, this part of the complex function code is clean and profitable. It is really sighful. It is really sighing. In order to enjoy Jiazhen, the other is cheeks, nostalgic!
ENGIN's trigger startup event, activate the only listner: enginconfig that is bound to engin.
This ENGINCONFIG class basically doesn't do anything, that is, set the debug level of EnginConfig to ENGIN. In addition, it is to output a few lines of text, indicating that Engin has been configured and does not do substantive work.
Note 1: The use of Mapping components is when a requirement will be transferred from the parent container to the child container, while the parent container has multiple sub-containers, which sub-container should be selected to handle the demand? This will be determined by the mapping component.
4. Host
Like ENGIN, it is also a Start () method that calls ContainerBase. However, it has been a task of being a person, it is the channel of Host's container (pipline), installed a name called
Valves of "Org.apache.catalina.valves.ErrorReportvalve".
The use of this valve is such that the demand will continue to pass to Context to do specific processing after being passed to HOST by Engin. The demand here is actually the Request, Response, which is passed as a parameter. Therefore, after Context handles the demand, the response will usually change. This org.apache.catalina.valves.ErrorReportvalve is to verify whether RESPONSE contains errors, if there is a corresponding process.
5. Context
When I arrived here, I finally turned to Tomcat's real heavy head, starting context.
StandardContext.Start () This method of launching the Context container is called by StandardHost.
5.1 WebAppResources the specific directory pointed to by this context
5.2 Installing DefaultContex, DefaultContext is the default context. If we install DefaultContext below, and DEFAULTCONTEXT has installed a database connection pool resource. Then all other Contexts under this Host can use this database connection pool directly without having to be configured.
5.3 Specify Loader. Usually use the default org.apache.catalina.Loader.Webapploader class. Loader is used to specify which classes will be used, what is the JAR package?
5.4 Specify manager. Usually use the default org.apache.catalina.session. StandardManager. Manager is used to manage session. In fact, the management of sessions is also very good. Take a simple session management as an example. When the demand passes, there is a sessionID attribute in the Request object. OK, after getting this sessionID, we can use it as a key, while we can place a hashmap. Hashmap, let us put things.
5.5 PostWorkDirectory (). Tomcat There is a work directory. We throw the temporary documents there. This step is to create a directory there. Generally speaking, you will generate a directory in% CATALINA_HOME% / Work / Standalone / localhost /.
5.6 Binding Thread. In the case of it, Class Loader should be interchanged. It is before you see all Class and Lib below Tomcat. Next, you need to see the Class of Class under the current context. So set up CONTEXTCLASSLOADER, but also record the old ClassLoader because it will be used later.
5.7 Start loader. Specify which classes to use this Context specifically to use which JAR files. If RELOABLE is set to True, start a thread to monitor Classes changes, restart the context if there is change.
5.8 Launch Logger
5.9 Trigger a listener installed on it.
Lifecycle.firelifeCycleevent (start_event, null);
As one of the listeners, ContextConfig will be activated. ContextConfig is used to configure web.xml. For example, how many servlets have this context, and how many filters are installed here for Context.
5.9.1 DefaultConfig. Each context has to configure this file with Tomcat / Conf / Web.xml.
5.9.2 ApplicationConfig Configuring your own web-inf / web.xml file
5.9.3 ValidateSecurityRoles Permissions Verification. Usually we are accessible when we are accessing / admin or / manager, or you can access. And we can also limit those resources that can be accessed, and which can't. It is achieved here.
5.9.4 TLDSCAN: Scan, which tags need to be used (TAG LAB)
5.10 Start Manager
5.11 PostwelComeFiles () We usually use the 3 startup file name:
Index.html, index.htm, index.jsp is tied by default on this context
5.12 ListenerStart Configuring Listener
5.13 FilterStart Configuration Filter
5.14 Start the servlet with
The order is from small to large: 1, 2, 3 ... finally 0
By default, at least the following three servlets are started:
Org.apache.catalina.servlets.defaultServlet
What pictorial, HTML, CSS, JS, what pictures, HTML, CSS, JS is looking for him
Org.apache.catalina.servlets.invokerseervlet
Processing not doing servlet maping of those servlet.jasper.servlet.jspservlets
Processing a JSP file.
5.15 Identifying context has been started.
How many steps have you gone? Context is finally started.
OK! Go here, each container and components are started. Tomcat is finally served for the people!