1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated using Executor Framework
12 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
13 * Bernd Hufmann - Update to org.eclipse.remote API 2.0
14 **********************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
;
17 import java
.io
.IOException
;
18 import java
.util
.List
;
19 import java
.util
.concurrent
.Callable
;
20 import java
.util
.concurrent
.ExecutorService
;
21 import java
.util
.concurrent
.Executors
;
22 import java
.util
.concurrent
.FutureTask
;
23 import java
.util
.concurrent
.TimeUnit
;
24 import java
.util
.concurrent
.TimeoutException
;
26 import org
.eclipse
.core
.commands
.ExecutionException
;
27 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
29 import org
.eclipse
.remote
.core
.IRemoteConnection
;
30 import org
.eclipse
.remote
.core
.IRemoteProcess
;
31 import org
.eclipse
.remote
.core
.IRemoteProcessService
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.messages
.Messages
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.preferences
.TmfRemotePreferences
;
37 * Implementation of remote command execution using IRemoteConnection.
40 * @author Patrick Tasse
41 * @author Bernd Hufmann
43 public class CommandShell
implements ICommandShell
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
48 private IRemoteConnection fConnection
= null;
49 private final ExecutorService fExecutor
= Executors
.newFixedThreadPool(1);
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Create a new command shell
58 * @param connection the remote connection for this shell
60 public CommandShell(IRemoteConnection connection
) {
61 fConnection
= connection
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 public void connect() throws ExecutionException
{
73 public void disconnect() {
78 public ICommandResult
executeCommand(final List
<String
> command
, final IProgressMonitor monitor
) throws ExecutionException
{
79 if (fConnection
.isOpen()) {
80 FutureTask
<CommandResult
> future
= new FutureTask
<>(new Callable
<CommandResult
>() {
82 public CommandResult
call() throws IOException
, InterruptedException
{
83 if (monitor
== null || !monitor
.isCanceled()) {
84 IRemoteProcess process
= fConnection
.getService(IRemoteProcessService
.class).getProcessBuilder(command
).start();
85 InputReader stdout
= new InputReader(process
.getInputStream());
86 InputReader stderr
= new InputReader(process
.getErrorStream());
89 stdout
.waitFor(monitor
);
90 stderr
.waitFor(monitor
);
91 if (monitor
== null || !monitor
.isCanceled()) {
92 return createResult(process
.waitFor(), stdout
.toString(), stderr
.toString());
94 } catch (OperationCanceledException e
) {
95 } catch (InterruptedException e
) {
96 return new CommandResult(1, new String
[0], new String
[] {e
.getMessage()});
103 return new CommandResult(1, new String
[0], new String
[] {"cancelled"}); //$NON-NLS-1$
107 fExecutor
.execute(future
);
110 return future
.get(TmfRemotePreferences
.getCommandTimeout(), TimeUnit
.SECONDS
);
111 } catch (java
.util
.concurrent
.ExecutionException ex
) {
112 throw new ExecutionException(Messages
.TraceControl_ExecutionFailure
, ex
);
113 } catch (InterruptedException ex
) {
114 throw new ExecutionException(Messages
.TraceControl_ExecutionCancelled
, ex
);
115 } catch (TimeoutException ex
) {
116 throw new ExecutionException(Messages
.TraceControl_ExecutionTimeout
, ex
);
121 throw new ExecutionException(Messages
.TraceControl_ShellNotConnected
, null);
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 private static CommandResult
createResult(int origResult
, String origStdout
, String origStderr
) {
130 final String stdout
, stderr
;
134 String
[] output
= splitLines(stdout
);
135 String
[] error
= splitLines(stderr
);
136 return new CommandResult(result
, output
, error
);
139 private static String
[] splitLines(String output
) {
140 if (output
== null) {
143 return output
.split("\\r?\\n"); //$NON-NLS-1$