You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2014/09/01 22:46:20 UTC

git commit: TAP5-2174: Add a CLI to Jetty7Runner, to make it easier to script

Repository: tapestry-5
Updated Branches:
  refs/heads/master 4b0c857b7 -> 72bb4e217


TAP5-2174: Add a CLI to Jetty7Runner, to make it easier to script


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/72bb4e21
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/72bb4e21
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/72bb4e21

Branch: refs/heads/master
Commit: 72bb4e217b0693a897ba5b788c6953b674555ab3
Parents: 4b0c857
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Mon Sep 1 13:46:26 2014 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Mon Sep 1 13:46:26 2014 -0700

----------------------------------------------------------------------
 tapestry-runner/build.gradle                    |   1 +
 .../org/apache/tapestry5/test/Jetty7Runner.java | 109 ++++++++++++++++++-
 2 files changed, 104 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/72bb4e21/tapestry-runner/build.gradle
----------------------------------------------------------------------
diff --git a/tapestry-runner/build.gradle b/tapestry-runner/build.gradle
index c719271..009c9f0 100644
--- a/tapestry-runner/build.gradle
+++ b/tapestry-runner/build.gradle
@@ -12,4 +12,5 @@ dependencies {
     compile "org.apache.tomcat:coyote:${versions.tomcat}"
 
     compile "org.apache.tomcat:dbcp:${versions.tomcat}"
+    compile "commons-cli:commons-cli:1.2"
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/72bb4e21/tapestry-runner/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
----------------------------------------------------------------------
diff --git a/tapestry-runner/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java b/tapestry-runner/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
index 4138528..00bcba9 100644
--- a/tapestry-runner/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
+++ b/tapestry-runner/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.test;
 
+import org.apache.commons.cli.*;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
 import org.eclipse.jetty.webapp.WebAppContext;
@@ -33,11 +34,13 @@ public class Jetty7Runner implements ServletContainerRunner
 
     private int sslPort;
 
-    public Jetty7Runner() {
+    public Jetty7Runner()
+    {
         // un-configured runner
     }
 
-    public Jetty7Runner(String webappFolder, String contextPath, int port, int sslPort) throws Exception {
+    public Jetty7Runner(String webappFolder, String contextPath, int port, int sslPort) throws Exception
+    {
         configure(webappFolder, contextPath, port, sslPort).start();
     }
 
@@ -79,11 +82,14 @@ public class Jetty7Runner implements ServletContainerRunner
         return this;
     }
 
-    public void start() throws Exception {
+    public void start() throws Exception
+    {
         jettyServer.start();
     }
 
-    /** Immediately shuts down the server instance. */
+    /**
+     * Immediately shuts down the server instance.
+     */
     @Override
     public void stop()
     {
@@ -93,8 +99,7 @@ public class Jetty7Runner implements ServletContainerRunner
         {
             // Stop immediately and not gracefully.
             jettyServer.stop();
-        }
-        catch (Exception ex)
+        } catch (Exception ex)
         {
             throw new RuntimeException("Error stopping Jetty instance: " + ex.toString(), ex);
         }
@@ -132,4 +137,96 @@ public class Jetty7Runner implements ServletContainerRunner
 
         return new File(TapestryRunnerConstants.MODULE_BASE_DIR, moduleLocalPath).getPath();
     }
+
+    /**
+     * Main entrypoint used to run the Jetty7 instance from the command line.
+     *
+     * @since 5.4
+     */
+    public static void main(String[] args) throws Exception
+    {
+        String commandName = Jetty7Runner.class.getName();
+
+        Options options = new Options();
+
+        String webapp = "src/main/webapp";
+        String context = "/";
+        int httpPort = 8080;
+        int sslPort = 8443;
+
+        options.addOption(OptionBuilder.withLongOpt("directory")
+                .withDescription("Root context directory (defaults to 'src/main/webapp')")
+                .hasArg().withArgName("DIR")
+                .create('d'))
+                .addOption(OptionBuilder.withLongOpt("context")
+                        .withDescription("Context path for application (defaults to '/')")
+                        .hasArg().withArgName("CONTEXT")
+                        .create('c'))
+                .addOption(OptionBuilder.withLongOpt("port")
+                        .withDescription("HTTP port (defaults to 8080)")
+                        .hasArg().withArgName("PORT")
+                        .create('p'))
+                .addOption(OptionBuilder.withLongOpt("secure-port")
+                        .withDescription("HTTPS port (defaults to 8443)")
+                        .hasArg().withArgName("PORT")
+                        .create('s'))
+                .addOption("h", "help", false, "Display command usage");
+
+
+        CommandLine line = new BasicParser().parse(options, args);
+
+        boolean usage = line.hasOption('h');
+
+        if (!usage)
+        {
+            if (line.hasOption('d'))
+            {
+                webapp = line.getOptionValue('d');
+            }
+
+            File folder = new File(webapp);
+
+            if (!folder.exists())
+            {
+                System.err.printf("%s: Directory `%s' does not exist.%n", commandName, webapp);
+                System.exit(-1);
+            }
+
+            if (line.hasOption('p'))
+            {
+                try
+                {
+                    httpPort = Integer.parseInt(line.getOptionValue('p'));
+                } catch (NumberFormatException e)
+                {
+                    usage = true;
+                }
+            }
+
+            if (line.hasOption('s'))
+            {
+                try
+                {
+                    sslPort = Integer.parseInt(line.getOptionValue('s'));
+                } catch (NumberFormatException e)
+                {
+                    usage = true;
+                }
+            }
+
+            if (line.hasOption('c'))
+            {
+                context = line.getOptionValue('c');
+            }
+
+        }
+
+        if (usage)
+        {
+            new HelpFormatter().printHelp(commandName, options);
+            System.exit(-1);
+        }
+
+        new Jetty7Runner(webapp, context, httpPort, sslPort);
+    }
 }