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
.internal
.tmf
.remote
.core
.shell
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.IOException
;
20 import java
.util
.concurrent
.Callable
;
21 import java
.util
.concurrent
.ExecutorService
;
22 import java
.util
.concurrent
.Executors
;
23 import java
.util
.concurrent
.FutureTask
;
24 import java
.util
.concurrent
.TimeUnit
;
25 import java
.util
.concurrent
.TimeoutException
;
27 import org
.eclipse
.core
.commands
.ExecutionException
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
30 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
31 import org
.eclipse
.jdt
.annotation
.NonNull
;
32 import org
.eclipse
.remote
.core
.IRemoteConnection
;
33 import org
.eclipse
.remote
.core
.IRemoteProcess
;
34 import org
.eclipse
.remote
.core
.IRemoteProcessService
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.messages
.Messages
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.preferences
.TmfRemotePreferences
;
37 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandInput
;
38 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandResult
;
39 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandShell
;
43 * Implementation of remote command execution using IRemoteConnection.
46 * @author Patrick Tasse
47 * @author Bernd Hufmann
49 public class CommandShell
implements ICommandShell
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
54 private final IRemoteConnection fConnection
;
55 private final ExecutorService fExecutor
= checkNotNull(Executors
.newFixedThreadPool(1));
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * Create a new command shell
64 * @param connection the remote connection for this shell
66 public CommandShell(IRemoteConnection connection
) {
67 fConnection
= connection
;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 public void dispose() {
80 public ICommandInput
createCommand() {
81 return new CommandInput();
85 public ICommandResult
executeCommand(final ICommandInput command
, final IProgressMonitor aMonitor
) throws ExecutionException
{
86 if (fConnection
.isOpen()) {
87 FutureTask
<CommandResult
> future
= new FutureTask
<>(new Callable
<CommandResult
>() {
89 public CommandResult
call() throws IOException
, InterruptedException
{
90 IProgressMonitor monitor
= aMonitor
;
91 if (monitor
== null) {
92 monitor
= new NullProgressMonitor();
94 if (!monitor
.isCanceled()) {
95 IRemoteProcess process
= fConnection
.getService(IRemoteProcessService
.class).getProcessBuilder(command
.getInput()).start();
96 InputReader stdout
= new InputReader(checkNotNull(process
.getInputStream()));
97 InputReader stderr
= new InputReader(checkNotNull(process
.getErrorStream()));
100 stdout
.waitFor(monitor
);
101 stderr
.waitFor(monitor
);
102 if (!monitor
.isCanceled()) {
103 return createResult(process
.waitFor(), stdout
.toString(), stderr
.toString());
105 } catch (OperationCanceledException e
) {
106 } catch (InterruptedException e
) {
107 return new CommandResult(1, new String
[0], new String
[] {e
.getMessage()});
114 return new CommandResult(1, new String
[0], new String
[] {"cancelled"}); //$NON-NLS-1$
118 fExecutor
.execute(future
);
121 return checkNotNull(future
.get(TmfRemotePreferences
.getCommandTimeout(), TimeUnit
.SECONDS
));
122 } catch (InterruptedException ex
) {
123 throw new ExecutionException(Messages
.RemoteConnection_ExecutionCancelled
, ex
);
124 } catch (TimeoutException ex
) {
125 throw new ExecutionException(Messages
.RemoteConnection_ExecutionTimeout
, ex
);
126 } catch (Exception ex
) {
127 throw new ExecutionException(Messages
.RemoteConnection_ExecutionFailure
, ex
);
133 throw new ExecutionException(Messages
.RemoteConnection_ShellNotConnected
, null);
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
140 private static CommandResult
createResult(int origResult
, String origStdout
, String origStderr
) {
142 final String stdout
, stderr
;
146 String
[] output
= splitLines(stdout
);
147 String
[] error
= splitLines(stderr
);
148 return new CommandResult(result
, output
, error
);
151 private static @NonNull String
[] splitLines(String output
) {
152 return checkNotNull(output
.split("\\r?\\n")); //$NON-NLS-1$