ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / model / impl / TargetNodeComponent.java
index 31159efa51a45410643524a09aa5a00ac3b2b2d0..8991cde3c9ab6152ae9674248e601d1c4c5d83cb 100644 (file)
@@ -1,5 +1,5 @@
 /**********************************************************************
- * 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
@@ -9,23 +9,28 @@
  * Contributors:
  *   Bernd Hufmann - Initial API and implementation
  *   Bernd Hufmann - Updated for support of LTTng Tools 2.1
+ *   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.model.impl;
 
+import static java.text.MessageFormat.format;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.util.List;
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.dialogs.ErrorDialog;
-import org.eclipse.rse.core.RSECorePlugin;
-import org.eclipse.rse.core.model.IHost;
-import org.eclipse.rse.core.model.IRSECallback;
-import org.eclipse.rse.core.model.ISystemRegistry;
-import org.eclipse.rse.core.subsystems.CommunicationsEvent;
-import org.eclipse.rse.core.subsystems.ICommunicationsListener;
+import org.eclipse.remote.core.IRemoteConnection;
+import org.eclipse.remote.core.IRemoteConnectionChangeListener;
+import org.eclipse.remote.core.RemoteConnectionChangeEvent;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
@@ -33,11 +38,10 @@ import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.property.TargetNodePropertySource;
-import org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote.ICommandShell;
-import org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote.IRemoteSystemProxy;
-import org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote.RemoteSystemProxy;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.ILttngControlService;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.LTTngControlServiceFactory;
+import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
+import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.views.properties.IPropertySource;
 
@@ -48,7 +52,7 @@ import org.eclipse.ui.views.properties.IPropertySource;
  *
  * @author Bernd Hufmann
  */
-public class TargetNodeComponent extends TraceControlComponent implements ICommunicationsListener {
+public class TargetNodeComponent extends TraceControlComponent implements IRemoteConnectionChangeListener {
 
     // ------------------------------------------------------------------------
     // Constants
@@ -63,6 +67,8 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      */
     public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
 
+    private static final ILttngControlService NULL_CONTROL_SERVICE = new NullControlService();
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -75,14 +81,10 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      * The image to be displayed in state disconnected.
      */
     private Image fDisconnectedImage = null;
-    /**
-     * The connection implementation.
-     */
-    private IHost fHost = null;
     /**
      * The remote proxy implementation.
      */
-    private IRemoteSystemProxy fRemoteProxy = null;
+    private @NonNull RemoteSystemProxy fRemoteProxy;
     /**
      * The control service for LTTng specific commands.
      */
@@ -98,28 +100,51 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
 
     /**
      * Constructor
-     * @param name - the name of the component
-     * @param parent - the parent of the component
-     * @param host - the host connection implementation
-     * @param proxy - the remote proxy implementation
+     *
+     * @param name
+     *            the name of the component
+     * @param parent
+     *            the parent of the component
+     * @param proxy
+     *            the remote proxy implementation
      */
-    public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host, IRemoteSystemProxy proxy) {
+    public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull RemoteSystemProxy proxy) {
         super(name, parent);
         setImage(TARGET_NODE_CONNECTED_ICON_FILE);
         fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
-        fHost = host;
         fRemoteProxy = proxy;
-        setToolTip(fHost.getHostName());
+        fRemoteProxy.getRemoteConnection().addConnectionChangeListener(this);
+        setToolTip(fRemoteProxy.getRemoteConnection().getName());
     }
 
     /**
      * Constructor (using default proxy)
-     * @param name - the name of the component
-     * @param parent - the parent of the component
-     * @param host - the host connection implementation
+     *
+     * @param name
+     *            the name of the component
+     * @param parent
+     *            the parent of the component
+     * @param host
+     *            the host connection implementation
      */
-    public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host) {
-        this(name, parent, host, new RemoteSystemProxy(host));
+    public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull IRemoteConnection host) {
+        this(name, parent, new RemoteSystemProxy(host));
+    }
+
+    @Override
+    public void dispose() {
+        fRemoteProxy.getRemoteConnection().removeConnectionChangeListener(this);
+        fRemoteProxy.dispose();
+        disposeControlService();
+    }
+
+    private void disposeControlService() {
+        fService = null;
+        final ICommandShell shell = fShell;
+        if (shell != null) {
+            shell.dispose();
+            fShell = null;
+        }
     }
 
     // ------------------------------------------------------------------------
