ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs ExecTask.java PumpStreamHandler.java StreamPumper.java
Date Fri, 07 Feb 2003 13:57:41 GMT
conor       2003/02/07 05:57:41

  Modified:    src/main/org/apache/tools/ant/taskdefs ExecTask.java
                        PumpStreamHandler.java StreamPumper.java
  Log:
  First cut at input redirection for <exec>.
  
  Revision  Changes    Path
  1.48      +22 -1     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
  
  Index: ExecTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -w -u -r1.47 -r1.48
  --- ExecTask.java	6 Feb 2003 13:10:21 -0000	1.47
  +++ ExecTask.java	7 Feb 2003 13:57:41 -0000	1.48
  @@ -59,9 +59,11 @@
   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
  +import java.io.FileInputStream;
   import java.io.IOException;
   import java.io.StringReader;
   import java.io.OutputStream;
  +import java.io.InputStream;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.Task;
  @@ -89,6 +91,7 @@
       private String os;
       private File out;
       private File error;
  +    private File input;
   
       private boolean logError = false;
       
  @@ -168,6 +171,13 @@
       }
   
       /**
  +     * Set the input to use for the task
  +     */
  +    public void setInput(File input) {
  +        this.input = input;
  +    }
  +    
  +    /**
        * File the output of the process is redirected to. If error is not 
        * redirected, it too will appear in the output
        */
  @@ -475,6 +485,7 @@
       protected ExecuteStreamHandler createHandler() throws BuildException {
           OutputStream outputStream = null;
           OutputStream errorStream = null;
  +        InputStream inputStream = null; 
           
           if (out == null && outputprop == null) {
               outputStream = new LogOutputStream(this, Project.MSG_INFO);
  @@ -541,7 +552,17 @@
               errorBaos = null;
           }
           
  -        return new PumpStreamHandler(outputStream, errorStream, true, true);         
  +        if (input != null) {
  +            try {
  +                inputStream = new FileInputStream(input);
  +            } catch (FileNotFoundException fne) {
  +                throw new BuildException("Cannot read from " + input, fne,
  +                                         getLocation());
  +            }
  +        }
  +        
  +        return new PumpStreamHandler(outputStream, errorStream, inputStream, 
  +                                     true, true, true);         
       }
   
       /**
  
  
  
  1.10      +64 -15    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java
  
  Index: PumpStreamHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -w -u -r1.9 -r1.10
  --- PumpStreamHandler.java	6 Feb 2003 13:10:21 -0000	1.9
  +++ PumpStreamHandler.java	7 Feb 2003 13:57:41 -0000	1.10
  @@ -69,24 +69,32 @@
    */
   public class PumpStreamHandler implements ExecuteStreamHandler {
   
  -    private Thread inputThread;
  +    private Thread outputThread;
       private Thread errorThread;
  +    private Thread inputThread;
   
       private OutputStream out;
       private OutputStream err;
  +    private InputStream input;
  +    
       private boolean closeOutOnStop = false;
       private boolean closeErrOnStop = false;
  +    private boolean closeInputOnStop = false;
   
       public PumpStreamHandler(OutputStream out, OutputStream err, 
  -                             boolean closeOutOnStop, boolean closeErrOnStop) {
  +                             InputStream input,
  +                             boolean closeOutOnStop, boolean closeErrOnStop,
  +                             boolean closeInputOnStop) {
           this.out = out;
           this.err = err;
  +        this.input = input;
           this.closeOutOnStop = closeOutOnStop;
           this.closeErrOnStop = closeErrOnStop;
  +        this.closeInputOnStop = closeInputOnStop;
       }
   
       public PumpStreamHandler(OutputStream out, OutputStream err) {
  -        this(out, err, false, false);
  +        this(out, err, null, false, false, false);
       }
   
       public PumpStreamHandler(OutputStream outAndErr) {
  @@ -103,39 +111,70 @@
   
   
       public void setProcessErrorStream(InputStream is) {
  +        if (err != null) {
           createProcessErrorPump(is, err);
       }
  -
  +    }
   
       public void setProcessInputStream(OutputStream os) {
  +        if (input != null) {
  +            inputThread = createPump(input, os, true);
  +        } else {
  +            try {
  +                os.close();
  +            } catch (IOException e) {
  +                //ignore
  +            }
  +        }            
       }
  -
   
       public void start() {
  -        inputThread.start();
  +        outputThread.start();
           errorThread.start();
  +        inputThread.start();
       }
   
  -
       public void stop() {
           try {
  -            inputThread.join();
  -        } catch (InterruptedException e) {}
  +            outputThread.join();
  +        } catch (InterruptedException e) {
  +            // ignore
  +        }
           try {
               errorThread.join();
  -        } catch (InterruptedException e) {}
  +        } catch (InterruptedException e) {
  +            // ignore
  +        }
  +
  +        if (inputThread != null) {
  +            try {
  +                inputThread.join();
  +                if (closeInputOnStop) {
  +                    input.close();
  +                }
  +            } catch (InterruptedException e) {
  +                // ignore
  +            } catch (IOException e) {
  +                // ignore
  +            } 
  +        }
  +
           try {
               err.flush();
               if (closeErrOnStop) {
                   err.close();
               }
  -        } catch (IOException e) {}
  +        } catch (IOException e) {
  +            // ignore
  +        }
           try {
               out.flush();
               if (closeOutOnStop) {
                   out.close();
               }
  -        } catch (IOException e) {}
  +        } catch (IOException e) {
  +            // ignore
  +        }
       }
   
       protected OutputStream getErr() {
  @@ -147,7 +186,7 @@
       }
   
       protected void createProcessOutputPump(InputStream is, OutputStream os) {
  -        inputThread = createPump(is, os);
  +        outputThread = createPump(is, os);
       }
   
       protected void createProcessErrorPump(InputStream is, OutputStream os) {
  @@ -160,7 +199,17 @@
        * given output stream.
        */
       protected Thread createPump(InputStream is, OutputStream os) {
  -        final Thread result = new Thread(new StreamPumper(is, os));
  +        return createPump(is, os, false);
  +    }
  +
  +    /**
  +     * Creates a stream pumper to copy the given input stream to the
  +     * given output stream.
  +     */
  +    protected Thread createPump(InputStream is, OutputStream os, 
  +                                boolean closeWhenExhausted) {
  +        final Thread result 
  +            = new Thread(new StreamPumper(is, os, closeWhenExhausted));
           result.setDaemon(true);
           return result;
       }
  
  
  
  1.9       +20 -2     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/StreamPumper.java
  
  Index: StreamPumper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/StreamPumper.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -w -u -r1.8 -r1.9
  --- StreamPumper.java	6 Feb 2003 12:38:05 -0000	1.8
  +++ StreamPumper.java	7 Feb 2003 13:57:41 -0000	1.9
  @@ -66,23 +66,38 @@
    */
   public class StreamPumper implements Runnable {
   
  -    // TODO: make SIZE and SLEEP instance variables.
  +    // TODO: make SIZE an instance variable.
       // TODO: add a status flag to note if an error occured in run.
   
       private static final int SIZE = 128;
       private InputStream is;
       private OutputStream os;
       private boolean finished;
  +    private boolean closeWhenExhausted;
   
       /**
        * Create a new stream pumper.
        *
        * @param is input stream to read data from
        * @param os output stream to write data to.
  +     * @param closeWhenExhausted if true, the output stream will be closed when
  +     *        the input is exhausted.
        */
  -    public StreamPumper(InputStream is, OutputStream os) {
  +    public StreamPumper(InputStream is, OutputStream os, 
  +                        boolean closeWhenExhausted) {
           this.is = is;
           this.os = os;
  +        this.closeWhenExhausted = closeWhenExhausted;        
  +    }
  +    
  +    /**
  +     * Create a new stream pumper.
  +     *
  +     * @param is input stream to read data from
  +     * @param os output stream to write data to.
  +     */
  +    public StreamPumper(InputStream is, OutputStream os) {
  +        this(is, os, false);
       }
   
   
  @@ -103,6 +118,9 @@
           try {
               while ((length = is.read(buf)) > 0) {
                   os.write(buf, 0, length);
  +            }
  +            if (closeWhenExhausted) {
  +                os.close();
               }
           } catch (IOException e) {
               // ignore errors
  
  
  

Mime
View raw message