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