blob: 0209d376f57b2caf70520150b60a66ceccecc9bf (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/**********************************************************************
* Copyright (c) 2012, 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
*
* Contributors:
* Bernd Hufmann - Initial API and implementation
* Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
* Bernd Hufmann - Update to org.eclipse.remote API 2.0
**********************************************************************/
package org.eclipse.tracecompass.tmf.remote.core.proxy;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionChangeListener;
import org.eclipse.remote.core.IRemoteConnectionType;
import org.eclipse.remote.core.IRemoteFileService;
import org.eclipse.remote.core.IRemoteProcessBuilder;
import org.eclipse.remote.core.IRemoteProcessService;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.RemoteConnectionChangeEvent;
import org.eclipse.remote.core.exception.RemoteConnectionException;
import org.eclipse.tracecompass.internal.tmf.remote.core.Activator;
import org.eclipse.tracecompass.tmf.remote.core.shell.CommandShell;
import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
/**
* <p>
* RemoteSystemProxy implementation.
* </p>
*
* @author Bernd Hufmann
*/
@NonNullByDefault
public class RemoteSystemProxy implements IRemoteConnectionChangeListener {
/** Name of a local connection */
public static final String LOCAL_CONNECTION_NAME = "Local"; //$NON-NLS-1$
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
private final IRemoteConnection fHost;
private boolean fExplicitConnect;
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
* Constructor
*
* @param host
* The host of this proxy
*/
public RemoteSystemProxy(IRemoteConnection host) {
fHost = host;
fHost.addConnectionChangeListener(this);
}
/**
* Returns the connection instance.
*
* @return the @link{IRemoteConnection} instance
*/
public IRemoteConnection getRemoteConnection() {
return fHost;
}
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
/**
* Finds the remote file system service.
*
* @return file service subsystem, or <code>null</code> if not found.
*/
public @Nullable IRemoteFileService getRemoteFileService() {
return fHost.getService(IRemoteFileService.class);
}
/**
* Returns a remote process builder for remote launching a process.
*
* @param command
* the command to be executed.
* @return the builder, or <code>null</code> if not possible.
*/
public @Nullable IRemoteProcessBuilder getProcessBuilder(String...command) {
return fHost.getService(IRemoteProcessService.class).getProcessBuilder(command);
}
/**
* Connects the remote connection.
*
* @param monitor
* a monitor to report progress.
*
* @throws ExecutionException
* If the connection fails
*/
public void connect(IProgressMonitor monitor) throws ExecutionException {
try {
if (!fHost.isOpen()) {
fExplicitConnect = true;
fHost.open(monitor);
}
} catch (RemoteConnectionException e) {
throw new ExecutionException("Cannot connect " + fHost.getName(), e); //$NON-NLS-1$
}
}
/**
* Disconnects from the remote connection.
*/
public void disconnect() {
fHost.close();
}
/**
* Disposes the proxy, may close the connection.
*/
public void dispose() {
fHost.removeConnectionChangeListener(this);
if (fExplicitConnect) {
fHost.close();
}
}
/**
* Creates a command shell.
*
* @return the command shell implementation
* @throws ExecutionException
* If the command fails
*/
public ICommandShell createCommandShell() throws ExecutionException {
ICommandShell shell = new CommandShell(fHost);
shell.connect();
return shell;
}
/**
* Method to add a communication listener to the connector service defined
* for the given connection.
*
* @param listener
* listener to add
*/
public void addConnectionChangeListener(IRemoteConnectionChangeListener listener) {
fHost.addConnectionChangeListener(listener);
}
/**
* Method to remove a communication listener from the connector service
* defined for the given connection.
*
* @param listener
* listener to remove
*/
public void removeConnectionChangeListener(IRemoteConnectionChangeListener listener) {
fHost.removeConnectionChangeListener(listener);
}
/**
* Returns the connection state.
*
* @return whether the remote host is currently connected.
*/
public boolean isConnected() {
return fHost.isOpen();
}
@Override
public void connectionChanged(@Nullable RemoteConnectionChangeEvent event) {
if (event != null) {
int type = event.getType();
if (type == RemoteConnectionChangeEvent.CONNECTION_ABORTED ||
type == RemoteConnectionChangeEvent.CONNECTION_CLOSED) {
fExplicitConnect = false;
}
}
}
/**
* Return the OSGi service with the given service interface.
*
* @param service
* service interface
* @return the specified service or null if it's not registered
*/
public static @Nullable <T> T getService(Class<T> service) {
return Activator.getService(service);
}
/**
* Return a remote connection using OSGI service.
*
* @param remoteServicesId
* ID of remote service
* @param name
* name of connection
* @return the corresponding remote connection or null
*/
public static @Nullable IRemoteConnection getRemoteConnection(final String remoteServicesId, final String name) {
IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
if (manager == null) {
return null;
}
FluentIterable<IRemoteConnection> connections = FluentIterable.from(manager.getAllRemoteConnections());
Optional<IRemoteConnection> ret = connections.firstMatch(new Predicate<IRemoteConnection>() {
@Override
public boolean apply(@Nullable IRemoteConnection input) {
return ((input != null) && input.getConnectionType().getId().equals(remoteServicesId.toString()) && input.getName().equals(name.toString()));
}
});
return ret.orNull();
}
/**
* Return a Local connection.
*
* @return the local connection
*/
public static @Nullable IRemoteConnection getLocalConnection() {
IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
if (manager != null) {
IRemoteConnectionType type = manager.getLocalConnectionType();
return type.getConnection(LOCAL_CONNECTION_NAME);
}
return null;
}
}
|