将 webApp 添加到 Tomcat 动态嵌入

Adding a webApp to Tomcat Embedded dynamically

我需要理解为什么使用下面的代码将 webapp 动态添加到嵌入式 tomcat 会出现以下错误,当我们在启动时添加 Context 时一切正常,代码是相同的。 代码:

/*
 * Add a context to the host and contexts map; be careful to call this only
 * within a synchronization block of this.contexts
 */
private Context addContextDynamically(String contextPath, String folderName) {
    logger.info("Adding context " + contextPath + " at " + folderName);

    Context contextNew = this.embedded.addWebapp(this.host, contextPath, folderName);
    // Context contextNew = this.embedded.addContext(this.host,contextPath,
    // folderName);
    // this.embedded.addContext(host, contextPath, dir)
    // logger.info("contextNew-->"+contextNew);
    List<File> filterdJarfiles = getAppJarFilesAlone(folderName);
    WebResourceRoot resources = new StandardRoot(contextNew);
    for (File jf : filterdJarfiles) {
        String st = jf.getAbsolutePath().substring(0, jf.getAbsolutePath().lastIndexOf(File.separator));
        logger.info("st-->"+st);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/lib", st, "/"));
    }
    logger.fine("Adding context setResources " + resources);
    logger.fine("Adding context contextNew "+ contextNew);
    logger.fine("Adding context filterdJarfiles " + filterdJarfiles.toString());
    try {
        contexts.put(contextNew.getPath(), contextNew);
        contextNew.setResources(resources);
        INexxWebappLoader loader = new INexxWebappLoader(contextNew.getParentClassLoader(), contextNew);
        contextNew.setLoader(loader);
        // host.addChild(context);
        cleanContextWorkDir((StandardContext) contextNew);

    }catch (Exception e) {
        logger.info("Caught exception while adding context :"+ e.getLocalizedMessage());
        e.printStackTrace();
    }
    return contextNew;
}

我们得到的错误是:

java.lang.IllegalStateException: Error starting static Resources
at org.apache.catalina.core.StandardContext.setResources(StandardContext.java:2470)
at com.medicity.iNexx.AppServer.addContextDynamically(AppServer.java:541)
at com.medicity.iNexx.AppServer.monitorEnablements(AppServer.java:439)
at com.medicity.iNexx.AppServer.run(AppServer.java:345)
at com.medicity.iNexx.AppServer.init(AppServer.java:244)
at com.medicity.iNexx.AppServer.main(AppServer.java:335)

所以基本上我们的产品下载 zip 并将其解压缩到 Web 应用程序目录和 server.xml 我们已经设置:

  <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

如果将Context添加到运行Host,它将自动启动,除非属性startChildren设置为[=15] =] 在 Host 组件上。您可能不希望这样,因为您的 addContextDynamically 需要先配置上下文。

因此在用于配置Tomcat的方法中你需要调用:

final Host host = this.embedded.getHost()
if (host instanceof ContainerBase) {
    ((ContainerBase) host).setStartChildren(false);
}

你需要添加到 addContextDynamically:

final State hostState = this.embedded.getHost().getState();
if (hostState.isAvailable() || LifecycleState.STARTING_PREP.equals(hostState)) {
    contextNew.start();
}

比照。 source code 了解更多信息。

备注:在Host开始之前添加的上下文不受startChildren 属性.

的影响