| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012 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.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; |
| 38 | |
| 39 | /** |
| 40 | * <p> |
| 41 | * Command handler for creation new connection for trace control. |
| 42 | * </p> |
| 43 | * |
| 44 | * @author Bernd Hufmann |
| 45 | */ |
| 46 | public class NewConnectionHandler extends BaseControlViewHandler { |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Constants |
| 50 | // ------------------------------------------------------------------------ |
| 51 | /** |
| 52 | * The trace control system type defined for LTTng version 2.0 and later. |
| 53 | */ |
| 54 | public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$ |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Attributes |
| 58 | // ------------------------------------------------------------------------ |
| 59 | /** |
| 60 | * The parent trace control component the new node will be added to. |
| 61 | */ |
| 62 | private ITraceControlComponent fRoot = null; |
| 63 | |
| 64 | /* |
| 65 | * (non-Javadoc) |
| 66 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 67 | */ |
| 68 | @Override |
| 69 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 70 | assert (fRoot != null); |
| 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 |
| 83 | IHost[] hosts = getSuitableHosts(); |
| 84 | |
| 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); |
| 89 | |
| 90 | if (dialog.open() != Window.OK) { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | String hostName = dialog.getConnectionName(); |
| 95 | String hostAddress = dialog.getHostName(); |
| 96 | |
| 97 | // get the singleton RSE registry |
| 98 | IHost host = null; |
| 99 | |
| 100 | for (int i = 0; i < hosts.length; i++) { |
| 101 | if (hosts[i].getAliasName().equals(hostName)) { |
| 102 | host = hosts[i]; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 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$ |
| 116 | } |
| 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; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (host != null) { |
| 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(); |
| 141 | } |
| 142 | } |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | |
| 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 | |
| 169 | /* |
| 170 | * (non-Javadoc) |
| 171 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
| 172 | */ |
| 173 | @Override |
| 174 | public boolean isEnabled() { |
| 175 | |
| 176 | // Get workbench page for the Control View |
| 177 | IWorkbenchPage page = getWorkbenchPage(); |
| 178 | if (page == null) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | ITraceControlComponent root = null; |
| 183 | |
| 184 | // no need to verify part because it has been already done in getWorkbenchPage() |
| 185 | IWorkbenchPart part = page.getActivePart(); |
| 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 | } |
| 199 | |
| 200 | return isEnabled; |
| 201 | } |
| 202 | } |