Search in sources :

Example 31 with XmlConfiguration

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);
}
Also used : Server(org.eclipse.jetty.server.Server) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 32 with XmlConfiguration

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);
}
Also used : Binding(javax.naming.Binding) ArrayList(java.util.ArrayList) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) NamingContext(org.eclipse.jetty.jndi.NamingContext)

Example 33 with XmlConfiguration

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;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 34 with XmlConfiguration

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;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Resource(org.eclipse.jetty.util.resource.Resource) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

Example 35 with XmlConfiguration

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();
}
Also used : Server(org.eclipse.jetty.server.Server) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration)

Aggregations

XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)43 Server (org.eclipse.jetty.server.Server)24 Resource (org.eclipse.jetty.util.resource.Resource)18 URL (java.net.URL)12 File (java.io.File)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 Handler (org.eclipse.jetty.server.Handler)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 FileInputStream (java.io.FileInputStream)4 URLClassLoader (java.net.URLClassLoader)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 JettyWebXmlConfiguration (org.eclipse.jetty.webapp.JettyWebXmlConfiguration)4 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)4 FileNotFoundException (java.io.FileNotFoundException)3 HttpURLConnection (java.net.HttpURLConnection)3 MalformedURLException (java.net.MalformedURLException)3