use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class GlobalWebappConfigBinding method processBinding.
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 context = (WebAppContext) handler;
if (LOG.isDebugEnabled()) {
LOG.debug("Binding: Configuring webapp context with global settings from: " + _jettyXml);
}
if (_jettyXml == null) {
LOG.warn("Binding: global context binding is enabled but no jetty-web.xml file has been registered");
}
Resource globalContextSettings = Resource.newResource(_jettyXml);
if (globalContextSettings.exists()) {
XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings.getInputStream());
Resource resource = Resource.newResource(app.getOriginId());
File file = resource.getFile();
jettyXmlConfig.getIdMap().put("Server", app.getDeploymentManager().getServer());
jettyXmlConfig.getProperties().put("jetty.home", System.getProperty("jetty.home", "."));
jettyXmlConfig.getProperties().put("jetty.base", System.getProperty("jetty.base", "."));
jettyXmlConfig.getProperties().put("jetty.webapp", file.getCanonicalPath());
jettyXmlConfig.getProperties().put("jetty.webapps", file.getParentFile().getCanonicalPath());
jettyXmlConfig.configure(context);
} else {
LOG.info("Binding: Unable to locate global webapp context settings: " + _jettyXml);
}
}
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class OverlayedAppProvider method createContextHandler.
/**
* Create Context Handler.
* <p>
* Callback from the deployment manager to create a context handler instance.
* @see org.eclipse.jetty.deploy.AppProvider#createContextHandler(org.eclipse.jetty.deploy.App)
*/
public synchronized ContextHandler createContextHandler(App app) throws Exception {
final OverlayedApp overlayed = (OverlayedApp) app;
final String origin = overlayed.getOriginId();
final Instance instance = overlayed.getInstance();
final Template template = instance.getTemplate();
final Webapp webapp = template.getWebapp();
final Node node = _node;
// remember the original loader
ClassLoader orig_loader = Thread.currentThread().getContextClassLoader();
try {
// Look for existing shared resources
String key = (node == null ? "" : node.getLoadedKey()) + template.getLoadedKey() + (webapp == null ? "" : webapp.getLoadedKey());
instance.setSharedKey(key);
TemplateContext shared = _shared.get(key);
// Create shared resourced
if (shared == null)
shared = createTemplateContext(key, webapp, template, node, orig_loader);
// Build the instance lib loader
ClassLoader shared_loader = shared.getWebappLoader() != null ? shared.getWebappLoader() : (shared.getLibLoader() != null ? shared.getLibLoader() : orig_loader);
ClassLoader loader = shared_loader;
Resource instance_lib = instance.getResource(LIB);
if (instance_lib.exists()) {
List<URL> libs = new ArrayList<URL>();
for (String jar : instance_lib.list()) {
if (!jar.toLowerCase(Locale.ENGLISH).endsWith(".jar"))
continue;
libs.add(instance_lib.addPath(jar).getURL());
}
__log.debug("{}: libs={}", origin, libs);
loader = URLClassLoader.newInstance(libs.toArray(new URL[] {}), loader);
}
// set the thread loader
Thread.currentThread().setContextClassLoader(loader);
// Create properties to be shared by overlay.xmls
Map<String, Object> idMap = new HashMap<String, Object>();
idMap.putAll(shared.getIdMap());
idMap.put(_serverID, getDeploymentManager().getServer());
// Create the instance context for the template
ContextHandler context = null;
Resource template_context_xml = template.getResource(OVERLAY_XML);
if (template_context_xml.exists()) {
__log.debug("{}: overlay.xml={}", origin, template_context_xml);
XmlConfiguration xmlc = newXmlConfiguration(template_context_xml.getURL(), idMap, template, instance);
context = (ContextHandler) xmlc.configure();
idMap = xmlc.getIdMap();
} else if (webapp == null)
// If there is no webapp, this is a plain context
context = new ContextHandler();
else
// It is a webapp context
context = new WebAppContext();
// Set the resource base
final Resource instance_webapp = instance.getResource(WEBAPP);
if (instance_webapp.exists()) {
context.setBaseResource(new ResourceCollection(instance_webapp, shared.getBaseResource()));
// Create the resource cache
ResourceCache cache = new ResourceCache(shared.getResourceCache(), instance_webapp, context.getMimeTypes(), false, false);
context.setAttribute(ResourceCache.class.getCanonicalName(), cache);
} else {
context.setBaseResource(shared.getBaseResource());
context.setAttribute(ResourceCache.class.getCanonicalName(), shared.getResourceCache());
}
__log.debug("{}: baseResource={}", origin, context.getResourceBase());
// Set the shared session scavenger timer
context.setAttribute("org.eclipse.jetty.server.session.timer", _sessionScavenger);
// Apply any node or instance overlay.xml
for (Resource context_xml : getLayeredResources(OVERLAY_XML, node, instance)) {
__log.debug("{}: overlay.xml={}", origin, context_xml);
XmlConfiguration xmlc = newXmlConfiguration(context_xml.getURL(), idMap, template, instance);
xmlc.getIdMap().put("Cache", context.getAttribute(ResourceCache.class.getCanonicalName()));
xmlc.configure(context);
idMap = xmlc.getIdMap();
}
// Is it a webapp?
if (context instanceof WebAppContext) {
final WebAppContext webappcontext = (WebAppContext) context;
if (Arrays.asList(((WebAppContext) context).getServerClasses()).toString().equals(Arrays.asList(WebAppContext.__dftServerClasses).toString())) {
__log.debug("clear server classes");
webappcontext.setServerClasses(null);
}
// set classloader
webappcontext.setCopyWebDir(false);
webappcontext.setCopyWebInf(false);
webappcontext.setExtractWAR(false);
if (instance_webapp.exists()) {
final Resource classes = instance_webapp.addPath("WEB-INF/classes");
final Resource lib = instance_webapp.addPath("WEB-INF/lib");
if (classes.exists() || lib.exists()) {
final AtomicBoolean locked = new AtomicBoolean(false);
WebAppClassLoader webapp_loader = new WebAppClassLoader(loader, webappcontext) {
@Override
public void addClassPath(Resource resource) throws IOException {
if (!locked.get())
super.addClassPath(resource);
}
@Override
public void addClassPath(String classPath) throws IOException {
if (!locked.get())
super.addClassPath(classPath);
}
@Override
public void addJars(Resource lib) {
if (!locked.get())
super.addJars(lib);
}
};
if (classes.exists())
webapp_loader.addClassPath(classes);
if (lib.exists())
webapp_loader.addJars(lib);
locked.set(true);
loader = webapp_loader;
}
}
// Make sure loader is unique for JNDI
if (loader == shared_loader)
loader = new URLClassLoader(new URL[] {}, shared_loader);
// add default descriptor
List<Resource> webdefaults = getLayeredResources(WEB_DEFAULT_XML, instance, node, template);
if (webdefaults.size() > 0) {
Resource webdefault = webdefaults.get(0);
__log.debug("{}: defaultweb={}", origin, webdefault);
webappcontext.setDefaultsDescriptor(webdefault.toString());
}
// add overlay descriptors
for (Resource override : getLayeredResources(WEB_FRAGMENT_XML, template, node, instance)) {
__log.debug("{}: web override={}", origin, override);
webappcontext.addOverrideDescriptor(override.toString());
}
}
context.setClassLoader(loader);
__log.debug("{}: baseResource={}", origin, context.getBaseResource());
Resource jetty_web_xml = context.getResource("/WEB-INF/" + JettyWebXmlConfiguration.JETTY_WEB_XML);
if (jetty_web_xml != null && jetty_web_xml.exists())
context.setAttribute(JettyWebXmlConfiguration.XML_CONFIGURATION, newXmlConfiguration(jetty_web_xml.getURL(), idMap, template, instance));
// Add listener to expand parameters from descriptors before other listeners execute
Map<String, String> params = new HashMap<String, String>();
populateParameters(params, template, instance);
context.addEventListener(new ParameterExpander(params, context));
System.err.println("created:\n" + context.dump());
return context;
} finally {
Thread.currentThread().setContextClassLoader(orig_loader);
}
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class TestConfiguration method testIt.
@Test
public void testIt() throws Exception {
ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
try {
InitialContext ic = new InitialContext();
Server server = new Server();
WebAppContext wac = new WebAppContext();
wac.setServer(server);
wac.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), wac));
MetaData metaData = new MetaData();
PlusDescriptorProcessor plusProcessor = new PlusDescriptorProcessor();
//bind some EnvEntrys at the server level
EnvEntry ee1 = new EnvEntry(server, "xxx/a", "100", true);
EnvEntry ee2 = new EnvEntry(server, "yyy/b", "200", false);
EnvEntry ee3 = new EnvEntry(server, "zzz/c", "300", false);
EnvEntry ee4 = new EnvEntry(server, "zzz/d", "400", false);
EnvEntry ee5 = new EnvEntry(server, "zzz/f", "500", true);
//bind some EnvEntrys at the webapp level
EnvEntry ee6 = new EnvEntry(wac, "xxx/a", "900", true);
EnvEntry ee7 = new EnvEntry(wac, "yyy/b", "910", true);
EnvEntry ee8 = new EnvEntry(wac, "zzz/c", "920", false);
EnvEntry ee9 = new EnvEntry(wac, "zzz/e", "930", false);
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "xxx/a"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "yyy/b"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/c"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/d"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "xxx/a"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "yyy/b"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/c"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/e"));
//make a new env configuration
EnvConfiguration envConfig = new EnvConfiguration();
Thread.currentThread().setContextClassLoader(wac.getClassLoader());
MetaData metadata = new MetaData();
envConfig.preConfigure(wac);
envConfig.configure(wac);
envConfig.bindEnvEntries(wac);
String val = (String) ic.lookup("java:comp/env/xxx/a");
//webapp naming overrides server
assertEquals("900", val);
val = (String) ic.lookup("java:comp/env/yyy/b");
//webapp overrides server
assertEquals("910", val);
val = (String) ic.lookup("java:comp/env/zzz/c");
//webapp overrides server
assertEquals("920", val);
val = (String) ic.lookup("java:comp/env/zzz/d");
//from server naming
assertEquals("400", val);
val = (String) ic.lookup("java:comp/env/zzz/e");
//from webapp naming
assertEquals("930", val);
NamingEntry ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/xxx/a");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/yyy/b");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/c");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/d");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/e");
assertNotNull(ne);
plusProcessor.bindEnvEntry("foo", "99");
assertEquals("99", ic.lookup("java:comp/env/foo"));
plusProcessor.bindEnvEntry("xxx/a", "7");
//webapp overrides web.xml
assertEquals("900", ic.lookup("java:comp/env/xxx/a"));
plusProcessor.bindEnvEntry("yyy/b", "7");
//webapp overrides web.xml
assertEquals("910", ic.lookup("java:comp/env/yyy/b"));
plusProcessor.bindEnvEntry("zzz/c", "7");
//webapp does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/c"));
plusProcessor.bindEnvEntry("zzz/d", "7");
//server does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/d"));
plusProcessor.bindEnvEntry("zzz/e", "7");
//webapp does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/e"));
plusProcessor.bindEnvEntry("zzz/f", "7");
//server overrides web.xml
assertEquals("500", ic.lookup("java:comp/env/zzz/f"));
((Context) ic.lookup("java:comp")).destroySubcontext("env");
ic.destroySubcontext("xxx");
ic.destroySubcontext("yyy");
ic.destroySubcontext("zzz");
} finally {
Thread.currentThread().setContextClassLoader(old_loader);
}
}
use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.
the class EnvConfiguration method deconfigure.
/**
* Remove jndi setup from start
* @throws Exception if unable to deconfigure
*/
@Override
public void deconfigure(WebAppContext context) throws Exception {
//get rid of any bindings for comp/env for webapp
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());
ContextFactory.associateClassLoader(context.getClassLoader());
try {
Context ic = new InitialContext();
Context compCtx = (Context) ic.lookup("java:comp");
compCtx.destroySubcontext("env");
//unbind any NamingEntries that were configured in this webapp's name space
@SuppressWarnings("unchecked") List<Bound> bindings = (List<Bound>) context.getAttribute(JETTY_ENV_BINDINGS);
context.setAttribute(JETTY_ENV_BINDINGS, null);
if (bindings != null) {
Collections.reverse(bindings);
for (Bound b : bindings) b._context.destroySubcontext(b._name);
}
} catch (NameNotFoundException e) {
LOG.warn(e);
} finally {
ContextFactory.disassociateClassLoader();
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
use of org.eclipse.jetty.webapp.WebAppContext in project jfinal by jfinal.
the class JettyServerForIDEA method doStart.
private void doStart() {
if (!available(port)) {
throw new IllegalStateException("port: " + port + " already in use!");
}
deleteSessionData();
System.out.println("Starting JFinal " + Const.JFINAL_VERSION);
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
server.addConnector(connector);
webApp = new WebAppContext();
// 在启动过程中允许抛出异常终止启动并退出 JVM
webApp.setThrowUnavailableOnStartupException(true);
webApp.setContextPath(context);
// webApp.setWar(webAppDir);
webApp.setResourceBase(webAppDir);
webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
// webApp.setInitParams(Collections.singletonMap("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false"));
webApp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
persistSession(webApp);
server.setHandler(webApp);
try {
System.out.println("Starting web server on port: " + port);
server.start();
System.out.println("Starting Complete. Welcome To The JFinal World :)");
server.join();
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
System.exit(100);
}
return;
}
Aggregations