control: Add enhanced support for loading of sessions
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / ImportHandler.java
CommitLineData
291cbdbf 1/**********************************************************************
68d1057b 2 * Copyright (c) 2012, 2015 Ericsson
cfdb727a 3 *
291cbdbf
BH
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
cfdb727a
AM
8 *
9 * Contributors:
291cbdbf 10 * Bernd Hufmann - Initial API and implementation
ba3a9bd2 11 * Bernd Hufmann - Updated for support of streamed traces
89730b51 12 * Patrick Tasse - Add support for source location
b732adaa 13 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
291cbdbf 14 **********************************************************************/
9bc60be7 15package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers;
291cbdbf 16
89730b51 17import java.net.URI;
b9c84b9c 18import java.net.URISyntaxException;
291cbdbf
BH
19import java.util.Iterator;
20import java.util.List;
21
22import org.eclipse.core.commands.ExecutionEvent;
23import org.eclipse.core.commands.ExecutionException;
24import org.eclipse.core.resources.IFolder;
25import org.eclipse.core.resources.IProject;
a6e37e4c 26import org.eclipse.core.resources.IResource;
6fd3c6e9 27import org.eclipse.core.resources.ResourcesPlugin;
291cbdbf 28import org.eclipse.core.runtime.CoreException;
89730b51 29import org.eclipse.core.runtime.IPath;
291cbdbf
BH
30import org.eclipse.core.runtime.IProgressMonitor;
31import org.eclipse.core.runtime.IStatus;
32import org.eclipse.core.runtime.NullProgressMonitor;
6fd3c6e9 33import org.eclipse.core.runtime.Path;
291cbdbf
BH
34import org.eclipse.core.runtime.Status;
35import org.eclipse.core.runtime.jobs.Job;
81d5dc3a
MAL
36import org.eclipse.jface.dialogs.MessageDialogWithToggle;
37import org.eclipse.jface.preference.IPreferenceStore;
291cbdbf
BH
38import org.eclipse.jface.viewers.ISelection;
39import org.eclipse.jface.viewers.StructuredSelection;
cd9821de 40import org.eclipse.jface.wizard.WizardDialog;
b9c84b9c
BH
41import org.eclipse.remote.core.IRemoteConnection;
42import org.eclipse.remote.core.IRemoteConnectionHostService;
6fd3c6e9 43import org.eclipse.swt.widgets.Display;
68d1057b 44import org.eclipse.tracecompass.common.core.NonNullUtils;
9bc60be7
AM
45import org.eclipse.tracecompass.internal.lttng2.control.core.model.TraceSessionState;
46import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
47import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConnectionInfo;
48import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConnectionManager;
49import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConsumer;
50import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
9bc60be7
AM
51import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
52import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
b9c84b9c
BH
53import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.RemoteFetchLogWizard;
54import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.RemoteFetchLogWizardRemotePage;
55import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportConnectionNodeElement;
56import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportProfileElement;
57import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportTraceGroupElement;
2bdf0193 58import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
b9c84b9c
BH
59import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
60import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
61import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
2bdf0193
AM
62import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException;
63import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
64import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
b9c84b9c 65import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
2bdf0193
AM
66import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
67import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
68import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
69import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
70import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
71import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
72import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
291cbdbf
BH
73import org.eclipse.ui.IWorkbenchPage;
74import org.eclipse.ui.IWorkbenchWindow;
75import org.eclipse.ui.PlatformUI;
76
77/**
291cbdbf 78 * <p>
6fd3c6e9
MAL
79 * Command handler implementation to import traces from a (remote) session to a
80 * tracing project.
291cbdbf 81 * </p>
cfdb727a 82 *
dbd4432d 83 * @author Bernd Hufmann
291cbdbf
BH
84 */
85public class ImportHandler extends BaseControlViewHandler {
86
cd9821de
BH
87 // ------------------------------------------------------------------------
88 // Constants
89 // ------------------------------------------------------------------------
81d5dc3a
MAL
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
291cbdbf
BH
93 // ------------------------------------------------------------------------
94 // Attributes
95 // ------------------------------------------------------------------------
6f4e8ec0
AM
96
97 /**
98 * The command parameter
99 */
291cbdbf 100 protected CommandParameter fParam;
cfdb727a 101
291cbdbf
BH
102 // ------------------------------------------------------------------------
103 // Operations
104 // ------------------------------------------------------------------------
cfdb727a 105
291cbdbf
BH
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
b9c84b9c 115 CommandParameter param;
291cbdbf
BH
116 fLock.lock();
117 try {
68d1057b
BH
118 param = fParam;
119 if (param == null) {
120 return null;
121 }
122 param = new CommandParameter(param);
b9c84b9c
BH
123 } finally {
124 fLock.unlock();
125 }
68d1057b 126
b9c84b9c
BH
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);
291cbdbf
BH
139 }
140
b9c84b9c
BH
141 // Streamed trace
142 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
143 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
67c7236e 144
b9c84b9c
BH
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 }
cd9821de 151
b9c84b9c
BH
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();
cd9821de 158
b9c84b9c
BH
159 if (!rc.hasService(IRemoteConnectionHostService.class)) {
160 return null;
161 }
cd9821de 162
b9c84b9c
BH
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();
89730b51 168
b9c84b9c
BH
169 URI remoteUri;
170 try {
171 remoteUri = new URI(scheme, user, address, port, null, null, null);
172 } catch (URISyntaxException e) {
173 return false;
291cbdbf 174 }
b9c84b9c
BH
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);
a5544859 178 RemoteImportTraceGroupElement group = new RemoteImportTraceGroupElement(connection, path.toString());
b9c84b9c
BH
179 group.setRecursive(true);
180 TracePackageElement element = new TracePackageTraceElement(group, "", ""); //$NON-NLS-1$//$NON-NLS-2$
a5544859 181 new TracePackageFilesElement(element, ".*"); //$NON-NLS-1$
b9c84b9c
BH
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
291cbdbf
BH
188 return null;
189 }
190
291cbdbf
BH
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();) {
cfdb727a 205 Object element = iterator.next();
291cbdbf 206 if (element instanceof TraceSessionComponent) {
6fd3c6e9
MAL
207 // Add only TraceSessionComponents that are inactive and not
208 // destroyed
291cbdbf 209 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
6fd3c6e9 210 if ((tmpSession.isSnapshotSession() || tmpSession.isLiveTrace() || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
291cbdbf
BH
211 session = tmpSession;
212 }
213 }
214 }
215 }
216 boolean isEnabled = session != null;
217
218 fLock.lock();
219 try {
220 fParam = null;
221 if (isEnabled) {
68d1057b 222 fParam = new CommandParameter(NonNullUtils.checkNotNull(session));
291cbdbf
BH
223 }
224 } finally {
225 fLock.unlock();
226 }
227 return isEnabled;
228 }
cfdb727a 229
291cbdbf
BH
230 // ------------------------------------------------------------------------
231 // Helper methods
232 // ------------------------------------------------------------------------
11252342 233
b9c84b9c 234 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo, final IProject project) {
6fd3c6e9
MAL
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) {
92fe6900 245 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.Messages.LttngRelaydConnectionManager_ConnectionError, e);
6fd3c6e9
MAL
246 }
247 initializeTraceResource(connectionInfo, lttngRelaydConsumer.getTracePath(), project);
248 return Status.OK_STATUS;
249 } catch (CoreException | TmfTraceImportException e) {
f4da4c59 250 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_LiveTraceInitError, e);
6fd3c6e9
MAL
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) {
f4da4c59 286 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_LiveTraceElementError));
6fd3c6e9
MAL
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 }
291cbdbf 303}
This page took 0.091106 seconds and 5 git commands to generate.