首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Tomcat源码-初始化器皿五

2012-10-08 
Tomcat源码---初始化容器五一,上面文章完成了对server.xml载入以及解析,现在主要做的是就是对里面所定义的

Tomcat源码---初始化容器五

一,上面文章完成了对server.xml载入以及解析,现在主要做的是就是对里面所定义的容器进行初始化工作.

查看org.apache.catalina.startup.Catalina#load()

?

  1. ?? ??//StandarServer开始进行实例化??
  2. ???????????????server.initialize(); ?

?

默认情况下是org.apache.catalina.core.StandardServer#initialize的初始化....

?

 public void initialize()        throws LifecycleException     {        if (initialized) {                log.info(sm.getString("standardServer.initialize.initialized"));            return;        }//容器周期的通知,这里使用了Adapter,Observer模式,以下文章对其详解        lifecycle.fireLifecycleEvent(INIT_EVENT, null);        //已经初始化        initialized = true;        if( oname==null ) {            try {//使用jmx进行对象管理                oname=new ObjectName( "Catalina:type=Server");                Registry.getRegistry(null, null)                    .registerComponent(this, oname, null );            } catch (Exception e) {                log.error("Error registering ",e);            }        }                // Register global String cache        try {            ObjectName oname2 =                 new ObjectName(oname.getDomain() + ":type=StringCache");            Registry.getRegistry(null, null)                .registerComponent(new StringCache(), oname2, null );        } catch (Exception e) {            log.error("Error registering ",e);        }        // Initialize our defined Services//按着server.xml里的流程,初始化完server完再初始化StandardService        for (int i = 0; i < services.length; i++) {            services[i].initialize();        }    }

?? //org.apache.catalina.core.StandardService#initialize() 初始化

?

     /**     * Invoke a pre-startup initialization. This is used to allow connectors     * to bind to restricted ports under Unix operating environments.     */    public void initialize()            throws LifecycleException    {        // Service shouldn't be used with embeded, so it doesn't matter        if (initialized) {            if(log.isInfoEnabled())                log.info(sm.getString("standardService.initialize.initialized"));            return;        }        initialized = true;        if( oname==null ) {            try {                // Hack - Server should be deprecated...                Container engine=this.getContainer();                domain=engine.getName();                oname=new ObjectName(domain + ":type=Service,serviceName="+name);                this.controller=oname;                Registry.getRegistry(null, null)                    .registerComponent(this, oname, null);                //对线程池的初始化,在消息传递时,如果有server.xml中设置了,就覆盖默认设置                Executor[] executors = findExecutors();                for (int i = 0; i < executors.length; i++) {                    ObjectName executorObjectName =                         new ObjectName(domain + ":type=Executor,name=" + executors[i].getName());                    Registry.getRegistry(null, null)                        .registerComponent(executors[i], executorObjectName, null);                }                            } catch (Exception e) {                log.error(sm.getString("standardService.register.failed",domain),e);            }                                }        if( server==null ) {            // Register with the server             // HACK: ServerFactory should be removed...                        ServerFactory.getServer().addService(this);        }                       // Initialize our defined Connectors        //往下一级初始化就是connector 两个 如8080 8009        synchronized (connectors) {                for (int i = 0; i < connectors.length; i++) {                    connectors[i].initialize();                }        }    }

??现在进入org.apache.catalina.connector#initialize

?

 /**     * Initialize this connector (create ServerSocket here!)     */    public void initialize()        throws LifecycleException    {        if (initialized) {            if(log.isInfoEnabled())                log.info(sm.getString("coyoteConnector.alreadyInitialized"));           return;        }        this.initialized = true;        if( oname == null && (container instanceof StandardEngine)) {            try {                // we are loaded directly, via API - and no name was given to us                StandardEngine cb=(StandardEngine)container;                oname = createObjectName(cb.getName(), "Connector");                Registry.getRegistry(null, null)                    .registerComponent(this, oname, null);                controller=oname;            } catch (Exception e) {                log.error( "Error registering connector ", e);            }            if(log.isDebugEnabled())                log.debug("Creating name for connector " + oname);        }        // Initializa adapter        //对Adapter的初始化,并把connector设置进去        adapter = new CoyoteAdapter(this);        //以下对象在构造函数中已经实例化,把adapter设置进去        protocolHandler.setAdapter(adapter);        IntrospectionUtils.setProperty(protocolHandler, "jkHome",                                       System.getProperty("catalina.base"));        try {            //继续下一步的实现            protocolHandler.init();        } catch (Exception e) {            throw new LifecycleException                (sm.getString                 ("coyoteConnector.protocolHandlerInitializationFailed", e));        }    }

???org.apache.coyote.http11.Http11Protocol(implements ProtocolHandler)#init

 public void init() throws Exception {        //JIoEndpoint在全局变量中已经实例化,以下为其设置相应的值        endpoint.setName(getName());        endpoint.setHandler(cHandler);        // Verify the validity of the configured socket factory        try {            if (isSSLEnabled()) {                sslImplementation =                    SSLImplementation.getInstance(sslImplementationName);                                socketFactory = sslImplementation.getServerSocketFactory();                endpoint.setServerSocketFactory(socketFactory);            } else if (socketFactoryName != null) {                socketFactory = (ServerSocketFactory) Class.forName(socketFactoryName).newInstance();                endpoint.setServerSocketFactory(socketFactory);            }        } catch (Exception ex) {            log.error(sm.getString("http11protocol.socketfactory.initerror"),                      ex);            throw ex;        }        if (socketFactory!=null) {            Iterator<String> attE = attributes.keySet().iterator();            while( attE.hasNext() ) {                String key = attE.next();                Object v=attributes.get(key);                socketFactory.setAttribute(key, v);            }        }                try {            //以上if语句都没有通过执行,直接执行到JIoEndpoint            endpoint.init();        } catch (Exception ex) {            log.error(sm.getString("http11protocol.endpoint.initerror"), ex);            throw ex;        }        if (log.isInfoEnabled())            log.info(sm.getString("http11protocol.init", getName()));    }

???org.apache.tomcat.util.net.JIoEndpoint#init ? 这个类里面对Socket进行了设置,以后每一次通信都从这个类开始

public void init()        throws Exception {        if (initialized)            return;                // Initialize thread count defaults for acceptor        if (acceptorThreadCount == 0) {            acceptorThreadCount = 1;        }        if (serverSocketFactory == null) {            //serverSocketFacotry的创建(工厂模式)            serverSocketFactory = ServerSocketFactory.getDefault();        }        if (serverSocket == null) {            try {                if (address == null) {                    //创建serverSocket                    serverSocket = serverSocketFactory.createSocket(port, backlog);                } else {                    serverSocket = serverSocketFactory.createSocket(port, backlog, address);                }            } catch (BindException be) {                if (address == null)                    throw new BindException(be.getMessage() + "<null>:" + port);                else                    throw new BindException(be.getMessage() + " " +                            address.toString() + ":" + port);            }        }        //if( serverTimeout >= 0 )        //    serverSocket.setSoTimeout( serverTimeout );                initialized = true;            }

??到这里tomcat的初始化工作已经基本完成,接下来进行相应容器的启动处理

热点排行