Fix the session input stream which should use the console input stream

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@792544 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/Console.java b/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/Console.java
index 24b5716..22bc4dd 100644
--- a/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/Console.java
+++ b/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/Console.java
@@ -21,6 +21,7 @@
 import jline.*;
 import org.osgi.service.command.CommandSession;
 import org.osgi.service.command.Converter;
+import org.osgi.service.command.CommandProcessor;
 import org.apache.felix.karaf.gshell.console.ansi.AnsiOutputStream;
 import org.apache.felix.karaf.gshell.console.Completer;
 
@@ -28,6 +29,7 @@
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.PrintWriter;
+import java.io.PrintStream;
 import java.lang.reflect.Method;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
@@ -47,26 +49,30 @@
     private boolean running;
     private Runnable closeCallback;
     private Terminal terminal;
+    private InputStream consoleInput;
+    private InputStream in;
+    private PrintStream out;
+    private PrintStream err;
 
-    public Console(CommandSession session, Terminal term) throws Exception
+    public Console(CommandProcessor processor,
+                   InputStream in,
+                   PrintStream out,
+                   PrintStream err,
+                   Terminal term,
+                   Completer completer,
+                   Runnable closeCallback) throws Exception
     {
-        this(session, term, null);
-    }
-
-    public Console(CommandSession session, Terminal term, Completer completer) throws Exception
-    {
-        this(session, term, completer, null);
-    }
-
-    public Console(CommandSession session, Terminal term, Completer completer, Runnable closeCallback) throws Exception
-    {
-        this.session = session;
+        this.in = in;
+        this.out = out;
+        this.err = err;
+        this.consoleInput = new ConsoleInputStream();
+        this.session = processor.createSession(this.consoleInput, this.out, this.err);
         this.terminal = term == null ? new UnsupportedTerminal() : term;
         this.closeCallback = closeCallback;
-        reader = new ConsoleReader(new ConsoleInputStream(),
-                                   new PrintWriter(session.getConsole()),
+        reader = new ConsoleReader(this.consoleInput,
+                                   new PrintWriter(this.out),
                                    getClass().getResourceAsStream("keybinding.properties"),
-                                   terminal);
+                                   this.terminal);
         if (completer != null) {
             reader.addCompletor(new CompleterAsCompletor(completer));
         }
@@ -75,6 +81,10 @@
         pipe.setDaemon(true);
     }
 
+    public CommandSession getSession() {
+        return session;
+    }
+
     public void close() {
         //System.err.println("Closing");
         running = false;
@@ -217,7 +227,6 @@
         public void run()
         {
             try {
-                InputStream in = session.getKeyboard();
                 while (running)
                 {
                     try
@@ -226,13 +235,13 @@
                         if (c == -1 || c == 4)
                         {
                             //System.err.println("Received  " + c + " ... closing");
-                            session.getConsole().println("^D");
+                            err.println("^D");
                             queue.put(c);
                             return;
                         }
                         else if (c == 3)
                         {
-                            session.getConsole().println("^C");
+                            err.println("^C");
                             reader.getCursorBuffer().clearBuffer();
                             interrupt();
                             queue.put(c);
diff --git a/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/ConsoleFactory.java b/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/ConsoleFactory.java
index fcce928..81373eb 100644
--- a/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/ConsoleFactory.java
+++ b/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/ConsoleFactory.java
@@ -71,10 +71,6 @@
             InputStream in = unwrap(System.in);
             PrintStream out = unwrap(System.out);
             PrintStream err = unwrap(System.err);
-            CommandSession session = this.commandProcessor.createSession(
-                    in, new PrintStream(new AnsiOutputStream(out)), new PrintStream(new AnsiOutputStream(err)));
-            session.put("USER", "karaf");
-            session.put("APPLICATION", System.getProperty("karaf.name", "root"));
             Runnable callback = new Runnable() {
                 public void run() {
                     try {
@@ -84,7 +80,10 @@
                     }
                 }
             };
-            this.console = new Console(session, terminal, new AggregateCompleter(completers), callback);
+            this.console = new Console(commandProcessor, in, out, err, terminal, new AggregateCompleter(completers), callback);
+            CommandSession session = console.getSession();
+            session.put("USER", "karaf");
+            session.put("APPLICATION", System.getProperty("karaf.name", "root"));
             new Thread(console, "Karaf Shell Console Thread").start();
         }
     }
diff --git a/karaf/gshell/gshell-ssh/src/main/java/org/apache/felix/karaf/gshell/ssh/ShellFactoryImpl.java b/karaf/gshell/gshell-ssh/src/main/java/org/apache/felix/karaf/gshell/ssh/ShellFactoryImpl.java
index 5c4ff98..a4a2820 100644
--- a/karaf/gshell/gshell-ssh/src/main/java/org/apache/felix/karaf/gshell/ssh/ShellFactoryImpl.java
+++ b/karaf/gshell/gshell-ssh/src/main/java/org/apache/felix/karaf/gshell/ssh/ShellFactoryImpl.java
@@ -86,12 +86,10 @@
 
         public void start(final Environment env) throws IOException {
             try {
-                CommandSession session = commandProcessor.createSession(in, new PrintStream(out), new PrintStream(err));
-                session.put("APPLICATION", System.getProperty("karaf.name", "root"));
-                for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
-                    session.put(e.getKey(), e.getValue());
-                }
-                Console console = new Console(session,
+                Console console = new Console(commandProcessor,
+                                              in,
+                                              new PrintStream(out),
+                                              new PrintStream(err),
                                               new SshTerminal(env),
                                               new AggregateCompleter(completers),
                                               new Runnable() {
@@ -99,6 +97,11 @@
                                                       destroy();
                                                   }
                                               });
+                CommandSession session = console.getSession();
+                session.put("APPLICATION", System.getProperty("karaf.name", "root"));
+                for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
+                    session.put(e.getKey(), e.getValue());
+                }
                 new Thread(console).start();
             } catch (Exception e) {
                 throw (IOException) new IOException("Unable to start shell").initCause(e);