Author: krosenvold
Date: Mon Jan 21 18:53:53 2013
New Revision: 1436551

URL: http://svn.apache.org/viewvc?rev=1436551&view=rev
Log:
[SUREFIRE-946] Allow execution of a callback after the process finished, or the 
timeout ran up.

The callback is invoked, before calling waitForPumpers, allowing clients to 
release possible locks on or in the in/out streams.

Patch by Andreas Gudian, applied unmodified

Modified:
    
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java

Modified: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java?rev=1436551&r1=1436550&r2=1436551&view=diff
==============================================================================
--- 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
 (original)
+++ 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
 Mon Jan 21 18:53:53 2013
@@ -28,6 +28,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
+
 import org.apache.maven.shared.utils.Os;
 import org.apache.maven.shared.utils.StringUtils;
 
@@ -110,8 +111,28 @@ public abstract class CommandLineUtils
                                           StreamConsumer systemErr, int 
timeoutInSeconds )
         throws CommandLineException
     {
+        return executeCommandLine( cl, systemIn, systemOut, systemErr, 
timeoutInSeconds, null );
+    }
+
+    /**
+     * @param cl               The command line to execute
+     * @param systemIn         The input to read from, must be thread safe
+     * @param systemOut        A consumer that receives output, must be thread 
safe
+     * @param systemErr        A consumer that receives system error stream 
output, must be thread safe
+     * @param timeoutInSeconds Positive integer to specify timeout, zero and 
negative integers for no timeout.
+     * @param runAfterProcessTermination Optional callback to run after the 
process terminated or the the timeout was
+     *  exceeded, but before waiting on the stream feeder and pumpers to 
finish.
+     * @return A return value, see {@link Process#exitValue()}
+     * @throws CommandLineException or CommandLineTimeOutException if time out 
occurs
+     * @noinspection ThrowableResultOfMethodCallIgnored
+     */
+    public static int executeCommandLine( Commandline cl, InputStream 
systemIn, StreamConsumer systemOut,
+                                          StreamConsumer systemErr, int 
timeoutInSeconds,
+                                          Runnable runAfterProcessTermination )
+        throws CommandLineException
+    {
         final CommandLineCallable future =
-            executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, 
timeoutInSeconds );
+            executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, 
timeoutInSeconds, runAfterProcessTermination );
         return future.call();
     }
 
@@ -123,6 +144,7 @@ public abstract class CommandLineUtils
      * @param systemOut        A consumer that receives output, must be thread 
safe
      * @param systemErr        A consumer that receives system error stream 
output, must be thread safe
      * @param timeoutInSeconds Positive integer to specify timeout, zero and 
negative integers for no timeout.
+     * @param runAfterProcessTermination Optional callback to run after the 
process terminated or the the timeout was
      * @return A CommandLineCallable that provides the process return value, 
see {@link Process#exitValue()}. "call" must be called on
      *         this to be sure the forked process has terminated, no 
guarantees is made about
      *         any internal state before after the completion of the call 
statements
@@ -132,7 +154,8 @@ public abstract class CommandLineUtils
     private static CommandLineCallable executeCommandLineAsCallable( final 
Commandline cl, final InputStream systemIn,
                                                                     final 
StreamConsumer systemOut,
                                                                     final 
StreamConsumer systemErr,
-                                                                    final int 
timeoutInSeconds )
+                                                                    final int 
timeoutInSeconds,
+                                                                    final 
Runnable runAfterProcessTermination )
         throws CommandLineException
     {
         if ( cl == null )
@@ -191,6 +214,11 @@ public abstract class CommandLineUtils
                         returnValue = p.exitValue();
                     }
 
+                    if ( runAfterProcessTermination != null )
+                    {
+                        runAfterProcessTermination.run();
+                    }
+
                     waitForAllPumpers( inputFeeder, outputPumper, errorPumper 
);
 
                     if ( outputPumper.getException() != null )


Reply via email to