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