zoukankan      html  css  js  c++  java
  • Tomcat 7 Connector 精读(1)

    这个类图是本人截取的最重要的类的方法和属性。

    其中ProtocalHandler是协议处理器,tomcat支持的协议以下方法可以看到。不同协议实现了不同的ProtocalHandler类。

    public void setProtocol(String protocol) {
    
            if (AprLifecycleListener.isAprAvailable()) {
                if ("HTTP/1.1".equals(protocol)) {
                    setProtocolHandlerClassName
                        ("org.apache.coyote.http11.Http11AprProtocol");
                } else if ("AJP/1.3".equals(protocol)) {
                    setProtocolHandlerClassName
                        ("org.apache.coyote.ajp.AjpAprProtocol");
                } else if (protocol != null) {
                    setProtocolHandlerClassName(protocol);
                } else {
                    setProtocolHandlerClassName
                        ("org.apache.coyote.http11.Http11AprProtocol");
                }
            } else {
                if ("HTTP/1.1".equals(protocol)) {
                    setProtocolHandlerClassName
                        ("org.apache.coyote.http11.Http11NioProtocol");
                } else if ("AJP/1.3".equals(protocol)) {
                    setProtocolHandlerClassName
                        ("org.apache.coyote.ajp.AjpNioProtocol");
                } else if (protocol != null) {
                    setProtocolHandlerClassName(protocol);
                }
            }
    
        }
    View Code

    ProtocalHandler是整个Connector类的核心。

    初始化Connector的时候;根据协议名字创建处理器对象。

     public Connector(String protocol) {
            setProtocol(protocol);
            // Instantiate protocol handler
            ProtocolHandler p = null;
            try {
                Class<?> clazz = Class.forName(protocolHandlerClassName);
                p = (ProtocolHandler) clazz.newInstance();
            } catch (Exception e) {
                log.error(sm.getString(
                        "coyoteConnector.protocolHandlerInstantiationFailed"), e);
            } finally {
                this.protocolHandler = p;
            }
    
            if (!Globals.STRICT_SERVLET_COMPLIANCE) {
                URIEncoding = "UTF-8";
                URIEncodingLower = URIEncoding.toLowerCase(Locale.ENGLISH);
            }
        }
    View Code

    首先是初始化协议处理器(去除了不太重要的代码)

    protected void initInternal() throws LifecycleException {
    
            super.initInternal();
    
            // 初始化Adapter
            adapter = new CoyoteAdapter(this);
            protocolHandler.setAdapter(adapter);
    // 每个协议处理器都有对应的适配器,适配器干啥的呢? protocolHandler.init(); }

    Connector的启动,实则是启动对应的协议处理器的启动,

     protected void startInternal() throws LifecycleException {
    
            // Validate settings before starting
            if (getPort() < 0) {
                throw new LifecycleException(sm.getString(
                        "coyoteConnector.invalidPort", Integer.valueOf(getPort())));
            }
    
            setState(LifecycleState.STARTING);
    
            try {
                protocolHandler.start();
            } catch (Exception e) {
                String errPrefix = "";
                if(this.service != null) {
                    errPrefix += "service.getName(): "" + this.service.getName() + ""; ";
                }
    
                throw new LifecycleException
                    (errPrefix + " " + sm.getString
                     ("coyoteConnector.protocolHandlerStartFailed"), e);
            }
        }
    View Code

    终止实则是终止协议处理器

     protected void startInternal() throws LifecycleException {
    
            // Validate settings before starting
            if (getPort() < 0) {
                throw new LifecycleException(sm.getString(
                        "coyoteConnector.invalidPort", Integer.valueOf(getPort())));
            }
    
            setState(LifecycleState.STARTING);
    
            try {
                protocolHandler.start();
            } catch (Exception e) {
                String errPrefix = "";
                if(this.service != null) {
                    errPrefix += "service.getName(): "" + this.service.getName() + ""; ";
                }
    
                throw new LifecycleException
                    (errPrefix + " " + sm.getString
                     ("coyoteConnector.protocolHandlerStartFailed"), e);
            }
        }
    View Code

    各位看管看到这里,其实看到连接器类需要做如下工作

    (1)创建请求对象

    /**
         * Create (or allocate) and return a Request object suitable for
         * specifying the contents of a Request to the responsible Container.
         */
        public Request createRequest() {
    
            Request request = new Request();
            request.setConnector(this);
            return (request);
    
        }

    (2)创建响应对象

      /**
         * Create (or allocate) and return a Response object suitable for
         * receiving the contents of a Response from the responsible Container.
         */
        public Response createResponse() {
    
            Response response = new Response();
            response.setConnector(this);
            return (response);
    
        }

    (3)传给这两个对象给容器,简单而言,就是在创建好对象后,传递给那个适配器类就OK了。CoyoteAdapter类

  • 相关阅读:
    去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手 zhuan zai
    软件架构师应该知道的97件事
    互联网创业需要哪些人? 留住人才,到一线去
    推荐:你可能需要的在线电子书 (转载)
    ios DOME(http://www.cocoachina.com/bbs/read.php?tid8101.html)
    在获得自信时常犯的三个错误 你的成功在于你每天养成的习惯
    Web开发性能优化总结 转载
    Android 移动平台概述
    互联网产品需求管理思考——统一需求管理
    Android 开发工具
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/4229756.html
Copyright © 2011-2022 走看看