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 * Bernd Hufmann - Initial API and implementation
11 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
12 * Bernd Hufmann - Update to org.eclipse.remote API 2.0
13 **********************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.remote
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.remote
.core
.IRemoteConnection
;
21 import org
.eclipse
.remote
.core
.IRemoteConnectionChangeListener
;
22 import org
.eclipse
.remote
.core
.IRemoteConnectionType
;
23 import org
.eclipse
.remote
.core
.IRemoteFileService
;
24 import org
.eclipse
.remote
.core
.IRemoteProcessBuilder
;
25 import org
.eclipse
.remote
.core
.IRemoteProcessService
;
26 import org
.eclipse
.remote
.core
.IRemoteServicesManager
;
27 import org
.eclipse
.remote
.core
.RemoteConnectionChangeEvent
;
28 import org
.eclipse
.remote
.core
.exception
.RemoteConnectionException
;
29 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
31 import com
.google
.common
.base
.Optional
;
32 import com
.google
.common
.base
.Predicate
;
33 import com
.google
.common
.collect
.FluentIterable
;
37 * RemoteSystemProxy implementation.
40 * @author Bernd Hufmann
42 public class RemoteSystemProxy
implements IRemoteSystemProxy
, IRemoteConnectionChangeListener
{
44 /** Name of a local connection */
45 public static final String LOCAL_CONNECTION_NAME
= "Local"; //$NON-NLS-1$
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
50 private final IRemoteConnection fHost
;
51 private boolean fExplicitConnect
;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
61 * The host of this proxy
63 public RemoteSystemProxy(IRemoteConnection host
) {
65 fHost
.addConnectionChangeListener(this);
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 public IRemoteFileService
getRemoteFileService() {
74 return fHost
.getService(IRemoteFileService
.class);
78 public IRemoteProcessBuilder
getProcessBuilder(String
...command
) {
79 return fHost
.getService(IRemoteProcessService
.class).getProcessBuilder(command
);
83 public void connect(IProgressMonitor monitor
) throws ExecutionException
{
85 if (!fHost
.isOpen()) {
86 fExplicitConnect
= true;
89 } catch (RemoteConnectionException e
) {
90 throw new ExecutionException("Cannot connect " + fHost
.getName(), e
); //$NON-NLS-1$
95 public void disconnect() throws ExecutionException
{
100 public void dispose() {
101 fHost
.removeConnectionChangeListener(this);
102 if (fExplicitConnect
) {
108 public ICommandShell
createCommandShell() throws ExecutionException
{
109 ICommandShell shell
= new CommandShell(fHost
);
115 public void addConnectionChangeListener(IRemoteConnectionChangeListener listener
) {
116 fHost
.addConnectionChangeListener(listener
);
120 public void removeConnectionChangeListener(IRemoteConnectionChangeListener listener
) {
121 fHost
.removeConnectionChangeListener(listener
);
125 public boolean isConnected() {
126 return fHost
.isOpen();
130 public void connectionChanged(RemoteConnectionChangeEvent event
) {
131 int type
= event
.getType();
132 if (type
== RemoteConnectionChangeEvent
.CONNECTION_ABORTED
||
133 type
== RemoteConnectionChangeEvent
.CONNECTION_CLOSED
) {
134 fExplicitConnect
= false;
139 * Return a remote connection using OSGI service.
141 * @param remoteServicesId
142 * ID of remote service
145 * @return the corresponding remote connection or null
147 public static @Nullable IRemoteConnection
getRemoteConnection(final @NonNull String remoteServicesId
, final @NonNull String name
) {
148 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
149 if (manager
== null) {
152 FluentIterable
<IRemoteConnection
> connections
= FluentIterable
.from(manager
.getAllRemoteConnections());
153 Optional
<IRemoteConnection
> ret
= connections
.firstMatch(new Predicate
<IRemoteConnection
>() {
155 public boolean apply(IRemoteConnection input
) {
156 return (input
.getConnectionType().getId().equals(remoteServicesId
.toString()) && input
.getName().equals(name
.toString()));
163 * Return a Local connection.
165 * @return the local connection
167 public static @Nullable IRemoteConnection
getLocalConnection() {
168 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
169 if (manager
== null) {
172 IRemoteConnectionType type
= manager
.getLocalConnectionType();
173 return type
.getConnection(LOCAL_CONNECTION_NAME
);