ca2ab91c3cda8cd9955af5ca67503b965169acec
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / ImportHandler.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
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;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Iterator;
20 import java.util.List;
21
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;
76
77 /**
78 * <p>
79 * Command handler implementation to import traces from a (remote) session to a
80 * tracing project.
81 * </p>
82 *
83 * @author Bernd Hufmann
84 */
85 public class ImportHandler extends BaseControlViewHandler {
86
87 // ------------------------------------------------------------------------
88 // Constants
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$
92
93 // ------------------------------------------------------------------------
94 // Attributes
95 // ------------------------------------------------------------------------
96
97 /**
98 * The command parameter
99 */
100 protected CommandParameter fParam;
101
102 // ------------------------------------------------------------------------
103 // Operations
104 // ------------------------------------------------------------------------
105
106 @Override
107 public Object execute(ExecutionEvent event) throws ExecutionException {
108
109 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
110
111 if (window == null) {
112 return false;
113 }
114
115 CommandParameter param;
116 fLock.lock();
117 try {
118 param = fParam;
119 if (param == null) {
120 return null;
121 }
122 param = new CommandParameter(param);
123 } finally {
124 fLock.unlock();
125 }
126
127 // create default project
128 IProject project = TmfProjectRegistry.createProject(RemoteFetchLogWizardRemotePage.DEFAULT_REMOTE_PROJECT_NAME, null, null);
129
130 if (param.getSession().isLiveTrace()) {
131 importLiveTrace(new LttngRelaydConnectionInfo(param.getSession().getLiveUrl(), param.getSession().getLivePort(), param.getSession().getName()), project);
132 return null;
133 } else if (param.getSession().isStreamedTrace()) {
134
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);
139 }
140
141 // Streamed trace
142 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
143 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
144
145 ImportTraceWizard wizard = new ImportTraceWizard();
146 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder));
147 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
148 dialog.open();
149 return null;
150 }
151
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();
158
159 if (!rc.hasService(IRemoteConnectionHostService.class)) {
160 return null;
161 }
162
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();
168
169 URI remoteUri;
170 try {
171 remoteUri = new URI(scheme, user, address, port, null, null, null);
172 } catch (URISyntaxException e) {
173 return false;
174 }
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$
182
183 RemoteFetchLogWizard wizard = new RemoteFetchLogWizard(profile);
184 wizard.init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
185 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
186 dialog.open();
187
188 return null;
189 }
190
191 @Override
192 public boolean isEnabled() {
193 // Get workbench page for the Control View
194 IWorkbenchPage page = getWorkbenchPage();
195 if (page == null) {
196 return false;
197 }
198
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
208 // destroyed
209 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
210 if ((tmpSession.isSnapshotSession() || tmpSession.isLiveTrace() || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
211 session = tmpSession;
212 }
213 }
214 }
215 }
216 boolean isEnabled = session != null;
217
218 fLock.lock();
219 try {
220 fParam = null;
221 if (isEnabled) {
222 fParam = new CommandParameter(NonNullUtils.checkNotNull(session));
223 }
224 } finally {
225 fLock.unlock();
226 }
227 return isEnabled;
228 }
229
230 // ------------------------------------------------------------------------
231 // Helper methods
232 // ------------------------------------------------------------------------
233
234 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo, final IProject project) {
235 Job job = new Job(Messages.TraceControl_ImportJob) {
236
237 @Override
238 protected IStatus run(final IProgressMonitor monitor) {
239 try {
240 // We initiate the connection first so that we can retrieve the trace path
241 LttngRelaydConsumer lttngRelaydConsumer = LttngRelaydConnectionManager.getInstance().getConsumer(connectionInfo);
242 try {
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);
246 }
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);
251 }
252 }
253
254 };
255 job.setSystem(true);
256 job.schedule();
257 }
258
259
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);
265 if (result.isOK()) {
266 traceFolder.createLink(location, IResource.REPLACE, new NullProgressMonitor());
267 } else {
268 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, result.getMessage()));
269 }
270
271 TraceTypeHelper selectedTraceType = TmfTraceTypeUIUtils.selectTraceType(location.toOSString(), null, null);
272 // No trace type was determined.
273 TmfTraceTypeUIUtils.setTraceType(traceFolder, selectedTraceType);
274
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())) {
281 found = candidate;
282 }
283 }
284
285 if (found == null) {
286 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportHandler_LiveTraceElementError));
287 }
288
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());
293
294 final TmfTraceElement finalTrace = found;
295 Display.getDefault().syncExec(new Runnable() {
296
297 @Override
298 public void run() {
299 TmfOpenTraceHelper.openTraceFromElement(finalTrace);
300 }
301 });
302 }
303 }
This page took 0.036907 seconds and 5 git commands to generate.