| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2015 École Polytechnique de Montréal |
| 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 | * Patrick-Jeffrey Pollo Guilbert - Added headers, exporting .lttng profiles |
| 11 | * William Enright - Added ProfileHandler implementation |
| 12 | * William Tri-Khiem Truong - Completed documentation |
| 13 | **********************************************************************/ |
| 14 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers; |
| 15 | |
| 16 | import java.io.File; |
| 17 | import java.io.IOException; |
| 18 | import java.io.OutputStream; |
| 19 | import java.nio.file.Files; |
| 20 | import java.util.ArrayList; |
| 21 | |
| 22 | import org.eclipse.core.commands.ExecutionEvent; |
| 23 | import org.eclipse.core.commands.ExecutionException; |
| 24 | import org.eclipse.core.commands.NotEnabledException; |
| 25 | import org.eclipse.core.commands.NotHandledException; |
| 26 | import org.eclipse.core.commands.common.NotDefinedException; |
| 27 | import org.eclipse.core.filesystem.EFS; |
| 28 | import org.eclipse.core.filesystem.IFileStore; |
| 29 | import org.eclipse.core.runtime.CoreException; |
| 30 | import org.eclipse.core.runtime.IProgressMonitor; |
| 31 | import org.eclipse.core.runtime.IStatus; |
| 32 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 33 | import org.eclipse.core.runtime.Path; |
| 34 | import org.eclipse.core.runtime.Status; |
| 35 | import org.eclipse.core.runtime.jobs.Job; |
| 36 | import org.eclipse.jface.viewers.ISelection; |
| 37 | import org.eclipse.jface.viewers.StructuredSelection; |
| 38 | import org.eclipse.jface.window.Window; |
| 39 | import org.eclipse.remote.core.IRemoteConnectionType; |
| 40 | import org.eclipse.remote.core.IRemoteFileService; |
| 41 | import org.eclipse.swt.SWT; |
| 42 | import org.eclipse.swt.graphics.Rectangle; |
| 43 | import org.eclipse.swt.widgets.Display; |
| 44 | import org.eclipse.swt.widgets.Shell; |
| 45 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
| 46 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView; |
| 47 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.ProfileDialog; |
| 48 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; |
| 49 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent; |
| 50 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionGroup; |
| 51 | import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy; |
| 52 | import org.eclipse.ui.IWorkbenchPage; |
| 53 | import org.eclipse.ui.IWorkbenchPart; |
| 54 | import org.eclipse.ui.handlers.HandlerUtil; |
| 55 | import org.eclipse.ui.handlers.IHandlerService; |
| 56 | |
| 57 | /** |
| 58 | * <p> |
| 59 | * Profile Handler to select and load session profiles for tracing purposes |
| 60 | * </p> |
| 61 | * |
| 62 | * @author William Tri-Khiem Truong, William Enright, Patrick-Jeffrey Pollo Guilbert |
| 63 | */ |
| 64 | public class ProfileHandler extends BaseControlViewHandler { |
| 65 | |
| 66 | /** |
| 67 | * Id of the parameter for the remote services id. |
| 68 | * |
| 69 | * @see NewConnectionHandler |
| 70 | * @see IRemoteConnectionType#getId() |
| 71 | */ |
| 72 | public static final String PARAMETER_REMOTE_SERVICES_ID = "org.eclipse.linuxtools.lttng2.control.ui.remoteServicesIdParameter"; //$NON-NLS-1$ |
| 73 | |
| 74 | /** |
| 75 | * (INewConnectionDialog) wd).getConnection() Id of the parameter for the |
| 76 | * name of the remote connection. |
| 77 | * |
| 78 | * @see NewConnectionHandler |
| 79 | * @see IRemoteConnectionType#getName() |
| 80 | */ |
| 81 | public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$ |
| 82 | |
| 83 | @Override |
| 84 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 85 | |
| 86 | Shell s = new Shell(SWT.CENTER); |
| 87 | |
| 88 | Rectangle screenSize = Display.getCurrent().getPrimaryMonitor().getBounds(); |
| 89 | s.setLocation((screenSize.width - s.getBounds().width) / 2, (screenSize.height - s.getBounds().height) / 2); |
| 90 | s.setMinimumSize(500, 500); |
| 91 | |
| 92 | IWorkbenchPage page = getWorkbenchPage(); |
| 93 | if (page == null) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | TargetNodeComponent elementParent = null; |
| 98 | |
| 99 | // Check if the session group project is selected |
| 100 | ISelection selection = page.getSelection(ControlView.ID); |
| 101 | if (selection instanceof StructuredSelection) { |
| 102 | Object element = ((StructuredSelection) selection).getFirstElement(); |
| 103 | final TraceSessionGroup sessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null; |
| 104 | if (sessionGroup != null) |
| 105 | { |
| 106 | if (sessionGroup.getParent() instanceof TargetNodeComponent) { |
| 107 | elementParent = (TargetNodeComponent) sessionGroup.getParent(); |
| 108 | final ProfileDialog btd = new ProfileDialog(s); |
| 109 | if (btd.open() != Window.OK) { |
| 110 | return null; |
| 111 | } |
| 112 | final String remotePath = sessionGroup.getTargetNode().getRemoteSystemProxy().getRemoteConnection().getProperty("user.home") + "/.lttng/sessions"; //$NON-NLS-1$//$NON-NLS-2$ |
| 113 | |
| 114 | RemoteSystemProxy proxy = elementParent.getRemoteSystemProxy(); |
| 115 | IRemoteFileService fsss = proxy.getRemoteConnection().getService(IRemoteFileService.class); |
| 116 | ArrayList<File> checkedFiles = btd.getCheckedFiles(); |
| 117 | |
| 118 | for (File file : checkedFiles) { |
| 119 | final IFileStore remoteFolder = fsss.getResource(remotePath); |
| 120 | final IFileStore remoteFile = remoteFolder.getFileStore(new Path(file.getName())); |
| 121 | try { |
| 122 | try (OutputStream out = remoteFile.openOutputStream(EFS.NONE, new NullProgressMonitor())) |
| 123 | { |
| 124 | Files.copy(file.toPath(), out); |
| 125 | } |
| 126 | } catch (CoreException e) { |
| 127 | e.printStackTrace(); |
| 128 | } catch (IOException e) { |
| 129 | e.printStackTrace(); |
| 130 | } |
| 131 | |
| 132 | Job job = new Job(Messages.TraceControl_LoadSessionJob) { |
| 133 | @Override |
| 134 | protected IStatus run(IProgressMonitor monitor) { |
| 135 | try { |
| 136 | sessionGroup.loadSession(remotePath + "/" + remoteFile.getName(), monitor); //$NON-NLS-1$ |
| 137 | } catch (ExecutionException e) { |
| 138 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_LoadSessionFailure, e); |
| 139 | } |
| 140 | return Status.OK_STATUS; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | job.setUser(true); |
| 145 | job.schedule(); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | // Refreshing the sessions in the control view by calling the refresh |
| 153 | // command |
| 154 | |
| 155 | IWorkbenchPart part = HandlerUtil.getActivePartChecked(event); |
| 156 | |
| 157 | IHandlerService service = part.getSite().getService(IHandlerService.class); |
| 158 | |
| 159 | try { |
| 160 | service.executeCommand("org.eclipse.linuxtools.internal.lttng2.ui.commands.control.refresh", null); //$NON-NLS-1$ |
| 161 | } catch (NotDefinedException e) { |
| 162 | |
| 163 | } catch (NotEnabledException e) { |
| 164 | |
| 165 | } catch (NotHandledException e) { |
| 166 | |
| 167 | } |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean isEnabled() { |
| 173 | |
| 174 | // Get workbench page for the Control View |
| 175 | IWorkbenchPage page = getWorkbenchPage(); |
| 176 | if (page == null) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | TraceSessionGroup sessionGroup = null; |
| 181 | |
| 182 | // Check if the session group project is selected |
| 183 | ISelection selection = page.getSelection(ControlView.ID); |
| 184 | if (selection instanceof StructuredSelection) { |
| 185 | Object element = ((StructuredSelection) selection).getFirstElement(); |
| 186 | sessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null; |
| 187 | } |
| 188 | |
| 189 | boolean isEnabled = sessionGroup != null; |
| 190 | fLock.lock(); |
| 191 | try { |
| 192 | if (isEnabled) { |
| 193 | } |
| 194 | } finally { |
| 195 | fLock.unlock(); |
| 196 | } |
| 197 | return isEnabled; |
| 198 | } |
| 199 | } |