1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.remote
.core
.proxy
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.text
.MessageFormat
;
19 import java
.util
.HashMap
;
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
;
34 * Factory for creation of remote connections programmatically.
36 * It creates {@link IRemoteConnection} instances base on host URI and name.
38 * @author Bernd Hufmann
41 public class TmfRemoteConnectionFactory
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
46 /** Name of a local connection */
47 public static final String LOCAL_CONNECTION_NAME
= "Local"; //$NON-NLS-1$
49 private static final Map
<String
, IConnectionFactory
> CONNECTION_FACTORIES
= new HashMap
<>();
50 private static final DefaultConnectionFactory DEFAULT_CONNECTION_FACTORY
= new DefaultConnectionFactory();
54 IRemoteServicesManager manager
= getService(IRemoteServicesManager
.class);
55 if (manager
!= null) {
56 CONNECTION_FACTORIES
.put(manager
.getLocalConnectionType().getId(), new LocalConnectionFactory());
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 * Registers a connection factory for a given {@link IRemoteConnectionType} ID.
65 * Previously registered factories with same ID will be overwritten.
67 * @param connectionTypeId
68 * ID of remote connection type
70 * the factory implementation
72 public static void registerConnectionFactory(String connectionTypeId
, IConnectionFactory factory
) {
73 CONNECTION_FACTORIES
.put(connectionTypeId
, factory
);
77 * Creates a remote connection instance.
83 * @return the remote connection {@link IRemoteConnection}
85 * @throws RemoteConnectionException
88 public static IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
90 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
91 IConnectionFactory connectionFactory
= CONNECTION_FACTORIES
.get(connectionType
.getId());
92 if (connectionFactory
== null) {
93 connectionFactory
= DEFAULT_CONNECTION_FACTORY
;
95 // Create and return a new connection
96 return connectionFactory
.createConnection(hostUri
, hostName
);
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
103 * Default {@link IConnectionFactory} implementation. It uses the built-in
104 * ssh implementation.
106 public static class DefaultConnectionFactory
implements IConnectionFactory
{
109 public IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
111 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
113 IRemoteConnection connection
= null;
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())) {
125 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_DuplicateConnectionError
, hostName
, hostService
.getHostname(), hostService
.getPort()));
130 if (connection
== null) {
131 // Create a new connection
132 IRemoteConnectionWorkingCopy wc
= null;
133 wc
= connectionType
.newConnection(hostName
);
136 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
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();
145 user
= System
.getProperty("user.name"); //$NON-NLS-1$
147 hostService
.setUsername(user
);
148 hostService
.setUsePassword(true);
150 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
154 connection
= wc
.save(); // Save the attributes
155 } catch (RemoteConnectionException e
) {
156 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
), e
);
160 if (connection
== null) {
161 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
169 * Default Local Connection Factory
171 public static class LocalConnectionFactory
implements IConnectionFactory
{
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
));
182 // ------------------------------------------------------------------------
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
));
190 IRemoteConnectionType connectionType
= manager
.getConnectionType(hostUri
);
191 if (connectionType
== null) {
192 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
194 return connectionType
;
197 // ------------------------------------------------------------------------
198 // Helper methods using OSGI service
199 // ------------------------------------------------------------------------
201 * Return the OSGi service with the given service interface.
205 * @return the specified service or null if it's not registered
207 public static @Nullable <T
> T
getService(Class
<T
> service
) {
208 return Activator
.getService(service
);
212 * Return a remote connection using OSGI service.
214 * @param remoteServicesId
215 * ID of remote service
218 * @return the corresponding remote connection or null
220 public static @Nullable IRemoteConnection
getRemoteConnection(final String remoteServicesId
, final String name
) {
221 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
222 if (manager
== null) {
225 return manager
.getAllRemoteConnections().stream()
226 .filter(connection
->
227 (connection
!= null) &&
228 connection
.getConnectionType().getId().equals(remoteServicesId
.toString()) &&
229 connection
.getName().equals(name
.toString()))
235 * Return a Local connection.
237 * @return the local connection
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
);