@@ -147,7 +172,7 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
 
     @Override
     public ILttngControlService getControlService() {
-        return fService;
+        return fService == null ? NULL_CONTROL_SERVICE : fService;
     }
 
     @Override
@@ -163,35 +188,13 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
         return null;
     }
 
-    /**
-     * @return remote host name
-     */
-    public String getHostName() {
-        return fHost.getHostName();
-    }
-
     /**
      * @return remote system proxy implementation
      */
-    public IRemoteSystemProxy getRemoteSystemProxy() {
+    public @NonNull RemoteSystemProxy getRemoteSystemProxy() {
         return fRemoteProxy;
     }
 
-    /**
-     * @return port of IP connection (shell) to be used
-     */
-    public int getPort() {
-        return fRemoteProxy.getPort();
-    }
-
-    /**
-     * Sets the port of the IP connections of the shell
-     * @param port - the IP port to set
-     */
-    public void setPort(int port) {
-        fRemoteProxy.setPort(port);
-    }
-
     /**
      * @return all available sessions.
      */
@@ -210,7 +213,7 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      */
     public String getNodeVersion() {
         // Control service is null during connection to node
-        if (getControlService() != null) {
+        if (getControlService() != NULL_CONTROL_SERVICE) {
             return getControlService().getVersionString();
         }
         return ""; //$NON-NLS-1$
@@ -281,58 +284,82 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
         return !getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
     }
 
+    /**
+     * Checks if given version is supported by this ILTTngControlService implementation.
+     *
+     * @param version The version to check
+     * @return <code>true</code> if version is supported else <code>false</code>
+     */
+    public boolean isVersionSupported(String version) {
+        return getControlService().isVersionSupported(version);
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-   @Override
-   public void communicationsStateChange(CommunicationsEvent e) {
-       if (e.getState() == CommunicationsEvent.AFTER_DISCONNECT ||
-               e.getState() == CommunicationsEvent.CONNECTION_ERROR) {
-           handleDisconnected();
-       } if ((e.getState() == CommunicationsEvent.AFTER_CONNECT) && (fState != TargetNodeState.CONNECTING)) {
-           handleConnected();
-       }
-   }
-
-   @Override
-   public boolean isPassiveCommunicationsListener() {
-       return true;
-   }
-
-   @Override
-   public void dispose() {
-       fRemoteProxy.removeCommunicationListener(this);
-   }
-
-   /**
-    * Method to connect this node component to the remote target node.
-    */
-   public void connect() {
-       if (fState == TargetNodeState.DISCONNECTED) {
-           try {
-               setTargetNodeState(TargetNodeState.CONNECTING);
-               fRemoteProxy.connect(new IRSECallback() {
-                   @Override
-                   public void done(IStatus status, Object result) {
-                       // Note: result might be null!
-                       if(status.isOK()) {
-                           handleConnected();
-                       } else {
-                           handleDisconnected();
-                       }
-                   }
-               });
-           } catch (Exception e) {
-               setTargetNodeState(TargetNodeState.DISCONNECTED);
-               Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
-           }
-       }
+    @Override
+    public void connectionChanged(RemoteConnectionChangeEvent e) {
+        if (fState == TargetNodeState.CONNECTING) {
+            return;
+        }
+
+        switch (e.getType()) {
+        case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
+        case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
+            handleDisconnected();
+            break;
+        case RemoteConnectionChangeEvent.CONNECTION_OPENED:
+            handleConnected();
+            break;
+        default:
+            break;
+        }
+    }
+
+    /**
+     * Method to connect this node component to the remote target node.
+     */
+    public void connect() {
+        if (fState == TargetNodeState.DISCONNECTED) {
+            try {
+                setTargetNodeState(TargetNodeState.CONNECTING);
+                Job job = new Job(format(Messages.TraceControl_OpenConnectionTo, getName())) {
+                    @Override
+                    protected IStatus run(IProgressMonitor monitor) {
+                        try {
+                            fRemoteProxy.connect(checkNotNull(monitor));
+                            return Status.OK_STATUS;
+                        } catch (Exception e) {
+                            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure, e);
+                        }
+                    }
+                };
+                job.addJobChangeListener(new JobChangeAdapter() {
+                    @Override
+                    public void done(IJobChangeEvent event) {
+                        IStatus status = event.getResult();
+                        if (status.isOK()) {
+                            handleConnected();
+                        } else {
+                            handleDisconnected();
+                            if (status.getSeverity() != IStatus.CANCEL) {
+                                Activator.getDefault().getLog().log(status);
+                            }
+                        }
+                    }
+                });
+                job.schedule();
+            } catch (Exception e) {
+                setTargetNodeState(TargetNodeState.DISCONNECTED);
+                Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
+            }
+        }
     }
 
-   /**
-    * Method to disconnect this node component to the remote target node.
-    */
+    /**
+     * Method to disconnect this node component to the remote target node.
+     */
     public void disconnect() {
         if (fState == TargetNodeState.CONNECTED) {
             try {
@@ -359,11 +386,12 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
                     // Get provider information from node
                     TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
                     addChild(providerGroup);
-                    providerGroup.getProviderFromNode(monitor);
 
                     // Get session information from node
                     TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
                     addChild(sessionGroup);
+
+                    providerGroup.getProviderFromNode(monitor);
                     sessionGroup.getSessionsFromNode(monitor);
                 } catch (ExecutionException e) {
                     removeAllChildren();
@@ -385,18 +413,6 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
         getConfigurationFromNode();
     }
 
-    /**
-     * Deregisters host from registry.
-     */
-    public void deregister() {
-        ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
-        // Don't remove local host because it cannot be recreated by
-        // LTTng NewConnection Dialog
-        if (!fRemoteProxy.isLocal()) {
-            registry.deleteHost(fHost);
-        }
-    }
-
     // ------------------------------------------------------------------------
     // Helper function
     // ------------------------------------------------------------------------
@@ -406,11 +422,16 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      * @throws ExecutionException
      */
     private ILttngControlService createControlService() throws ExecutionException {
-        if (fShell == null) {
-            fShell = fRemoteProxy.createCommandShell();
-            fRemoteProxy.addCommunicationListener(this);
+        if (fService == null) {
+            try {
+                ICommandShell shell = fRemoteProxy.createCommandShell();
+                fShell = shell;
+                fService = LTTngControlServiceFactory.getLttngControlService(shell);
+            } catch (ExecutionException e) {
+                disposeControlService();
+                throw e;
+            }
         }
-        fService = LTTngControlServiceFactory.getInstance().getLttngControlService(fShell);
         return fService;
     }
 
@@ -418,13 +439,16 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      * Handles the connected event.
      */
     private void handleConnected() {
-        setTargetNodeState(TargetNodeState.CONNECTED);
         try {
             createControlService();
             getConfigurationFromNode();
+            // Set connected only after the control service has been created and the jobs for creating the
+            // sub-nodes are scheduled.
+            setTargetNodeState(TargetNodeState.CONNECTED);
         } catch (final ExecutionException e) {
             // Disconnect only if no control service, otherwise stay connected.
-            if (getControlService() == null) {
+            if (getControlService() == NULL_CONTROL_SERVICE) {
+                fState = TargetNodeState.CONNECTED;
                 disconnect();
             }
 
@@ -447,9 +471,16 @@ public class TargetNodeComponent extends TraceControlComponent implements ICommu
      * Handles the disconnected event.
      */
     private void handleDisconnected() {
-        removeAllChildren();
+        disposeControlService();
         setTargetNodeState(TargetNodeState.DISCONNECTED);
-        fShell = null;
-        fService = null;
+        removeAllChildren();
+    }
+
+    @Override
+    public void addChild(ITraceControlComponent component) {
+        if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
+            return;
+        }
+        super.addChild(component);
     }
 }
This page took 0.030415 seconds and 5 git commands to generate.