| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2013 Ericsson |
| 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 | * Bernd Hufmann - Updated for support of streamed traces |
| 12 | **********************************************************************/ |
| 13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
| 14 | |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.core.commands.ExecutionEvent; |
| 19 | import org.eclipse.core.commands.ExecutionException; |
| 20 | import org.eclipse.core.resources.IFolder; |
| 21 | import org.eclipse.core.resources.IProject; |
| 22 | import org.eclipse.core.runtime.CoreException; |
| 23 | import org.eclipse.core.runtime.IProgressMonitor; |
| 24 | import org.eclipse.core.runtime.IStatus; |
| 25 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 26 | import org.eclipse.core.runtime.Status; |
| 27 | import org.eclipse.core.runtime.jobs.Job; |
| 28 | import org.eclipse.jface.viewers.ISelection; |
| 29 | import org.eclipse.jface.viewers.StructuredSelection; |
| 30 | import org.eclipse.jface.window.Window; |
| 31 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState; |
| 32 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
| 34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog; |
| 35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ImportFileInfo; |
| 36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; |
| 37 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 38 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
| 39 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 40 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
| 41 | import org.eclipse.rse.services.files.IFileService; |
| 42 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; |
| 43 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem; |
| 44 | import org.eclipse.ui.IWorkbenchPage; |
| 45 | import org.eclipse.ui.IWorkbenchWindow; |
| 46 | import org.eclipse.ui.PlatformUI; |
| 47 | |
| 48 | /** |
| 49 | * <p> |
| 50 | * Command handler implementation to import traces from a (remote) session to a tracing project. |
| 51 | * </p> |
| 52 | * |
| 53 | * @author Bernd Hufmann |
| 54 | */ |
| 55 | public class ImportHandler extends BaseControlViewHandler { |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // Attributes |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | /** |
| 62 | * The command parameter |
| 63 | */ |
| 64 | protected CommandParameter fParam; |
| 65 | |
| 66 | // ------------------------------------------------------------------------ |
| 67 | // Operations |
| 68 | // ------------------------------------------------------------------------ |
| 69 | |
| 70 | @Override |
| 71 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 72 | |
| 73 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 74 | |
| 75 | if (window == null) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | fLock.lock(); |
| 80 | try { |
| 81 | final CommandParameter param = fParam.clone(); |
| 82 | |
| 83 | final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog(); |
| 84 | dialog.setSession(param.getSession()); |
| 85 | |
| 86 | if ((dialog.open() != Window.OK) || param.getSession().isStreamedTrace()) { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | Job job = new Job(Messages.TraceControl_ImportJob) { |
| 91 | @Override |
| 92 | protected IStatus run(IProgressMonitor monitor) { |
| 93 | try { |
| 94 | List<ImportFileInfo> traces = dialog.getTracePathes(); |
| 95 | IProject project = dialog.getProject(); |
| 96 | |
| 97 | for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) { |
| 98 | ImportFileInfo remoteFile = iterator.next(); |
| 99 | downloadTrace(remoteFile, project); |
| 100 | } |
| 101 | |
| 102 | } catch (ExecutionException e) { |
| 103 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e); |
| 104 | } |
| 105 | return Status.OK_STATUS; |
| 106 | } |
| 107 | }; |
| 108 | job.setUser(true); |
| 109 | job.schedule(); |
| 110 | } finally { |
| 111 | fLock.unlock(); |
| 112 | } |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean isEnabled() { |
| 118 | // Get workbench page for the Control View |
| 119 | IWorkbenchPage page = getWorkbenchPage(); |
| 120 | if (page == null) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // Check if one or more session are selected |
| 125 | ISelection selection = page.getSelection(ControlView.ID); |
| 126 | TraceSessionComponent session = null; |
| 127 | if (selection instanceof StructuredSelection) { |
| 128 | StructuredSelection structered = ((StructuredSelection) selection); |
| 129 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 130 | Object element = iterator.next(); |
| 131 | if (element instanceof TraceSessionComponent) { |
| 132 | // Add only TraceSessionComponents that are inactive and not destroyed |
| 133 | TraceSessionComponent tmpSession = (TraceSessionComponent) element; |
| 134 | if ((tmpSession.getSessionState() == TraceSessionState.INACTIVE) && (!tmpSession.isDestroyed())) { |
| 135 | session = tmpSession; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | boolean isEnabled = session != null; |
| 141 | |
| 142 | fLock.lock(); |
| 143 | try { |
| 144 | fParam = null; |
| 145 | if (isEnabled) { |
| 146 | fParam = new CommandParameter(session); |
| 147 | } |
| 148 | } finally { |
| 149 | fLock.unlock(); |
| 150 | } |
| 151 | return isEnabled; |
| 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | // Helper methods |
| 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
| 159 | * Downloads a trace from the remote host to the given project. |
| 160 | * |
| 161 | * @param trace |
| 162 | * - trace information of trace to import |
| 163 | * @param project |
| 164 | * - project to import to |
| 165 | * @throws ExecutionException |
| 166 | */ |
| 167 | private static void downloadTrace(ImportFileInfo trace, IProject project) |
| 168 | throws ExecutionException { |
| 169 | try { |
| 170 | IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem(); |
| 171 | |
| 172 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); |
| 173 | if (!traceFolder.exists()) { |
| 174 | throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ |
| 175 | } |
| 176 | |
| 177 | String traceName = trace.getLocalTraceName(); |
| 178 | IFolder folder = traceFolder.getFolder(traceName); |
| 179 | if (folder.exists()) { |
| 180 | if(!trace.isOverwrite()) { |
| 181 | throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$ |
| 182 | } |
| 183 | } else { |
| 184 | folder.create(true, true, null); |
| 185 | } |
| 186 | |
| 187 | IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor()); |
| 188 | |
| 189 | String[] destinations = new String[sources.length]; |
| 190 | String[] encodings = new String[sources.length]; |
| 191 | for (int i = 0; i < sources.length; i++) { |
| 192 | destinations[i] = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString(); |
| 193 | encodings[i] = null; |
| 194 | } |
| 195 | |
| 196 | fsss.downloadMultiple(sources, destinations, encodings, new NullProgressMonitor()); |
| 197 | |
| 198 | } catch (SystemMessageException e) { |
| 199 | throw new ExecutionException(e.toString(), e); |
| 200 | } catch (CoreException e) { |
| 201 | throw new ExecutionException(e.toString(), e); |
| 202 | } |
| 203 | } |
| 204 | } |