Author: sgoeschl Date: Thu Aug 12 18:08:25 2010 New Revision: 984885 URL: http://svn.apache.org/viewvc?rev=984885&view=rev Log: [EXEC-42] Applied the patch and updated documentation
Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteStreamHandler.java commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteStreamHandler.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteStreamHandler.java?rev=984885&r1=984884&r2=984885&view=diff ============================================================================== --- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteStreamHandler.java (original) +++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteStreamHandler.java Thu Aug 12 18:08:25 2010 @@ -60,6 +60,13 @@ public interface ExecuteStreamHandler { /** * Stop handling of the streams - will not be restarted. + * Will wait for pump threads to complete. */ void stop(); + + /** + * Stop handling of the streams - will not be restarted. + * @param join if true, wait for the pump threads to complete + */ + void stop(boolean join); } Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java?rev=984885&r1=984884&r2=984885&view=diff ============================================================================== --- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java (original) +++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java Thu Aug 12 18:08:25 2010 @@ -44,28 +44,29 @@ public class PumpStreamHandler implement private final InputStream input; private InputStreamPumper inputStreamPumper; + + private boolean alwaysWaitForStreamThreads = true; /** * Construct a new <CODE>PumpStreamHandler</CODE>. - * - * @param out - * the output <CODE>OutputStream</CODE>. - * @param err - * the error <CODE>OutputStream</CODE>. - * @param input - * the input <CODE>InputStream</CODE>. */ - public PumpStreamHandler(final OutputStream out, final OutputStream err, - final InputStream input) { - - this.out = out; - this.err = err; - this.input = input; + public PumpStreamHandler() { + this(System.out, System.err); } /** * Construct a new <CODE>PumpStreamHandler</CODE>. - * + * + * @param outAndErr + * the output/error <CODE>OutputStream</CODE>. + */ + public PumpStreamHandler(final OutputStream outAndErr) { + this(outAndErr, outAndErr); + } + + /** + * Construct a new <CODE>PumpStreamHandler</CODE>. + * * @param out * the output <CODE>OutputStream</CODE>. * @param err @@ -78,18 +79,19 @@ public class PumpStreamHandler implement /** * Construct a new <CODE>PumpStreamHandler</CODE>. * - * @param outAndErr - * the output/error <CODE>OutputStream</CODE>. + * @param out + * the output <CODE>OutputStream</CODE>. + * @param err + * the error <CODE>OutputStream</CODE>. + * @param input + * the input <CODE>InputStream</CODE>. */ - public PumpStreamHandler(final OutputStream outAndErr) { - this(outAndErr, outAndErr); - } + public PumpStreamHandler(final OutputStream out, final OutputStream err, + final InputStream input) { - /** - * Construct a new <CODE>PumpStreamHandler</CODE>. - */ - public PumpStreamHandler() { - this(System.out, System.err); + this.out = out; + this.err = err; + this.input = input; } /** @@ -131,7 +133,8 @@ public class PumpStreamHandler implement inputThread = createSystemInPump(input, os); } else { inputThread = createPump(input, os, true); - } } else { + } } + else { try { os.close(); } catch (IOException e) { @@ -140,8 +143,31 @@ public class PumpStreamHandler implement } } } + + + /** + * Whether to always wait for (join) stream threads, even if the process + * is was "killed" by a Watchdog. + * @return true, to wait always (original behavior); false, to NOT wait if killed + */ + public boolean isAlwaysWaitForStreamThreads() { + return alwaysWaitForStreamThreads; + } /** + * Whether to always wait for (join) stream threads, even if the process + * is was "killed" by a Watchdog. Please note that skipping the wait might + * leave up to three threads behind so and cause severe problems in a + * production environment. + * + * @param alwaysWaitForStreamThreads if true, wait always (original behavior); if false, do NOT wait when killed + */ + public void setAlwaysWaitForStreamThreads(boolean alwaysWaitForStreamThreads) { + this.alwaysWaitForStreamThreads = alwaysWaitForStreamThreads; + } + + + /** * Start the <CODE>Thread</CODE>s. */ public void start() { @@ -160,35 +186,60 @@ public class PumpStreamHandler implement * Stop pumping the streams. */ public void stop() { + stop(this.alwaysWaitForStreamThreads); + } - if (outputThread != null) { - try { - outputThread.join(); - outputThread = null; - } catch (InterruptedException e) { - // ignore - } + /** + * Stop pumping the streams. + * @param join if true, wait for the pump threads to complete, if false don't wait + */ + public void stop(boolean join) { + + if (inputStreamPumper != null) { + inputStreamPumper.stopProcessing(); } - if (errorThread != null) { - try { - errorThread.join(); - errorThread = null; - } catch (InterruptedException e) { - // ignore + if (join) { + if (outputThread != null) { + try { + outputThread.join(); + outputThread = null; + } catch (InterruptedException e) { + // ignore + } } - } - if (inputStreamPumper != null) { - inputStreamPumper.stopProcessing(); + if (errorThread != null) { + try { + errorThread.join(); + errorThread = null; + } catch (InterruptedException e) { + // ignore + } + } + + if (inputThread != null) { + try { + inputThread.join(); + inputThread = null; + } catch (InterruptedException e) { + // ignore + } + } } + else { + // well, give each thread a chance to terminate itself befoew + // we leave them alone + if (outputThread != null) { + outputThread.interrupt(); + } - if (inputThread != null) { - try { - inputThread.join(); - inputThread = null; - } catch (InterruptedException e) { - // ignore + if (errorThread != null) { + errorThread.interrupt(); + } + + if (inputThread != null) { + inputThread.interrupt(); } } @@ -299,4 +350,5 @@ public class PumpStreamHandler implement result.setDaemon(true); return result; } + }