Commit | Line | Data |
---|---|---|
291cbdbf | 1 | /********************************************************************** |
ba3a9bd2 | 2 | * Copyright (c) 2012, 2013 Ericsson |
cfdb727a | 3 | * |
291cbdbf BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
291cbdbf | 10 | * Bernd Hufmann - Initial API and implementation |
ba3a9bd2 | 11 | * Bernd Hufmann - Updated for support of streamed traces |
291cbdbf BH |
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; | |
9315aeee | 31 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState; |
291cbdbf BH |
32 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
291cbdbf | 34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog; |
f455db37 | 35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ImportFileInfo; |
291cbdbf | 36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; |
9315aeee | 37 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
291cbdbf BH |
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 | /** | |
291cbdbf BH |
49 | * <p> |
50 | * Command handler implementation to import traces from a (remote) session to a tracing project. | |
51 | * </p> | |
cfdb727a | 52 | * |
dbd4432d | 53 | * @author Bernd Hufmann |
291cbdbf BH |
54 | */ |
55 | public class ImportHandler extends BaseControlViewHandler { | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Attributes | |
59 | // ------------------------------------------------------------------------ | |
6f4e8ec0 AM |
60 | |
61 | /** | |
62 | * The command parameter | |
63 | */ | |
291cbdbf | 64 | protected CommandParameter fParam; |
cfdb727a | 65 | |
291cbdbf BH |
66 | // ------------------------------------------------------------------------ |
67 | // Operations | |
68 | // ------------------------------------------------------------------------ | |
cfdb727a | 69 | |
291cbdbf BH |
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(); | |
cfdb727a | 82 | |
291cbdbf BH |
83 | final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog(); |
84 | dialog.setSession(param.getSession()); | |
85 | ||
f3b33d40 | 86 | if ((dialog.open() != Window.OK) || param.getSession().isStreamedTrace()) { |
291cbdbf BH |
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(); | |
cfdb727a | 96 | |
291cbdbf | 97 | for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) { |
cfdb727a | 98 | ImportFileInfo remoteFile = iterator.next(); |
291cbdbf BH |
99 | downloadTrace(remoteFile, project); |
100 | } | |
101 | ||
102 | } catch (ExecutionException e) { | |
cfdb727a AM |
103 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e); |
104 | } | |
291cbdbf BH |
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 | ||
291cbdbf BH |
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();) { | |
cfdb727a | 130 | Object element = iterator.next(); |
291cbdbf BH |
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 | } | |
cfdb727a | 153 | |
291cbdbf BH |
154 | // ------------------------------------------------------------------------ |
155 | // Helper methods | |
156 | // ------------------------------------------------------------------------ | |
11252342 | 157 | |
291cbdbf BH |
158 | /** |
159 | * Downloads a trace from the remote host to the given project. | |
cfdb727a AM |
160 | * |
161 | * @param trace | |
162 | * - trace information of trace to import | |
163 | * @param project | |
164 | * - project to import to | |
291cbdbf BH |
165 | * @throws ExecutionException |
166 | */ | |
0a78d11a AM |
167 | private static void downloadTrace(ImportFileInfo trace, IProject project) |
168 | throws ExecutionException { | |
291cbdbf BH |
169 | try { |
170 | IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem(); | |
cfdb727a | 171 | |
291cbdbf BH |
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 | } | |
cfdb727a | 195 | |
291cbdbf | 196 | fsss.downloadMultiple(sources, destinations, encodings, new NullProgressMonitor()); |
cfdb727a | 197 | |
291cbdbf | 198 | } catch (SystemMessageException e) { |
9fa32496 | 199 | throw new ExecutionException(e.toString(), e); |
291cbdbf | 200 | } catch (CoreException e) { |
9fa32496 | 201 | throw new ExecutionException(e.toString(), e); |
291cbdbf BH |
202 | } |
203 | } | |
204 | } |