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
;
16 import java
.text
.MessageFormat
;
17 import java
.util
.HashMap
;
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
;
31 import com
.google
.common
.base
.Optional
;
32 import com
.google
.common
.base
.Predicate
;
33 import com
.google
.common
.collect
.FluentIterable
;
36 * Factory for creation of remote connections programmatically.
38 * It creates {@link IRemoteConnection} instances base on host URI and name.
40 * @author Bernd Hufmann
43 public class TmfRemoteConnectionFactory
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
48 /** Name of a local connection */
49 public static final String LOCAL_CONNECTION_NAME
= "Local"; //$NON-NLS-1$
51 private static final Map
<String
, IConnectionFactory
> CONNECTION_FACTORIES
= new HashMap
<>();
52 private static final DefaultConnectionFactory DEFAULT_CONNECTION_FACTORY
= new DefaultConnectionFactory();
56 IRemoteServicesManager manager
= getService(IRemoteServicesManager
.class);
57 if (manager
!= null) {
58 CONNECTION_FACTORIES
.put(manager
.getLocalConnectionType().getId(), new LocalConnectionFactory());
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 * Registers a connection factory for a given {@link IRemoteConnectionType} ID.
67 * Previously registered factories with same ID will be overwritten.
69 * @param connectionTypeId
70 * ID of remote connection type
72 * the factory implementation
74 public static void registerConnectionFactory(String connectionTypeId
, IConnectionFactory factory
) {
75 CONNECTION_FACTORIES
.put(connectionTypeId
, factory
);
79 * Creates a remote connection instance.
85 * @return the remote connection {@link IRemoteConnection}
87 * @throws RemoteConnectionException
90 public static IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
92 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
93 IConnectionFactory connectionFactory
= CONNECTION_FACTORIES
.get(connectionType
.getId());
94 if (connectionFactory
== null) {
95 connectionFactory
= DEFAULT_CONNECTION_FACTORY
;
97 // Create and return a new connection
98 return connectionFactory
.createConnection(hostUri
, hostName
);
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
105 * Default {@link IConnectionFactory} implementation. It uses the built-in
106 * ssh implementation.
108 public static class DefaultConnectionFactory
implements IConnectionFactory
{
111 public IRemoteConnection
createConnection(URI hostUri
, String hostName
) throws RemoteConnectionException
{
113 IRemoteConnectionType connectionType
= getConnectionType(hostUri
);
115 IRemoteConnection connection
= null;
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())) {
127 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_DuplicateConnectionError
, hostName
, hostService
.getHostname(), hostService
.getPort()));
132 if (connection
== null) {
133 // Create a new connection
134 IRemoteConnectionWorkingCopy wc
= null;
135 wc
= connectionType
.newConnection(hostName
);
138 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
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();
147 user
= System
.getProperty("user.name"); //$NON-NLS-1$
149 hostService
.setUsername(user
);
150 hostService
.setUsePassword(true);
152 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
156 connection
= wc
.save(); // Save the attributes
157 } catch (RemoteConnectionException e
) {
158 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
), e
);
162 if (connection
== null) {
163 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
171 * Default Local Connection Factory
173 public static class LocalConnectionFactory
implements IConnectionFactory
{
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
));
184 // ------------------------------------------------------------------------
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
));
192 IRemoteConnectionType connectionType
= manager
.getConnectionType(hostUri
);
193 if (connectionType
== null) {
194 throw new RemoteConnectionException(MessageFormat
.format(Messages
.RemoteConnection_ConnectionError
, hostUri
));
196 return connectionType
;
199 // ------------------------------------------------------------------------
200 // Helper methods using OSGI service
201 // ------------------------------------------------------------------------
203 * Return the OSGi service with the given service interface.
207 * @return the specified service or null if it's not registered
209 public static @Nullable <T
> T
getService(Class
<T
> service
) {
210 return Activator
.getService(service
);
214 * Return a remote connection using OSGI service.
216 * @param remoteServicesId
217 * ID of remote service
220 * @return the corresponding remote connection or null
222 public static @Nullable IRemoteConnection
getRemoteConnection(final String remoteServicesId
, final String name
) {
223 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
224 if (manager
== null) {
227 FluentIterable
<IRemoteConnection
> connections
= FluentIterable
.from(manager
.getAllRemoteConnections());
228 Optional
<IRemoteConnection
> ret
= connections
.firstMatch(new Predicate
<IRemoteConnection
>() {
230 public boolean apply(@Nullable IRemoteConnection input
) {
231 return ((input
!= null) && input
.getConnectionType().getId().equals(remoteServicesId
.toString()) && input
.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
);