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

Tomcat8.x 上下文(StandardServer)中命名上下文(NamingContext)的创建

2016-08-06 18:47 330 查看
(StandardServer)中命名上下文(NamingContext)的创建

// 命名上下文的创建---------1
class org.apache.catalina.core.StandardServer{

// 构造函数
public StandardServer() {
// “命名资源”
globalNamingResources = new NamingResourcesImpl();
globalNamingResources.setContainer(this);
// 创建“命名上下文”监听器
namingContextListener = new NamingContextListener();
addLifecycleListener(namingContextListener);
}

protected void startInternal() throws LifecycleException {
// 触发监听器
// org.apache.catalina.core.NamingContextListener.lifecycleEvent(...)
fireLifecycleEvent(CONFIGURE_START_EVENT, null); // "configure_start" 事件

//  org.apache.catalina.deploy.NamingResourcesImpl
globalNamingResources.start();
}

}

// "命名资源"
class org.apache.catalina.deploy.NamingResourcesImpl{
protected void startInternal() throws LifecycleException {
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
setState(LifecycleState.STARTING);
}
}

// "命名上下文"监听器
class org.apache.catalina.core.NamingContextListener{

// 事件处理器,创建"命名上下文"
public void lifecycleEvent(LifecycleEvent event) {
container = event.getLifecycle();

namingResources = ((Server) container).getGlobalNamingResources();
if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
Hashtable<String, Object> contextEnv = new Hashtable<>();
namingContext = new NamingContext(contextEnv, getName()); // 创建"命名上下文"文----------
ContextAccessController.setSecurityToken(getName(), token);
ContextAccessController.setSecurityToken(container, token);
ContextBindings.bindContext(container, namingContext, token);
createNamingContext(); // 创建“名称上下文”
}
}

// 创建"命名上下文"
private void createNamingContext()
throws NamingException {
// container === org.apache.catalina.core.StandardServer
if (container instanceof Server) { // 走这里
compCtx = namingContext; // org.apache.naming.NamingContext
envCtx = namingContext;
} else {
compCtx = namingContext.createSubcontext("comp");
envCtx = compCtx.createSubcontext("env");
}
// Resources
ContextResource[] resources = namingResources.findResources();
for (i = 0; i < resources.length; i++) {
addResource(resources[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: