1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.PrintWriter ; 28 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.MagicNames; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.Task; 33 34 46 public class Exec extends Task { 47 private String os; 48 private String out; 49 private File dir; 50 private String command; 51 protected PrintWriter fos = null; 53 private boolean failOnError = false; 55 56 60 public Exec() { 61 System.err.println("As of Ant 1.2 released in October 2000, " 62 + "the Exec class"); 63 System.err.println("is considered to be dead code by the Ant " 64 + "developers and is unmaintained."); 65 System.err.println("Don\'t use it!"); 66 } 67 68 72 public void execute() throws BuildException { 73 run(command); 74 } 75 76 82 protected int run(String command) throws BuildException { 83 84 int err = -1; 86 String myos = System.getProperty("os.name"); 88 log("Myos = " + myos, Project.MSG_VERBOSE); 89 if ((os != null) && (os.indexOf(myos) < 0)) { 90 log("Not found in " + os, Project.MSG_VERBOSE); 92 return 0; 93 } 94 95 if (dir == null) { 97 dir = getProject().getBaseDir(); 98 } 99 100 if (myos.toLowerCase().indexOf("windows") >= 0) { 101 if (!dir.equals(getProject().resolveFile("."))) { 102 if (myos.toLowerCase().indexOf("nt") >= 0) { 103 command = "cmd /c cd " + dir + " && " + command; 104 } else { 105 String ant = getProject().getProperty(MagicNames.ANT_HOME); 106 if (ant == null) { 107 throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not " 108 + "found", getLocation()); 109 } 110 111 String antRun = getProject().resolveFile(ant + "/bin/antRun.bat").toString(); 112 command = antRun + " " + dir + " " + command; 113 } 114 } 115 } else { 116 String ant = getProject().getProperty(MagicNames.ANT_HOME); 117 if (ant == null) { 118 throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not found", 119 getLocation()); 120 } 121 String antRun = getProject().resolveFile(ant + "/bin/antRun").toString(); 122 123 command = antRun + " " + dir + " " + command; 124 } 125 126 try { 127 log(command, Project.MSG_VERBOSE); 129 130 Process proc = Runtime.getRuntime().exec(command); 132 133 if (out != null) { 134 fos = new PrintWriter (new FileWriter (out)); 135 log("Output redirected to " + out, Project.MSG_VERBOSE); 136 } 137 138 StreamPumper inputPumper = 140 new StreamPumper(proc.getInputStream(), Project.MSG_INFO); 141 StreamPumper errorPumper = 142 new StreamPumper(proc.getErrorStream(), Project.MSG_WARN); 143 144 inputPumper.start(); 146 errorPumper.start(); 147 148 proc.waitFor(); 150 inputPumper.join(); 151 errorPumper.join(); 152 proc.destroy(); 153 154 logFlush(); 156 157 err = proc.exitValue(); 159 if (err != 0) { 160 if (failOnError) { 161 throw new BuildException("Exec returned: " + err, getLocation()); 162 } else { 163 log("Result: " + err, Project.MSG_ERR); 164 } 165 } 166 } catch (IOException ioe) { 167 throw new BuildException("Error exec: " + command, ioe, getLocation()); 168 } catch (InterruptedException ex) { 169 } 171 172 return err; 173 } 174 175 179 public void setDir(String d) { 180 this.dir = getProject().resolveFile(d); 181 } 182 183 187 public void setOs(String os) { 188 this.os = os; 189 } 190 191 195 public void setCommand(String command) { 196 this.command = command; 197 } 198 199 203 public void setOutput(String out) { 204 this.out = out; 205 } 206 207 212 public void setFailonerror(boolean fail) { 213 failOnError = fail; 214 } 215 216 222 protected void outputLog(String line, int messageLevel) { 223 if (fos == null) { 224 log(line, messageLevel); 225 } else { 226 fos.println(line); 227 } 228 } 229 230 233 protected void logFlush() { 234 if (fos != null) { 235 fos.close(); 236 } 237 } 238 239 class StreamPumper extends Thread { 242 private BufferedReader din; 243 private int messageLevel; 244 private boolean endOfStream = false; 245 private static final int SLEEP_TIME = 5; 246 247 public StreamPumper(InputStream is, int messageLevel) { 248 this.din = new BufferedReader (new InputStreamReader (is)); 249 this.messageLevel = messageLevel; 250 } 251 252 public void pumpStream() throws IOException { 253 if (!endOfStream) { 254 String line = din.readLine(); 255 256 if (line != null) { 257 outputLog(line, messageLevel); 258 } else { 259 endOfStream = true; 260 } 261 } 262 } 263 264 public void run() { 265 try { 266 try { 267 while (!endOfStream) { 268 pumpStream(); 269 sleep(SLEEP_TIME); 270 } 271 } catch (InterruptedException ie) { 272 } 274 din.close(); 275 } catch (IOException ioe) { 276 } 278 } 279 } 280 } 281 | Popular Tags |