1 /**********************************************************************
2 * Copyright (c) 2012, 2015 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
.common
.core
.NonNullUtils
;
45 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceSessionState
;
46 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
47 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionInfo
;
48 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConnectionManager
;
49 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.LttngRelaydConsumer
;
50 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.ControlView
;
51 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
52 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
53 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.RemoteFetchLogWizard
;
54 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.RemoteFetchLogWizardRemotePage
;
55 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportConnectionNodeElement
;
56 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportProfileElement
;
57 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportTraceGroupElement
;
58 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.ImportTraceWizard
;
59 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
60 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
61 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceImportException
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
64 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
65 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.proxy
.RemoteSystemProxy
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
73 import org
.eclipse
.ui
.IWorkbenchPage
;
74 import org
.eclipse
.ui
.IWorkbenchWindow
;
75 import org
.eclipse
.ui
.PlatformUI
;
79 * Command handler implementation to import traces from a (remote) session to a
83 * @author Bernd Hufmann
85 public class ImportHandler
extends BaseControlViewHandler
{
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
90 /** The preference key to remeber whether or not the user wants the notification shown next time **/
91 private static final String NOTIFY_IMPORT_STREAMED_PREF_KEY
= "NOTIFY_IMPORT_STREAMED"; //$NON-NLS-1$
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * The command parameter
100 protected CommandParameter fParam
;
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
109 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
111 if (window
== null) {
115 CommandParameter param
;
122 param
= new CommandParameter(param
);
127 // create default project
128 IProject project
= TmfProjectRegistry
.createProject(RemoteFetchLogWizardRemotePage
.DEFAULT_REMOTE_PROJECT_NAME
, null, null);
130 if (param
.getSession().isLiveTrace()) {
131 importLiveTrace(new LttngRelaydConnectionInfo(param
.getSession().getLiveUrl(), param
.getSession().getLivePort(), param
.getSession().getName()), project
);
133 } else if (param
.getSession().isStreamedTrace()) {
135 IPreferenceStore store
= Activator
.getDefault().getPreferenceStore();
136 String notify
= store
.getString(NOTIFY_IMPORT_STREAMED_PREF_KEY
);
137 if (!MessageDialogWithToggle
.ALWAYS
.equals(notify
)) {
138 MessageDialogWithToggle
.openInformation(window
.getShell(), null, Messages
.TraceControl_ImportDialogStreamedTraceNotification
, Messages
.TraceControl_ImportDialogStreamedTraceNotificationToggle
, false, store
, NOTIFY_IMPORT_STREAMED_PREF_KEY
);
142 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
143 TmfTraceFolder traceFolder
= projectElement
.getTracesFolder();
145 ImportTraceWizard wizard
= new ImportTraceWizard();
146 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(traceFolder
));
147 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
152 // Generate the profile
153 RemoteImportProfileElement profile
= new RemoteImportProfileElement(null, "LTTng Remote Traces"); //$NON-NLS-1$
154 TraceSessionComponent session
= param
.getSession();
155 RemoteSystemProxy proxy
= session
.getTargetNode().getRemoteSystemProxy();
156 IRemoteConnection rc
= proxy
.getRemoteConnection();
157 String name
= rc
.getName();
159 if (!rc
.hasService(IRemoteConnectionHostService
.class)) {
163 String scheme
= rc
.getConnectionType().getScheme();
164 IRemoteConnectionHostService hostService
= rc
.getService(IRemoteConnectionHostService
.class);
165 String address
= hostService
.getHostname();
166 String user
= hostService
.getUsername();
167 int port
= hostService
.getPort();
171 remoteUri
= new URI(scheme
, user
, address
, port
, null, null, null);
172 } catch (URISyntaxException e
) {
175 RemoteImportConnectionNodeElement connection
= new RemoteImportConnectionNodeElement(profile
, name
, remoteUri
.toString());
176 String pathString
= session
.isSnapshotSession() ? session
.getSnapshotInfo().getSnapshotPath() : session
.getSessionPath();
177 IPath path
= new Path(pathString
);
178 RemoteImportTraceGroupElement group
= new RemoteImportTraceGroupElement(connection
, path
.toString());
179 group
.setRecursive(true);
180 TracePackageElement element
= new TracePackageTraceElement(group
, "", ""); //$NON-NLS-1$//$NON-NLS-2$
181 new TracePackageFilesElement(element
, ".*"); //$NON-NLS-1$
183 RemoteFetchLogWizard wizard
= new RemoteFetchLogWizard(profile
);
184 wizard
.init(PlatformUI
.getWorkbench(), StructuredSelection
.EMPTY
);
185 WizardDialog dialog
= new WizardDialog(window
.getShell(), wizard
);
192 public boolean isEnabled() {
193 // Get workbench page for the Control View
194 IWorkbenchPage page
= getWorkbenchPage();
199 // Check if one or more session are selected
200 ISelection selection
= page
.getSelection(ControlView
.ID
);
201 TraceSessionComponent session
= null;
202 if (selection
instanceof StructuredSelection
) {
203 StructuredSelection structered
= ((StructuredSelection
) selection
);
204 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
205 Object element
= iterator
.next();
206 if (element
instanceof TraceSessionComponent
) {
207 // Add only TraceSessionComponents that are inactive and not
209 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
210 if ((tmpSession
.isSnapshotSession() || tmpSession
.isLiveTrace() || (tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
)) && (!tmpSession
.isDestroyed())) {
211 session
= tmpSession
;
216 boolean isEnabled
= session
!= null;
222 fParam
= new CommandParameter(NonNullUtils
.checkNotNull(session
));
230 // ------------------------------------------------------------------------
232 // ------------------------------------------------------------------------
234 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo
, final IProject project
) {
235 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
238 protected IStatus
run(final IProgressMonitor monitor
) {
240 // We initiate the connection first so that we can retrieve the trace path
241 LttngRelaydConsumer lttngRelaydConsumer
= LttngRelaydConnectionManager
.getInstance().getConsumer(connectionInfo
);
243 lttngRelaydConsumer
.connect();
244 } catch (CoreException e
) {
245 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.relayd
.Messages
.LttngRelaydConnectionManager_ConnectionError
, e
);
247 initializeTraceResource(connectionInfo
, lttngRelaydConsumer
.getTracePath(), project
);
248 return Status
.OK_STATUS
;
249 } catch (CoreException
| TmfTraceImportException e
) {
250 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceInitError
, e
);
260 private static void initializeTraceResource(final LttngRelaydConnectionInfo connectionInfo
, final String tracePath
, final IProject project
) throws CoreException
, TmfTraceImportException
{
261 IFolder folder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
262 IFolder traceFolder
= folder
.getFolder(connectionInfo
.getSessionName());
263 Path location
= new Path(tracePath
);
264 IStatus result
= ResourcesPlugin
.getWorkspace().validateLinkLocation(folder
, location
);
266 traceFolder
.createLink(location
, IResource
.REPLACE
, new NullProgressMonitor());
268 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, result
.getMessage()));
271 TraceTypeHelper selectedTraceType
= TmfTraceTypeUIUtils
.selectTraceType(location
.toOSString(), null, null);
272 // No trace type was determined.
273 TmfTraceTypeUIUtils
.setTraceType(traceFolder
, selectedTraceType
);
275 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
276 final TmfTraceFolder tracesFolder
= projectElement
.getTracesFolder();
277 final List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
278 TmfTraceElement found
= null;
279 for (TmfTraceElement candidate
: traces
) {
280 if (candidate
.getName().equals(connectionInfo
.getSessionName())) {
286 throw new CoreException(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.ImportHandler_LiveTraceElementError
));
289 // Properties used to be able to reopen a trace in live mode
290 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_HOST
, connectionInfo
.getHost());
291 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_PORT
, Integer
.toString(connectionInfo
.getPort()));
292 traceFolder
.setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, connectionInfo
.getSessionName());
294 final TmfTraceElement finalTrace
= found
;
295 Display
.getDefault().syncExec(new Runnable() {
299 TmfOpenTraceHelper
.openTraceFromElement(finalTrace
);