gdbtrace: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / handlers / NewConnectionHandler.java
CommitLineData
eb1bab5b 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson and others
ea21cd65 3 *
eb1bab5b
BH
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
ea21cd65
AM
8 *
9 * Contributors:
ef5356e1
BH
10 * Bernd Hufmann - Initial API and implementation
11 * Anna Dushistova(Montavista) - [382684] Allow reusing already defined connections that have Files and Shells subsystems
eb1bab5b 12 **********************************************************************/
8e8c0226 13package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers;
eb1bab5b 14
ef5356e1
BH
15import java.util.ArrayList;
16import java.util.Arrays;
17
eb1bab5b
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
eb1bab5b
BH
20import org.eclipse.jface.dialogs.MessageDialog;
21import org.eclipse.jface.window.Window;
8e8c0226
AM
22import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
23import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.INewConnectionDialog;
24import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
25import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
26import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.ITraceControlComponent;
27import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
28import org.eclipse.linuxtools.internal.lttng2.control.ui.views.remote.IRemoteSystemProxy;
eb1bab5b
BH
29import org.eclipse.rse.core.IRSESystemType;
30import org.eclipse.rse.core.RSECorePlugin;
31import org.eclipse.rse.core.model.IHost;
32import org.eclipse.rse.core.model.ISystemRegistry;
ef5356e1
BH
33import org.eclipse.rse.core.subsystems.ISubSystem;
34import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
eb1bab5b
BH
35import org.eclipse.ui.IWorkbenchPage;
36import org.eclipse.ui.IWorkbenchPart;
37import org.eclipse.ui.IWorkbenchWindow;
38import org.eclipse.ui.PlatformUI;
39
40/**
eb1bab5b
BH
41 * <p>
42 * Command handler for creation new connection for trace control.
43 * </p>
ea21cd65 44 *
dbd4432d 45 * @author Bernd Hufmann
eb1bab5b 46 */
498704b3 47public class NewConnectionHandler extends BaseControlViewHandler {
eb1bab5b
BH
48
49 // ------------------------------------------------------------------------
50 // Constants
51 // ------------------------------------------------------------------------
11252342 52
eb1bab5b 53 /**
ea21cd65 54 * The trace control system type defined for LTTng version 2.0 and later.
eb1bab5b 55 */
77735e82 56 public static final String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$
eb1bab5b
BH
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
11252342 61
eb1bab5b 62 /**
ea21cd65 63 * The parent trace control component the new node will be added to.
eb1bab5b 64 */
4775bcbf 65 private ITraceControlComponent fRoot = null;
eb1bab5b 66
eb1bab5b
BH
67 @Override
68 public Object execute(ExecutionEvent event) throws ExecutionException {
4775bcbf 69 assert (fRoot != null);
eb1bab5b
BH
70
71 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
72 if (window == null) {
73 return false;
74 }
75
76 ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
ea21cd65 77
eb1bab5b
BH
78 // get system type definition for LTTng 2.x connection
79 IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE);
ea21cd65 80
eb1bab5b 81 // get all hosts for this system type
ef5356e1 82 IHost[] hosts = getSuitableHosts();
eb1bab5b
BH
83
84 // Open dialog box for the node name and address
d132bcc7
BH
85 final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog();
86 dialog.setTraceControlParent(fRoot);
87 dialog.setHosts(hosts);
00de7b32 88 dialog.setPort(IRemoteSystemProxy.INVALID_PORT_NUMBER);
eb1bab5b 89
6503ae0f
BH
90 if (dialog.open() != Window.OK) {
91 return null;
92 }
eb1bab5b 93
ea21cd65 94 String hostName = dialog.getConnectionName();
6503ae0f 95 String hostAddress = dialog.getHostName();
00de7b32 96 int port = dialog.getPort();
eb1bab5b 97
6503ae0f
BH
98 // get the singleton RSE registry
99 IHost host = null;
eb1bab5b 100
6503ae0f
BH
101 for (int i = 0; i < hosts.length; i++) {
102 if (hosts[i].getAliasName().equals(hostName)) {
103 host = hosts[i];
104 break;
eb1bab5b 105 }
6503ae0f 106 }
eb1bab5b 107
6503ae0f
BH
108 if (host == null) {
109 // if there's no host then we will create it
110 try {
111 // create the host object as an SSH Only connection
112 host = registry.createHost(
113 sysType, //System Type Name
114 hostName, //Connection name
ea21cd65 115 hostAddress, //IP Address
6503ae0f 116 "Connection to Host"); //description //$NON-NLS-1$
eb1bab5b 117 }
6503ae0f
BH
118 catch (Exception e) {
119 MessageDialog.openError(window.getShell(),
120 Messages.TraceControl_EclipseCommandFailure,
121 Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
122 return null;
eb1bab5b
BH
123 }
124 }
ea21cd65 125
6503ae0f 126 if (host != null) {
c56972bb
BH
127 fLock.lock();
128 try {
129 // successful creation of host
130 TargetNodeComponent node = null;
131 if (!fRoot.containsChild(hostName)) {
132 node = new TargetNodeComponent(hostName, fRoot, host);
00de7b32 133 node.setPort(port);
c56972bb
BH
134 fRoot.addChild(node);
135 }
136 else {
137 node = (TargetNodeComponent)fRoot.getChild(hostName);
138 }
139
140 node.connect();
141 } finally {
142 fLock.unlock();
6503ae0f 143 }
6503ae0f 144 }
eb1bab5b
BH
145 return null;
146 }
147
ea21cd65
AM
148 private static IHost[] getSuitableHosts() {
149 // need shells and files
e0838ca1
AM
150 ArrayList<IHost> result = new ArrayList<>();
151 ArrayList<IHost> shellConnections = new ArrayList<>(
ea21cd65
AM
152 Arrays.asList(RSECorePlugin.getTheSystemRegistry()
153 .getHostsBySubSystemConfigurationCategory("shells"))); //$NON-NLS-1$
154
155 for (IHost connection : shellConnections) {
a657b111
BH
156 ISubSystem[] subSystems = connection.getSubSystems();
157 for (int i = 0; i < subSystems.length; i++) {
158 if (subSystems[i] instanceof IFileServiceSubSystem) {
159 result.add(connection);
160 break;
ea21cd65
AM
161 }
162 }
163 }
4775bcbf 164
ea21cd65
AM
165 return result.toArray(new IHost[result.size()]);
166 }
ef5356e1 167
eb1bab5b
BH
168 @Override
169 public boolean isEnabled() {
ea21cd65 170
498704b3
BH
171 // Get workbench page for the Control View
172 IWorkbenchPage page = getWorkbenchPage();
173 if (page == null) {
eb1bab5b
BH
174 return false;
175 }
176
c56972bb 177 ITraceControlComponent root = null;
eb1bab5b 178
498704b3 179 // no need to verify part because it has been already done in getWorkbenchPage()
ea21cd65 180 IWorkbenchPart part = page.getActivePart();
c56972bb 181 root = ((ControlView) part).getTraceControlRoot();
ea21cd65 182
c56972bb 183 boolean isEnabled = root != null;
ea21cd65 184
c56972bb
BH
185 fLock.lock();
186 try {
187 fRoot = null;
188 if (isEnabled) {
189 fRoot = root;
190 }
191 } finally {
192 fLock.unlock();
193 }
ea21cd65 194
c56972bb 195 return isEnabled;
eb1bab5b
BH
196 }
197}
This page took 0.072731 seconds and 5 git commands to generate.