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