Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 265   Methods: 13
NCLOC: 152   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Exec.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,2002 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.BufferedReader;
 58   
 import java.io.File;
 59   
 import java.io.FileWriter;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStream;
 62   
 import java.io.InputStreamReader;
 63   
 import java.io.PrintWriter;
 64   
 import org.apache.tools.ant.BuildException;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.Task;
 67   
 
 68   
 /**
 69   
  * Executes a given command if the os platform is appropriate.
 70   
  *
 71   
  * <p><strong>As of Ant 1.2, this class is no longer the
 72   
  * implementation of Ant's &lt;exec&gt; task - it is considered to be
 73   
  * dead code by the Ant developers and is unmaintained.  Don't use
 74   
  * it.</strong></p>
 75   
 
 76   
  * @author duncan@x180.com
 77   
  * @author rubys@us.ibm.com
 78   
  *
 79   
  * @deprecated delegate to {@link org.apache.tools.ant.taskdefs.Execute Execute} 
 80   
  *             instead.
 81   
  */
 82   
 public class Exec extends Task {
 83   
     private String os;
 84   
     private String out;
 85   
     private File dir;
 86   
     private String command;
 87   
     protected PrintWriter fos = null;
 88   
     private boolean failOnError = false;
 89   
 
 90  0
     public Exec() {
 91  0
         System.err.println("As of Ant 1.2 released in October 2000, " 
 92   
             + "the Exec class");
 93  0
         System.err.println("is considered to be dead code by the Ant " 
 94   
             + "developers and is unmaintained.");
 95  0
         System.err.println("Don\'t use it!");
 96   
     }
 97   
 
 98  0
     public void execute() throws BuildException {
 99  0
         run(command);
 100   
     }
 101   
 
 102  0
     protected int run(String command) throws BuildException {
 103   
 
 104  0
         int err = -1; // assume the worst
 105   
 
 106   
         // test if os match
 107  0
         String myos = System.getProperty("os.name");
 108  0
         log("Myos = " + myos, Project.MSG_VERBOSE);
 109  0
         if ((os != null) && (os.indexOf(myos) < 0)){
 110   
             // this command will be executed only on the specified OS
 111  0
             log("Not found in " + os, Project.MSG_VERBOSE);
 112  0
             return 0;
 113   
         }
 114   
 
 115   
         // default directory to the project's base directory
 116  0
         if (dir == null) {
 117  0
           dir = getProject().getBaseDir();
 118   
         }
 119   
 
 120  0
         if (myos.toLowerCase().indexOf("windows") >= 0) {
 121  0
             if (!dir.equals(getProject().resolveFile("."))) {
 122  0
                 if (myos.toLowerCase().indexOf("nt") >= 0) {
 123  0
                     command = "cmd /c cd " + dir + " && " + command;
 124   
                 } else {
 125  0
                     String ant = getProject().getProperty("ant.home");
 126  0
                     if (ant == null) {
 127  0
                         throw new BuildException("Property 'ant.home' not " 
 128   
                             + "found", getLocation());
 129   
                     }
 130   
                 
 131  0
                     String antRun = getProject().resolveFile(ant + "/bin/antRun.bat").toString();
 132  0
                     command = antRun + " " + dir + " " + command;
 133   
                 }
 134   
             }
 135   
         } else {
 136  0
             String ant = getProject().getProperty("ant.home");
 137  0
             if (ant == null) {
 138  0
               throw new BuildException("Property 'ant.home' not found",
 139   
                                        getLocation());
 140   
             }
 141  0
             String antRun = getProject().resolveFile(ant + "/bin/antRun").toString();
 142   
 
 143  0
             command = antRun + " " + dir + " " + command;
 144   
         }
 145   
 
 146  0
         try {
 147   
             // show the command
 148  0
             log(command, Project.MSG_VERBOSE);
 149   
 
 150   
             // exec command on system runtime
 151  0
             Process proc = Runtime.getRuntime().exec(command);
 152   
 
 153  0
             if (out != null)  {
 154  0
                 fos = new PrintWriter(new FileWriter(out));
 155  0
                 log("Output redirected to " + out, Project.MSG_VERBOSE);
 156   
             }
 157   
 
 158   
             // copy input and error to the output stream
 159  0
             StreamPumper inputPumper =
 160   
                 new StreamPumper(proc.getInputStream(), Project.MSG_INFO);
 161  0
             StreamPumper errorPumper =
 162   
                 new StreamPumper(proc.getErrorStream(), Project.MSG_WARN);
 163   
 
 164   
             // starts pumping away the generated output/error
 165  0
             inputPumper.start();
 166  0
             errorPumper.start();
 167   
 
 168   
             // Wait for everything to finish
 169  0
             proc.waitFor();
 170  0
             inputPumper.join();
 171  0
             errorPumper.join();
 172  0
             proc.destroy();
 173   
 
 174   
             // close the output file if required
 175  0
             logFlush();
 176   
 
 177   
             // check its exit value
 178  0
             err = proc.exitValue();
 179  0
             if (err != 0) {
 180  0
                 if (failOnError) {
 181  0
                     throw new BuildException("Exec returned: " + err, getLocation());
 182   
                 } else {
 183  0
                     log("Result: " + err, Project.MSG_ERR);
 184   
                 }
 185   
             }
 186   
         } catch (IOException ioe) {
 187  0
             throw new BuildException("Error exec: " + command, ioe, getLocation());
 188   
         } catch (InterruptedException ex) {}
 189   
 
 190  0
         return err;
 191   
     }
 192   
 
 193  0
     public void setDir(String d) {
 194  0
         this.dir = getProject().resolveFile(d);
 195   
     }
 196   
 
 197  0
     public void setOs(String os) {
 198  0
         this.os = os;
 199   
     }
 200   
 
 201  0
     public void setCommand(String command) {
 202  0
         this.command = command;
 203   
     }
 204   
 
 205  0
     public void setOutput(String out) {
 206  0
         this.out = out;
 207   
     }
 208   
 
 209  0
     public void setFailonerror(boolean fail) {
 210  0
         failOnError = fail;
 211   
     }
 212   
 
 213  0
     protected void outputLog(String line, int messageLevel) {
 214  0
         if (fos == null) {
 215  0
             log(line, messageLevel); 
 216   
         } else {
 217  0
             fos.println(line);
 218   
         }
 219   
     }
 220   
 
 221  0
     protected void logFlush() {
 222  0
         if (fos != null) {
 223  0
           fos.close();
 224   
         }
 225   
     }
 226   
 
 227   
     // Inner class for continually pumping the input stream during
 228   
     // Process's runtime.
 229   
     class StreamPumper extends Thread {
 230   
         private BufferedReader din;
 231   
         private int messageLevel;
 232   
         private boolean endOfStream = false;
 233   
         private int SLEEP_TIME = 5;
 234   
 
 235  0
         public StreamPumper(InputStream is, int messageLevel) {
 236  0
             this.din = new BufferedReader(new InputStreamReader(is));
 237  0
             this.messageLevel = messageLevel;
 238   
         }
 239   
 
 240  0
         public void pumpStream() throws IOException {
 241  0
             if (!endOfStream) {
 242  0
                 String line = din.readLine();
 243   
 
 244  0
                 if (line != null) {
 245  0
                     outputLog(line, messageLevel);
 246   
                 } else {
 247  0
                     endOfStream = true;
 248   
                 }
 249   
             }
 250   
         }
 251   
 
 252  0
         public void run() {
 253  0
             try {
 254  0
                 try {
 255  0
                     while (!endOfStream) {
 256  0
                         pumpStream();
 257  0
                         sleep(SLEEP_TIME);
 258   
                     }
 259   
                 } catch (InterruptedException ie) {}
 260  0
                 din.close();
 261   
             } catch (IOException ioe) {}
 262   
         }
 263   
     }
 264   
 }
 265