Tomcat源码分析(十二) -- Connector

481 阅读1分钟

Tomcat 中的 Connector

Connector 的 initInternal 方法

protected void initInternal() throws LifecycleException {

    super.initInternal();

    // Initialize adapter
    //Connector 中的处理是Endpoint接收请求 -> ProtocolHandler把Socket协议转换为Http协议 -> Adapter转换为HttpSevletRequest -> Container
    adapter = new CoyoteAdapter(this);
    protocolHandler.setAdapter(adapter);

    // Make sure parseBodyMethodsSet has a default
    if (null == parseBodyMethodsSet) {
        setParseBodyMethods(getParseBodyMethods());
    }

    if (protocolHandler.isAprRequired() && !AprLifecycleListener.isInstanceCreated()) {
        throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprListener",
                                                  getProtocolHandlerClassName()));
    }
    if (protocolHandler.isAprRequired() && !AprLifecycleListener.isAprAvailable()) {
        throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprLibrary",
                                                  getProtocolHandlerClassName()));
    }
    if (AprLifecycleListener.isAprAvailable() && AprLifecycleListener.getUseOpenSSL() &&
        protocolHandler instanceof AbstractHttp11JsseProtocol) {
        AbstractHttp11JsseProtocol<?> jsseProtocolHandler =
            (AbstractHttp11JsseProtocol<?>) protocolHandler;
        if (jsseProtocolHandler.isSSLEnabled() &&
            jsseProtocolHandler.getSslImplementationName() == null) {
            // OpenSSL is compatible with the JSSE configuration, so use it if APR is available
            jsseProtocolHandler.setSslImplementationName(OpenSSLImplementation.class.getName());
        }
    }

    try {
        //核心在这里,调用 protocolHandler 的init方法
        //这里进入的是 org.apache.coyote.http11.AbstractHttp11Protocol 的init方法
        protocolHandler.init();
    } catch (Exception e) {
        throw new LifecycleException(
            sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e);
    }
}

Connector 的 startInternal 方法

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 方法
        protocolHandler.start();
    } catch (Exception e) {
        throw new LifecycleException(
            sm.getString("coyoteConnector.protocolHandlerStartFailed"), e);
    }
}

Connector 的 stopInternal方法

protected void stopInternal() throws LifecycleException {

    setState(LifecycleState.STOPPING);

    try {
        //调用 protocolHandler 的 stop 方法
        protocolHandler.stop();
    } catch (Exception e) {
        throw new LifecycleException(
            sm.getString("coyoteConnector.protocolHandlerStopFailed"), e);
    }
}

Connector 的 stopInternal方法

protected void destroyInternal() throws LifecycleException {
    try {
        //调用 protocolHandler 的 destroy 方法
        protocolHandler.destroy();
    } catch (Exception e) {
        throw new LifecycleException(
            sm.getString("coyoteConnector.protocolHandlerDestroyFailed"), e);
    }

    if (getService() != null) {
        //解绑service
        getService().removeConnector(this);
    }

    super.destroyInternal();
}

小结

  • Connector 所有的操作都是基于 ProtocolHandler 对象完成的
  • Connector 初始化了一个 CoyoteAdapter 对象并设置给了 ProtocolHandler