Commit | Line | Data |
---|---|---|
eb1bab5b | 1 | /********************************************************************** |
00de7b32 | 2 | * Copyright (c) 2012, 2013 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 | 13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
eb1bab5b | 14 | |
ef5356e1 BH |
15 | import java.util.ArrayList; |
16 | import java.util.Arrays; | |
17 | ||
eb1bab5b BH |
18 | import org.eclipse.core.commands.ExecutionEvent; |
19 | import org.eclipse.core.commands.ExecutionException; | |
eb1bab5b BH |
20 | import org.eclipse.jface.dialogs.MessageDialog; |
21 | import org.eclipse.jface.window.Window; | |
115b4a01 | 22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
115b4a01 BH |
23 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog; |
24 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; | |
9315aeee | 25 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
115b4a01 BH |
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; | |
00de7b32 | 28 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy; |
eb1bab5b BH |
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; | |
ef5356e1 BH |
33 | import org.eclipse.rse.core.subsystems.ISubSystem; |
34 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; | |
eb1bab5b BH |
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 | /** | |
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 | 47 | public class NewConnectionHandler extends BaseControlViewHandler { |
eb1bab5b BH |
48 | |
49 | // ------------------------------------------------------------------------ | |
50 | // Constants | |
51 | // ------------------------------------------------------------------------ | |
52 | /** | |
ea21cd65 | 53 | * The trace control system type defined for LTTng version 2.0 and later. |
eb1bab5b | 54 | */ |
115b4a01 | 55 | public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$ |
eb1bab5b BH |
56 | |
57 | // ------------------------------------------------------------------------ | |
58 | // Attributes | |
59 | // ------------------------------------------------------------------------ | |
60 | /** | |
ea21cd65 | 61 | * The parent trace control component the new node will be added to. |
eb1bab5b | 62 | */ |
4775bcbf | 63 | private ITraceControlComponent fRoot = null; |
eb1bab5b BH |
64 | |
65 | /* | |
66 | * (non-Javadoc) | |
4775bcbf | 67 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
eb1bab5b BH |
68 | */ |
69 | @Override | |
70 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
4775bcbf | 71 | assert (fRoot != null); |
eb1bab5b BH |
72 | |
73 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
74 | if (window == null) { | |
75 | return false; | |
76 | } | |
77 | ||
78 | ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); | |
ea21cd65 | 79 | |
eb1bab5b BH |
80 | // get system type definition for LTTng 2.x connection |
81 | IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE); | |
ea21cd65 | 82 | |
eb1bab5b | 83 | // get all hosts for this system type |
ef5356e1 | 84 | IHost[] hosts = getSuitableHosts(); |
eb1bab5b BH |
85 | |
86 | // Open dialog box for the node name and address | |
d132bcc7 BH |
87 | final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog(); |
88 | dialog.setTraceControlParent(fRoot); | |
89 | dialog.setHosts(hosts); | |
00de7b32 | 90 | dialog.setPort(IRemoteSystemProxy.INVALID_PORT_NUMBER); |
eb1bab5b | 91 | |
6503ae0f BH |
92 | if (dialog.open() != Window.OK) { |
93 | return null; | |
94 | } | |
eb1bab5b | 95 | |
ea21cd65 | 96 | String hostName = dialog.getConnectionName(); |
6503ae0f | 97 | String hostAddress = dialog.getHostName(); |
00de7b32 | 98 | int port = dialog.getPort(); |
eb1bab5b | 99 | |
6503ae0f BH |
100 | // get the singleton RSE registry |
101 | IHost host = null; | |
eb1bab5b | 102 | |
6503ae0f BH |
103 | for (int i = 0; i < hosts.length; i++) { |
104 | if (hosts[i].getAliasName().equals(hostName)) { | |
105 | host = hosts[i]; | |
106 | break; | |
eb1bab5b | 107 | } |
6503ae0f | 108 | } |
eb1bab5b | 109 | |
6503ae0f BH |
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 | |
ea21cd65 | 117 | hostAddress, //IP Address |
6503ae0f | 118 | "Connection to Host"); //description //$NON-NLS-1$ |
eb1bab5b | 119 | } |
6503ae0f BH |
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; | |
eb1bab5b BH |
125 | } |
126 | } | |
ea21cd65 | 127 | |
6503ae0f | 128 | if (host != null) { |
c56972bb BH |
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); | |
00de7b32 | 135 | node.setPort(port); |
c56972bb BH |
136 | fRoot.addChild(node); |
137 | } | |
138 | else { | |
139 | node = (TargetNodeComponent)fRoot.getChild(hostName); | |
140 | } | |
141 | ||
142 | node.connect(); | |
143 | } finally { | |
144 | fLock.unlock(); | |
6503ae0f | 145 | } |
6503ae0f | 146 | } |
eb1bab5b BH |
147 | return null; |
148 | } | |
149 | ||
ea21cd65 AM |
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 | } | |
4775bcbf | 168 | |
ea21cd65 AM |
169 | return result.toArray(new IHost[result.size()]); |
170 | } | |
ef5356e1 | 171 | |
eb1bab5b BH |
172 | /* |
173 | * (non-Javadoc) | |
4775bcbf | 174 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
eb1bab5b BH |
175 | */ |
176 | @Override | |
177 | public boolean isEnabled() { | |
ea21cd65 | 178 | |
498704b3 BH |
179 | // Get workbench page for the Control View |
180 | IWorkbenchPage page = getWorkbenchPage(); | |
181 | if (page == null) { | |
eb1bab5b BH |
182 | return false; |
183 | } | |
184 | ||
c56972bb | 185 | ITraceControlComponent root = null; |
eb1bab5b | 186 | |
498704b3 | 187 | // no need to verify part because it has been already done in getWorkbenchPage() |
ea21cd65 | 188 | IWorkbenchPart part = page.getActivePart(); |
c56972bb | 189 | root = ((ControlView) part).getTraceControlRoot(); |
ea21cd65 | 190 | |
c56972bb | 191 | boolean isEnabled = root != null; |
ea21cd65 | 192 | |
c56972bb BH |
193 | fLock.lock(); |
194 | try { | |
195 | fRoot = null; | |
196 | if (isEnabled) { | |
197 | fRoot = root; | |
198 | } | |
199 | } finally { | |
200 | fLock.unlock(); | |
201 | } | |
ea21cd65 | 202 | |
c56972bb | 203 | return isEnabled; |
eb1bab5b BH |
204 | } |
205 | } |