fix for FELIX-1403


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@797111 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
index 7c17b12..7e5c920 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
@@ -54,9 +54,12 @@
 
             if (pipes.isEmpty())
             {
-                current.setIn(session.in);
-                current.setOut(session.out);
-                current.setErr(session.err);    // XXX: derek.baum@paremus.com
+		if (current.out == null)
+		{
+		    current.setIn(session.in);
+		    current.setOut(session.out);
+		    current.setErr(session.err);
+		}
             }
             else
             {
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
index 3b1b253..ed92238 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
@@ -16,19 +16,25 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-// DWB16: redirect System.err when creating pipe
 package org.apache.felix.gogo.runtime.shell;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.util.List;
+
 import org.osgi.service.command.Converter;
 
-import java.io.*;
-import java.util.List;
-
 public class Pipe extends Thread
 {
+    static final ThreadLocal<InputStream> tIn = new ThreadLocal<InputStream>();
+    static final ThreadLocal<PrintStream> tOut = new ThreadLocal<PrintStream>();
+    static final ThreadLocal<PrintStream> tErr = new ThreadLocal<PrintStream>();
     InputStream in;
     PrintStream out;
-    PrintStream err;    // derek
+    PrintStream err;
     PipedOutputStream pout;
     Closure closure;
     Exception exception;
@@ -40,6 +46,10 @@
         super("pipe-" + statements);
         this.closure = closure;
         this.statements = statements;
+
+        in = tIn.get();
+        out = tOut.get();
+        err = tErr.get();
     }
 
     public void setIn(InputStream in)
@@ -65,13 +75,15 @@
         next.setIn(new PipedInputStream(pout));
         out = new PrintStream(pout);
         return next;
-
     }
 
     public void run()
     {
-        //closure.session.service.threadIO.setStreams(in, out, System.err);
-        closure.session.service.threadIO.setStreams(in, out, err);    // derek
+        tIn.set(in);
+        tOut.set(out);
+        tErr.set(err);
+        closure.session.service.threadIO.setStreams(in, out, err);
+
         try
         {
             for (List<CharSequence> statement : statements)
@@ -91,6 +103,10 @@
         {
             out.flush();
             closure.session.service.threadIO.close();
+            tIn.set(in);
+            tOut.set(out);
+            tErr.set(err);
+
             try
             {
                 if (in instanceof PipedInputStream)
diff --git a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
index 3e0e27b..df6018f 100644
--- a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
+++ b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/TestParser.java
@@ -85,7 +85,11 @@
         c.addCommand("echo", this);
         c.addCommand("capture", this);
         c.addCommand("grep", this);
+        c.addCommand("echoout", this);
+        c.execute("myecho = { echoout $args }");
         assertEquals("def", c.execute("echo def|grep (d.*)|capture"));
+        assertEquals("def", c.execute("echoout def|grep (d.*)|capture"));
+        assertEquals("def", c.execute("myecho def|grep (d.*)|capture"));
         assertEquals("def", c.execute("echo abc; echo def; echo ghi|grep (d.*)|capture"));
         assertEquals("hello world", c.execute("echo hello world|capture"));
         assertEquals("defghi", c.execute("echo abc; echo def; echo ghi|grep (def|ghi)|capture"));
@@ -241,6 +245,11 @@
         return sb;
     }
 
+    public void echoout(Object args[])
+    {
+        System.out.println(echo(args));
+    }
+
     public void testContext() throws Exception
     {
         Context c = new Context();