Commit | Line | Data |
---|---|---|
291cbdbf | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 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 |
89730b51 | 12 | * Patrick Tasse - Add support for source location |
291cbdbf | 13 | **********************************************************************/ |
8e8c0226 | 14 | package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers; |
291cbdbf | 15 | |
89730b51 PT |
16 | import java.net.URI; |
17 | import java.net.URISyntaxException; | |
291cbdbf BH |
18 | import java.util.Iterator; |
19 | import java.util.List; | |
20 | ||
21 | import org.eclipse.core.commands.ExecutionEvent; | |
22 | import org.eclipse.core.commands.ExecutionException; | |
23 | import org.eclipse.core.resources.IFolder; | |
24 | import org.eclipse.core.resources.IProject; | |
a6e37e4c | 25 | import org.eclipse.core.resources.IResource; |
291cbdbf | 26 | import org.eclipse.core.runtime.CoreException; |
89730b51 | 27 | import org.eclipse.core.runtime.IPath; |
291cbdbf BH |
28 | import org.eclipse.core.runtime.IProgressMonitor; |
29 | import org.eclipse.core.runtime.IStatus; | |
cd9821de | 30 | import org.eclipse.core.runtime.MultiStatus; |
291cbdbf BH |
31 | import org.eclipse.core.runtime.NullProgressMonitor; |
32 | import org.eclipse.core.runtime.Status; | |
374565c6 | 33 | import org.eclipse.core.runtime.SubMonitor; |
89730b51 | 34 | import org.eclipse.core.runtime.URIUtil; |
291cbdbf BH |
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; | |
cd9821de | 39 | import org.eclipse.jface.wizard.WizardDialog; |
8e8c0226 AM |
40 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState; |
41 | import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator; | |
42 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView; | |
43 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.IImportDialog; | |
44 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.ImportFileInfo; | |
45 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory; | |
46 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages; | |
47 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; | |
89730b51 | 48 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
47aafe74 AM |
49 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
50 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
cd9821de BH |
51 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
52 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; | |
291cbdbf | 53 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
47aafe74 | 54 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; |
cd9821de | 55 | import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.BatchImportTraceWizard; |
291cbdbf BH |
56 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
57 | import org.eclipse.rse.services.files.IFileService; | |
58 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; | |
59 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem; | |
60 | import org.eclipse.ui.IWorkbenchPage; | |
61 | import org.eclipse.ui.IWorkbenchWindow; | |
62 | import org.eclipse.ui.PlatformUI; | |
63 | ||
64 | /** | |
291cbdbf BH |
65 | * <p> |
66 | * Command handler implementation to import traces from a (remote) session to a tracing project. | |
67 | * </p> | |
cfdb727a | 68 | * |
dbd4432d | 69 | * @author Bernd Hufmann |
291cbdbf BH |
70 | */ |
71 | public class ImportHandler extends BaseControlViewHandler { | |
72 | ||
cd9821de BH |
73 | // ------------------------------------------------------------------------ |
74 | // Constants | |
75 | // ------------------------------------------------------------------------ | |
76 | /** Trace Type ID for LTTng Kernel traces */ | |
77 | private static final String LTTNG_KERNEL_TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype"; //$NON-NLS-1$ | |
78 | /** Trace Type ID for Generic CTF traces */ | |
79 | private static final String GENERIC_CTF_TRACE_TYPE = "org.eclipse.linuxtools.tmf.ui.type.ctf"; //$NON-NLS-1$ | |
80 | /** Name of default project to import traces to */ | |
81 | public static final String DEFAULT_REMOTE_PROJECT_NAME = "Remote"; //$NON-NLS-1$ | |
82 | ||
291cbdbf BH |
83 | // ------------------------------------------------------------------------ |
84 | // Attributes | |
85 | // ------------------------------------------------------------------------ | |
6f4e8ec0 AM |
86 | |
87 | /** | |
88 | * The command parameter | |
89 | */ | |
291cbdbf | 90 | protected CommandParameter fParam; |
cfdb727a | 91 | |
291cbdbf BH |
92 | // ------------------------------------------------------------------------ |
93 | // Operations | |
94 | // ------------------------------------------------------------------------ | |
cfdb727a | 95 | |
291cbdbf BH |
96 | @Override |
97 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
98 | ||
99 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
100 | ||
101 | if (window == null) { | |
102 | return false; | |
103 | } | |
104 | ||
105 | fLock.lock(); | |
106 | try { | |
107 | final CommandParameter param = fParam.clone(); | |
cfdb727a | 108 | |
cd9821de BH |
109 | // create default project |
110 | IProject project = TmfProjectRegistry.createProject(DEFAULT_REMOTE_PROJECT_NAME, null, null); | |
cd9821de BH |
111 | |
112 | if (param.getSession().isStreamedTrace()) { | |
113 | // Streamed trace | |
f537c959 | 114 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); |
cd9821de BH |
115 | TmfTraceFolder traceFolder = projectElement.getTracesFolder(); |
116 | ||
117 | BatchImportTraceWizard wizard = new BatchImportTraceWizard(); | |
118 | wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder)); | |
119 | WizardDialog dialog = new WizardDialog(window.getShell(), wizard); | |
120 | dialog.open(); | |
cd9821de BH |
121 | return null; |
122 | } | |
123 | ||
124 | // Remote trace | |
291cbdbf BH |
125 | final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog(); |
126 | dialog.setSession(param.getSession()); | |
cd9821de | 127 | dialog.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME); |
291cbdbf | 128 | |
cd9821de | 129 | if (dialog.open() != Window.OK) { |
291cbdbf BH |
130 | return null; |
131 | } | |
132 | ||
133 | Job job = new Job(Messages.TraceControl_ImportJob) { | |
134 | @Override | |
135 | protected IStatus run(IProgressMonitor monitor) { | |
cfdb727a | 136 | |
cd9821de BH |
137 | MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, Messages.TraceControl_ImportFailure, null); |
138 | List<ImportFileInfo> traces = dialog.getTracePathes(); | |
139 | IProject selectedProject = dialog.getProject(); | |
140 | for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) { | |
141 | try { | |
cfdb727a | 142 | ImportFileInfo remoteFile = iterator.next(); |
291cbdbf | 143 | |
374565c6 | 144 | downloadTrace(remoteFile, selectedProject, monitor); |
cd9821de BH |
145 | |
146 | // Set trace type | |
147 | IFolder traceFolder = selectedProject.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
374565c6 | 148 | |
f537c959 | 149 | if (monitor.isCanceled()) { |
374565c6 BH |
150 | status.add(Status.CANCEL_STATUS); |
151 | break; | |
152 | } | |
153 | ||
a6e37e4c | 154 | IResource file = traceFolder.findMember(remoteFile.getLocalTraceName()); |
cd9821de BH |
155 | |
156 | TraceTypeHelper helper = null; | |
157 | ||
158 | if (remoteFile.isKernel()) { | |
159 | helper = TmfTraceType.getInstance().getTraceType(LTTNG_KERNEL_TRACE_TYPE); | |
160 | } else { | |
161 | helper = TmfTraceType.getInstance().getTraceType(GENERIC_CTF_TRACE_TYPE); | |
162 | } | |
163 | ||
164 | if (helper != null) { | |
a6e37e4c | 165 | status.add(TmfTraceTypeUIUtils.setTraceType(file, helper)); |
cd9821de | 166 | } |
89730b51 PT |
167 | |
168 | try { | |
169 | final String scheme = "sftp"; //$NON-NLS-1$ | |
170 | String host = remoteFile.getImportFile().getHost().getName(); | |
171 | int port = remoteFile.getImportFile().getParentRemoteFileSubSystem().getConnectorService().getPort(); | |
172 | String path = remoteFile.getImportFile().getAbsolutePath(); | |
173 | if (file instanceof IFolder) { | |
174 | path += IPath.SEPARATOR; | |
175 | } | |
176 | URI uri = new URI(scheme, null, host, port, path, null, null); | |
177 | String sourceLocation = URIUtil.toUnencodedString(uri); | |
178 | file.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); | |
179 | } catch (URISyntaxException e) { | |
180 | } | |
cd9821de BH |
181 | } catch (ExecutionException e) { |
182 | status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e)); | |
183 | } catch (CoreException e) { | |
184 | status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e)); | |
185 | } | |
cfdb727a | 186 | } |
cd9821de | 187 | return status; |
291cbdbf BH |
188 | } |
189 | }; | |
190 | job.setUser(true); | |
191 | job.schedule(); | |
192 | } finally { | |
193 | fLock.unlock(); | |
194 | } | |
195 | return null; | |
196 | } | |
197 | ||
291cbdbf BH |
198 | @Override |
199 | public boolean isEnabled() { | |
200 | // Get workbench page for the Control View | |
201 | IWorkbenchPage page = getWorkbenchPage(); | |
202 | if (page == null) { | |
203 | return false; | |
204 | } | |
205 | ||
206 | // Check if one or more session are selected | |
207 | ISelection selection = page.getSelection(ControlView.ID); | |
208 | TraceSessionComponent session = null; | |
209 | if (selection instanceof StructuredSelection) { | |
210 | StructuredSelection structered = ((StructuredSelection) selection); | |
211 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 212 | Object element = iterator.next(); |
291cbdbf BH |
213 | if (element instanceof TraceSessionComponent) { |
214 | // Add only TraceSessionComponents that are inactive and not destroyed | |
215 | TraceSessionComponent tmpSession = (TraceSessionComponent) element; | |
589d0d33 | 216 | if (((tmpSession.isSnapshotSession()) || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) { |
291cbdbf BH |
217 | session = tmpSession; |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | boolean isEnabled = session != null; | |
223 | ||
224 | fLock.lock(); | |
225 | try { | |
226 | fParam = null; | |
227 | if (isEnabled) { | |
228 | fParam = new CommandParameter(session); | |
229 | } | |
230 | } finally { | |
231 | fLock.unlock(); | |
232 | } | |
233 | return isEnabled; | |
234 | } | |
cfdb727a | 235 | |
291cbdbf BH |
236 | // ------------------------------------------------------------------------ |
237 | // Helper methods | |
238 | // ------------------------------------------------------------------------ | |
11252342 | 239 | |
291cbdbf BH |
240 | /** |
241 | * Downloads a trace from the remote host to the given project. | |
cfdb727a AM |
242 | * |
243 | * @param trace | |
244 | * - trace information of trace to import | |
245 | * @param project | |
246 | * - project to import to | |
374565c6 BH |
247 | * @param monitor |
248 | * - a progress monitor | |
291cbdbf BH |
249 | * @throws ExecutionException |
250 | */ | |
374565c6 | 251 | private static void downloadTrace(ImportFileInfo trace, IProject project, IProgressMonitor monitor) |
0a78d11a | 252 | throws ExecutionException { |
291cbdbf BH |
253 | try { |
254 | IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem(); | |
cfdb727a | 255 | |
291cbdbf BH |
256 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); |
257 | if (!traceFolder.exists()) { | |
258 | throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ | |
259 | } | |
260 | ||
261 | String traceName = trace.getLocalTraceName(); | |
262 | IFolder folder = traceFolder.getFolder(traceName); | |
263 | if (folder.exists()) { | |
264 | if(!trace.isOverwrite()) { | |
265 | throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$ | |
266 | } | |
267 | } else { | |
268 | folder.create(true, true, null); | |
269 | } | |
270 | ||
271 | IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor()); | |
374565c6 BH |
272 | SubMonitor subMonitor = SubMonitor.convert(monitor, sources.length); |
273 | subMonitor.beginTask(Messages.TraceControl_DownloadTask, sources.length); | |
291cbdbf | 274 | |
291cbdbf | 275 | for (int i = 0; i < sources.length; i++) { |
374565c6 BH |
276 | if (subMonitor.isCanceled()) { |
277 | monitor.setCanceled(true); | |
278 | return; | |
279 | } | |
280 | String destination = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString(); | |
281 | subMonitor.setTaskName(Messages.TraceControl_DownloadTask + ' ' + traceName + '/' +sources[i].getName()); | |
282 | fsss.download(sources[i], destination, null, subMonitor.newChild(1)); | |
291cbdbf | 283 | } |
291cbdbf | 284 | } catch (SystemMessageException e) { |
9fa32496 | 285 | throw new ExecutionException(e.toString(), e); |
291cbdbf | 286 | } catch (CoreException e) { |
9fa32496 | 287 | throw new ExecutionException(e.toString(), e); |
291cbdbf BH |
288 | } |
289 | } | |
290 | } |