From 8557fcb14481e120e8cd857ce225363b238574c9 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 6 Jan 2009 17:27:01 +0000 Subject: [PATCH] tiny refactoring. Allow JUnit task to be less concerned about formatters (right now formatters could close System.out/err unless they guard against it). git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@732010 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/SQLExec.java | 3 +- .../optional/junit/FormatterElement.java | 6 +++- .../ant/taskdefs/optional/ssh/SSHExec.java | 4 ++- .../tools/ant/util/KeepAliveInputStream.java | 10 +++++++ .../tools/ant/util/KeepAliveOutputStream.java | 28 +++++++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 27371dace..3445d483a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -567,8 +567,7 @@ public class SQLExec extends JDBCTask { } try { - PrintStream out = - new PrintStream(new KeepAliveOutputStream(System.out)); + PrintStream out = KeepAliveOutputStream.wrapSystemOut(); try { if (output != null) { log("Opening PrintStream to output Resource " + output, Project.MSG_VERBOSE); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java index 2a8a977ec..bed93335e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java @@ -29,6 +29,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.util.KeepAliveOutputStream; /** *

A wrapper for the implementations of JUnitResultFormatter. @@ -57,7 +58,7 @@ public class FormatterElement { private String classname; private String extension; - private OutputStream out = System.out; + private OutputStream out = new KeepAliveOutputStream(System.out); private File outFile; private boolean useFile = true; private String ifProperty; @@ -170,6 +171,9 @@ public class FormatterElement { * @param out the output stream to use. */ public void setOutput(OutputStream out) { + if (out == System.out || out == System.err) { + out = new KeepAliveOutputStream(out); + } this.out = out; } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java index 72aae2ba3..cb6174d31 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java @@ -224,7 +224,9 @@ public class SSHExec extends SSHBase { private void executeCommand(Session session, String cmd, StringBuffer sb) throws BuildException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out)); + TeeOutputStream tee = + new TeeOutputStream(out, + KeepAliveOutputStream.wrapSystemOut()); InputStream istream = null ; if (inputFile != null) { diff --git a/src/main/org/apache/tools/ant/util/KeepAliveInputStream.java b/src/main/org/apache/tools/ant/util/KeepAliveInputStream.java index cc79d6727..ac5919710 100644 --- a/src/main/org/apache/tools/ant/util/KeepAliveInputStream.java +++ b/src/main/org/apache/tools/ant/util/KeepAliveInputStream.java @@ -54,4 +54,14 @@ public class KeepAliveInputStream extends FilterInputStream { public void close() throws IOException { // do not close the stream } + + /** + * Convenience factory method that returns a non-closing + * InputStream around System.in. + * + * @since Ant 1.8.0 + */ + public static InputStream wrapSystemIn() { + return new KeepAliveInputStream(System.in); + } } diff --git a/src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java b/src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java index 0437a1f04..4e437e906 100644 --- a/src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java +++ b/src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java @@ -20,6 +20,7 @@ package org.apache.tools.ant.util; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintStream; /** * Class that can be used to wrap System.out and System.err @@ -53,4 +54,31 @@ public class KeepAliveOutputStream extends FilterOutputStream { public void close() throws IOException { // do not close the stream } + + /** + * Convenience factory method that returns a non-closing + * PrintStream around System.out. + * + * @since Ant 1.8.0 + */ + public static PrintStream wrapSystemOut() { + return wrap(System.out); + } + + /** + * Convenience factory method that returns a non-closing + * PrintStream around System.err. + * + * @since Ant 1.8.0 + */ + public static PrintStream wrapSystemErr() { + return wrap(System.err); + } + + /** + * @since Ant 1.8.0 + */ + private static PrintStream wrap(PrintStream ps) { + return new PrintStream(new KeepAliveOutputStream(ps)); + } }