Fix for bug 382684 (connection re-use)
[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
eb1bab5b
BH
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:
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>
dbd4432d
BH
43 *
44 * @author Bernd Hufmann
eb1bab5b 45 */
498704b3 46public class NewConnectionHandler extends BaseControlViewHandler {
eb1bab5b
BH
47
48 // ------------------------------------------------------------------------
49 // Constants
50 // ------------------------------------------------------------------------
51 /**
52 * The trace control system type defined for LTTng version 2.0 and later.
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 /**
60 * The parent trace control component the new node will be added to.
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();
78
79 // get system type definition for LTTng 2.x connection
80 IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE);
81
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
6503ae0f
BH
94 String hostName = dialog.getConnectionName();
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
114 hostAddress, //IP Address
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 }
c56972bb 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
4775bcbf 146
ef5356e1
BH
147 private IHost[] getSuitableHosts() {
148 //need shells and files
149 ArrayList<IHost> result = new ArrayList<IHost>();
150 ArrayList<IHost> shellConnections = new ArrayList<IHost>(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 }
164
165 return (IHost[]) result.toArray(new IHost[result.size()]);
166 }
167
168
eb1bab5b
BH
169 /*
170 * (non-Javadoc)
4775bcbf 171 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
eb1bab5b
BH
172 */
173 @Override
174 public boolean isEnabled() {
498704b3
BH
175
176 // Get workbench page for the Control View
177 IWorkbenchPage page = getWorkbenchPage();
178 if (page == null) {
eb1bab5b
BH
179 return false;
180 }
181
c56972bb 182 ITraceControlComponent root = null;
eb1bab5b 183
498704b3
BH
184 // no need to verify part because it has been already done in getWorkbenchPage()
185 IWorkbenchPart part = page.getActivePart();
c56972bb
BH
186 root = ((ControlView) part).getTraceControlRoot();
187
188 boolean isEnabled = root != null;
189
190 fLock.lock();
191 try {
192 fRoot = null;
193 if (isEnabled) {
194 fRoot = root;
195 }
196 } finally {
197 fLock.unlock();
198 }
eb1bab5b 199
c56972bb 200 return isEnabled;
eb1bab5b
BH
201 }
202}
This page took 0.039173 seconds and 5 git commands to generate.