The Study of Tomcat Source Code -- Part I

来源:互联网 发布:中小学远程教育软件 编辑:程序博客网 时间:2024/05/17 07:38

Tomcat is a big 'box'.

For a simple example from "How Tomcat  Works". To build a Tomcat, a 'container' will be used. As we know a big box can contain a small one. In tomcat. there are four kinds of box, hierarchically , Engine, Host, Context, Wrapper. They are all interface. 


For a simple illustrate, we take 'Context' and 'Wrapper' as the base containers in this article.


At first we declare a Object simpleContext from the class 'SimpleContext' which implements the Context.

Then declare two Object simpleWrapper from the class 'SimpleWrapper' which implements the Wrapper and are put into the simpleContext.

Well, box is ok. but box must have the tomcat work as a web server. 

So something must be put into box:  a connector.

   The connector is help to work as a ServerSocket to produce socket for the biggest box.It implements Runnable to run itself in a independent  thread. It create a serverSocket and hold it. When it starts, several HttpProcessors will be created, which is help to do some preparation, implementing runnable interface. When no socket is accepted, HttpProcessors is running but keep waiting. When connector's serverSocket accept a socket, connector pass the socket to one of the HttpProcessors.HttpProcessors get the request and response from socket.


    Here,the simpleContext is the biggest container, which is installed with connector(s),Wrapper(s). 

   After HttpProcessors's work, HttpProcessors get its container(connector set for them) and then  call container's invoke method.

   Well, container use a pipeline to process a request. In a pipeline there are many 'valves' ,which type is interface. Every instantiation of valve  is an action to a request. Generally the basic Valve action is the servlet being request.  

    Wrapper is help to represent an executor of a servlet. It can be set Name and ClassName to map a servlet.

    When context's initialed , At first a 'simpleContextValve'(class implements valve interface ) is created and was set as the basic valve. Then many other  valves(being instantiated) added into pipeline. Well here the simlpeContextValve is just a classifer to choose which serlvet to be called. These servlets to be call are their children Wrappers.

    So we need some wrapper with name and classname to be add into the context's child set which help to execute the basic valves.

    At last, we need a mapper to help choose the child and a loader to help with load the class of serlvet, which are also need to be added into context.