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
.TmfTraceType
;
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
.wizards
.importtrace
.BatchImportTraceWizard
;
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
;
66 * Command handler implementation to import traces from a (remote) session to a tracing project.
69 * @author Bernd Hufmann
71 public class ImportHandler
extends BaseControlViewHandler
{
73 // ------------------------------------------------------------------------
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$
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * The command parameter
90 protected CommandParameter fParam
;
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
99 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
101 if (window
== null) {
107 final CommandParameter param
= fParam
.clone();
109 // create default project
110 IProject project
= TmfProjectRegistry
.createProject(DEFAULT_REMOTE_PROJECT_NAME
, null, null);
112 if (param
.getSession().isStreamedTrace()) {
114 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
115 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
117 BatchImportTraceWizard wizard
= new BatchImportTraceWizard();
118 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
119 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
125 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
126 dialog
.setSession(param
.getSession());
127 dialog
.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME
);
129 if (dialog
.open() != Window
.OK
) {
133 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
135 protected IStatus
run(IProgressMonitor monitor
) {
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();) {
142 ImportFileInfo remoteFile
= iterator
.next();
144 downloadTrace(remoteFile
, selectedProject
, monitor
);
147 IFolder traceFolder
= selectedProject
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
149 if (monitor
.isCanceled()) {
150 status
.add(Status
.CANCEL_STATUS
);
154 IResource file
= traceFolder
.findMember(remoteFile
.getLocalTraceName());
156 TraceTypeHelper helper
= null;
158 if (remoteFile
.isKernel()) {
159 helper
= TmfTraceType
.getInstance().getTraceType(LTTNG_KERNEL_TRACE_TYPE
);
161 helper
= TmfTraceType
.getInstance().getTraceType(GENERIC_CTF_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
) {
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
));
199 public boolean isEnabled() {
200 // Get workbench page for the Control View
201 IWorkbenchPage page
= getWorkbenchPage();
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();) {
212 Object element
= iterator
.next();
213 if (element
instanceof TraceSessionComponent
) {
214 // Add only TraceSessionComponents that are inactive and not destroyed
215 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
216 if (((tmpSession
.isSnapshotSession()) || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
217 session
= tmpSession
;
222 boolean isEnabled
= session
!= null;
228 fParam
= new CommandParameter(session
);
236 // ------------------------------------------------------------------------
238 // ------------------------------------------------------------------------
241 * Downloads a trace from the remote host to the given project.
244 * - trace information of trace to import
246 * - project to import to
248 * - a progress monitor
249 * @throws ExecutionException
251 private static void downloadTrace(ImportFileInfo trace
, IProject project
, IProgressMonitor monitor
)
252 throws ExecutionException
{
254 IRemoteFileSubSystem fsss
= trace
.getImportFile().getParentRemoteFileSubSystem();
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$
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$
268 folder
.create(true, true, null);
271 IRemoteFile
[] sources
= fsss
.list(trace
.getImportFile(), IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
272 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, sources
.length
);
273 subMonitor
.beginTask(Messages
.TraceControl_DownloadTask
, sources
.length
);
275 for (int i
= 0; i
< sources
.length
; i
++) {
276 if (subMonitor
.isCanceled()) {
277 monitor
.setCanceled(true);
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));
284 } catch (SystemMessageException e
) {
285 throw new ExecutionException(e
.toString(), e
);
286 } catch (CoreException e
) {
287 throw new ExecutionException(e
.toString(), e
);