git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270558 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -1,42 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.OutputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| /** | |||||
| * Logs standard output and error of a subprocess to the log system of ant. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| public class LogStreamHandler | |||||
| extends PumpStreamHandler | |||||
| { | |||||
| public LogStreamHandler( final OutputStream output, final OutputStream error ) | |||||
| { | |||||
| super( output, error ); | |||||
| } | |||||
| public void stop() | |||||
| throws TaskException | |||||
| { | |||||
| super.stop(); | |||||
| try | |||||
| { | |||||
| getErr().close(); | |||||
| getOut().close(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| // plain impossible | |||||
| throw new TaskException( "Error", e ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,136 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import java.io.OutputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| /** | |||||
| * Copies standard output and error of subprocesses to standard output and error | |||||
| * of the parent process. TODO: standard input of the subprocess is not | |||||
| * implemented. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| public class PumpStreamHandler | |||||
| implements ExecuteStreamHandler | |||||
| { | |||||
| private Thread m_errorThread; | |||||
| private Thread m_inputThread; | |||||
| private OutputStream m_output; | |||||
| private OutputStream m_error; | |||||
| public PumpStreamHandler( final OutputStream output, | |||||
| final OutputStream error ) | |||||
| { | |||||
| m_output = output; | |||||
| m_error = error; | |||||
| } | |||||
| public PumpStreamHandler( OutputStream outAndErr ) | |||||
| { | |||||
| this( outAndErr, outAndErr ); | |||||
| } | |||||
| public PumpStreamHandler() | |||||
| { | |||||
| this( System.out, System.err ); | |||||
| } | |||||
| public void setProcessErrorStream( final InputStream error ) | |||||
| { | |||||
| createProcessErrorPump( error, m_error ); | |||||
| } | |||||
| public void setProcessInputStream( final OutputStream standardInput ) | |||||
| { | |||||
| } | |||||
| public void setProcessOutputStream( final InputStream standardOutput ) | |||||
| { | |||||
| createProcessOutputPump( standardOutput, m_output ); | |||||
| } | |||||
| public void start() | |||||
| { | |||||
| m_inputThread.start(); | |||||
| m_errorThread.start(); | |||||
| } | |||||
| public void stop() | |||||
| throws TaskException | |||||
| { | |||||
| try | |||||
| { | |||||
| m_inputThread.join(); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_errorThread.join(); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_error.flush(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_output.flush(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| protected OutputStream getErr() | |||||
| { | |||||
| return m_error; | |||||
| } | |||||
| protected OutputStream getOut() | |||||
| { | |||||
| return m_output; | |||||
| } | |||||
| protected void createProcessErrorPump( InputStream is, OutputStream os ) | |||||
| { | |||||
| m_errorThread = createPump( is, os ); | |||||
| } | |||||
| protected void createProcessOutputPump( InputStream is, OutputStream os ) | |||||
| { | |||||
| m_inputThread = createPump( is, os ); | |||||
| } | |||||
| /** | |||||
| * Creates a stream pumper to copy the given input stream to the given | |||||
| * output stream. | |||||
| * | |||||
| * @param is Description of Parameter | |||||
| * @param os Description of Parameter | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| protected Thread createPump( final InputStream input, | |||||
| final OutputStream output ) | |||||
| { | |||||
| final Thread result = new Thread( new StreamPumper( input, output ) ); | |||||
| result.setDaemon( true ); | |||||
| return result; | |||||
| } | |||||
| } | |||||
| @@ -1,71 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import java.io.OutputStream; | |||||
| /** | |||||
| * Copies all data from an input stream to an output stream. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| class StreamPumper | |||||
| implements Runnable | |||||
| { | |||||
| // TODO: make SIZE and SLEEP instance variables. | |||||
| // TODO: add a status flag to note if an error occured in run. | |||||
| private final static int SLEEP = 5; | |||||
| private final static int SIZE = 128; | |||||
| private InputStream m_input; | |||||
| private OutputStream m_output; | |||||
| /** | |||||
| * Create a new stream pumper. | |||||
| * | |||||
| * @param is input stream to read data from | |||||
| * @param os output stream to write data to. | |||||
| */ | |||||
| public StreamPumper( final InputStream input, | |||||
| final OutputStream output ) | |||||
| { | |||||
| m_input = input; | |||||
| m_output = output; | |||||
| } | |||||
| /** | |||||
| * Copies data from the input stream to the output stream. Terminates as | |||||
| * soon as the input stream is closed or an error occurs. | |||||
| */ | |||||
| public void run() | |||||
| { | |||||
| final byte[] buf = new byte[ SIZE ]; | |||||
| int length; | |||||
| try | |||||
| { | |||||
| while( ( length = m_input.read( buf ) ) > 0 ) | |||||
| { | |||||
| m_output.write( buf, 0, length ); | |||||
| try | |||||
| { | |||||
| Thread.sleep( SLEEP ); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,42 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.OutputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| /** | |||||
| * Logs standard output and error of a subprocess to the log system of ant. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| public class LogStreamHandler | |||||
| extends PumpStreamHandler | |||||
| { | |||||
| public LogStreamHandler( final OutputStream output, final OutputStream error ) | |||||
| { | |||||
| super( output, error ); | |||||
| } | |||||
| public void stop() | |||||
| throws TaskException | |||||
| { | |||||
| super.stop(); | |||||
| try | |||||
| { | |||||
| getErr().close(); | |||||
| getOut().close(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| // plain impossible | |||||
| throw new TaskException( "Error", e ); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,136 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import java.io.OutputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| /** | |||||
| * Copies standard output and error of subprocesses to standard output and error | |||||
| * of the parent process. TODO: standard input of the subprocess is not | |||||
| * implemented. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| public class PumpStreamHandler | |||||
| implements ExecuteStreamHandler | |||||
| { | |||||
| private Thread m_errorThread; | |||||
| private Thread m_inputThread; | |||||
| private OutputStream m_output; | |||||
| private OutputStream m_error; | |||||
| public PumpStreamHandler( final OutputStream output, | |||||
| final OutputStream error ) | |||||
| { | |||||
| m_output = output; | |||||
| m_error = error; | |||||
| } | |||||
| public PumpStreamHandler( OutputStream outAndErr ) | |||||
| { | |||||
| this( outAndErr, outAndErr ); | |||||
| } | |||||
| public PumpStreamHandler() | |||||
| { | |||||
| this( System.out, System.err ); | |||||
| } | |||||
| public void setProcessErrorStream( final InputStream error ) | |||||
| { | |||||
| createProcessErrorPump( error, m_error ); | |||||
| } | |||||
| public void setProcessInputStream( final OutputStream standardInput ) | |||||
| { | |||||
| } | |||||
| public void setProcessOutputStream( final InputStream standardOutput ) | |||||
| { | |||||
| createProcessOutputPump( standardOutput, m_output ); | |||||
| } | |||||
| public void start() | |||||
| { | |||||
| m_inputThread.start(); | |||||
| m_errorThread.start(); | |||||
| } | |||||
| public void stop() | |||||
| throws TaskException | |||||
| { | |||||
| try | |||||
| { | |||||
| m_inputThread.join(); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_errorThread.join(); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_error.flush(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| try | |||||
| { | |||||
| m_output.flush(); | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| protected OutputStream getErr() | |||||
| { | |||||
| return m_error; | |||||
| } | |||||
| protected OutputStream getOut() | |||||
| { | |||||
| return m_output; | |||||
| } | |||||
| protected void createProcessErrorPump( InputStream is, OutputStream os ) | |||||
| { | |||||
| m_errorThread = createPump( is, os ); | |||||
| } | |||||
| protected void createProcessOutputPump( InputStream is, OutputStream os ) | |||||
| { | |||||
| m_inputThread = createPump( is, os ); | |||||
| } | |||||
| /** | |||||
| * Creates a stream pumper to copy the given input stream to the given | |||||
| * output stream. | |||||
| * | |||||
| * @param is Description of Parameter | |||||
| * @param os Description of Parameter | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| protected Thread createPump( final InputStream input, | |||||
| final OutputStream output ) | |||||
| { | |||||
| final Thread result = new Thread( new StreamPumper( input, output ) ); | |||||
| result.setDaemon( true ); | |||||
| return result; | |||||
| } | |||||
| } | |||||
| @@ -1,71 +0,0 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import java.io.OutputStream; | |||||
| /** | |||||
| * Copies all data from an input stream to an output stream. | |||||
| * | |||||
| * @author thomas.haas@softwired-inc.com | |||||
| */ | |||||
| class StreamPumper | |||||
| implements Runnable | |||||
| { | |||||
| // TODO: make SIZE and SLEEP instance variables. | |||||
| // TODO: add a status flag to note if an error occured in run. | |||||
| private final static int SLEEP = 5; | |||||
| private final static int SIZE = 128; | |||||
| private InputStream m_input; | |||||
| private OutputStream m_output; | |||||
| /** | |||||
| * Create a new stream pumper. | |||||
| * | |||||
| * @param is input stream to read data from | |||||
| * @param os output stream to write data to. | |||||
| */ | |||||
| public StreamPumper( final InputStream input, | |||||
| final OutputStream output ) | |||||
| { | |||||
| m_input = input; | |||||
| m_output = output; | |||||
| } | |||||
| /** | |||||
| * Copies data from the input stream to the output stream. Terminates as | |||||
| * soon as the input stream is closed or an error occurs. | |||||
| */ | |||||
| public void run() | |||||
| { | |||||
| final byte[] buf = new byte[ SIZE ]; | |||||
| int length; | |||||
| try | |||||
| { | |||||
| while( ( length = m_input.read( buf ) ) > 0 ) | |||||
| { | |||||
| m_output.write( buf, 0, length ); | |||||
| try | |||||
| { | |||||
| Thread.sleep( SLEEP ); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | |||||
| catch( IOException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | |||||