首页 > 代码库 > Tomcat 7 Connector 精读(1)

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类

 

Tomcat 7 Connector 精读(1)