How tomcat works 读书笔记十三 Host和Engine(二)

2015-01-27 06:04:56 · 作者: · 浏览: 16
准实现的applicationConfig中
    private void applicationConfig() {
    ...
   synchronized (webDigester) {
            try {
                URL url =
                    servletContext.getResource(Constants.ApplicationWebXml);
    .....
    }

servletContext为ApplicationContext的实例
    public URL getResource(String path)
        throws MalformedURLException {

        DirContext resources = context.getResources();
        if (resources != null) {
            String fullPath = context.getName() + path;

            // this is the problem. Host must not be null
            String hostName = context.getParent().getName();
       ....
    }
看到最后一行的getParent大家都明白了吧。
当然如果使用的是之前的SimpleContextConfig,就不需要Host了。

Engine

在tomcat中,Engine的标准实现是:org.apache.catalina.core.StandardEngine。
同样的Engine也有一个基础阀:StandardEngineva lve
同是Engine不能再有父容器了,子容器也只能是Host。

StandardEngineva lve

StandardEngineva lve的invoke如下:
 public void invoke(Request request, Response response,
                       ValveContext valveContext)
        throws IOException, ServletException {
        ..........

        // Select the Host to be used for this Request
        StandardEngine engine = (StandardEngine) getContainer();
        Host host = (Host) engine.map(request, true);        //同样调用的是Containerbase的map
                                 //addDefaultMapper 怎么来的就不用说//了吧
        if (host == null) {
            ((HttpServletResponse) response.getResponse()).sendError
                (HttpServletResponse.SC_BAD_REQUEST,
                 sm.getString("standardEngine.noHost",
                              request.getRequest().getServerName()));
            return;
        }

        // Ask this Host to process this request
        host.invoke(request, response);
    }

验证了request与response后,就调用Containerbase的map获得一个Host,然后调用host的invoke。