1 /**********************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Bernd Hufmann - Updated for support of streamed traces
12 * Patrick Tasse - Add support for source location
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.handlers
;
17 import java
.net
.URISyntaxException
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
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
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.MultiStatus
;
31 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.Status
;
33 import org
.eclipse
.core
.runtime
.SubMonitor
;
34 import org
.eclipse
.core
.runtime
.URIUtil
;
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
;
39 import org
.eclipse
.jface
.wizard
.WizardDialog
;
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
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceImportException
;
50 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
52 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
53 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
54 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTracesFolder
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceUtils
;
57 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
.importtrace
.ImportTraceWizard
;
58 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
59 import org
.eclipse
.rse
.services
.files
.IFileService
;
60 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
61 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFileSubSystem
;
62 import org
.eclipse
.ui
.IWorkbenchPage
;
63 import org
.eclipse
.ui
.IWorkbenchWindow
;
64 import org
.eclipse
.ui
.PlatformUI
;
68 * Command handler implementation to import traces from a (remote) session to a tracing project.
71 * @author Bernd Hufmann
73 public class ImportHandler
extends BaseControlViewHandler
{
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
78 /** Name of default project to import traces to */
79 public static final String DEFAULT_REMOTE_PROJECT_NAME
= "Remote"; //$NON-NLS-1$
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * The command parameter
88 protected CommandParameter fParam
;
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
97 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
105 final CommandParameter param
= fParam
.clone();
107 // create default project
108 IProject project
= TmfProjectRegistry
.createProject(DEFAULT_REMOTE_PROJECT_NAME
, null, null);
110 if (param
.getSession().isStreamedTrace()) {
112 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
113 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
115 ImportTraceWizard wizard
= new ImportTraceWizard();
116 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
117 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
123 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
124 dialog
.setSession(param
.getSession());
125 dialog
.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME
);
127 if (dialog
.open() != Window
.OK
) {
131 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
133 protected IStatus
run(IProgressMonitor monitor
) {
135 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, Messages
.TraceControl_ImportFailure
, null);
136 List
<ImportFileInfo
> traces
= dialog
.getTracePathes();
137 IProject selectedProject
= dialog
.getProject();
138 for (Iterator
<ImportFileInfo
> iterator
= traces
.iterator(); iterator
.hasNext();) {
141 if (monitor
.isCanceled()) {
142 status
.add(Status
.CANCEL_STATUS
);
146 ImportFileInfo remoteFile
= iterator
.next();
148 downloadTrace(remoteFile
, selectedProject
, monitor
);
151 IFolder traceFolder
= remoteFile
.getDestinationFolder();
153 IResource file
= traceFolder
.findMember(remoteFile
.getLocalTraceName());
156 TraceTypeHelper helper
= null;
159 helper
= TmfTraceTypeUIUtils
.selectTraceType(file
.getLocationURI().getPath(), null, null);
160 } catch (TmfTraceImportException e
) {
161 // the trace did not match any trace type
164 if (helper
!= null) {
165 status
.add(TmfTraceTypeUIUtils
.setTraceType(file
, helper
));
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
;
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
) {
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
));
200 public boolean isEnabled() {
201 // Get workbench page for the Control View
202 IWorkbenchPage page
= getWorkbenchPage();
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();) {
213 Object element
= iterator
.next();
214 if (element
instanceof TraceSessionComponent
) {
215 // Add only TraceSessionComponents that are inactive and not destroyed
216 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
217 if (((tmpSession
.isSnapshotSession()) || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
218 session
= tmpSession
;
223 boolean isEnabled
= session
!= null;
229 fParam
= new CommandParameter(session
);
237 // ------------------------------------------------------------------------
239 // ------------------------------------------------------------------------
242 * Downloads a trace from the remote host to the given project.
245 * - trace information of trace to import
247 * - project to import to
249 * - a progress monitor
250 * @throws ExecutionException
252 private static void downloadTrace(ImportFileInfo trace
, IProject project
, IProgressMonitor monitor
)
253 throws ExecutionException
{
255 IRemoteFileSubSystem fsss
= trace
.getImportFile().getParentRemoteFileSubSystem();
257 IFolder traceFolder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
258 if (!traceFolder
.exists()) {
259 throw new ExecutionException(Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTracesFolder
.TRACES_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
262 IFolder destinationFolder
= trace
.getDestinationFolder();
263 TraceUtils
.createFolder(destinationFolder
, monitor
);
265 String traceName
= trace
.getLocalTraceName();
266 IFolder folder
= destinationFolder
.getFolder(traceName
);
267 if (folder
.exists()) {
268 if(!trace
.isOverwrite()) {
269 throw new ExecutionException(Messages
.TraceControl_ImportDialogTraceAlreadyExistError
+ ": " + traceName
); //$NON-NLS-1$
272 folder
.create(true, true, null);
275 IRemoteFile
[] sources
= fsss
.list(trace
.getImportFile(), IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
276 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, sources
.length
);
277 subMonitor
.beginTask(Messages
.TraceControl_DownloadTask
, sources
.length
);
279 for (int i
= 0; i
< sources
.length
; i
++) {
280 if (subMonitor
.isCanceled()) {
281 monitor
.setCanceled(true);
284 String destination
= folder
.getLocation().addTrailingSeparator().append(sources
[i
].getName()).toString();
285 subMonitor
.setTaskName(Messages
.TraceControl_DownloadTask
+ ' ' + traceName
+ '/' +sources
[i
].getName());
286 fsss
.download(sources
[i
], destination
, null, subMonitor
.newChild(1));
288 } catch (SystemMessageException e
) {
289 throw new ExecutionException(e
.toString(), e
);
290 } catch (CoreException e
) {
291 throw new ExecutionException(e
.toString(), e
);