1 /**********************************************************************
2 * Copyright (c) 2012 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
.rse
.core
.IRSESystemType
;
29 import org
.eclipse
.rse
.core
.RSECorePlugin
;
30 import org
.eclipse
.rse
.core
.model
.IHost
;
31 import org
.eclipse
.rse
.core
.model
.ISystemRegistry
;
32 import org
.eclipse
.rse
.core
.subsystems
.ISubSystem
;
33 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.IFileServiceSubSystem
;
34 import org
.eclipse
.ui
.IWorkbenchPage
;
35 import org
.eclipse
.ui
.IWorkbenchPart
;
36 import org
.eclipse
.ui
.IWorkbenchWindow
;
37 import org
.eclipse
.ui
.PlatformUI
;
41 * Command handler for creation new connection for trace control.
44 * @author Bernd Hufmann
46 public class NewConnectionHandler
extends BaseControlViewHandler
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 * The trace control system type defined for LTTng version 2.0 and later.
54 public final static String TRACE_CONTROL_SYSTEM_TYPE
= "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 * The parent trace control component the new node will be added to.
62 private ITraceControlComponent fRoot
= null;
66 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
69 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
70 assert (fRoot
!= null);
72 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
77 ISystemRegistry registry
= RSECorePlugin
.getTheSystemRegistry();
79 // get system type definition for LTTng 2.x connection
80 IRSESystemType sysType
= RSECorePlugin
.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE
);
82 // get all hosts for this system type
83 IHost
[] hosts
= getSuitableHosts();
85 // Open dialog box for the node name and address
86 final INewConnectionDialog dialog
= TraceControlDialogFactory
.getInstance().getNewConnectionDialog();
87 dialog
.setTraceControlParent(fRoot
);
88 dialog
.setHosts(hosts
);
90 if (dialog
.open() != Window
.OK
) {
94 String hostName
= dialog
.getConnectionName();
95 String hostAddress
= dialog
.getHostName();
97 // get the singleton RSE registry
100 for (int i
= 0; i
< hosts
.length
; i
++) {
101 if (hosts
[i
].getAliasName().equals(hostName
)) {
108 // if there's no host then we will create it
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$
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$
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
);
135 node
= (TargetNodeComponent
)fRoot
.getChild(hostName
);
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$
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
);
165 return (IHost
[]) result
.toArray(new IHost
[result
.size()]);
171 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
174 public boolean isEnabled() {
176 // Get workbench page for the Control View
177 IWorkbenchPage page
= getWorkbenchPage();
182 ITraceControlComponent root
= null;
184 // no need to verify part because it has been already done in getWorkbenchPage()
185 IWorkbenchPart part
= page
.getActivePart();
186 root
= ((ControlView
) part
).getTraceControlRoot();
188 boolean isEnabled
= root
!= null;