r262 - trunk/nuiton-web/src/main/java/org/nuiton/web/war
Author: tchemit Date: 2013-04-29 20:51:54 +0200 (Mon, 29 Apr 2013) New Revision: 262 Url: http://nuiton.org/projects/nuiton-web/repository/revisions/262 Log: fixes #2675: Customize JettyLauncher port and contextPath Modified: trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java Modified: trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java =================================================================== --- trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java 2013-04-29 13:05:12 UTC (rev 261) +++ trunk/nuiton-web/src/main/java/org/nuiton/web/war/JettyLauncher.java 2013-04-29 18:51:54 UTC (rev 262) @@ -57,12 +57,11 @@ /** * War main class launcher (jetty based). - * + * <p/> * To use it : - * java -jar app-xxx.war - * + * java -jar app-xxx.war + * * @author chatellier - * @version $Id$ * @since 1.0 */ public class JettyLauncher implements ActionListener, MouseListener { @@ -76,26 +75,34 @@ /** Default port. */ protected int port = 8888; + /** + * Default context path. + * + * @since 1.13 + */ + protected String contextPath = "/"; + /** Server name. */ protected String serverName; - + /** * Main method (used by war in manifest). - * + * * @param args args * @throws Exception */ - public static void main(String[] args) throws Exception { + public static void main(String... args) throws Exception { JettyLauncher launcher = new JettyLauncher(); + launcher.configure(args); launcher.readInformation(); - launcher.startServer(args); + launcher.startServer(); launcher.installSystemTray(); launcher.openBrowser(); } /** * Parse WEB-INF/web.xml file and get server display name. - * + * * @since 1.1.3 */ protected void readInformation() { @@ -120,9 +127,9 @@ /** * Read input stream as string. - * + * <p/> * Code from commons io. - * + * * @param stream stream to read * @return content as string * @since 1.1.3 @@ -142,13 +149,26 @@ return sw.toString(); } + protected void configure(String... args) throws Exception { + + for (int i = 0; i < args.length; i++) { + if ("--port".equals(args[i])) + port = Integer.parseInt(args[++i]); + else if ("--path".equals(args[i])) { + contextPath = args[++i]; + if (!contextPath.startsWith("/")) { + contextPath = "/" + contextPath; + } + } + } + } + /** * Launch servlet container. - * - * @param args args + * * @throws Exception */ - protected void startServer(String[] args) throws Exception { + protected void startServer() throws Exception { System.out.println("Starting server embedded mode..."); String fqnLauncherFile = JettyLauncher.class.getName().replaceAll("\\.", "/") + ".class"; @@ -168,10 +188,10 @@ jettyServer.setConnectors(new Connector[]{connector}); WebAppContext webappcontext = new WebAppContext(); - webappcontext.setContextPath("/"); + webappcontext.setContextPath(contextPath); webappcontext.setWar(me.getAbsolutePath()); - HandlerCollection handlers= new HandlerCollection(); + HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[]{webappcontext, new DefaultHandler()}); jettyServer.setHandler(handlers); @@ -179,15 +199,13 @@ // build server uri try { - serverUri = new URI("http://localhost:" + port); + serverUri = new URI("http://localhost:" + port + contextPath); } catch (URISyntaxException e) { e.printStackTrace(); } } - /** - * Shutdown server. - */ + /** Shutdown server. */ protected void stopServer() { if (jettyServer != null) { try { @@ -199,9 +217,7 @@ } } - /** - * Install system tray to stop server. - */ + /** Install system tray to stop server. */ protected void installSystemTray() { if (SystemTray.isSupported()) { // build menu @@ -251,8 +267,8 @@ /** * Open browser. - * - * @throws IOException + * + * @throws IOException */ protected void openBrowser() { if (Desktop.isDesktopSupported() && serverUri != null) {
participants (1)
-
tchemit@users.nuiton.org