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
.IFolder
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.jface
.viewers
.ISelection
;
29 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.window
.Window
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.TraceSessionState
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.ControlView
;
34 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.IImportDialog
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.ImportFileInfo
;
36 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.TraceControlDialogFactory
;
37 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
38 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
40 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
41 import org
.eclipse
.rse
.services
.files
.IFileService
;
42 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
43 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFileSubSystem
;
44 import org
.eclipse
.ui
.IWorkbenchPage
;
45 import org
.eclipse
.ui
.IWorkbenchWindow
;
46 import org
.eclipse
.ui
.PlatformUI
;
50 * Command handler implementation to import traces from a (remote) session to a tracing project.
53 * @author Bernd Hufmann
55 public class ImportHandler
extends BaseControlViewHandler
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * The command parameter
64 protected CommandParameter fParam
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
76 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
79 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
81 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
89 final CommandParameter param
= fParam
.clone();
91 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
92 dialog
.setSession(param
.getSession());
94 if ((dialog
.open() != Window
.OK
) || param
.getSession().isStreamedTrace()) {
98 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
100 protected IStatus
run(IProgressMonitor monitor
) {
102 List
<ImportFileInfo
> traces
= dialog
.getTracePathes();
103 IProject project
= dialog
.getProject();
105 for (Iterator
<ImportFileInfo
> iterator
= traces
.iterator(); iterator
.hasNext();) {
106 ImportFileInfo remoteFile
= iterator
.next();
107 downloadTrace(remoteFile
, project
);
110 } catch (ExecutionException e
) {
111 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
);
113 return Status
.OK_STATUS
;
126 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
129 public boolean isEnabled() {
130 // Get workbench page for the Control View
131 IWorkbenchPage page
= getWorkbenchPage();
136 // Check if one or more session are selected
137 ISelection selection
= page
.getSelection(ControlView
.ID
);
138 TraceSessionComponent session
= null;
139 if (selection
instanceof StructuredSelection
) {
140 StructuredSelection structered
= ((StructuredSelection
) selection
);
141 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
142 Object element
= iterator
.next();
143 if (element
instanceof TraceSessionComponent
) {
144 // Add only TraceSessionComponents that are inactive and not destroyed
145 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
146 if ((tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
) && (!tmpSession
.isDestroyed())) {
147 session
= tmpSession
;
152 boolean isEnabled
= session
!= null;
158 fParam
= new CommandParameter(session
);
166 // ------------------------------------------------------------------------
168 // ------------------------------------------------------------------------
170 * Downloads a trace from the remote host to the given project.
173 * - trace information of trace to import
175 * - project to import to
176 * @throws ExecutionException
178 private static void downloadTrace(ImportFileInfo trace
, IProject project
)
179 throws ExecutionException
{
181 IRemoteFileSubSystem fsss
= trace
.getImportFile().getParentRemoteFileSubSystem();
183 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
184 if (!traceFolder
.exists()) {
185 throw new ExecutionException(Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
188 String traceName
= trace
.getLocalTraceName();
189 IFolder folder
= traceFolder
.getFolder(traceName
);
190 if (folder
.exists()) {
191 if(!trace
.isOverwrite()) {
192 throw new ExecutionException(Messages
.TraceControl_ImportDialogTraceAlreadyExistError
+ ": " + traceName
); //$NON-NLS-1$
195 folder
.create(true, true, null);
198 IRemoteFile
[] sources
= fsss
.list(trace
.getImportFile(), IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
200 String
[] destinations
= new String
[sources
.length
];
201 String
[] encodings
= new String
[sources
.length
];
202 for (int i
= 0; i
< sources
.length
; i
++) {
203 destinations
[i
] = folder
.getLocation().addTrailingSeparator().append(sources
[i
].getName()).toString();
207 fsss
.downloadMultiple(sources
, destinations
, encodings
, new NullProgressMonitor());
209 } catch (SystemMessageException e
) {
210 throw new ExecutionException(e
.toString(), e
);
211 } catch (CoreException e
) {
212 throw new ExecutionException(e
.toString(), e
);