码迷,mamicode.com
首页 > Web开发 > 详细

tomcat(supplement)HttpConnector.initialize() 和 start() 方法 以及 StandardContext.start()方法的分析

时间:2016-05-12 22:46:57      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

【0】README

0.0)本文中源代码的背景,参见 tomcat(9)session管理

0.1)本文主要以图片的形式分析他们大致的调用过程;

0.2)HttpConnector == org.apache.catalina.connector.http.HttpConnector; 而StandardContext == org.apache.catalina.core.StandardContext;

0.3)应用程序的source code 如下:

public final class Bootstrap {
  public static void main(String[] args) {
    System.setProperty("catalina.base", System.getProperty("user.dir"));
    Connector connector = new HttpConnector();
    Wrapper wrapper1 = new SimpleWrapper();
    wrapper1.setName("Session");
    wrapper1.setServletClass("SessionServlet");

    Context context = new StandardContext();
    context.setPath("/myApp");
    context.setDocBase("myApp");
    context.addChild(wrapper1);

    context.addServletMapping("/myApp/Session", "Session");
    LifecycleListener listener = new SimpleContextConfig();
    ((Lifecycle) context).addLifecycleListener(listener);
    Loader loader = new WebappLoader();
    context.setLoader(loader);
    connector.setContainer(context);

    Manager manager = new StandardManager();
    context.setManager(manager);

    try {
      connector.initialize(); // highlight line.
      ((Lifecycle) connector).start(); // highlight line.
      ((Lifecycle) context).start(); // highlight line.

      // make the application wait until we press a key.
      System.in.read();
      ((Lifecycle) context).stop();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

【1】HttpConnector.initialize()

技术分享


【2】HttpConnector.start()

技术分享技术分享


【3】StandardContext.start()

技术分享技术分享技术分享技术分享


/**
     * Load and initialize all servlets marked "load on startup" in the
     * web application deployment descriptor.
     *
     * @param children Array of wrappers for all currently defined
     *  servlets (including those not declared load on startup)
     */
    public void loadOnStartup(Container children[]) { // org.apache.catalina.core.StandardContext.loadOnStartUp().

        // Collect "load on startup" servlets that need to be initialized
        TreeMap map = new TreeMap();
        for (int i = 0; i < children.length; i++) {
            Wrapper wrapper = (Wrapper) children[i];
            int loadOnStartup = wrapper.getLoadOnStartup();
            if (loadOnStartup < 0)
                continue;
            if (loadOnStartup == 0)     // Arbitrarily put them last
                loadOnStartup = Integer.MAX_VALUE;
            Integer key = new Integer(loadOnStartup);
            ArrayList list = (ArrayList) map.get(key);
            if (list == null) {
                list = new ArrayList();
                map.put(key, list);
            }
            list.add(wrapper);
        }

        // Load the collected "load on startup" servlets
        Iterator keys = map.keySet().iterator();
        while (keys.hasNext()) {
            Integer key = (Integer) keys.next();
            ArrayList list = (ArrayList) map.get(key);
            Iterator wrappers = list.iterator();
            while (wrappers.hasNext()) {
                Wrapper wrapper = (Wrapper) wrappers.next();
                try {
                    wrapper.load();
                } catch (ServletException e) {
                    log(sm.getString("standardWrapper.loadException",
                                     getName()), e);
                    // NOTE: load errors (including a servlet that throws
                    // UnavailableException from tht init() method) are NOT
                    // fatal to application startup
                }
            }
        }

    }


tomcat(supplement)HttpConnector.initialize() 和 start() 方法 以及 StandardContext.start()方法的分析

标签:

原文地址:http://blog.csdn.net/pacosonswjtu/article/details/51345357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!