1 /**********************************************************************
2 * Copyright (c) 2012, 2013 Ericsson and others
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
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
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
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
;
42 * Command handler for creation new connection for trace control.
45 * @author Bernd Hufmann
47 public class NewConnectionHandler
extends BaseControlViewHandler
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 * The trace control system type defined for LTTng version 2.0 and later.
55 public final static String TRACE_CONTROL_SYSTEM_TYPE
= "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 * The parent trace control component the new node will be added to.
63 private ITraceControlComponent fRoot
= null;
67 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
70 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
71 assert (fRoot
!= null);
73 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
78 ISystemRegistry registry
= RSECorePlugin
.getTheSystemRegistry();
80 // get system type definition for LTTng 2.x connection
81 IRSESystemType sysType
= RSECorePlugin
.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE
);
83 // get all hosts for this system type
84 IHost
[] hosts
= getSuitableHosts();
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
);
92 if (dialog
.open() != Window
.OK
) {
96 String hostName
= dialog
.getConnectionName();
97 String hostAddress
= dialog
.getHostName();
98 int port
= dialog
.getPort();
100 // get the singleton RSE registry
103 for (int i
= 0; i
< hosts
.length
; i
++) {
104 if (hosts
[i
].getAliasName().equals(hostName
)) {
111 // if there's no host then we will create it
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$
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$
131 // successful creation of host
132 TargetNodeComponent node
= null;
133 if (!fRoot
.containsChild(hostName
)) {
134 node
= new TargetNodeComponent(hostName
, fRoot
, host
);
136 fRoot
.addChild(node
);
139 node
= (TargetNodeComponent
)fRoot
.getChild(hostName
);
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$
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
);
169 return result
.toArray(new IHost
[result
.size()]);
174 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
177 public boolean isEnabled() {
179 // Get workbench page for the Control View
180 IWorkbenchPage page
= getWorkbenchPage();
185 ITraceControlComponent root
= null;
187 // no need to verify part because it has been already done in getWorkbenchPage()
188 IWorkbenchPart part
= page
.getActivePart();
189 root
= ((ControlView
) part
).getTraceControlRoot();
191 boolean isEnabled
= root
!= null;