Search in sources :

Example 1 with XmlConfiguration

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

the class AntWebAppContext method doStart.

/**
     * 
     */
public void doStart() {
    try {
        TaskLog.logWithTimestamp("Starting web application " + this.getDescriptor());
        if (jettyEnvXml != null && jettyEnvXml.exists())
            envConfiguration.setJettyEnvXml(Resource.toURL(jettyEnvXml));
        ClassLoader parentLoader = this.getClass().getClassLoader();
        if (parentLoader instanceof AntClassLoader)
            parentLoader = new AntURLClassLoader((AntClassLoader) parentLoader);
        setClassLoader(new WebAppClassLoader(parentLoader, this));
        if (attributes != null && attributes.getAttributes() != null) {
            for (Attribute a : attributes.getAttributes()) setAttribute(a.getName(), a.getValue());
        }
        //apply a context xml file if one was supplied
        if (contextXml != null) {
            XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(contextXml));
            TaskLog.log("Applying context xml file " + contextXml);
            xmlConfiguration.configure(this);
        }
        super.doStart();
    } catch (Exception e) {
        TaskLog.log(e.toString());
    }
}
Also used : Attribute(org.eclipse.jetty.ant.types.Attribute) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) WebXmlConfiguration(org.eclipse.jetty.webapp.WebXmlConfiguration) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 2 with XmlConfiguration

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

the class QuickStartTest method testStandardTestWar.

@Test
public void testStandardTestWar() throws Exception {
    PreconfigureStandardTestWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-standard-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-standard-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test.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() + "/test/dump/info");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    assertThat(IO.toString((InputStream) connection.getContent()), Matchers.containsString("Dump Servlet"));
    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 3 with XmlConfiguration

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

the class Quickstart method main.

public static void main(String... args) throws Exception {
    if (args.length < 1)
        error("No WAR file or directory given");
    //war file or dir to start
    String war = args[0];
    //optional jetty context xml file to configure the webapp
    Resource contextXml = null;
    if (args.length > 1)
        contextXml = Resource.newResource(args[1]);
    Server server = new Server(8080);
    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();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration)

Example 4 with XmlConfiguration

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

the class ServerSupport method applyXmlConfigurations.

/**
     * Apply xml files to server startup, passing in ourselves as the 
     * "Server" instance.
     * 
     * @param server the server to apply the xml to
     * @param files the list of xml files
     * @return the Server implementation, after the xml is applied
     * @throws Exception if unable to apply the xml configuration
     */
public static Server applyXmlConfigurations(Server server, List<File> files) throws Exception {
    if (files == null || files.isEmpty())
        return server;
    Map<String, Object> lastMap = new HashMap<String, Object>();
    if (server != null)
        lastMap.put("Server", server);
    for (File xmlFile : files) {
        if (PluginLog.getLog() != null)
            PluginLog.getLog().info("Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath());
        XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
        //chain ids from one config file to another
        if (lastMap != null)
            xmlConfiguration.getIdMap().putAll(lastMap);
        //Set the system properties each time in case the config file set a new one
        Enumeration<?> ensysprop = System.getProperties().propertyNames();
        while (ensysprop.hasMoreElements()) {
            String name = (String) ensysprop.nextElement();
            xmlConfiguration.getProperties().put(name, System.getProperty(name));
        }
        xmlConfiguration.configure();
        lastMap = xmlConfiguration.getIdMap();
    }
    return (Server) lastMap.get("Server");
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

Example 5 with XmlConfiguration

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

the class AbstractJettyMojo method configureWebApplication.

/**
     * Subclasses should invoke this to setup basic info
     * on the webapp
     * 
     * @throws Exception if unable to configure web application 
     */
public void configureWebApplication() throws Exception {
    //As of jetty-7, you must use a <webApp> element
    if (webApp == null)
        webApp = new JettyWebAppContext();
    //context xml file can OVERRIDE those settings
    if (contextXml != null) {
        File file = FileUtils.getFile(contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(file));
        getLog().info("Applying context xml file " + contextXml);
        xmlConfiguration.configure(webApp);
    }
    //If no contextPath was specified, go with default of project artifactid
    String cp = webApp.getContextPath();
    if (cp == null || "".equals(cp)) {
        cp = "/" + project.getArtifactId();
        webApp.setContextPath(cp);
    }
    //If no tmp directory was specified, and we have one, use it
    if (webApp.getTempDirectory() == null) {
        File target = new File(project.getBuild().getDirectory());
        File tmp = new File(target, "tmp");
        if (!tmp.exists())
            tmp.mkdirs();
        webApp.setTempDirectory(tmp);
    }
    getLog().info("Context path = " + webApp.getContextPath());
    getLog().info("Tmp directory = " + (webApp.getTempDirectory() == null ? " determined at runtime" : webApp.getTempDirectory()));
    getLog().info("Web defaults = " + (webApp.getDefaultsDescriptor() == null ? " jetty default" : webApp.getDefaultsDescriptor()));
    getLog().info("Web overrides = " + (webApp.getOverrideDescriptor() == null ? " none" : webApp.getOverrideDescriptor()));
}
Also used : XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

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