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