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