protected void doStart() throws Exception
{
try
{
// 初始化四个类WebInfConfiguration,WebXmlConfiguration, JettyWebXmlConfiguration,TagLibConfiguration
loadConfigurations();
for (int i=0;i<_configurations.length;i++)
_configurations.setWebAppContext(this);
// Configure classloader
_ownClassLoader=false;
if (getClassLoader()==null)
{
WebAppClassLoader classLoader = new WebAppClassLoader(this);
setClassLoader(classLoader);//生成classloader加载应用的war
_ownClassLoader=true;
}
if (Log.isDebugEnabled())//打印日志
{
ClassLoader loader = getClassLoader();
Log.debug("Thread Context class loader is: " + loader);
loader=loader.getParent();
while(loader!=null)
{
Log.debug("Parent class loader is: " + loader);
loader=loader.getParent();
}
}
for (int i=0;i<_configurations.length;i++)
_configurations.configureClassLoader();//WebInfConfiguration类加载war包的lib,classes目录
getTempDirectory();
if (_tmpDir!=null && !_isExistingTmpDir && !isTempWorkDirectory())
{
File sentinel = new File(_tmpDir, ".active");
if(!sentinel.exists())
sentinel.mkdir();
}
super.doStart();
if (isLogUrlOnStart())
dumpUrl();
}
catch (Exception e)
{
//start up of the webapp context failed, make sure it is not started
Log.warn("Failed startup of context "+this, e);
_unavailableException=e;
_unavailable = true;
}
}
WebInfConfiguration类的configureClassLoader()方法如下:
public void configureClassLoader()
throws Exception
{
//cannot configure if the context is already started
if (_context.isStarted())
{
if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp after it is started");}
return;
}
Resource web_inf=_context.getWebInf();//生成临时目录将war拷过来
// Add WEB-INF classes and lib classpaths
if (web_inf != null && web_inf.isDirectory() && _context.getClassLoader() instanceof WebAppClassLoader)
{
// Look for classes directory
Resource classes= web_inf.addPath("classes/");
if (classes.exists())
((WebAppClassLoader)_context.getClassLoader()).addClassPath(classes.toString());
// Look for jars
Resource lib= web_inf.addPath("lib/");
if (lib.exists() || lib.isDirectory())
((WebAppClassLoader)_context.getClassLoader()).addJars(lib);
}
}
WebAppContext的start()方法里的 super.doStart()会调用ContextHandler的doStart()方法,方法如下:
protected void doStart() throws Exception
{
if (_contextPath==null)
throw new IllegalStateException("Null contextPath");
_logger=Log.getLogger(getDisplayName()==null?getContextPath():getDisplayName());
ClassLoader old_classloader=null;
Thread current_thread=null;
SContext old_context=null;
_contextAttributes=new AttributesMap();
try
{
// Set the classloader
if (_classLoader!=null)
{
current_thread=Thread.currentThread();
old_classloader=current_thread.getContextClassLoader();
current_thread.setContextClassLoader(_classLoader);
}
if (_mimeTypes==null)
_mimeTypes=new MimeTypes();
old_context=(SContext)__context.get();
__context.set(_scontext);
if (_errorHandler==null)
setErrorHandler(new ErrorHandler());
startContext();
}
finally
{
__context.set(old_context);
// reset the classloader
if (_classLoader!=null)
{
current_thread.setContextClassLoader(old_classloader);
}
}
}
startContext()方法会调用WebAppContext类的startContext()方法,如下: