Last active
August 29, 2015 14:01
-
-
Save joakime/a3ca2b3abd27f50b6d18 to your computer and use it in GitHub Desktop.
Jetty 9.x Embedded Example: loading a WebApp from a context.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> | |
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> | |
<Set name="contextPath">/r</Set> | |
<Set name="war"><Property name="wars.base" default="test-wars"/>/webapp-b.war</Set> | |
</Configure> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jetty; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.handler.DefaultHandler; | |
import org.eclipse.jetty.server.handler.HandlerList; | |
import org.eclipse.jetty.util.resource.Resource; | |
import org.eclipse.jetty.webapp.WebAppContext; | |
import org.eclipse.jetty.xml.XmlConfiguration; | |
public class WebAppFromXML | |
{ | |
public static void main(String[] args) | |
{ | |
Server server = new Server(7080); | |
HandlerList handlers = new HandlerList(); | |
server.setHandler(handlers); | |
// Define some common environmental properties for XMLConfiguration to use | |
File warsBase = new File("src/test/wars"); | |
Map<String, String> props = new HashMap<>(); | |
props.put("wars.base",warsBase.getAbsolutePath()); | |
try | |
{ | |
// Load specific webapps | |
handlers.addHandler(loadWebAppContext(new File(warsBase,"b.xml"),props)); | |
// lastly, add DefaultHandler for error handling | |
handlers.addHandler(new DefaultHandler()); | |
server.start(); | |
server.dumpStdErr(); | |
server.join(); | |
} | |
catch (Throwable t) | |
{ | |
t.printStackTrace(System.err); | |
} | |
} | |
private static WebAppContext loadWebAppContext(File contextXmlFile, Map<String, String> props) throws Exception | |
{ | |
try | |
{ | |
Resource contextXml = Resource.newResource(contextXmlFile.toURI()); | |
try (InputStream in = contextXml.getInputStream()) | |
{ | |
XmlConfiguration configuration = new XmlConfiguration(in); | |
configuration.getProperties().putAll(props); | |
return (WebAppContext)configuration.configure(); | |
} | |
} | |
catch (Exception e) | |
{ | |
System.err.printf("Unable to load WebAppContext from: %s%n",contextXmlFile); | |
throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment