remote: provide output listener for command execution
authorBernd Hufmann <Bernd.Hufmann@ericsson.com>
Thu, 26 Nov 2015 12:20:45 +0000 (07:20 -0500)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Thu, 3 Dec 2015 22:57:21 +0000 (17:57 -0500)
This listener interface will allow users of the ICommandShell to provide
2 callback methods to get intermediate results for the standard and
error output. Useful for long lasting command executions.

Change-Id: Ibc90bfd679226eee7ef34f170b63abd819bb75e5
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/61586
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
common/org.eclipse.tracecompass.common.core/annotations/java/lang/String.eea
lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/stubs/org/eclipse/tracecompass/internal/lttng2/control/stubs/shells/LTTngToolsFileShell.java
tmf/org.eclipse.tracecompass.tmf.remote.core.tests/stubs/org/eclipse/tracecompass/internal/tmf/remote/core/stubs/shells/TestCommandShell.java
tmf/org.eclipse.tracecompass.tmf.remote.core/META-INF/MANIFEST.MF
tmf/org.eclipse.tracecompass.tmf.remote.core/pom.xml
tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/internal/tmf/remote/core/shell/CommandShell.java
tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/internal/tmf/remote/core/shell/InputReader.java
tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/tmf/remote/core/shell/ICommandOutputListener.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/tmf/remote/core/shell/ICommandShell.java

index 328ef38b149dc5015a990195637856a226132eb4..b89055daf811f4b1fd89f16acff8b87bc423b5ed 100644 (file)
@@ -41,3 +41,6 @@ valueOf
 valueOf
  ([C)Ljava/lang/String;
  ([C)L1java/lang/String;
+valueOf
+ ([CII)Ljava/lang/String;
+ ([CII)L1java/lang/String;
index 391a4b831c60791e1a6afb58620a6498f39f76ae..ffd8e119a85bdc1f212c48a98f49278378dc7ffd 100644 (file)
@@ -32,6 +32,7 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.tracecompass.internal.tmf.remote.core.stubs.shells.TestCommandShell;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
+import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandOutputListener;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
 
 @SuppressWarnings("javadoc")
@@ -246,6 +247,11 @@ public class LTTngToolsFileShell extends TestCommandShell {
 
     @Override
     public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException {
+        return executeCommand(command, monitor, null);
+    }
+
+    @Override
+    public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor, ICommandOutputListener listener) throws ExecutionException {
         Map<String, ICommandResult> commands = checkNotNull(fScenarioMap.get(fScenario));
         String commandLine = command.toString();
         String fullCommand = commandLine;
index 3522b4336a292998af7fd67b36227fb408f04ee7..63d94f4d75164678091a476eae7dbad63a8c34a3 100644 (file)
@@ -18,6 +18,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.tracecompass.internal.tmf.remote.core.shell.CommandInput;
 import org.eclipse.tracecompass.internal.tmf.remote.core.shell.CommandResult;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
+import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandOutputListener;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
 
@@ -36,6 +37,11 @@ public class TestCommandShell implements ICommandShell {
 
     @Override
     public ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException {
+        return executeCommand(command, monitor, null);
+    }
+
+    @Override
+    public ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor, ICommandOutputListener listener) throws ExecutionException {
         if (fIsConnected) {
             return createCommandResult(0, new String[0], new String[0]);
         }
index f929afaff644f5885bd774ad8e01112195763912..5bdc5c95ab04a779ea6571c8e941346524292741 100644 (file)
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name
 Bundle-Vendor: %Bundle-Vendor
-Bundle-Version: 1.0.0.qualifier
+Bundle-Version: 2.0.0.qualifier
 Bundle-Localization: plugin
 Bundle-SymbolicName: org.eclipse.tracecompass.tmf.remote.core;singleton:=true
 Bundle-Activator: org.eclipse.tracecompass.internal.tmf.remote.core.Activator
index 765d8fa7fee87b973acde0c20fc146f540081d3b..a62892cb9f643ca0101cb649b22ba78d3f656daf 100644 (file)
@@ -18,7 +18,7 @@
   </parent>
 
   <artifactId>org.eclipse.tracecompass.tmf.remote.core</artifactId>
-  <version>1.0.0-SNAPSHOT</version>
+  <version>2.0.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 
   <name>Trace Compass TMF Remote Core Plug-in</name>
index 5bea2d0e4ffa02eb4636e8578400f181d12cc489..82b0911fcbccf42e325ce4f8de4025890b1d6ec6 100644 (file)
@@ -36,6 +36,7 @@ import org.eclipse.remote.core.IRemoteProcessService;
 import org.eclipse.tracecompass.internal.tmf.remote.core.messages.Messages;
 import org.eclipse.tracecompass.internal.tmf.remote.core.preferences.TmfRemotePreferences;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
+import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandOutputListener;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
 
@@ -84,6 +85,11 @@ public class CommandShell implements ICommandShell {
 
     @Override
     public ICommandResult executeCommand(final ICommandInput command, final IProgressMonitor aMonitor) throws ExecutionException {
+        return executeCommand(command, aMonitor, null);
+    }
+
+    @Override
+    public ICommandResult executeCommand(final ICommandInput command, final IProgressMonitor aMonitor, ICommandOutputListener listener) throws ExecutionException {
         if (fConnection.isOpen()) {
             FutureTask<CommandResult> future = new FutureTask<>(new Callable<CommandResult>() {
                 @Override
@@ -98,8 +104,8 @@ public class CommandShell implements ICommandShell {
                             return new CommandResult(1, new @NonNull String[0], new @NonNull String[] { nullToEmptyString(Messages.RemoteConnection_ServiceNotDefined) });
                         }
                         IRemoteProcess process = service.getProcessBuilder(command.getInput()).start();
-                        InputReader stdout = new InputReader(checkNotNull(process.getInputStream()));
-                        InputReader stderr = new InputReader(checkNotNull(process.getErrorStream()));
+                        InputReader stdout = new InputReader(checkNotNull(process.getInputStream()), listener, true);
+                        InputReader stderr = new InputReader(checkNotNull(process.getErrorStream()), listener, false);
 
                         try {
                             stdout.waitFor(monitor);
index 924e09811e61b33c58264eebacccbe41b59d3691..b6dbecb82b2c3adc9a6d185727d3fd24a2d65d98 100644 (file)
@@ -19,6 +19,8 @@ import java.io.InputStreamReader;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandOutputListener;
 
 @NonNullByDefault
 class InputReader {
@@ -30,7 +32,7 @@ class InputReader {
     private final StringBuilder fResult;
     private volatile boolean fDone;
 
-    public InputReader(InputStream inputStream) {
+    public InputReader(InputStream inputStream, @Nullable final ICommandOutputListener listener, final boolean isStdOut) {
         fResult = new StringBuilder();
         fReader = new InputStreamReader(inputStream);
         fThread = new Thread() {
@@ -41,6 +43,13 @@ class InputReader {
                 try {
                     while (!fDone && (read = fReader.read(buffer)) > 0) {
                         fResult.append(buffer, 0, read);
+                        if (listener != null) {
+                            if (isStdOut) {
+                                listener.outputUpdated(String.valueOf(buffer, 0, read));
+                            } else {
+                                listener.errorOutputUpdated(String.valueOf(buffer, 0, read));
+                            }
+                        }
                     }
                 } catch (IOException e) {
                 }
diff --git a/tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/tmf/remote/core/shell/ICommandOutputListener.java b/tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/tmf/remote/core/shell/ICommandOutputListener.java
new file mode 100644 (file)
index 0000000..492ef0f
--- /dev/null
@@ -0,0 +1,40 @@
+/**********************************************************************
+ * Copyright (c) 2015 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ **********************************************************************/
+package org.eclipse.tracecompass.tmf.remote.core.shell;
+
+/**
+ * Interface for a providing a command output listener
+ *
+ * @author Bernd Hufmann
+ * @since 2.0
+ */
+public interface ICommandOutputListener {
+
+    /**
+     * Call back with new output String. It will provide the delta String
+     * in comparison to the previous call of this method.
+     *
+     * The implementer of this method must not block the current thread.
+     *
+     * @param updatedString
+     *            The new outputString
+     */
+    void outputUpdated(String updatedString);
+
+    /**
+     * Call back with new error output String. It will provide the delta String
+     * in comparison to the previous call of this method.
+     *
+     * The implementer of this method must not block the current thread.
+     *
+     * @param updatedString
+     *            The new outputString
+     */
+    void errorOutputUpdated(String updatedString);
+}
index c72b70f5bcbc2ba3febf1f5d28cc6b70d36512a0..fb9dd340b3970fa50ed31a0e1dce9d9e5834388b 100644 (file)
@@ -41,6 +41,22 @@ public interface ICommandShell {
      */
     ICommandResult executeCommand(ICommandInput command, @Nullable IProgressMonitor monitor) throws ExecutionException;
 
+    /**
+     * Method to execute a command on the command shell.
+     *
+     * @param command
+     *            The command to executed
+     * @param monitor
+     *            A progress monitor
+     * @param listener
+     *            A command output listener (can be null if not used)
+     * @return a {@link ICommandResult} instance
+     * @throws ExecutionException
+     *             If the command fails
+     * @since 2.0
+     */
+    ICommandResult executeCommand(ICommandInput command, @Nullable IProgressMonitor monitor, @Nullable ICommandOutputListener listener) throws ExecutionException;
+
     /**
      * Creates a command input instance
      *
This page took 0.028993 seconds and 5 git commands to generate.