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 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
14 **********************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.handlers
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
20 import java
.io
.OutputStream
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import org
.eclipse
.core
.commands
.ExecutionEvent
;
26 import org
.eclipse
.core
.commands
.ExecutionException
;
27 import org
.eclipse
.core
.filesystem
.EFS
;
28 import org
.eclipse
.core
.filesystem
.IFileInfo
;
29 import org
.eclipse
.core
.filesystem
.IFileStore
;
30 import org
.eclipse
.core
.resources
.IFolder
;
31 import org
.eclipse
.core
.resources
.IProject
;
32 import org
.eclipse
.core
.resources
.IResource
;
33 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
34 import org
.eclipse
.core
.runtime
.CoreException
;
35 import org
.eclipse
.core
.runtime
.IPath
;
36 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
37 import org
.eclipse
.core
.runtime
.IStatus
;
38 import org
.eclipse
.core
.runtime
.MultiStatus
;
39 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.Path
;
41 import org
.eclipse
.core
.runtime
.Status
;
42 import org
.eclipse
.core
.runtime
.SubMonitor
;
43 import org
.eclipse
.core
.runtime
.URIUtil
;
44 import org
.eclipse
.core
.runtime
.jobs
.Job
;
45 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
46 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
47 import org
.eclipse
.jface
.viewers
.ISelection
;
48 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
49 import org
.eclipse
.jface
.window
.Window
;
50 import org
.eclipse
.jface
.wizard
.WizardDialog
;
51 import org
.eclipse
.swt
.widgets
.Display
;
52 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceSessionState
;
53 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
54 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionInfo
;
55 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionManager
;
56 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConsumer
;
57 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.ControlView
;
58 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.IImportDialog
;
59 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.ImportFileInfo
;
60 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.TraceControlDialogFactory
;
61 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
62 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
63 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.ImportTraceWizard
;
64 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
65 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceImportException
;
66 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
67 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TraceUtils
;
76 import org
.eclipse
.ui
.IWorkbenchPage
;
77 import org
.eclipse
.ui
.IWorkbenchWindow
;
78 import org
.eclipse
.ui
.PlatformUI
;
82 * Command handler implementation to import traces from a (remote) session to a
86 * @author Bernd Hufmann
88 public class ImportHandler
extends BaseControlViewHandler
{
90 private static final int BUFFER_IN_KB
= 16;
92 private static final int BYTES_PER_KB
= 1024;
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
97 /** Name of default project to import traces to */
98 public static final String DEFAULT_REMOTE_PROJECT_NAME
= "Remote"; //$NON-NLS-1$
100 /** The preference key to remeber whether or not the user wants the notification shown next time **/
101 private static final String NOTIFY_IMPORT_STREAMED_PREF_KEY
= "NOTIFY_IMPORT_STREAMED"; //$NON-NLS-1$
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * The command parameter
110 protected CommandParameter fParam
;
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
117 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
119 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
121 if (window
== null) {
127 final CommandParameter param
= fParam
.clone();
129 // create default project
130 IProject project
= TmfProjectRegistry
.createProject(DEFAULT_REMOTE_PROJECT_NAME
, null, null);
132 if (param
.getSession().isLiveTrace()) {
133 importLiveTrace(new LttngRelaydConnectionInfo(param
.getSession().getLiveUrl(), param
.getSession().getLivePort(), param
.getSession().getName()), project
);
135 } else if (param
.getSession().isStreamedTrace()) {
137 IPreferenceStore store
= Activator
.getDefault().getPreferenceStore();
138 String notify
= store
.getString(NOTIFY_IMPORT_STREAMED_PREF_KEY
);
139 if (!MessageDialogWithToggle
.ALWAYS
.equals(notify
)) {
140 MessageDialogWithToggle
.openInformation(window
.getShell(), null, Messages
.TraceControl_ImportDialogStreamedTraceNotification
, Messages
.TraceControl_ImportDialogStreamedTraceNotificationToggle
, false, store
, NOTIFY_IMPORT_STREAMED_PREF_KEY
);
144 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
145 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
147 ImportTraceWizard wizard
= new ImportTraceWizard();
148 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
149 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
155 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
156 dialog
.setSession(param
.getSession());
157 dialog
.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME
);
159 if (dialog
.open() != Window
.OK
) {
163 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
165 protected IStatus
run(IProgressMonitor monitor
) {
167 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, Messages
.TraceControl_ImportFailure
, null);
168 List
<ImportFileInfo
> traces
= dialog
.getTracePathes();
169 IProject selectedProject
= dialog
.getProject();
170 for (Iterator
<ImportFileInfo
> iterator
= traces
.iterator(); iterator
.hasNext();) {
173 if (monitor
.isCanceled()) {
174 status
.add(Status
.CANCEL_STATUS
);
178 ImportFileInfo remoteFile
= iterator
.next();
180 downloadTrace(remoteFile
, selectedProject
, monitor
);
183 IFolder traceFolder
= remoteFile
.getDestinationFolder();
185 IResource file
= traceFolder
.findMember(remoteFile
.getLocalTraceName());
188 TraceTypeHelper helper
= null;
191 helper
= TmfTraceTypeUIUtils
.selectTraceType(file
.getLocation().toOSString(), null, null);
192 } catch (TmfTraceImportException e
) {
193 // the trace did not match any trace type
196 if (helper
!= null) {
197 status
.add(TmfTraceTypeUIUtils
.setTraceType(file
, helper
));
200 URI uri
= remoteFile
.getImportFile().toURI();
201 String sourceLocation
= URIUtil
.toUnencodedString(uri
);
202 file
.setPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
, sourceLocation
);
204 } catch (ExecutionException e
) {
205 status
.add(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
));
206 } catch (CoreException e
) {
207 status
.add(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
));
222 public boolean isEnabled() {
223 // Get workbench page for the Control View
224 IWorkbenchPage page
= getWorkbenchPage();
229 // Check if one or more session are selected
230 ISelection selection
= page
.getSelection(ControlView
.ID
);
231 TraceSessionComponent session
= null;
232 if (selection
instanceof StructuredSelection
) {
233 StructuredSelection structered
= ((StructuredSelection
) selection
);
234 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
235 Object element
= iterator
.next();
236 if (element
instanceof TraceSessionComponent
) {
237 // Add only TraceSessionComponents that are inactive and not
239 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
240 if ((tmpSession
.isSnapshotSession() || tmpSession
.isLiveTrace() || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
241 session
= tmpSession
;
246 boolean isEnabled
= session
!= null;
252 fParam
= new CommandParameter(session
);
260 // ------------------------------------------------------------------------
262 // ------------------------------------------------------------------------
265 * Downloads a trace from the remote host to the given project.
268 * - trace information of trace to import
270 * - project to import to
272 * - a progress monitor
273 * @throws ExecutionException
275 private static void downloadTrace(ImportFileInfo trace
, IProject project
, IProgressMonitor monitor
)
276 throws ExecutionException
{
278 IFileStore importRoot
= trace
.getImportFile();
280 IFolder traceFolder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
281 if (!traceFolder
.exists()) {
282 throw new ExecutionException(Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTracesFolder
.TRACES_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
285 IFolder destinationFolder
= trace
.getDestinationFolder();
286 TraceUtils
.createFolder(destinationFolder
, monitor
);
288 String traceName
= trace
.getLocalTraceName();
289 IFolder folder
= destinationFolder
.getFolder(traceName
);
290 if (folder
.exists()) {
291 if (!trace
.isOverwrite()) {
292 throw new ExecutionException(Messages
.TraceControl_ImportDialogTraceAlreadyExistError
+ ": " + traceName
); //$NON-NLS-1$
295 folder
.create(true, true, null);
298 IFileStore
[] sources
= importRoot
.childStores(EFS
.NONE
, new NullProgressMonitor());
299 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, sources
.length
);
300 subMonitor
.beginTask(Messages
.TraceControl_DownloadTask
, sources
.length
);
302 for (IFileStore source
: sources
) {
303 if (subMonitor
.isCanceled()) {
304 monitor
.setCanceled(true);
307 SubMonitor childMonitor
= subMonitor
.newChild(1);
308 IFileInfo info
= source
.fetchInfo();
309 if (!info
.isDirectory()) {
310 IPath destination
= folder
.getLocation().addTrailingSeparator().append(source
.getName());
311 subMonitor
.setTaskName(Messages
.TraceControl_DownloadTask
+ ' ' + traceName
+ '/' + source
.getName());
312 try (InputStream in
= source
.openInputStream(EFS
.NONE
, new NullProgressMonitor())) {
313 copy(in
, destination
, childMonitor
, info
.getLength());
317 } catch (IOException e
) {
318 throw new ExecutionException(e
.toString(), e
);
319 } catch (CoreException e
) {
320 throw new ExecutionException(e
.toString(), e
);
324 private static void copy(InputStream in
, IPath destination
, SubMonitor monitor
, long length
) throws IOException
{
325 try (OutputStream out
= new FileOutputStream(destination
.toFile())) {
326 monitor
.setWorkRemaining((int) (length
/ BYTES_PER_KB
));
327 byte[] buf
= new byte[BYTES_PER_KB
* BUFFER_IN_KB
];
330 int n
= in
.read(buf
);
334 out
.write(buf
, 0, n
);
335 counter
= (counter
% BYTES_PER_KB
) + n
;
336 monitor
.worked(counter
/ BYTES_PER_KB
);
341 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo
, final IProject project
) {
342 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
345 protected IStatus
run(final IProgressMonitor monitor
) {
347 // We initiate the connection first so that we can retrieve the trace path
348 LttngRelaydConsumer lttngRelaydConsumer
= LttngRelaydConnectionManager
.getInstance().getConsumer(connectionInfo
);
350 lttngRelaydConsumer
.connect();
351 } catch (CoreException e
) {
352 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.Messages
.LttngRelaydConnectionManager_ConnectionError
, e
);
354 initializeTraceResource(connectionInfo
, lttngRelaydConsumer
.getTracePath(), project
);
355 return Status
.OK_STATUS
;
356 } catch (CoreException
| TmfTraceImportException e
) {
357 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceInitError
, e
);
367 private static void initializeTraceResource(final LttngRelaydConnectionInfo connectionInfo
, final String tracePath
, final IProject project
) throws CoreException
, TmfTraceImportException
{
368 IFolder folder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
369 IFolder traceFolder
= folder
.getFolder(connectionInfo
.getSessionName());
370 Path location
= new Path(tracePath
);
371 IStatus result
= ResourcesPlugin
.getWorkspace().validateLinkLocation(folder
, location
);
373 traceFolder
.createLink(location
, IResource
.REPLACE
, new NullProgressMonitor());
375 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, result
.getMessage()));
378 TraceTypeHelper selectedTraceType
= TmfTraceTypeUIUtils
.selectTraceType(location
.toOSString(), null, null);
379 // No trace type was determined.
380 TmfTraceTypeUIUtils
.setTraceType(traceFolder
, selectedTraceType
);
382 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
383 final TmfTraceFolder tracesFolder
= projectElement
.getTracesFolder();
384 final List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
385 TmfTraceElement found
= null;
386 for (TmfTraceElement candidate
: traces
) {
387 if (candidate
.getName().equals(connectionInfo
.getSessionName())) {
393 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceElementError
));
396 // Properties used to be able to reopen a trace in live mode
397 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_HOST
, connectionInfo
.getHost());
398 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_PORT
, Integer
.toString(connectionInfo
.getPort()));
399 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, connectionInfo
.getSessionName());
401 final TmfTraceElement finalTrace
= found
;
402 Display
.getDefault().syncExec(new Runnable() {
406 TmfOpenTraceHelper
.openTraceFromElement(finalTrace
);