d3dfa907a77da39579be7196f13a1ac76c4242a5
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.remote.core / src / org / eclipse / tracecompass / tmf / remote / core / proxy / TmfRemoteConnectionFactory.java
1 /*******************************************************************************
2 * Copyright (c) 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 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.remote.core.proxy;
14
15 import java.net.URI;
16 import java.text.MessageFormat;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.remote.core.IRemoteConnection;
23 import org.eclipse.remote.core.IRemoteConnectionHostService;
24 import org.eclipse.remote.core.IRemoteConnectionType;
25 import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
26 import org.eclipse.remote.core.IRemoteServicesManager;
27 import org.eclipse.remote.core.exception.RemoteConnectionException;
28 import org.eclipse.tracecompass.internal.tmf.remote.core.Activator;
29 import org.eclipse.tracecompass.internal.tmf.remote.core.messages.Messages;
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 * Factory for creation of remote connections programmatically.
37 *
38 * It creates {@link IRemoteConnection} instances base on host URI and name.
39 *
40 * @author Bernd Hufmann
41 */
42 @NonNullByDefault
43 public class TmfRemoteConnectionFactory {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /** Name of a local connection */
49 public static final String LOCAL_CONNECTION_NAME = "Local"; //$NON-NLS-1$
50
51 private static final Map<String, IConnectionFactory> CONNECTION_FACTORIES = new HashMap<>();
52 private static final DefaultConnectionFactory DEFAULT_CONNECTION_FACTORY = new DefaultConnectionFactory();
53
54 static {
55 // Add local services
56 IRemoteServicesManager manager = getService(IRemoteServicesManager.class);
57 if (manager != null) {
58 CONNECTION_FACTORIES.put(manager.getLocalConnectionType().getId(), new LocalConnectionFactory());
59 }
60 }
61
62 // ------------------------------------------------------------------------
63 // Operations
64 // ------------------------------------------------------------------------
65 /**
66 * Registers a connection factory for a given {@link IRemoteConnectionType} ID.
67 * Previously registered factories with same ID will be overwritten.
68 *
69 * @param connectionTypeId
70 * ID of remote connection type
71 * @param factory
72 * the factory implementation
73 */
74 public static void registerConnectionFactory(String connectionTypeId, IConnectionFactory factory) {
75 CONNECTION_FACTORIES.put(connectionTypeId, factory);
76 }
77
78 /**
79 * Creates a remote connection instance.
80 *
81 * @param hostUri
82 * The host URI
83 * @param hostName
84 * The hostname
85 * @return the remote connection {@link IRemoteConnection}
86 *
87 * @throws RemoteConnectionException
88 * In case of an error
89 */
90 public static IRemoteConnection createConnection(URI hostUri, String hostName) throws RemoteConnectionException {
91
92 IRemoteConnectionType connectionType = getConnectionType(hostUri);
93 IConnectionFactory connectionFactory = CONNECTION_FACTORIES.get(connectionType.getId());
94 if (connectionFactory == null) {
95 connectionFactory = DEFAULT_CONNECTION_FACTORY;
96 }
97 // Create and return a new connection
98 return connectionFactory.createConnection(hostUri, hostName);
99 }
100
101 // ------------------------------------------------------------------------
102 // Helper classes
103 // ------------------------------------------------------------------------
104 /**
105 * Default {@link IConnectionFactory} implementation. It uses the built-in
106 * ssh implementation.
107 */
108 public static class DefaultConnectionFactory implements IConnectionFactory {
109
110 @Override
111 public IRemoteConnection createConnection(URI hostUri, String hostName) throws RemoteConnectionException {
112
113 IRemoteConnectionType connectionType = getConnectionType(hostUri);
114
115 IRemoteConnection connection = null;
116
117 // Look for existing connections
118 for (IRemoteConnection conn : connectionType.getConnections()) {
119 if (conn.getName().equals(hostName)) {
120 IRemoteConnectionHostService hostService = conn.getService(IRemoteConnectionHostService.class);
121 if (hostService != null) {
122 if ((hostService.getHostname().equals(hostUri.getHost())) &&
123 (hostUri.getPort() == -1 || hostService.getPort() == hostUri.getPort())) {
124 connection = conn;
125 break;
126 }
127 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_DuplicateConnectionError, hostName, hostService.getHostname(), hostService.getPort()));
128 }
129 }
130 }
131
132 if (connection == null) {
133 // Create a new connection
134 IRemoteConnectionWorkingCopy wc = null;
135 wc = connectionType.newConnection(hostName);
136
137 if (wc == null) {
138 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
139 }
140
141 if (wc.hasService(IRemoteConnectionHostService.class)) {
142 IRemoteConnectionHostService hostService = wc.getService(IRemoteConnectionHostService.class);
143 hostService.setHostname(hostUri.getHost());
144 hostService.setPort(hostUri.getPort());
145 String user = hostUri.getUserInfo();
146 if (user == null) {
147 user = System.getProperty("user.name"); //$NON-NLS-1$
148 }
149 hostService.setUsername(user);
150 hostService.setUsePassword(true);
151 } else {
152 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
153 }
154
155 try {
156 connection = wc.save(); // Save the attributes
157 } catch (RemoteConnectionException e) {
158 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri), e);
159 }
160 }
161
162 if (connection == null) {
163 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
164 }
165
166 return connection;
167 }
168 }
169
170 /**
171 * Default Local Connection Factory
172 */
173 public static class LocalConnectionFactory implements IConnectionFactory {
174 @Override
175 public IRemoteConnection createConnection(URI hostUri, String hostName) throws RemoteConnectionException {
176 IRemoteConnection connection = getLocalConnection();
177 if (connection == null) {
178 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
179 }
180 return connection;
181 }
182 }
183
184 // ------------------------------------------------------------------------
185 // Helper method(s)
186 // ------------------------------------------------------------------------
187 private static IRemoteConnectionType getConnectionType(URI hostUri) throws RemoteConnectionException {
188 IRemoteServicesManager manager = getService(IRemoteServicesManager.class);
189 if (manager == null) {
190 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
191 }
192 IRemoteConnectionType connectionType = manager.getConnectionType(hostUri);
193 if (connectionType == null) {
194 throw new RemoteConnectionException(MessageFormat.format(Messages.RemoteConnection_ConnectionError, hostUri));
195 }
196 return connectionType;
197 }
198
199 // ------------------------------------------------------------------------
200 // Helper methods using OSGI service
201 // ------------------------------------------------------------------------
202 /**
203 * Return the OSGi service with the given service interface.
204 *
205 * @param service
206 * service interface
207 * @return the specified service or null if it's not registered
208 */
209 public static @Nullable <T> T getService(Class<T> service) {
210 return Activator.getService(service);
211 }
212
213 /**
214 * Return a remote connection using OSGI service.
215 *
216 * @param remoteServicesId
217 * ID of remote service
218 * @param name
219 * name of connection
220 * @return the corresponding remote connection or null
221 */
222 public static @Nullable IRemoteConnection getRemoteConnection(final String remoteServicesId, final String name) {
223 IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
224 if (manager == null) {
225 return null;
226 }
227 FluentIterable<IRemoteConnection> connections = FluentIterable.from(manager.getAllRemoteConnections());
228 Optional<IRemoteConnection> ret = connections.firstMatch(new Predicate<IRemoteConnection>() {
229 @Override
230 public boolean apply(@Nullable IRemoteConnection input) {
231 return ((input != null) && input.getConnectionType().getId().equals(remoteServicesId.toString()) && input.getName().equals(name.toString()));
232 }
233 });
234 return ret.orNull();
235 }
236
237 /**
238 * Return a Local connection.
239 *
240 * @return the local connection
241 */
242 public static @Nullable IRemoteConnection getLocalConnection() {
243 IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
244 if (manager != null) {
245 IRemoteConnectionType type = manager.getLocalConnectionType();
246 return type.getConnection(LOCAL_CONNECTION_NAME);
247 }
248 return null;
249 }
250
251 }
This page took 0.037921 seconds and 4 git commands to generate.