1 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個贊
沒有系統(tǒng)屬性可用于在 WebAppContext 上配置服務(wù)器或系統(tǒng)類。
這是因?yàn)檫@種更改被認(rèn)為屬于特定的 WebApp,而不是所有 WebApp。
但是,您還有另一種選擇,如果您在嵌入式碼頭中使用DeploymentManager
,那么您很幸運(yùn),您可以選擇代碼。
您需要創(chuàng)建一個自定義項(xiàng)AppLifeCycle.Binding
,在任何已部署的項(xiàng)上設(shè)置這些屬性WebAppContext
(我建議綁定到deploying
)。
下面是一個強(qiáng)制 WebAppContext 始終使用服務(wù)器/系統(tǒng)類加載器中的日志庫的示例。
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppLifeCycle;
import org.eclipse.jetty.deploy.graph.Node;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.webapp.WebAppContext;
public class CentralizedWebAppLoggingBinding implements AppLifeCycle.Binding
{
? ? public String[] getBindingTargets()
? ? {
? ? ? ? return new String[]
? ? ? ? { "deploying" };
? ? }
? ? public void processBinding(Node node, App app) throws Exception
? ? {
? ? ? ? ContextHandler handler = app.getContextHandler();
? ? ? ? if (handler == null)
? ? ? ? {
? ? ? ? ? ? throw new NullPointerException("No Handler created for App: " + app);
? ? ? ? }
? ? ? ? if (handler instanceof WebAppContext)
? ? ? ? {
? ? ? ? ? ? WebAppContext webapp = (WebAppContext)handler;
? ? ? ? ? ? webapp.addSystemClass("org.apache.log4j.");
? ? ? ? ? ? webapp.addSystemClass("org.slf4j.");
? ? ? ? ? ? webapp.addSystemClass("org.apache.commons.logging.");
? ? ? ? }
? ? }
}
這是一個從嵌入式碼頭使用 DeploymentManager 的示例(CentralizedWebAppLoggingBinding也包含上述內(nèi)容)。
ContextHandlerCollection contexts = new ContextHandlerCollection();
DeploymentManager deployer = new DeploymentManager();
if(debugIsEnabled)
{
? ? DebugListener debug = new DebugListener(System.err, true, true, true);
? ? server.addBean(debug);
? ? deployer.addLifeCycleBinding(new DebugListenerBinding(debug));
}
deployer.setContexts(contexts);
deployer.setContextAttribute(
? ? "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
? ? ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");
WebAppProvider webAppProvider = new WebAppProvider();
webAppProvider.setMonitoredDirName(jettyBase + "/webapps");
webAppProvider.setDefaultsDescriptor(jettyHome + "/etc/webdefault.xml");
webAppProvider.setScanInterval(1);
webAppProvider.setExtractWars(true);
webAppProvider.setConfigurationManager(new PropertiesConfigurationManager());
webAppProvider.addLifeCycleListener(new CentralizedWebAppLoggingBinding());
添加回答
舉報(bào)