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