前言
上篇文章讲到了 Engine 的 init 和 start 方法,在 Engine 的 start(ContainerBase#startInternal) 方法里调用了子容器的 start 方法,而 Engine 的子容器就是 Host。在 Engine 的 init 方法中并无调用子容器的 init 方法,而是在 start 方法中调用子容器的 start 方法的时候,在 LifecycleBase 方法里 调用了子容器的 init 方法。web
1 Host#initInternal 方法
Host 的实现类是 StandardHost。StandardHost 也是继承自 ContainerBase,可是没有重载 initInternal 方法。所以,在调用 Host#init 方法的时候,执行的是 ContainerBase#initInternal 方法,关于 ContainerBase#initInternal 在上篇文章就讲过了,这里再也不赘述。apache
2 Host#startInternal 方法app
/** * Start this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { // Set error report valve String errorValve = getErrorReportValveClass(); if ((errorValve != null) && (!errorValve.equals(""))) { try { boolean found = false; Valve[] valves = getPipeline().getValves(); for (Valve valve : valves) { if (errorValve.equals(valve.getClass().getName())) { found = true; break; } } if(!found) { Valve valve = (Valve) Class.forName(errorValve).getConstructor().newInstance(); getPipeline().addValve(valve); } } catch (Throwable t) { ExceptionUtils.handleThrowable(t); log.error(sm.getString( "standardHost.invalidErrorReportValveClass", errorValve), t); } } super.startInternal(); } public String getErrorReportValveClass() { return this.errorReportValveClass; } /** * The Java class name of the default error reporter implementation class * for deployed web applications. */ private String errorReportValveClass = "org.apache.catalina.valves.ErrorReportValve";
Host#startInternal 的逻辑很简单,就是看本身的 Pipeline 对象里是否包含了 org.apache.catalina.valves.ErrorReportValve 这个 Valve 对象,若是没有,就添加一个 org.apache.catalina.valves.ErrorReportValve 到本身的 Pipeline 对象里。ide
而后调用 ContainerBase 的 startInternal 方法,ContainerBase#startInternal 方法在上篇文章也讲到了,这里就很少讲了。ui
2 Host#backgroundProcess 方法
上篇文章分析了,StandardEngine 在调用父类 ContainerBase 的 startInternal 的时候递归调用了子容器的 backgroundProcess 方法。
StandardHost 没有重载 backgroundProcess 方法,所以调用 backgroundProcess 方法时,执行的仍是 ContainerBase 的 backgroundProcess 方法,这在上篇文章就分析过了,这里再也不多说了。this
小结
本篇文章简单的 StandardHost 的 init 和 start 方法。StandardHost 有一些重要的属性,好比,appBase、autoDeploy、unpackWARs 等。code