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
;
18 import java
.net
.URISyntaxException
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import org
.eclipse
.core
.commands
.ExecutionEvent
;
23 import org
.eclipse
.core
.commands
.ExecutionException
;
24 import org
.eclipse
.core
.resources
.IFolder
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.IPath
;
30 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
31 import org
.eclipse
.core
.runtime
.IStatus
;
32 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
33 import org
.eclipse
.core
.runtime
.Path
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.core
.runtime
.jobs
.Job
;
36 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
37 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
38 import org
.eclipse
.jface
.viewers
.ISelection
;
39 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
40 import org
.eclipse
.jface
.wizard
.WizardDialog
;
41 import org
.eclipse
.remote
.core
.IRemoteConnection
;
42 import org
.eclipse
.remote
.core
.IRemoteConnectionHostService
;
43 import org
.eclipse
.swt
.widgets
.Display
;
44 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceSessionState
;
45 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
46 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionInfo
;
47 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionManager
;
48 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConsumer
;
49 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.ControlView
;
50 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
51 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
52 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.RemoteFetchLogWizard
;
53 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.RemoteFetchLogWizardRemotePage
;
54 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportConnectionNodeElement
;
55 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportProfileElement
;
56 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportTraceGroupElement
;
57 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.ImportTraceWizard
;
58 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
59 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
60 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceImportException
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
63 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
64 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.proxy
.RemoteSystemProxy
;
65 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
72 import org
.eclipse
.ui
.IWorkbenchPage
;
73 import org
.eclipse
.ui
.IWorkbenchWindow
;
74 import org
.eclipse
.ui
.PlatformUI
;
78 * Command handler implementation to import traces from a (remote) session to a
82 * @author Bernd Hufmann
84 public class ImportHandler
extends BaseControlViewHandler
{
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
89 /** The preference key to remeber whether or not the user wants the notification shown next time **/
90 private static final String NOTIFY_IMPORT_STREAMED_PREF_KEY
= "NOTIFY_IMPORT_STREAMED"; //$NON-NLS-1$
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * The command parameter
99 protected CommandParameter fParam
;
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
108 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
110 if (window
== null) {
114 CommandParameter param
;
117 param
= fParam
.clone();
121 // create default project
122 IProject project
= TmfProjectRegistry
.createProject(RemoteFetchLogWizardRemotePage
.DEFAULT_REMOTE_PROJECT_NAME
, null, null);
124 if (param
.getSession().isLiveTrace()) {
125 importLiveTrace(new LttngRelaydConnectionInfo(param
.getSession().getLiveUrl(), param
.getSession().getLivePort(), param
.getSession().getName()), project
);
127 } else if (param
.getSession().isStreamedTrace()) {
129 IPreferenceStore store
= Activator
.getDefault().getPreferenceStore();
130 String notify
= store
.getString(NOTIFY_IMPORT_STREAMED_PREF_KEY
);
131 if (!MessageDialogWithToggle
.ALWAYS
.equals(notify
)) {
132 MessageDialogWithToggle
.openInformation(window
.getShell(), null, Messages
.TraceControl_ImportDialogStreamedTraceNotification
, Messages
.TraceControl_ImportDialogStreamedTraceNotificationToggle
, false, store
, NOTIFY_IMPORT_STREAMED_PREF_KEY
);
136 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
137 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
139 ImportTraceWizard wizard
= new ImportTraceWizard();
140 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
141 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
146 // Generate the profile
147 RemoteImportProfileElement profile
= new RemoteImportProfileElement(null, "LTTng Remote Traces"); //$NON-NLS-1$
148 TraceSessionComponent session
= param
.getSession();
149 RemoteSystemProxy proxy
= session
.getTargetNode().getRemoteSystemProxy();
150 IRemoteConnection rc
= proxy
.getRemoteConnection();
151 String name
= rc
.getName();
153 if (!rc
.hasService(IRemoteConnectionHostService
.class)) {
157 String scheme
= rc
.getConnectionType().getScheme();
158 IRemoteConnectionHostService hostService
= rc
.getService(IRemoteConnectionHostService
.class);
159 String address
= hostService
.getHostname();
160 String user
= hostService
.getUsername();
161 int port
= hostService
.getPort();
165 remoteUri
= new URI(scheme
, user
, address
, port
, null, null, null);
166 } catch (URISyntaxException e
) {
169 RemoteImportConnectionNodeElement connection
= new RemoteImportConnectionNodeElement(profile
, name
, remoteUri
.toString());
170 String pathString
= session
.isSnapshotSession() ? session
.getSnapshotInfo().getSnapshotPath() : session
.getSessionPath();
171 IPath path
= new Path(pathString
);
172 RemoteImportTraceGroupElement group
= new RemoteImportTraceGroupElement(connection
, path
.removeLastSegments(1).toString());
173 group
.setRecursive(true);
174 TracePackageElement element
= new TracePackageTraceElement(group
, "", ""); //$NON-NLS-1$//$NON-NLS-2$
175 new TracePackageFilesElement(element
, path
.lastSegment() + "/.*"); //$NON-NLS-1$
177 RemoteFetchLogWizard wizard
= new RemoteFetchLogWizard(profile
);
178 wizard
.init(PlatformUI
.getWorkbench(), StructuredSelection
.EMPTY
);
179 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
186 public boolean isEnabled() {
187 // Get workbench page for the Control View
188 IWorkbenchPage page
= getWorkbenchPage();
193 // Check if one or more session are selected
194 ISelection selection
= page
.getSelection(ControlView
.ID
);
195 TraceSessionComponent session
= null;
196 if (selection
instanceof StructuredSelection
) {
197 StructuredSelection structered
= ((StructuredSelection
) selection
);
198 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
199 Object element
= iterator
.next();
200 if (element
instanceof TraceSessionComponent
) {
201 // Add only TraceSessionComponents that are inactive and not
203 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
204 if ((tmpSession
.isSnapshotSession() || tmpSession
.isLiveTrace() || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
205 session
= tmpSession
;
210 boolean isEnabled
= session
!= null;
216 fParam
= new CommandParameter(session
);
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo
, final IProject project
) {
229 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
232 protected IStatus
run(final IProgressMonitor monitor
) {
234 // We initiate the connection first so that we can retrieve the trace path
235 LttngRelaydConsumer lttngRelaydConsumer
= LttngRelaydConnectionManager
.getInstance().getConsumer(connectionInfo
);
237 lttngRelaydConsumer
.connect();
238 } catch (CoreException e
) {
239 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.Messages
.LttngRelaydConnectionManager_ConnectionError
, e
);
241 initializeTraceResource(connectionInfo
, lttngRelaydConsumer
.getTracePath(), project
);
242 return Status
.OK_STATUS
;
243 } catch (CoreException
| TmfTraceImportException e
) {
244 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceInitError
, e
);
254 private static void initializeTraceResource(final LttngRelaydConnectionInfo connectionInfo
, final String tracePath
, final IProject project
) throws CoreException
, TmfTraceImportException
{
255 IFolder folder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
256 IFolder traceFolder
= folder
.getFolder(connectionInfo
.getSessionName());
257 Path location
= new Path(tracePath
);
258 IStatus result
= ResourcesPlugin
.getWorkspace().validateLinkLocation(folder
, location
);
260 traceFolder
.createLink(location
, IResource
.REPLACE
, new NullProgressMonitor());
262 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, result
.getMessage()));
265 TraceTypeHelper selectedTraceType
= TmfTraceTypeUIUtils
.selectTraceType(location
.toOSString(), null, null);
266 // No trace type was determined.
267 TmfTraceTypeUIUtils
.setTraceType(traceFolder
, selectedTraceType
);
269 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
270 final TmfTraceFolder tracesFolder
= projectElement
.getTracesFolder();
271 final List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
272 TmfTraceElement found
= null;
273 for (TmfTraceElement candidate
: traces
) {
274 if (candidate
.getName().equals(connectionInfo
.getSessionName())) {
280 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceElementError
));
283 // Properties used to be able to reopen a trace in live mode
284 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_HOST
, connectionInfo
.getHost());
285 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_PORT
, Integer
.toString(connectionInfo
.getPort()));
286 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, connectionInfo
.getSessionName());
288 final TmfTraceElement finalTrace
= found
;
289 Display
.getDefault().syncExec(new Runnable() {
293 TmfOpenTraceHelper
.openTraceFromElement(finalTrace
);