Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 200   Methods: 15
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PumpStreamHandler.java 50% 86.1% 93.3% 83.1%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs;
 56   
 
 57   
 import java.io.IOException;
 58   
 import java.io.InputStream;
 59   
 import java.io.OutputStream;
 60   
 
 61   
 /**
 62   
  * Copies standard output and error of subprocesses to standard output and
 63   
  * error of the parent process.
 64   
  *
 65   
  * TODO: standard input of the subprocess is not implemented.
 66   
  *
 67   
  * @author thomas.haas@softwired-inc.com
 68   
  * @since Ant 1.2
 69   
  */
 70   
 public class PumpStreamHandler implements ExecuteStreamHandler {
 71   
 
 72   
     private Thread outputThread;
 73   
     private Thread errorThread;
 74   
     private Thread inputThread;
 75   
 
 76   
     private OutputStream out;
 77   
     private OutputStream err;
 78   
     private InputStream input;
 79   
     
 80  30
     public PumpStreamHandler(OutputStream out, OutputStream err, 
 81   
                              InputStream input) {
 82  30
         this.out = out;
 83  30
         this.err = err;
 84  30
         this.input = input;
 85   
     }
 86   
 
 87  15
     public PumpStreamHandler(OutputStream out, OutputStream err) {
 88  15
         this(out, err, null);
 89   
     }
 90   
 
 91  1
     public PumpStreamHandler(OutputStream outAndErr) {
 92  1
         this(outAndErr, outAndErr);
 93   
     }
 94   
 
 95  0
     public PumpStreamHandler() {
 96  0
         this(System.out, System.err);
 97   
     }
 98   
 
 99  30
     public void setProcessOutputStream(InputStream is) {
 100  30
         createProcessOutputPump(is, out);
 101   
     }
 102   
 
 103   
 
 104  30
     public void setProcessErrorStream(InputStream is) {
 105  30
         if (err != null) {
 106  30
             createProcessErrorPump(is, err);
 107   
         }
 108   
     }
 109   
 
 110  30
     public void setProcessInputStream(OutputStream os) {
 111  30
         if (input != null) {
 112  0
             inputThread = createPump(input, os, true);
 113   
         } else {
 114  30
             try {
 115  30
                 os.close();
 116   
             } catch (IOException e) {
 117   
                 //ignore
 118   
             }
 119   
         }            
 120   
     }
 121   
 
 122  30
     public void start() {
 123  30
         outputThread.start();
 124  30
         errorThread.start();
 125  30
         if (inputThread != null) {
 126  0
             inputThread.start();
 127   
         }
 128   
     }
 129   
 
 130  30
     public void stop() {
 131  30
         try {
 132  30
             outputThread.join();
 133   
         } catch (InterruptedException e) {
 134   
             // ignore
 135   
         }
 136  30
         try {
 137  30
             errorThread.join();
 138   
         } catch (InterruptedException e) {
 139   
             // ignore
 140   
         }
 141   
 
 142  30
         if (inputThread != null) {
 143  0
             try {
 144  0
                 inputThread.join();
 145   
             } catch (InterruptedException e) {
 146   
                 // ignore
 147   
             } 
 148   
         }
 149   
 
 150  30
         try {
 151  30
             err.flush();
 152   
         } catch (IOException e) {
 153   
             // ignore
 154   
         }
 155  30
         try {
 156  30
             out.flush();
 157   
         } catch (IOException e) {
 158   
             // ignore
 159   
         }
 160   
     }
 161   
 
 162  14
     protected OutputStream getErr() {
 163  14
         return err;
 164   
     }
 165   
 
 166  14
     protected OutputStream getOut() {
 167  14
         return out;
 168   
     }
 169   
 
 170  30
     protected void createProcessOutputPump(InputStream is, OutputStream os) {
 171  30
         outputThread = createPump(is, os);
 172   
     }
 173   
 
 174  30
     protected void createProcessErrorPump(InputStream is, OutputStream os) {
 175  30
         errorThread = createPump(is, os);
 176   
     }
 177   
 
 178   
 
 179   
     /**
 180   
      * Creates a stream pumper to copy the given input stream to the
 181   
      * given output stream.
 182   
      */
 183  60
     protected Thread createPump(InputStream is, OutputStream os) {
 184  60
         return createPump(is, os, false);
 185   
     }
 186   
 
 187   
     /**
 188   
      * Creates a stream pumper to copy the given input stream to the
 189   
      * given output stream.
 190   
      */
 191  60
     protected Thread createPump(InputStream is, OutputStream os, 
 192   
                                 boolean closeWhenExhausted) {
 193  60
         final Thread result 
 194   
             = new Thread(new StreamPumper(is, os, closeWhenExhausted));
 195  60
         result.setDaemon(true);
 196  60
         return result;
 197   
     }
 198   
 
 199   
 }
 200