diff --git a/src/bin/antRun b/src/bin/antRun index 45bc847d3..ac1a54d05 100644 --- a/src/bin/antRun +++ b/src/bin/antRun @@ -11,4 +11,4 @@ if test -e $CMD.sh; then fi echo $CMD $@ -$CMD $@ 2>&1 +$CMD $@ diff --git a/src/main/org/apache/tools/ant/taskdefs/Cvs.java b/src/main/org/apache/tools/ant/taskdefs/Cvs.java index 1f0b85203..6a326e1a7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Cvs.java +++ b/src/main/org/apache/tools/ant/taskdefs/Cvs.java @@ -64,7 +64,7 @@ import java.io.*; * @author stefano@apache.org */ -public class Cvs extends Task { +public class Cvs extends Exec { private String cvsRoot; private String dest; @@ -76,47 +76,18 @@ public class Cvs extends Task { // XXX: we should use JCVS (www.ice.com/JCVS) instead of command line // execution so that we don't rely on having native CVS stuff around (SM) - try { - String ant=project.getProperty("ant.home"); - if(ant==null) throw new BuildException("Needs ant.home"); + String ant=project.getProperty("ant.home"); + if(ant==null) throw new BuildException("Needs ant.home"); - StringBuffer sb=new StringBuffer(); - sb.append(ant).append("/bin/antRun ").append(dest); - sb.append(" cvs -d ").append( cvsRoot ).append(" checkout "); - if(tag!=null) - sb.append("-r ").append(tag).append(" "); + StringBuffer sb=new StringBuffer(); + sb.append(ant).append("/bin/antRun ").append(dest); + sb.append(" cvs -d ").append( cvsRoot ).append(" checkout "); + if (tag!=null) + sb.append("-r ").append(tag).append(" "); - sb.append( pack ); - String command=sb.toString(); + sb.append( pack ); - project.log(command, "cvs", Project.MSG_WARN); - - - // exec command on system runtime - Process proc = Runtime.getRuntime().exec( command); - - // ignore response - InputStreamReader isr=new InputStreamReader(proc.getInputStream()); - BufferedReader din = new BufferedReader(isr); - - // pipe CVS output to STDOUT - String line; - while((line = din.readLine()) != null) { - project.log(line, "cvs", Project.MSG_WARN); - //System.out.println(line); - } - - proc.waitFor(); - int err = proc.exitValue(); - if (err != 0) { - throw new BuildException( "Error " + err + "in " + command); - } - - } catch (IOException ioe) { - ioe.printStackTrace(); - throw new BuildException("Error checking out: " + pack ); - } catch (InterruptedException ex) { - } + run(sb.toString()); } public void setCvsRoot(String root) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Exec.java b/src/main/org/apache/tools/ant/taskdefs/Exec.java index 37c1e6507..0818e8fbe 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Exec.java +++ b/src/main/org/apache/tools/ant/taskdefs/Exec.java @@ -69,6 +69,8 @@ public class Exec extends Task { private String dir; private String command; + private static final int BUFFER_SIZE = 512; + public void execute() throws BuildException { // test if os match String myos = System.getProperty("os.name"); @@ -103,13 +105,22 @@ public class Exec extends Task { fos=new PrintWriter( new FileWriter( out ) ); project.log("Output redirected to " + out, Project.MSG_VERBOSE); } - pipeOutput(proc.getInputStream(), "exec", fos); - pipeOutput(proc.getErrorStream(), "error", fos); - if (null != fos) - fos.close(); + // copy input and error to the output stream + StreamPumper inputPumper = + new StreamPumper(proc.getInputStream(), "exec", project, fos); + StreamPumper errorPumper = + new StreamPumper(proc.getErrorStream(), "error", project, fos); + + inputPumper.start(); + errorPumper.start(); + + // Wait for everything to finish proc.waitFor(); - + inputPumper.join(); + errorPumper.join(); + proc.destroy(); + // close the output file if required if (fos != null) fos.close(); @@ -140,20 +151,54 @@ public class Exec extends Task { this.out = out; } - private void pipeOutput(InputStream is, String name, PrintWriter fos) - throws IOException - { -project.log("pipeOutput", name, Project.MSG_INFO); - InputStreamReader isr=new InputStreamReader(is); - BufferedReader din = new BufferedReader(isr); - - // pipe output to STDOUT - String line; - while((line = din.readLine()) != null) { - if( fos==null) - project.log(line, name, Project.MSG_INFO); - else - fos.println(line); - } + // Inner class for continually pumping the input stream during + // Process's runtime. + class StreamPumper extends Thread { + private BufferedReader din; + private String name; + private boolean endOfStream = false; + private int SLEEP_TIME = 5; + private Project project; + private PrintWriter fos; + + public StreamPumper(InputStream is, String name, Project project, PrintWriter fos) { + this.din = new BufferedReader(new InputStreamReader(is)); + this.name = name; + this.project = project; + this.fos = fos; + } + + public void pumpStream() + throws IOException + { + byte[] buf = new byte[BUFFER_SIZE]; + if (!endOfStream) { + String line = din.readLine(); + + if (line != null) { + if (fos==null) + project.log(line, name, Project.MSG_INFO); + else + fos.println(line); + } else { + endOfStream=true; + } + } + } + + public void run() { + try { + try { + while (!endOfStream) { + pumpStream(); + sleep(SLEEP_TIME); + } + } catch (InterruptedException ie) { + } + din.close(); + } catch (IOException ioe) { + } + } } + }