Author: sgoeschl Date: Sun Feb 1 21:18:31 2009 New Revision: 739841 URL: http://svn.apache.org/viewvc?rev=739841&view=rev Log: [EXEC-32] Handling null streams more consistently
Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java 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=739841&r1=739840&r2=739841&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 Sun Feb 1 21:18:31 2009 @@ -26,7 +26,8 @@ /** * Copies standard output and error of subprocesses to standard output and error - * of the parent process. + * of the parent process. If output or error stream are set to null, any feedback + * from that stream will be lost. */ public class PumpStreamHandler implements ExecuteStreamHandler { @@ -96,6 +97,9 @@ * the <CODE>InputStream</CODE>. */ public void setProcessOutputStream(final InputStream is) { + if (out != null) { + createProcessOutputPump(is, out); + } createProcessOutputPump(is, out); } @@ -136,8 +140,12 @@ * Start the <CODE>Thread</CODE>s. */ public void start() { - outputThread.start(); - errorThread.start(); + if (outputThread != null) { + outputThread.start(); + } + if (errorThread != null) { + errorThread.start(); + } if (inputThread != null) { inputThread.start(); } @@ -147,37 +155,53 @@ * Stop pumping the streams. */ public void stop() { - try { - outputThread.join(); - } catch (InterruptedException e) { - // ignore - } - try { - errorThread.join(); - } catch (InterruptedException e) { - // ignore + + if (outputThread != null) { + try { + outputThread.join(); + outputThread = null; + } catch (InterruptedException e) { + // ignore + } + } + + if (errorThread != null) { + try { + errorThread.join(); + errorThread = null; + } catch (InterruptedException e) { + // ignore + } } if (inputThread != null) { try { inputThread.join(); + inputThread = null; } catch (InterruptedException e) { // ignore } } - try { - err.flush(); - } catch (IOException e) { - String msg = "Got exception while flushing the error stream"; - DebugUtils.handleException(msg ,e); - } - try { - out.flush(); - } catch (IOException e) { - String msg = "Got exception while flushing the output stream"; - DebugUtils.handleException(msg ,e); - } + if (err != null && err != out) { + try { + err.flush(); + err = null; + } catch (IOException e) { + String msg = "Got exception while flushing the error stream"; + DebugUtils.handleException(msg ,e); + } + } + + if (out != null) { + try { + out.flush(); + out = null; + } catch (IOException e) { + String msg = "Got exception while flushing the output stream"; + DebugUtils.handleException(msg ,e); + } + } } /** Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?rev=739841&r1=739840&r2=739841&view=diff ============================================================================== --- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original) +++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Sun Feb 1 21:18:31 2009 @@ -239,6 +239,28 @@ assertEquals("cmd /C convert source.jpg -resize \"500x> \" target.jpg", cmdl.toString()); } + /** + * Another command line parsing puzzle from Kai Hu - as + * far as I understand it there is no way to express that + * in a one-line command string. + */ + public void testParseComplexCommandLine2() { + + String commandline = "./script/jrake cruise:publish_installers " + + "INSTALLER_VERSION=unstable_2_1 " + + "INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\" " + + "INSTALLER_DOWNLOAD_SERVER=\'something\' " + + "WITHOUT_HELP_DOC=true"; + + CommandLine cmdl = CommandLine.parse(commandline); + String[] args = cmdl.getArguments(); + assertEquals(args[0], "cruise:publish_installers"); + assertEquals(args[1], "INSTALLER_VERSION=unstable_2_1"); + // assertEquals(args[2], "INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\""); + // assertEquals(args[3], "INSTALLER_DOWNLOAD_SERVER='something'"); + assertEquals(args[4], "WITHOUT_HELP_DOC=true"); + } + /** * Create a command line with pre-quoted strings to test SANDBOX-192, * e.g. "runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC", "\"-XX:ParallelGCThreads=2\"" Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=739841&r1=739840&r2=739841&view=diff ============================================================================== --- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java (original) +++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Sun Feb 1 21:18:31 2009 @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; @@ -383,4 +384,46 @@ int exitValue = executor.execute(cl); assertFalse(exec.isFailure(exitValue)); } + + /** + * Start a process and connect stdout and stderr. + */ + public void testExecuteWithStdOutErr() throws Exception + { + CommandLine cl = new CommandLine(testScript); + PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( System.out, System.err ); + DefaultExecutor executor = new DefaultExecutor(); + executor.setStreamHandler( pumpStreamHandler ); + int exitValue = executor.execute(cl); + assertFalse(exec.isFailure(exitValue)); + } + + /** + * Start a process and connect it to no stream. + */ + public void testExecuteWithNullOutErr() throws Exception + { + CommandLine cl = new CommandLine(testScript); + PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( null, null ); + DefaultExecutor executor = new DefaultExecutor(); + executor.setStreamHandler( pumpStreamHandler ); + int exitValue = executor.execute(cl); + assertFalse(exec.isFailure(exitValue)); + } + + /** + * Start a process and connect out and err to a file. + */ + public void testExecuteWithRedirectOutErr() throws Exception + { + File outfile = File.createTempFile("EXEC", ".test"); + outfile.deleteOnExit(); + CommandLine cl = new CommandLine(testScript); + PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( new FileOutputStream(outfile) ); + DefaultExecutor executor = new DefaultExecutor(); + executor.setStreamHandler( pumpStreamHandler ); + int exitValue = executor.execute(cl); + assertFalse(exec.isFailure(exitValue)); + assertTrue(outfile.exists()); + } }