1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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 **********************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.handlers
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
18 import org
.eclipse
.core
.commands
.ExecutionEvent
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.resources
.IFile
;
21 import org
.eclipse
.core
.resources
.IFolder
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.MultiStatus
;
27 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.core
.runtime
.SubMonitor
;
30 import org
.eclipse
.core
.runtime
.jobs
.Job
;
31 import org
.eclipse
.jface
.viewers
.ISelection
;
32 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
33 import org
.eclipse
.jface
.window
.Window
;
34 import org
.eclipse
.jface
.wizard
.WizardDialog
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.TraceSessionState
;
36 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
37 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.ControlView
;
38 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.IImportDialog
;
39 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.ImportFileInfo
;
40 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.TraceControlDialogFactory
;
41 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
42 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
.importtrace
.BatchImportTraceWizard
;
50 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
51 import org
.eclipse
.rse
.services
.files
.IFileService
;
52 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
53 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFileSubSystem
;
54 import org
.eclipse
.ui
.IWorkbenchPage
;
55 import org
.eclipse
.ui
.IWorkbenchWindow
;
56 import org
.eclipse
.ui
.PlatformUI
;
60 * Command handler implementation to import traces from a (remote) session to a tracing project.
63 * @author Bernd Hufmann
65 public class ImportHandler
extends BaseControlViewHandler
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
70 /** Trace Type ID for LTTng Kernel traces */
71 private static final String LTTNG_KERNEL_TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype"; //$NON-NLS-1$
72 /** Trace Type ID for Generic CTF traces */
73 private static final String GENERIC_CTF_TRACE_TYPE
= "org.eclipse.linuxtools.tmf.ui.type.ctf"; //$NON-NLS-1$
74 /** Name of default project to import traces to */
75 public static final String DEFAULT_REMOTE_PROJECT_NAME
= "Remote"; //$NON-NLS-1$
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 * The command parameter
84 protected CommandParameter fParam
;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
93 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
101 final CommandParameter param
= fParam
.clone();
103 // create default project
104 IProject project
= TmfProjectRegistry
.createProject(DEFAULT_REMOTE_PROJECT_NAME
, null, null);
106 if (param
.getSession().isStreamedTrace()) {
108 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
109 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
111 BatchImportTraceWizard wizard
= new BatchImportTraceWizard();
112 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
113 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
119 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
120 dialog
.setSession(param
.getSession());
121 dialog
.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME
);
123 if (dialog
.open() != Window
.OK
) {
127 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
129 protected IStatus
run(IProgressMonitor monitor
) {
131 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, Messages
.TraceControl_ImportFailure
, null);
132 List
<ImportFileInfo
> traces
= dialog
.getTracePathes();
133 IProject selectedProject
= dialog
.getProject();
134 for (Iterator
<ImportFileInfo
> iterator
= traces
.iterator(); iterator
.hasNext();) {
136 ImportFileInfo remoteFile
= iterator
.next();
138 downloadTrace(remoteFile
, selectedProject
, monitor
);
141 IFolder traceFolder
= selectedProject
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
143 if (monitor
.isCanceled()) {
144 status
.add(Status
.CANCEL_STATUS
);
148 IFile file
= traceFolder
.getFile(remoteFile
.getLocalTraceName());
150 TraceTypeHelper helper
= null;
152 if (remoteFile
.isKernel()) {
153 helper
= TmfTraceType
.getInstance().getTraceType(LTTNG_KERNEL_TRACE_TYPE
);
155 helper
= TmfTraceType
.getInstance().getTraceType(GENERIC_CTF_TRACE_TYPE
);
158 if (helper
!= null) {
159 status
.add(TmfTraceTypeUIUtils
.setTraceType(file
.getFullPath(), helper
));
161 } catch (ExecutionException e
) {
162 status
.add(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
));
163 } catch (CoreException e
) {
164 status
.add(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
));
179 public boolean isEnabled() {
180 // Get workbench page for the Control View
181 IWorkbenchPage page
= getWorkbenchPage();
186 // Check if one or more session are selected
187 ISelection selection
= page
.getSelection(ControlView
.ID
);
188 TraceSessionComponent session
= null;
189 if (selection
instanceof StructuredSelection
) {
190 StructuredSelection structered
= ((StructuredSelection
) selection
);
191 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
192 Object element
= iterator
.next();
193 if (element
instanceof TraceSessionComponent
) {
194 // Add only TraceSessionComponents that are inactive and not destroyed
195 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
196 if (((tmpSession
.isSnapshotSession()) || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
197 session
= tmpSession
;
202 boolean isEnabled
= session
!= null;
208 fParam
= new CommandParameter(session
);
216 // ------------------------------------------------------------------------
218 // ------------------------------------------------------------------------
221 * Downloads a trace from the remote host to the given project.
224 * - trace information of trace to import
226 * - project to import to
228 * - a progress monitor
229 * @throws ExecutionException
231 private static void downloadTrace(ImportFileInfo trace
, IProject project
, IProgressMonitor monitor
)
232 throws ExecutionException
{
234 IRemoteFileSubSystem fsss
= trace
.getImportFile().getParentRemoteFileSubSystem();
236 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
237 if (!traceFolder
.exists()) {
238 throw new ExecutionException(Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
241 String traceName
= trace
.getLocalTraceName();
242 IFolder folder
= traceFolder
.getFolder(traceName
);
243 if (folder
.exists()) {
244 if(!trace
.isOverwrite()) {
245 throw new ExecutionException(Messages
.TraceControl_ImportDialogTraceAlreadyExistError
+ ": " + traceName
); //$NON-NLS-1$
248 folder
.create(true, true, null);
251 IRemoteFile
[] sources
= fsss
.list(trace
.getImportFile(), IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
252 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, sources
.length
);
253 subMonitor
.beginTask(Messages
.TraceControl_DownloadTask
, sources
.length
);
255 for (int i
= 0; i
< sources
.length
; i
++) {
256 if (subMonitor
.isCanceled()) {
257 monitor
.setCanceled(true);
260 String destination
= folder
.getLocation().addTrailingSeparator().append(sources
[i
].getName()).toString();
261 subMonitor
.setTaskName(Messages
.TraceControl_DownloadTask
+ ' ' + traceName
+ '/' +sources
[i
].getName());
262 fsss
.download(sources
[i
], destination
, null, subMonitor
.newChild(1));
264 } catch (SystemMessageException e
) {
265 throw new ExecutionException(e
.toString(), e
);
266 } catch (CoreException e
) {
267 throw new ExecutionException(e
.toString(), e
);