Browse Source

Simultaneously consume both the stdout and stderr during exec calls

(StreamPumper code based on org.apache.jasper.compiler.JikesJavaCompiler)


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267562 13f79535-47bb-0310-9956-ffa450edef68
master
Sam Ruby 25 years ago
parent
commit
a037ae329b
3 changed files with 76 additions and 60 deletions
  1. +1
    -1
      src/bin/antRun
  2. +10
    -39
      src/main/org/apache/tools/ant/taskdefs/Cvs.java
  3. +65
    -20
      src/main/org/apache/tools/ant/taskdefs/Exec.java

+ 1
- 1
src/bin/antRun View File

@@ -11,4 +11,4 @@ if test -e $CMD.sh; then
fi

echo $CMD $@
$CMD $@ 2>&1
$CMD $@

+ 10
- 39
src/main/org/apache/tools/ant/taskdefs/Cvs.java View File

@@ -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) {


+ 65
- 20
src/main/org/apache/tools/ant/taskdefs/Exec.java View File

@@ -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) {
}
}
}

}

Loading…
Cancel
Save