总结一下ZZ Coder
对我有很大指导的答案:
您必须创建一个实现ContainerServlet
和覆盖setWrapper
方法的 servlet 来获取org.apache.catalina.Wrapper
对象。
为此,您必须privileged="true"
在context.xml
Context
标签中包含,否则会引发异常。然后您可以使用该Wrapper
对象并:
StandardContext context = (StandardContext) wrapper.getParent();
StandardHost currentHost = (StandardHost) context.getParent();
StandardEngine engine = (StandardEngine) currentHost.getParent();
StandardHost host = new StandardHost();
host.setAppBase(currentHost.getAppBase()); //in my case I created another instance of the same application
host.setDomain(currentHost.getDomain());
host.setAutoDeploy(false); // not restarting app whenever changes happen
host.setName("domain.com");
host.setThrowOnFailure(true);// tell it to throw an exception here if it fails to create the host
host.setDeployOnStartup(true);
host.setStartChildren(true);
host.setParent(engine);
// you can add multiple aliases
host.addAlias(alias);
StandardContext ctx = new StandardContext();
ctx.setDocBase(context.getDocBase()); //again I reused my same application setting
ctx.setPath("");
if(currentHost.getWorkDir() != null)
{//create a working directory based on your new host's name
ctx.setWorkDir(currentHost.getWorkDir().replace(currentHost.getName(), host.getName()));
}
ctx.setName(host.getDomain());
//some extra config that you can use
ctx.setUseHttpOnly(false);
ctx.setReloadable(false);
ctx.setXmlValidation(false);
ctx.setXmlNamespaceAware(false);
ctx.setCrossContext(false);
ctx.setParent(host);
// you have to have this or it will not work!!
ctx.addLifecycleListener(new ContextConfig());
//you can also create resources and add it to the context like so:
final ContextResource res = new ContextResource();
res.setName("name");
res.setAuth("Container");
res.setType("javax.sql.DataSource");
ctx.getNamingResources().addResource(res);
host.addChild(ctx);
engine.addChild(host);
您可以通过调用向资源添加属性res.setProperty("name", "value")
可以使用的一些属性是:
initialSize
, maxTotal
, maxIdle
, maxWaitMillis
, removeAbandonedOnBorrow
, removeAbandonedTimeout
, validationQuery
, timeBetweenEvictionRunsMillis
, driverClassName
, url
, username
, password
。
另一个令人兴奋的事情是通过调用engine.findChild(domain)
和使用从tomcat引擎获取主机stop()
,并拥有自己的Tomcat管理面板start()
!getStateName()