package net.whydah.admin; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.webapp.WebAppContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.bridge.SLF4JBridgeHandler; import java.net.URL; import java.util.logging.Level; import java.util.logging.LogManager; /** * @author <a href="bard.lind@gmail.com">Bard Lind</a> */ public class MainWithJetty { public static final int DEFAULT_PORT_NO = 9992; public static final String CONTEXT_PATH = "/useradminservice"; private static final Logger log = LoggerFactory.getLogger(MainWithJetty.class); private Server server; private String resourceBase; private final int jettyPort; public static void main(String[] arguments) throws Exception { // http://stackoverflow.com/questions/9117030/jul-to-slf4j-bridge // Jersey uses java.util.logging - bridge to slf4 LogManager.getLogManager().reset(); SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); LogManager.getLogManager().getLogger("").setLevel(Level.FINEST); MainWithJetty main = new MainWithJetty(DEFAULT_PORT_NO); main.start(); main.join(); } /** * http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Setting_a_ServletContext */ public MainWithJetty(int jettyPort) { this.jettyPort = jettyPort; server = new Server(jettyPort); URL url = ClassLoader.getSystemResource("webapp/WEB-INF/web.xml"); resourceBase = url.toExternalForm().replace("/WEB-INF/web.xml", ""); } public void start() throws Exception { WebAppContext context = new WebAppContext(); log.debug("Start Jetty using resourcebase={}", resourceBase); context.setDescriptor(resourceBase + "/WEB-INF/web.xml"); context.setResourceBase(resourceBase); context.setContextPath(CONTEXT_PATH); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); int localPort = getPortNumber(); log.info("Jetty server started on port {}, context path {}", localPort, CONTEXT_PATH); } public void stop() throws Exception { server.stop(); } public void join() throws InterruptedException { server.join(); } //TODO public String getBasePath() { String path = "http://localhost:" + jettyPort + CONTEXT_PATH; return path; } public int getPortNumber() { return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); } public void setResourceBase(String resourceBase) { this.resourceBase = resourceBase; } public String getResourceBase() { return resourceBase; } }