[Tomcat6.0源码]组件的初始化
2012-09-11 18:40
330 查看
1.Bootstrap.main()做的几件事:
1)daemon.init();创建Catalina对象
2)daemon.load(args);调用Catalina.load();通过解析server.xml对相关组件的实例化和初始化
3)daemon.start();调用Catalina.start()依次启动各组件
前面已经了解Catalina.load()用Digester实例化各组件的过程。接着看看各组件的初始化:
2.getServer().initialize();调用StandardServer.initialize():
3.循环执行services[i].initialize();即调用与其相关的所有StandardService.initialize():
4.循环执行connectors[i].initialize();即调用与其相关的所有Connector.initialize():
5.protocolHandler.init();调用Http11Protocol.init();
6.endpoint.init();(endpoint对象是在Http11Protocol的属性中直接new出来的和之前用Digester解析配置不一样)。调用JIoEndpoint.init():
7.serverSocketFactory.createSocket()创建ServerSocket占用地址、端口开始监听请求。
初始化过程其实是初始化Connector这边的组件。StandardEngine的并没有初始化。
1)daemon.init();创建Catalina对象
2)daemon.load(args);调用Catalina.load();通过解析server.xml对相关组件的实例化和初始化
3)daemon.start();调用Catalina.start()依次启动各组件
前面已经了解Catalina.load()用Digester实例化各组件的过程。接着看看各组件的初始化:
if (getServer() instanceof Lifecycle) { try { getServer().initialize(); } catch (LifecycleException e) { if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) throw new java.lang.Error(e); else log.error("Catalina.start", e); } }
2.getServer().initialize();调用StandardServer.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 { if (initialized) { log.info(sm.getString("standardServer.initialize.initialized")); return; } lifecycle.fireLifecycleEvent(INIT_EVENT, null); initialized = true; if( oname==null ) { try { 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 for (int i = 0; i < services.length; i++) { services[i].initialize(); } }
3.循环执行services[i].initialize();即调用与其相关的所有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); 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 synchronized (connectors) { for (int i = 0; i < connectors.length; i++) { try { connectors[i].initialize(); } catch (Exception e) { String message = sm.getString( "standardService.connector.initFailed", connectors[i]); log.error(message, e); if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) throw new LifecycleException(message); } } } }
4.循环执行connectors[i].initialize();即调用与其相关的所有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 = new CoyoteAdapter(this); 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)); } }
5.protocolHandler.init();调用Http11Protocol.init();
public void init() throws Exception { 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 { 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())); }
6.endpoint.init();(endpoint对象是在Http11Protocol的属性中直接new出来的和之前用Digester解析配置不一样)。调用JIoEndpoint.init():
public void init() throws Exception { if (initialized) return; // Initialize thread count defaults for acceptor if (acceptorThreadCount == 0) { acceptorThreadCount = 1; } if (serverSocketFactory == null) { serverSocketFactory = ServerSocketFactory.getDefault(); } if (serverSocket == null) { try { if (address == null) { serverSocket = serverSocketFactory.createSocket(port, backlog); } else { serverSocket = serverSocketFactory.createSocket(port, backlog, address); } } catch (BindException orig) { String msg; if (address == null) msg = orig.getMessage() + " <null>:" + port; else msg = orig.getMessage() + " " + address.toString() + ":" + port; BindException be = new BindException(msg); be.initCause(orig); throw be; } } //if( serverTimeout >= 0 ) // serverSocket.setSoTimeout( serverTimeout ); initialized = true; }
7.serverSocketFactory.createSocket()创建ServerSocket占用地址、端口开始监听请求。
初始化过程其实是初始化Connector这边的组件。StandardEngine的并没有初始化。
相关文章推荐
- Tomcat源码阅读之初始化连接器组件
- [Tomcat6.0源码]组件的启动
- [Tomcat6.0源码]组件的实例化
- Tomcat源码解析(四):tomcat核心组件初始化和启动
- Tomcat源码阅读之初始化Server组件
- 死磕Tomcat7源码之二:web组件初始化
- tomcat server组件监听shutdown命令关闭服务器之源码分析
- Tomcat6.0 源码 eclipse 简单配置
- [Tomcat6.0源码]从BootStrap的main开始
- [Tomcat6.0源码]请求的处理二Request
- Vue源码翻译之组件初始化。
- SpringMVC源码(三)-组件初始化
- tomcat源码分析-Container初始化与加载
- tomcat源码研读笔记—tomcat的初始化之一 StandardServer的初始化
- 【Tomcat9源码分析】组件与框架概述
- 配置tomcat6.0,tomcat5.5的admin组件教程
- tomcat源码阅读笔记二——初始化过程(下)
- tomcat源码研读笔记—tomcat的初始化之二 StandardService的初始化
- 深入理解 Tomcat(六)源码剖析Tomcat 启动过程----生命周期和容器组件
- Tomcat6.0源码学习--Connector架构