Search in sources :

Example 26 with XmlConfiguration

use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.

the class JettyWebXmlConfiguration method configure.

/**
     * Configure
     * Apply web-jetty.xml configuration
     * @see Configuration#configure(WebAppContext)
     */
@Override
public void configure(WebAppContext context) throws Exception {
    //cannot configure if the _context is already started
    if (context.isStarted()) {
        LOG.debug("Cannot configure webapp after it is started");
        return;
    }
    LOG.debug("Configuring web-jetty.xml");
    Resource web_inf = context.getWebInf();
    // handle any WEB-INF descriptors
    if (web_inf != null && web_inf.isDirectory()) {
        // do jetty.xml file
        Resource jetty = web_inf.addPath("jetty8-web.xml");
        if (!jetty.exists())
            jetty = web_inf.addPath(JETTY_WEB_XML);
        if (!jetty.exists())
            jetty = web_inf.addPath("web-jetty.xml");
        if (jetty.exists()) {
            if (LOG.isDebugEnabled())
                LOG.debug("Configure: " + jetty);
            Object xml_attr = context.getAttribute(XML_CONFIGURATION);
            context.removeAttribute(XML_CONFIGURATION);
            final XmlConfiguration jetty_config = xml_attr instanceof XmlConfiguration ? (XmlConfiguration) xml_attr : new XmlConfiguration(jetty.getURI().toURL());
            setupXmlConfiguration(jetty_config, web_inf);
            try {
                WebAppClassLoader.runWithServerClassAccess(() -> {
                    jetty_config.configure(context);
                    return null;
                });
            } catch (Exception e) {
                LOG.warn("Error applying {}", jetty);
                throw e;
            }
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) IOException(java.io.IOException)

Example 27 with XmlConfiguration

use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.

the class Starter method configureJetty.

public void configureJetty() throws Exception {
    LOG.debug("Starting Jetty Server ...");
    Resource.setDefaultUseCaches(false);
    //apply any configs from jetty.xml files first 
    applyJettyXml();
    //ensure there's a connector
    ServerSupport.configureConnectors(server, null);
    //check if contexts already configured, create if not
    ServerSupport.configureHandlers(server, null);
    //Set up list of default Configurations to apply to a webapp
    ServerSupport.configureDefaultConfigurationClasses(server);
    webApp = new JettyWebAppContext();
    //configure webapp from properties file describing unassembled webapp
    configureWebApp();
    //make it a quickstart if the quickstart-web.xml file exists
    if (webApp.getTempDirectory() != null) {
        File qs = new File(webApp.getTempDirectory(), "quickstart-web.xml");
        if (qs.exists() && qs.isFile())
            webApp.setQuickStartWebDescriptor(Resource.newResource(qs));
    }
    //already configured by the time the context xml file is applied.
    if (contextXml != null) {
        XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(contextXml));
        xmlConfiguration.getIdMap().put("Server", server);
        xmlConfiguration.configure(webApp);
    }
    ServerSupport.addWebApplication(server, webApp);
    if (stopPort > 0 && stopKey != null) {
        ShutdownMonitor monitor = ShutdownMonitor.getInstance();
        monitor.setPort(stopPort);
        monitor.setKey(stopKey);
        monitor.setExitVm(true);
    }
}
Also used : ShutdownMonitor(org.eclipse.jetty.server.ShutdownMonitor) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

Example 28 with XmlConfiguration

use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.

the class QuickStartTest method testSpecWar.

@Test
public void testSpecWar() throws Exception {
    PreconfigureSpecWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-spec-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-spec-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test-spec.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    assertThat(IO.toString((InputStream) connection.getContent()), Matchers.containsString("Test Specification WebApp"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 29 with XmlConfiguration

use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.

the class QuickStartTest method testJNDIWar.

@Test
public void testJNDIWar() throws Exception {
    PreconfigureJNDIWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-jndi-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-jndi-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test-jndi.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURI().toURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    String content = IO.toString((InputStream) connection.getContent());
    assertThat(content, Matchers.containsString("JNDI Test WebApp"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 30 with XmlConfiguration

use of org.eclipse.jetty.xml.XmlConfiguration in project jetty.project by eclipse.

the class TestableJettyServer 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);
        System.err.println("configuring: " + configURL);
        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(1000);
}
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)

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