You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by io...@apache.org on 2012/06/10 18:57:57 UTC

svn commit: r1348624 - /karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java

Author: iocanel
Date: Sun Jun 10 16:57:56 2012
New Revision: 1348624

URL: http://svn.apache.org/viewvc?rev=1348624&view=rev
Log:
[KARAF-1529] Added supprot for aborting a watch task when a users stops the command-watch.

Modified:
    karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java

Modified: karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java?rev=1348624&r1=1348623&r2=1348624&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java (original)
+++ karaf/branches/karaf-2.2.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/WatchAction.java Sun Jun 10 16:57:56 2012
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.PrintStream;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
@@ -57,6 +58,7 @@ public class WatchAction extends Abstrac
             executorService.scheduleAtFixedRate(watchTask, 0, interval, TimeUnit.SECONDS);
             try {
                 session.getKeyboard().read();
+                watchTask.abort();
             } finally {
                 executorService.shutdownNow();
                 watchTask.close();
@@ -73,6 +75,7 @@ public class WatchAction extends Abstrac
         CommandSession session;
         ByteArrayOutputStream byteArrayOutputStream = null;
         PrintStream printStream = null;
+        boolean doDisplay = true;
 
         public WatchTask(CommandProcessor processor, String command) {
             this.processor = processor;
@@ -81,16 +84,19 @@ public class WatchAction extends Abstrac
 
         public void run() {
             try {
-                 byteArrayOutputStream = new ByteArrayOutputStream();
+                byteArrayOutputStream = new ByteArrayOutputStream();
                 printStream = new PrintStream(byteArrayOutputStream);
                 session = commandProcessor.createSession(null, printStream, printStream);
                 String output = "";
                 session.execute(command);
                 output = byteArrayOutputStream.toString();
-                System.out.print("\33[2J");
-                System.out.print("\33[1;1H");
-                System.out.print(output);
-                System.out.flush();
+                //Make sure before displaying that this is not a forgotten long running task.
+                if (doDisplay) {
+                    System.out.print("\33[2J");
+                    System.out.print("\33[1;1H");
+                    System.out.print(output);
+                    System.out.flush();
+                }
                 byteArrayOutputStream.close();
                 session.close();
             } catch (Exception e) {
@@ -98,6 +104,18 @@ public class WatchAction extends Abstrac
             }
         }
 
+        /**
+         * Aborts the task that it currently running.
+         * It will prevent it from displaying anything.
+         */
+        public void abort() {
+           doDisplay = false;
+        }
+
+        /**
+         * Closes the the {@CommandSession} and the streams for I/O.
+         * @throws IOException
+         */
         public void close() throws IOException {
             if (this.session != null) {
                 this.session.close();