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 IRemoteConnectionType type
= manager
.getLocalConnectionType();
58 CONNECTION_FACTORIES
.put(checkNotNull(type
.getId()), new LocalConnectionFactory());
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 * Registers a connection factory for a given {@link IRemoteConnectionType} ID.
68 * Previously registered factories with same ID will be overwritten.
70 * @param connectionTypeId
71 * ID of remote connection type
73 * the factory implementation
75 public static void registerConnectionFactory(String connectionTypeId
, IConnectionFactory factory
) {
76 CONNECTION_FACTORIES
.put(connectionTypeId
, factory
);
80 * Creates a remote connection instance.
86 * @return the remote connection {@link IRemoteConnection}
88 * @throws RemoteConnectionException
91 public static IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
93 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
94 IConnectionFactory connectionFactory
= CONNECTION_FACTORIES
.get(connectionType
.getId());
95 if (connectionFactory
== null) {
96 connectionFactory
= DEFAULT_CONNECTION_FACTORY
;
98 // Create and return a new connection
99 return connectionFactory
.createConnection(hostUri
, hostName
);
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
106 * Default {@link IConnectionFactory} implementation. It uses the built-in
107 * ssh implementation.
109 public static class DefaultConnectionFactory
implements IConnectionFactory
{
112 public IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
114 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
116 IRemoteConnection connection
= null;
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())) {
128 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_DuplicateConnectionError
, hostName
, hostService
.getHostname(), hostService
.getPort()));
133 if (connection
== null) {
134 // Create a new connection
135 IRemoteConnectionWorkingCopy wc
= null;
136 wc
= connectionType
.newConnection(hostName
);
139 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
142 if (wc
.hasService(IRemoteConnectionHostService
.class)) {
143 IRemoteConnectionHostService hostService
= checkNotNull(wc
.getService(IRemoteConnectionHostService
.class));
144 hostService
.setHostname(hostUri
.getHost());
145 hostService
.setPort(hostUri
.getPort());
146 String user
= hostUri
.getUserInfo();
148 user
= System
.getProperty("user.name"); //$NON-NLS-1$
150 hostService
.setUsername(user
);
151 hostService
.setUsePassword(true);
153 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
157 connection
= wc
.save(); // Save the attributes
158 } catch (RemoteConnectionException e
) {
159 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
), e
);
163 if (connection
== null) {
164 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
172 * Default Local Connection Factory
174 public static class LocalConnectionFactory
implements IConnectionFactory
{
176 public IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
177 IRemoteConnection connection
= getLocalConnection();
178 if (connection
== null) {
179 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
185 // ------------------------------------------------------------------------
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
));
193 IRemoteConnectionType connectionType
= manager
.getConnectionType(hostUri
);
194 if (connectionType
== null) {
195 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
197 return connectionType
;
200 // ------------------------------------------------------------------------
201 // Helper methods using OSGI service
202 // ------------------------------------------------------------------------
204 * Return the OSGi service with the given service interface.
208 * @return the specified service or null if it's not registered
210 public static @Nullable <T
> T
getService(Class
<T
> service
) {
211 return Activator
.getService(service
);
215 * Return a remote connection using OSGI service.
217 * @param remoteServicesId
218 * ID of remote service
221 * @return the corresponding remote connection or null
223 public static @Nullable IRemoteConnection
getRemoteConnection(final String remoteServicesId
, final String name
) {
224 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
225 if (manager
== null) {
228 return manager
.getAllRemoteConnections().stream()
229 .filter(connection
->
230 (connection
!= null) &&
231 connection
.getConnectionType().getId().equals(remoteServicesId
.toString()) &&
232 connection
.getName().equals(name
.toString()))
238 * Return a Local connection.
240 * @return the local connection
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
);