您的位置:首页 > 运维架构 > Tomcat

Tomcat启动流程之StandardContext启动

2020-04-03 07:42 1126 查看

StandardContext启动
对一些启动过程进行详细分析一下这个分析不是完整的完整的请看我的收藏这里只是对一些不清楚的地方补充了一下

protected void deployApps() {

File appBase = host.getAppBaseFile();
File configBase = host.getConfigBaseFile();
// 过滤出 webapp 要部署应用的目录
String[] filteredAppPaths = filterAppPaths(appBase.list());
// 部署 xml 描述文件
deployDescriptors(configBase, configBase.list());
// 解压 war 包,但是这里还不会去启动应用
//StandardContext的启动入口
//看其他文章找了好久
deployWARs(appBase, filteredAppPaths);
// 处理已经存在的目录,前面解压的 war 包不会再行处理
deployDirectories(appBase, filteredAppPaths);

}

代码有点长我也贴出来了 具体分析我就不解释了可以看注释

protected void deployWARs(File appBase, String[] files) {

if (files == null)
return;

ExecutorService es = host.getStartStopExecutor();
List<Future<?>> results = new ArrayList<>();

for (int i = 0; i < files.length; i++) {

if (files[i].equalsIgnoreCase("META-INF"))
continue;
if (files[i].equalsIgnoreCase("WEB-INF"))
continue;
File war = new File(appBase, files[i]);
if (files[i].toLowerCase(Locale.ENGLISH).endsWith(".war") &&
war.isFile() && !invalidWars.contains(files[i]) ) {

ContextName cn = new ContextName(files[i], true);

if (isServiced(cn.getName())) {
continue;
}
if (deploymentExists(cn.getName())) {
DeployedApplication app = deployed.get(cn.getName());
boolean unpackWAR = unpackWARs;
if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
}
if (!unpackWAR && app != null) {
// Need to check for a directory that should not be
// there
File dir = new File(appBase, cn.getBaseName());
if (dir.exists()) {
if (!app.loggedDirWarning) {
log.warn(sm.getString(
"hostConfig.deployWar.hiddenDir",
dir.getAbsoluteFile(),
war.getAbsoluteFile()));
app.loggedDirWarning = true;
}
} else {
app.loggedDirWarning = false;
}
}
continue;
}

// Check for WARs with /../ /./ or similar sequences in the name
if (!validateContextPath(appBase, cn.getBaseName())) {
log.error(sm.getString(
"hostConfig.illegalWarName", files[i]));
invalidWars.add(files[i]);
continue;
}
// 进入DeployWar方法这个方法太长了
results.add(es.submit(new DeployWar(this, cn, war)));
}
}
Class<?> clazz = Class.forName(host.getConfigClass());
LifecycleListener listener = (LifecycleListener) clazz.getConstructor().newInstance();
context.addLifecycleListener(listener);

context.setName(cn.getName());
context.setPath(cn.getPath());
context.setWebappVersion(cn.getVersion());
context.setDocBase(cn.getBaseName() + ".war");
//从这进去
host.addChild(context);
@Override
public void addChild(Container child) {

if (!(child instanceof Context))
throw new IllegalArgumentException
(sm.getString("standardHost.notContext"));

child.addLifecycleListener(new MemoryLeakTrackingListener());

// Avoid NPE for case where Context is defined in server.xml with only a
// docBase
Context context = (Context) child;
if (context.getPath() == null) {
ContextName cn = new ContextName(context.getDocBase(), true);
context.setPath(cn.getPath());
}
//调用父类ContainerBase
super.addChild(child);

}
@Override
public void addChild(Container child) {
if (Globals.IS_SECURITY_ENABLED) {
PrivilegedAction<Void> dp =
new PrivilegedAddChild(child);
AccessController.doPrivileged(dp);
} else {
//继续进去
addChildInternal(child);
}
}
private void addChildInternal(Container child) {

if( log.isDebugEnabled() )
log.debug("Add child " + child + " " + this);
synchronized(children) {
if (children.get(child.getName()) != null)
throw new IllegalArgumentException("addChild:  Child name '" +
child.getName() +
"' is not unique");
child.setParent(this);  // May throw IAE
children.put(child.getName(), child);
}

// Start child
// Don't do this inside sync block - start can be a slow process and
// locking the children object can cause problems elsewhere
try {
if ((getState().isAvailable() ||
LifecycleState.STARTING_PREP.equals(getState())) &&
startChildren) {
//这里开始启动StandardContext
child.start();
}
} catch (LifecycleException e) {
log.error("ContainerBase.addChild: start: ", e);
throw new IllegalStateException("ContainerBase.addChild: start: " + e);
} finally {
fireContainerEvent(ADD_CHILD_EVENT, child);
}
}

到这里只是进入到了StandardContext下面的步骤就看我的收藏吧

  • 点赞
  • 收藏
  • 分享
  • 文章举报
一场梦@bhl 发布了15 篇原创文章 · 获赞 5 · 访问量 1548 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: