Bug 570647 - Use CommandSession console for confirm messages

The streams gogo uses for System.out end up buffering the output
even when System.out.flush() is called.  This appears to be done to
support the piping in gogo.  If a message needs to be printed which
bypasses the piping then the CommandSession.getConsole() PrintStream
should be used.

Change-Id: I1919a48e1bf78133c506d3ffece6bec6a53dd344
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/DisconnectCommand.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/DisconnectCommand.java
index 967b49c..d87f9be 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/DisconnectCommand.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/DisconnectCommand.java
@@ -16,11 +16,14 @@
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.PrintStream;
 import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.felix.service.command.CommandProcessor;
 import org.apache.felix.service.command.CommandSession;
+import org.eclipse.equinox.console.telnet.TelnetConnection;
+
 import java.io.Closeable;
 import org.osgi.framework.BundleContext;
 
@@ -28,7 +31,6 @@
  * This class implements functionality to disconnect from telnet or ssh console.
  */
 public class DisconnectCommand {
-	private static final String CLOSEABLE = "CLOSEABLE";
 	private static final String DISCONNECT_MESSAGE = "Disconnect from console? (y/n; default=y) ";
 	private static final String DISCONNECT_CONFIRMATION_Y = "y";
 	
@@ -46,20 +48,21 @@
 	}
 	
 	public void disconnect(CommandSession session) {
-		System.out.print(DISCONNECT_MESSAGE);
-		System.out.flush();
+		PrintStream consoleStream = session.getConsole();
+		consoleStream.print(DISCONNECT_MESSAGE);
+		consoleStream.flush();
 		
 		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 		String reply = null;
 		try {
 			reply = reader.readLine();
 		} catch (IOException e) {
-			System.out.println("Error while reading confirmation");
+			consoleStream.println("Error while reading confirmation");
 		}
 		
 		if (reply != null) {
 			if (reply.toLowerCase().startsWith(DISCONNECT_CONFIRMATION_Y) || reply.length() == 0) {
-				Closeable closable = (Closeable)session.get(CLOSEABLE);
+				Closeable closable = (Closeable)session.get(TelnetConnection.CLOSEABLE);
 				if (closable != null) {
 					try {
 						closable.close();
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
index c835dbd..1304ab3 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
@@ -19,6 +19,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.PrintStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URL;
@@ -40,6 +41,7 @@
 import java.util.TreeSet;
 
 import org.apache.felix.service.command.CommandProcessor;
+import org.apache.felix.service.command.CommandSession;
 import org.apache.felix.service.command.Converter;
 import org.apache.felix.service.command.Descriptor;
 import org.apache.felix.service.command.Parameter;
@@ -197,8 +199,8 @@
 	 *  Handle the exit command.  Exit immediately (System.exit)
 	 */
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_EXIT_COMMAND_DESCRIPTION)
-	public void exit() throws Exception {
-		if (confirmStop()) {
+	public void exit(CommandSession session) throws Exception {
+		if (confirmStop(session)) {
 			System.out.println();
 			System.exit(0);
 		}
@@ -1084,8 +1086,8 @@
 
 	 */
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_CLOSE_COMMAND_DESCRIPTION)
-	public void close() throws Exception {
-		if (confirmStop()) {
+	public void close(CommandSession session) throws Exception {
+		if (confirmStop(session)) {
 			context.getBundle(0).stop();
 		}
 	}
@@ -1992,16 +1994,17 @@
 
 	}
 
-	private boolean confirmStop() {
-		System.out.print(ConsoleMsg.CONSOLE_STOP_MESSAGE);
-		System.out.flush();
+	private boolean confirmStop(CommandSession session) {
+		PrintStream consoleStream = session.getConsole();
+		consoleStream.print(ConsoleMsg.CONSOLE_STOP_MESSAGE);
+		consoleStream.flush();
 
 		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 		String reply = null;
 		try {
 			reply = reader.readLine();
 		} catch (IOException e) {
-			System.out.println(ConsoleMsg.CONSOLE_STOP_ERROR_READ_CONFIRMATION);
+			consoleStream.println(ConsoleMsg.CONSOLE_STOP_ERROR_READ_CONFIRMATION);
 		}
 
 		if (reply != null) {
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/telnet/TelnetConnection.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/telnet/TelnetConnection.java
index aa5a829..3f92343 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/telnet/TelnetConnection.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/telnet/TelnetConnection.java
@@ -44,8 +44,8 @@
 	private static final String OSGI_PROMPT = "osgi> ";
 	private static final String SCOPE = "SCOPE";
 	private static final String EQUINOX_SCOPE = "equinox:*";
-	private static final String CLOSEABLE = "CLOSEABLE";
-	
+	public static final String CLOSEABLE = "CLOSEABLE";
+
 	public TelnetConnection (Socket socket, CommandProcessor processor, BundleContext context) {
 		this.socket = socket;
 		this.processor = processor;