use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.
the class XmlConfiguredJetty method load.
public void load() throws Exception {
XmlConfiguration last = null;
Object[] obj = new Object[this._xmlConfigurations.size()];
// Configure everything
for (int i = 0; i < this._xmlConfigurations.size(); i++) {
URL configURL = this._xmlConfigurations.get(i);
XmlConfiguration configuration = new XmlConfiguration(configURL);
if (last != null)
configuration.getIdMap().putAll(last.getIdMap());
configuration.getProperties().putAll(_properties);
obj[i] = configuration.configure();
last = configuration;
}
// Test for Server Instance.
Server foundServer = null;
int serverCount = 0;
for (int i = 0; i < this._xmlConfigurations.size(); i++) {
if (obj[i] instanceof Server) {
if (obj[i].equals(foundServer)) {
// Identical server instance found
break;
}
foundServer = (Server) obj[i];
serverCount++;
}
}
if (serverCount <= 0) {
throw new Exception("Load failed to configure a " + Server.class.getName());
}
Assert.assertEquals("Server load count", 1, serverCount);
this._server = foundServer;
this._server.setStopTimeout(10);
}
use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.
the class EnvConfiguration method configure.
@Override
public void configure(WebAppContext context) throws Exception {
if (LOG.isDebugEnabled())
LOG.debug("Created java:comp/env for webapp " + context.getContextPath());
//look in WEB-INF/jetty-env.xml
if (jettyEnvXmlUrl == null) {
//look for a file called WEB-INF/jetty-env.xml
//and process it if it exists
org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
if (web_inf != null && web_inf.isDirectory()) {
org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
if (jettyEnv.exists()) {
jettyEnvXmlUrl = jettyEnv.getURL();
}
}
}
if (jettyEnvXmlUrl != null) {
synchronized (localContextRoot.getRoot()) {
// create list and listener to remember the bindings we make.
final List<Bound> bindings = new ArrayList<Bound>();
NamingContext.Listener listener = new NamingContext.Listener() {
public void unbind(NamingContext ctx, Binding binding) {
}
public Binding bind(NamingContext ctx, Binding binding) {
bindings.add(new Bound(ctx, binding.getName()));
return binding;
}
};
try {
localContextRoot.getRoot().addListener(listener);
XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
WebAppClassLoader.runWithServerClassAccess(() -> {
configuration.configure(context);
return null;
});
} finally {
localContextRoot.getRoot().removeListener(listener);
context.setAttribute(JETTY_ENV_BINDINGS, bindings);
}
}
}
//add java:comp/env entries for any EnvEntries that have been defined so far
bindEnvEntries(context);
}
use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.
the class OverlayedAppProvider method createTemplateContext.
/* ------------------------------------------------------------ */
private TemplateContext createTemplateContext(final String key, Webapp webapp, Template template, Node node, ClassLoader parent) throws Exception {
__log.info("created {}", key);
// look for libs
// If we have libs directories, create classloader and make it available to
// the XMLconfiguration
List<URL> libs = new ArrayList<URL>();
for (Resource lib : getLayeredResources(LIB, node, template)) {
for (String jar : lib.list()) {
if (!jar.toLowerCase(Locale.ENGLISH).endsWith(".jar"))
continue;
libs.add(lib.addPath(jar).getURL());
}
}
final ClassLoader libLoader;
if (libs.size() > 0) {
__log.debug("{}: libs={}", key, libs);
libLoader = new URLClassLoader(libs.toArray(new URL[] {}), parent) {
public String toString() {
return "libLoader@" + Long.toHexString(hashCode()) + "-lib-" + key;
}
};
} else
libLoader = parent;
Thread.currentThread().setContextClassLoader(libLoader);
// Make the shared resourceBase
List<Resource> bases = new ArrayList<Resource>();
for (Resource wa : getLayers(node, template)) bases.add(wa);
if (webapp != null)
bases.add(webapp.getBaseResource());
Resource baseResource = bases.size() == 1 ? bases.get(0) : new ResourceCollection(bases.toArray(new Resource[bases.size()]));
__log.debug("{}: baseResource={}", key, baseResource);
// Make the shared context
TemplateContext shared = new TemplateContext(key, getDeploymentManager().getServer(), baseResource, libLoader);
_shared.put(key, shared);
// Create properties to be shared by overlay.xmls
Map<String, Object> idMap = new HashMap<String, Object>();
idMap.put(_serverID, getDeploymentManager().getServer());
// otherwise create an instance ourselves
for (Resource template_xml : getLayeredResources(TEMPLATE_XML, template, node)) {
__log.debug("{}: template.xml={}", key, template_xml);
XmlConfiguration xmlc = newXmlConfiguration(template_xml.getURL(), idMap, template, null);
xmlc.getIdMap().putAll(idMap);
xmlc.configure(shared);
idMap = xmlc.getIdMap();
}
shared.setIdMap(idMap);
shared.start();
return shared;
}
use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.
the class WebAppProvider method createContextHandler.
/* ------------------------------------------------------------ */
@Override
public ContextHandler createContextHandler(final App app) throws Exception {
Resource resource = Resource.newResource(app.getOriginId());
File file = resource.getFile();
if (!resource.exists())
throw new IllegalStateException("App resource does not exist " + resource);
String context = file.getName();
if (resource.exists() && FileID.isXmlFile(file)) {
XmlConfiguration xmlc = new XmlConfiguration(resource.getURI().toURL()) {
@Override
public void initializeDefaults(Object context) {
super.initializeDefaults(context);
if (context instanceof WebAppContext) {
WebAppContext webapp = (WebAppContext) context;
initializeWebAppContextDefaults(webapp);
}
}
};
xmlc.getIdMap().put("Server", getDeploymentManager().getServer());
xmlc.getProperties().put("jetty.home", System.getProperty("jetty.home", "."));
xmlc.getProperties().put("jetty.base", System.getProperty("jetty.base", "."));
xmlc.getProperties().put("jetty.webapp", file.getCanonicalPath());
xmlc.getProperties().put("jetty.webapps", file.getParentFile().getCanonicalPath());
if (getConfigurationManager() != null)
xmlc.getProperties().putAll(getConfigurationManager().getProperties());
return (ContextHandler) xmlc.configure();
} else if (file.isDirectory()) {
// must be a directory
} else if (FileID.isWebArchiveFile(file)) {
// Context Path is the same as the archive.
context = context.substring(0, context.length() - 4);
} else {
throw new IllegalStateException("unable to create ContextHandler for " + app);
}
// Ensure "/" is Not Trailing in context paths.
if (context.endsWith("/") && context.length() > 0) {
context = context.substring(0, context.length() - 1);
}
// Start building the webapplication
WebAppContext webAppContext = new WebAppContext();
webAppContext.setDisplayName(context);
// special case of archive (or dir) named "root" is / context
if (context.equalsIgnoreCase("root")) {
context = URIUtil.SLASH;
} else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-")) {
int dash = context.toLowerCase(Locale.ENGLISH).indexOf('-');
String virtual = context.substring(dash + 1);
webAppContext.setVirtualHosts(new String[] { virtual });
context = URIUtil.SLASH;
}
// Ensure "/" is Prepended to all context paths.
if (context.charAt(0) != '/') {
context = "/" + context;
}
webAppContext.setContextPath(context);
webAppContext.setWar(file.getAbsolutePath());
initializeWebAppContextDefaults(webAppContext);
return webAppContext;
}
use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.
the class PreconfigureQuickStartWar method preconfigure.
/**
* @param war The war (or directory) to preconfigure
* @param dir The directory to expand the war into (or null if war is a directory)
* @param xml A context XML to apply (or null if none)
* @throws Exception if unable to pre configure
*/
public static void preconfigure(Resource war, Resource dir, Resource xml) throws Exception {
// Do we need to unpack a war?
if (war != null) {
if (war.isDirectory())
error("war file is directory");
if (!dir.exists())
dir.getFile().mkdirs();
JarResource.newJarResource(war).copyTo(dir.getFile());
}
final Server server = new Server();
QuickStartWebApp webapp = new QuickStartWebApp();
if (xml != null) {
if (xml.isDirectory() || !xml.toString().toLowerCase(Locale.ENGLISH).endsWith(".xml"))
error("Bad context.xml: " + xml);
XmlConfiguration xmlConfiguration = new XmlConfiguration(xml.getURL());
xmlConfiguration.configure(webapp);
}
webapp.setResourceBase(dir.getFile().getAbsolutePath());
webapp.setPreconfigure(true);
server.setHandler(webapp);
server.start();
server.stop();
}
Aggregations