Author: rmannibucau
Date: Thu Mar 20 18:41:39 2014
New Revision: 1579708

URL: http://svn.apache.org/r1579708
Log:
TOMEE-1144 better handling of process + handling CATALINA_OPTS

Modified:
    
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
    
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
    
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java

Modified: 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java?rev=1579708&r1=1579707&r2=1579708&view=diff
==============================================================================
--- 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
 (original)
+++ 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
 Thu Mar 20 18:41:39 2014
@@ -704,6 +704,74 @@ public abstract class AbstractTomEEMojo 
             classpaths = new ArrayList<String>();
         }
 
+        final List<String> strings = generateJVMArgs();
+
+        // init env for RemoteServer
+        System.setProperty("openejb.home", catalinaBase.getAbsolutePath());
+        if (debug) {
+            System.setProperty("openejb.server.debug", "true");
+            System.setProperty("server.debug.port", 
Integer.toString(debugPort));
+        }
+        System.setProperty("server.shutdown.port", 
Integer.toString(tomeeShutdownPort));
+        System.setProperty("server.shutdown.command", tomeeShutdownCommand);
+
+        server = new RemoteServer(getConnectAttempts(), false);
+        server.setAdditionalClasspath(getAdditionalClasspath());
+
+        addShutdownHooks(server); // some shutdown hooks are always added (see 
UpdatableTomEEMojo)
+
+        if ("TomEE".equals(container)) {
+            getLog().info("Running '" + 
getClass().getSimpleName().replace("TomEEMojo", "").toLowerCase(Locale.ENGLISH)
+                + "'. Configured TomEE in plugin is " + tomeeHost + ":" + 
tomeeHttpPort
+                + " (plugin shutdown port is " + tomeeShutdownPort + ")");
+        } else {
+            getLog().info("Running '" + 
getClass().getSimpleName().replace("TomEEMojo", 
"").toLowerCase(Locale.ENGLISH));
+        }
+
+        final InputStream originalIn = System.in; // piped when starting 
resmote server so saving it
+
+        serverCmd(server, strings);
+
+        if (getWaitTomEE()) {
+            final CountDownLatch stopCondition = new CountDownLatch(1);
+            Runtime.getRuntime().addShutdownHook(new Thread() {
+                @Override
+                public void run() {
+                    stopServer(stopCondition);
+                }
+            });
+
+            if (useConsole) {
+                final Scanner reader = new Scanner(originalIn);
+
+                System.out.flush();
+                getLog().info("Waiting for command: " + availableCommands());
+
+                String line;
+                while ((line = reader.nextLine()) != null) {
+                    if (QUIT_CMD.equalsIgnoreCase(line) || 
EXIT_CMD.equalsIgnoreCase(line)) {
+                        break;
+                    }
+
+                    if (!handleLine(line.trim())) {
+                        System.out.flush();
+                        getLog().warn("Command '" + line + "' not understood. 
Use one of " + availableCommands());
+                    }
+                }
+
+                reader.close();
+                stopServer(stopCondition); // better than using shutdown hook 
since it doesn't rely on the hook which are not sent by eclipse for instance
+            }
+
+            try {
+                stopCondition.await();
+            } catch (InterruptedException e) {
+                // no-op
+            }
+        }
+    }
+
+    protected List<String> generateJVMArgs() {
         final String deployOpenEjbAppKey = "openejb.system.apps";
         final String servletCompliance = 
"org.apache.catalina.STRICT_SERVLET_COMPLIANCE";
 
@@ -781,73 +849,11 @@ public abstract class AbstractTomEEMojo 
             strings.add("-Dtomee.force-reloadable=true");
         }
 
-        // init env for RemoteServer
-        System.setProperty("openejb.home", catalinaBase.getAbsolutePath());
-        if (debug) {
-            System.setProperty("openejb.server.debug", "true");
-            System.setProperty("server.debug.port", 
Integer.toString(debugPort));
-        }
-        System.setProperty("server.shutdown.port", 
Integer.toString(tomeeShutdownPort));
-        System.setProperty("server.shutdown.command", tomeeShutdownCommand);
-
-        server = new RemoteServer(getConnectAttempts(), false);
-        server.setAdditionalClasspath(getAdditionalClasspath());
-
-        addShutdownHooks(server); // some shutdown hooks are always added (see 
UpdatableTomEEMojo)
-
         if (!getWaitTomEE()) {
             strings.add("-Dtomee.noshutdownhook=true");
         }
 
