You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2002/03/02 02:07:14 UTC
cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl DefaultExecManager.java Environment.java ProcessMonitor.java
donaldp 02/03/01 17:07:14
Modified: proposal/myrmidon/src/java/org/apache/aut/nativelib/impl
DefaultExecManager.java Environment.java
ProcessMonitor.java
Log:
Rework it so that ExecManager does not shutdown streams.
Important when you pass in System.out/System.err or want to do something
special to stream after processing.
Submitted By: Darrell DeBoer <da...@apache.org>
Revision Changes Path
1.9 +2 -2 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java
Index: DefaultExecManager.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DefaultExecManager.java 21 Feb 2002 09:27:21 -0000 1.8
+++ DefaultExecManager.java 2 Mar 2002 01:07:13 -0000 1.9
@@ -32,7 +32,7 @@
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
- * @version $Revision: 1.8 $ $Date: 2002/02/21 09:27:21 $
+ * @version $Revision: 1.9 $ $Date: 2002/03/02 01:07:13 $
* @see ExecManager
* @see ExecMetaData
* @see Environment
@@ -115,7 +115,7 @@
final CommandLauncher launcher = getLauncher( command );
final Process process = launcher.exec( command );
final ProcessMonitor monitor =
- new ProcessMonitor( process, input, output, error, timeout );
+ new ProcessMonitor( process, input, output, error, timeout, false );
final Thread thread = new Thread( monitor, "ProcessMonitor" );
thread.start();
1.4 +12 -7 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/Environment.java
Index: Environment.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/Environment.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Environment.java 21 Feb 2002 09:27:21 -0000 1.3
+++ Environment.java 2 Mar 2002 01:07:13 -0000 1.4
@@ -19,6 +19,7 @@
import org.apache.aut.nativelib.ExecMetaData;
import org.apache.aut.nativelib.Os;
import org.apache.avalon.excalibur.util.StringUtil;
+import org.apache.avalon.excalibur.io.IOUtil;
/**
* This is the class that can be used to retrieve the environment
@@ -26,7 +27,7 @@
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
- * @version $Revision: 1.3 $ $Date: 2002/02/21 09:27:21 $
+ * @version $Revision: 1.4 $ $Date: 2002/03/02 01:07:13 $
*/
final class Environment
{
@@ -157,13 +158,17 @@
final ExecMetaData metaData = new ExecMetaData( command, null, workingDirectory );
final ByteArrayOutputStream output = new ByteArrayOutputStream();
- final int retval = m_execManager.execute( metaData, null, output, output, 0 );
- if( retval != 0 )
- {
- // Just try to use what we got
- }
+ try {
+ final int retval = m_execManager.execute( metaData, null, output, output, 0 );
+ if( retval != 0 )
+ {
+ // Just try to use what we got
+ }
- return output.toString();
+ return output.toString();
+ } finally {
+ IOUtil.shutdownStream( output );
+ }
}
/**
1.6 +39 -5 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/ProcessMonitor.java
Index: ProcessMonitor.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/ProcessMonitor.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ProcessMonitor.java 6 Feb 2002 13:34:46 -0000 1.5
+++ ProcessMonitor.java 2 Mar 2002 01:07:13 -0000 1.6
@@ -21,7 +21,7 @@
* It will also in the future do the same for stdin.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.5 $ $Date: 2002/02/06 13:34:46 $
+ * @version $Revision: 1.6 $ $Date: 2002/03/02 01:07:13 $
*/
class ProcessMonitor
extends AbstractLogEnabled
@@ -72,11 +72,33 @@
*/
private final OutputStream m_error;
+ /**
+ * Specifies whether the monitor should shutdown
+ * input, output and error Streams when it finishes execution.
+ * This should be set to <code>true</code> for processes which
+ * will run asynchronously.
+ */
+ private final boolean m_shutdownStreams;
+
+ /**
+ * Creates a monitor for a given {@link java.lang.Process}, which pipes
+ * input from the given input stream to the process, and pipes the process
+ * output to the given OutputStreams.
+ *
+ * @param process the Process to be monitored
+ * @param input is read into the Process' stdin
+ * @param output receives the Process' stdout
+ * @param error receives the Process' stderr
+ * @param timeoutDuration how long to let the Process run before killing it.
+ * @param shutdownStreams specifies if the monitor should shutdown the
+ * streams when the Process exits.
+ */
public ProcessMonitor( final Process process,
final InputStream input,
final OutputStream output,
final OutputStream error,
- final long timeoutDuration )
+ final long timeoutDuration,
+ final boolean shutdownStreams )
{
if( null == process )
{
@@ -100,6 +122,7 @@
m_output = output;
m_error = error;
m_timeout = timeout;
+ m_shutdownStreams = shutdownStreams;
}
/**
@@ -139,9 +162,20 @@
//that we have got all the data
processStreams();
- IOUtil.shutdownStream( m_input );
- IOUtil.shutdownStream( m_output );
- IOUtil.shutdownStream( m_error );
+ cleanupStreams();
+ }
+
+ /**
+ * Utility method which cleans up all IO Streams, if required.
+ */
+ private void cleanupStreams()
+ {
+ if( m_shutdownStreams )
+ {
+ IOUtil.shutdownStream( m_input );
+ IOUtil.shutdownStream( m_output );
+ IOUtil.shutdownStream( m_error );
+ }
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>