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