/**********************************************************************
- * Copyright (c) 2012, 2013 Ericsson
+ * Copyright (c) 2012, 2015 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
*
* Contributors:
* Bernd Hufmann - Initial API and implementation
+ * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
+ * Bernd Hufmann - Update to org.eclipse.remote API 2.0
**********************************************************************/
package org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote;
import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.rse.core.model.IHost;
-import org.eclipse.rse.core.model.IRSECallback;
-import org.eclipse.rse.core.subsystems.ICommunicationsListener;
-import org.eclipse.rse.core.subsystems.IConnectorService;
-import org.eclipse.rse.core.subsystems.ISubSystem;
-import org.eclipse.rse.services.IService;
-import org.eclipse.rse.services.shells.IShellService;
-import org.eclipse.rse.services.terminals.ITerminalService;
-import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.remote.core.IRemoteConnection;
+import org.eclipse.remote.core.IRemoteConnectionChangeListener;
+import org.eclipse.remote.core.IRemoteConnectionType;
+import org.eclipse.remote.core.IRemoteFileService;
+import org.eclipse.remote.core.IRemoteProcessBuilder;
+import org.eclipse.remote.core.IRemoteProcessService;
+import org.eclipse.remote.core.IRemoteServicesManager;
+import org.eclipse.remote.core.RemoteConnectionChangeEvent;
+import org.eclipse.remote.core.exception.RemoteConnectionException;
+import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
/**
* <p>
*
* @author Bernd Hufmann
*/
-public class RemoteSystemProxy implements IRemoteSystemProxy {
+public class RemoteSystemProxy implements IRemoteSystemProxy, IRemoteConnectionChangeListener {
+
+ /** Name of a local connection */
+ public static final String LOCAL_CONNECTION_NAME = "Local"; //$NON-NLS-1$
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
-
- private final IHost fHost;
+ private final IRemoteConnection fHost;
+ private boolean fExplicitConnect;
// ------------------------------------------------------------------------
// Constructors
* @param host
* The host of this proxy
*/
- public RemoteSystemProxy(IHost host) {
+ public RemoteSystemProxy(IRemoteConnection host) {
fHost = host;
+ fHost.addConnectionChangeListener(this);
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
@Override
- public IShellService getShellService() {
- ISubSystem ss = getShellServiceSubSystem();
- if (ss != null) {
- return (IShellService)ss.getSubSystemConfiguration().getService(fHost).getAdapter(IShellService.class);
- }
- return null;
+ public IRemoteFileService getRemoteFileService() {
+ return fHost.getService(IRemoteFileService.class);
}
@Override
- public ITerminalService getTerminalService() {
- ISubSystem ss = getTerminalServiceSubSystem();
- if (ss != null) {
- return (ITerminalService)ss.getSubSystemConfiguration().getService(fHost).getAdapter(ITerminalService.class);
- }
- return null;
+ public IRemoteProcessBuilder getProcessBuilder(String...command) {
+ return fHost.getService(IRemoteProcessService.class).getProcessBuilder(command);
}
@Override
- public ISubSystem getShellServiceSubSystem() {
- if (fHost == null) {
- return null;
- }
- ISubSystem[] subSystems = fHost.getSubSystems();
- IShellService ssvc = null;
- for (int i = 0; subSystems != null && i < subSystems.length; i++) {
- IService svc = subSystems[i].getSubSystemConfiguration().getService(fHost);
- if (svc!=null) {
- ssvc = (IShellService)svc.getAdapter(IShellService.class);
- if (ssvc != null) {
- return subSystems[i];
- }
+ public void connect(IProgressMonitor monitor) throws ExecutionException {
+ try {
+ if (!fHost.isOpen()) {
+ fExplicitConnect = true;
+ fHost.open(monitor);
}
+ } catch (RemoteConnectionException e) {
+ throw new ExecutionException("Cannot connect " + fHost.getName(), e); //$NON-NLS-1$
}
- return null;
}
@Override
- public ISubSystem getTerminalServiceSubSystem() {
- if (fHost == null) {
- return null;
- }
- ISubSystem[] subSystems = fHost.getSubSystems();
- ITerminalService ssvc = null;
- for (int i = 0; subSystems != null && i < subSystems.length; i++) {
- IService svc = subSystems[i].getSubSystemConfiguration().getService(fHost);
- if (svc!=null) {
- ssvc = (ITerminalService)svc.getAdapter(ITerminalService.class);
- if (ssvc != null) {
- return subSystems[i];
- }
- }
- }
- return null;
- }
-
- @Override
- public IFileServiceSubSystem getFileServiceSubSystem() {
- if (fHost == null) {
- return null;
- }
- ISubSystem[] subSystems = fHost.getSubSystems();
- for (int i = 0; subSystems != null && i < subSystems.length; i++) {
- if (subSystems[i] instanceof IFileServiceSubSystem) {
- return (IFileServiceSubSystem)subSystems[i];
- }
- }
- return null;
+ public void disconnect() throws ExecutionException {
+ fHost.close();
}
@Override
- public int getPort() {
- if (getShellServiceSubSystem() != null) {
- return getShellServiceSubSystem().getConnectorService().getPort();
+ public void dispose() {
+ fHost.removeConnectionChangeListener(this);
+ if (fExplicitConnect) {
+ fHost.close();
}
- return IRemoteSystemProxy.INVALID_PORT_NUMBER;
}
@Override
- public void setPort(int port) {
- if ((getShellServiceSubSystem() != null) && (port > 0)) {
- getShellServiceSubSystem().getConnectorService().setPort(port);
- }
+ public ICommandShell createCommandShell() throws ExecutionException {
+ ICommandShell shell = new CommandShell(fHost);
+ shell.connect();
+ return shell;
}
@Override
- public void connect(IRSECallback callback) throws ExecutionException {
- ISubSystem shellSubSystem = getShellServiceSubSystem();
- if (shellSubSystem != null) {
- if (!shellSubSystem.isConnected()) {
- try {
- shellSubSystem.connect(false, callback);
- } catch (OperationCanceledException e) {
- callback.done(Status.CANCEL_STATUS, null);
- }
- catch (Exception e) {
- throw new ExecutionException(e.toString(), e);
- }
- } else {
- callback.done(Status.OK_STATUS, null);
- }
- }
+ public void addConnectionChangeListener(IRemoteConnectionChangeListener listener) {
+ fHost.addConnectionChangeListener(listener);
}
@Override
- public void disconnect() throws ExecutionException {
- ISubSystem shellSubSystem = getShellServiceSubSystem();
- if (shellSubSystem != null) {
- try {
- shellSubSystem.disconnect();
- } catch (Exception e) {
- throw new ExecutionException(e.toString(), e);
- }
- }
+ public void removeConnectionChangeListener(IRemoteConnectionChangeListener listener) {
+ fHost.removeConnectionChangeListener(listener);
}
@Override
- public ICommandShell createCommandShell() throws ExecutionException {
- ICommandShell shell = new CommandShell(this);
- shell.connect();
- return shell;
+ public boolean isConnected() {
+ return fHost.isOpen();
}
@Override
- public void addCommunicationListener(ICommunicationsListener listener) {
- IConnectorService[] css = fHost.getConnectorServices();
- for (IConnectorService cs : css) {
- cs.addCommunicationsListener(listener);
+ public void connectionChanged(RemoteConnectionChangeEvent event) {
+ int type = event.getType();
+ if (type == RemoteConnectionChangeEvent.CONNECTION_ABORTED ||
+ type == RemoteConnectionChangeEvent.CONNECTION_CLOSED) {
+ fExplicitConnect = false;
}
}
- @Override
- public void removeCommunicationListener(ICommunicationsListener listener) {
- IConnectorService[] css = fHost.getConnectorServices();
- for (IConnectorService cs : css) {
- cs.removeCommunicationsListener(listener);
+ /**
+ * Return a remote connection using OSGI service.
+ *
+ * @param remoteServicesId
+ * ID of remote service
+ * @param name
+ * name of connection
+ * @return the corresponding remote connection or null
+ */
+ public static @Nullable IRemoteConnection getRemoteConnection(final @NonNull String remoteServicesId, final @NonNull String name) {
+ IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
+ if (manager == null) {
+ return null;
}
+ FluentIterable<IRemoteConnection> connections = FluentIterable.from(manager.getAllRemoteConnections());
+ Optional<IRemoteConnection> ret = connections.firstMatch(new Predicate<IRemoteConnection>() {
+ @Override
+ public boolean apply(IRemoteConnection input) {
+ return (input.getConnectionType().getId().equals(remoteServicesId.toString()) && input.getName().equals(name.toString()));
+ }
+ });
+ return ret.orNull();
}
- @Override
- public boolean isLocal() {
- return fHost.getSystemType().isLocal();
+ /**
+ * Return a Local connection.
+ *
+ * @return the local connection
+ */
+ public static @Nullable IRemoteConnection getLocalConnection() {
+ IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class);
+ if (manager == null) {
+ return null;
+ }
+ IRemoteConnectionType type = manager.getLocalConnectionType();
+ return type.getConnection(LOCAL_CONNECTION_NAME);
}
}