24ce75ced3d467520daf03e78e1092b0c85c6948
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / remote / RemoteSystemProxy.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
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;
15
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;
30
31 import com.google.common.base.Optional;
32 import com.google.common.base.Predicate;
33 import com.google.common.collect.FluentIterable;
34
35 /**
36 * <p>
37 * RemoteSystemProxy implementation.
38 * </p>
39 *
40 * @author Bernd Hufmann
41 */
42 public class RemoteSystemProxy implements IRemoteSystemProxy, IRemoteConnectionChangeListener {
43
44 /** Name of a local connection */
45 public static final String LOCAL_CONNECTION_NAME = "Local"; //$NON-NLS-1$
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50 private final IRemoteConnection fHost;
51 private boolean fExplicitConnect;
52
53 // ------------------------------------------------------------------------
54 // Constructors
55 // ------------------------------------------------------------------------
56
57 /**
58 * Constructor
59 *
60 * @param host
61 * The host of this proxy
62 */
63 public RemoteSystemProxy(IRemoteConnection host) {
64 fHost = host;
65 fHost.addConnectionChangeListener(this);
66 }
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 @Override
73 public IRemoteFileService getRemoteFileService() {
74 return fHost.getService(IRemoteFileService.class);
75 }
76
77 @Override
78 public IRemoteProcessBuilder getProcessBuilder(String...command) {
79 return fHost.getService(IRemoteProcessService.class).getProcessBuilder(command);
80 }
81
82 @Override
83 public void connect(IProgressMonitor monitor) throws ExecutionException {
84 try {
85 if (!fHost.isOpen()) {
86 fExplicitConnect = true;
87 fHost.open(monitor);
88 }
89 } catch (RemoteConnectionException e) {
90 throw new ExecutionException("Cannot connect " + fHost.getName(), e); //$NON-NLS-1$
91 }
92 }
93
94 @Override
95 public void disconnect() throws ExecutionException {
96 fHost.close();
97 }
98
99 @Override
100 public void dispose() {
101 fHost.removeConnectionChangeListener(this);
102 if (fExplicitConnect) {
103 fHost.close();
104 }
105 }
106
107 @Override
108 public ICommandShell createCommandShell() throws ExecutionException {
109 ICommandShell shell = new CommandShell(fHost);
110 shell.connect();
111 return shell;
112 }
113
114 @Override
115 public void addConnectionChangeListener(IRemoteConnectionChangeListener listener) {
116 fHost.addConnectionChangeListener(listener);
117 }
118
119 @Override
120 public void removeConnectionChangeListener(IRemoteConnectionChangeListener listener) {
121 fHost.removeConnectionChangeListener(listener);
122 }
123
124 @Override
125 public boolean isConnected() {
126 return fHost.isOpen();
127 }
128
129 @Override
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;
135 }
136 }
137
138 /**
139 * Return a remote connection using OSGI service.
140 *
141 * @param remoteServicesId
142 * ID of remote service
143 * @param name
144 * name of connection
145 * @return the corresponding remote connection or null
146 */
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) {
150 return null;
151 }
152 FluentIterable<IRemoteConnection> connections = FluentIterable.from(manager.getAllRemoteConnections());
153 Optional<IRemoteConnection> ret = connections.firstMatch(new Predicate<IRemoteConnection>() {
154 @Override
155 public boolean apply(IRemoteConnection input) {
156 return (input.getConnectionType().getId().equals(remoteServicesId.toString()) && input.getName().equals(name.toString()));
157 }
158 });
159 return ret.orNull();
160 }
161
162 /**
163 * Return a Local connection.
164 *
165 * @return the local connection
166 */
167 public static @Nullable IRemoteConnection getLocalConnection() {
168 IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
169 if (manager == null) {
170 return null;
171 }
172 IRemoteConnectionType type = manager.getLocalConnectionType();
173 return type.getConnection(LOCAL_CONNECTION_NAME);
174 }
175 }
This page took 0.037709 seconds and 4 git commands to generate.