-        if ("TomEE".equals(container)) {
-            getLog().info("Running '" + 
getClass().getSimpleName().replace("TomEEMojo", "").toLowerCase(Locale.ENGLISH)
-                + "'. Configured TomEE in plugin is " + tomeeHost + ":" + 
tomeeHttpPort
-                + " (plugin shutdown port is " + tomeeShutdownPort + ")");
-        } else {
-            getLog().info("Running '" + 
getClass().getSimpleName().replace("TomEEMojo", 
"").toLowerCase(Locale.ENGLISH));
-        }
-
-        final InputStream originalIn = System.in; // piped when starting 
resmote server so saving it
-
-        serverCmd(server, strings);
-
-        if (getWaitTomEE()) {
-            final CountDownLatch stopCondition = new CountDownLatch(1);
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-                @Override
-                public void run() {
-                    stopServer(stopCondition);
-                }
-            });
-
-            if (useConsole) {
-                final Scanner reader = new Scanner(originalIn);
-
-                System.out.flush();
-                getLog().info("Waiting for command: " + availableCommands());
-
-                String line;
-                while ((line = reader.nextLine()) != null) {
-                    if (QUIT_CMD.equalsIgnoreCase(line) || 
EXIT_CMD.equalsIgnoreCase(line)) {
-                        break;
-                    }
-
-                    if (!handleLine(line.trim())) {
-                        System.out.flush();
-                        getLog().warn("Command '" + line + "' not understood. 
Use one of " + availableCommands());
-                    }
-                }
-
-                reader.close();
-                stopServer(stopCondition); // better than using shutdown hook 
since it doesn't rely on the hook which are not sent by eclipse for instance
-            }
-
-            try {
-                stopCondition.await();
-            } catch (InterruptedException e) {
-                // no-op
-            }
-        }
+        return strings;
     }
 
     protected Collection<String> availableCommands() {

Modified: 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java?rev=1579708&r1=1579707&r2=1579708&view=diff
==============================================================================
--- 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
 (original)
+++ 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
 Thu Mar 20 18:41:39 2014
@@ -35,6 +35,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.StringWriter;
+import java.util.List;
 import java.util.Properties;
 
 import static org.apache.openejb.loader.Files.mkdirs;
@@ -87,6 +88,7 @@ public class ExecMojo extends BuildTomEE
         config.put("distribution", distributionName);
         config.put("workingDir", runtimeWorkingDir);
         config.put("command", script);
+        config.put("catalinaOpts", toString(generateJVMArgs()));
 
         // create an executable jar with main runner and zipFile
         final FileOutputStream fileOutputStream = new 
FileOutputStream(execFile);
@@ -137,4 +139,12 @@ public class ExecMojo extends BuildTomEE
         IOUtil.close(os);
         IOUtil.close(fileOutputStream);
     }
+
+    private static String toString(final List<String> strings) {
+        final StringBuilder builder = new StringBuilder();
+        for (final String s : strings) {
+            builder.append(s).append(" ");
+        }
+        return builder.toString();
+    }
 }

Modified: 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java?rev=1579708&r1=1579707&r2=1579708&view=diff
==============================================================================
--- 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
 (original)
+++ 
tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
 Thu Mar 20 18:41:39 2014
@@ -20,8 +20,6 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Locale;
@@ -31,28 +29,7 @@ import java.util.zip.ZipInputStream;
 
 import static java.util.Arrays.asList;
 
-public class ExecRunner implements Runnable {
-    private final InputStream in;
-    private final OutputStream out;
-
-    private ExecRunner(final InputStream in, final OutputStream out) {
-        this.in = in;
-        this.out = out;
-    }
-
-    @Override
-    public void run() {
-        try {
-            int i;
-            final byte[] buf = new byte[1];
-            while ((i = in.read(buf)) != -1) {
-                out.write(buf, 0, i);
-            }
-        } catch (final Exception e) {
-            e.printStackTrace();
-        }
-    }
-
+public class ExecRunner {
     private static final String SH_BAT_AUTO = "[.sh|.bat]";
 
     public static void main(final String[] args) throws Exception {
@@ -102,22 +79,23 @@ public class ExecRunner implements Runna
             params.addAll(asList(args));
         }
 
-        final Process process = Runtime.getRuntime().exec(params.toArray(new 
String[params.size()]), null, distribOutput);
-        pipe("exec-runner-out", process.getInputStream(), System.out);
-        pipe("exec-runner-err", process.getErrorStream(), System.err);
+        final ProcessBuilder builder = new ProcessBuilder(params.toArray(new 
String[params.size()])).directory(distribOutput);
+
+        final String catalinaOpts = config.getProperty("catalinaOpts");
+        if (catalinaOpts != null) { // inherit from existing env
+            builder.environment().put("CATALINA_OPTS", catalinaOpts);
+        }
+
+        builder.redirectError(ProcessBuilder.Redirect.INHERIT);
+        builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
+
+        final Process process = builder.start();
         process.waitFor();
         System.out.flush();
         System.err.flush();
         System.out.println("Exit status: " + process.exitValue());
     }
 
-    private static void pipe(final String name, final InputStream errorStream, 
final PrintStream err) {
-        final Thread thread = new Thread(new ExecRunner(errorStream, err));
-        thread.setName(name);
-        thread.setDaemon(true);
-        thread.start();
-    }
-
     // duplicated to avoid deps, if this class has any dep then it can't be run
     private static void extract(final InputStream distrib, final File output) 
throws IOException {
         mkdirs(output);
@@ -166,4 +144,8 @@ public class ExecRunner implements Runna
             throw new IllegalArgumentException("Can't create " + 
output.getAbsolutePath());
         }
     }
+
+    private ExecRunner() {
+        // no-op
+    }
 }


Reply via email to