control: replace ImportDialog with TMF remote fetch wizard
[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/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 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;
9bc60be7
AM
44import org.eclipse.tracecompass.internal.lttng2.control.core.model.TraceSessionState;
45import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
46import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConnectionInfo;
47import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConnectionManager;
48import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConsumer;
49import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
9bc60be7
AM
50import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
51import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
b9c84b9c
BH
52import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.RemoteFetchLogWizard;
53import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.RemoteFetchLogWizardRemotePage;
54import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportConnectionNodeElement;
55import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportProfileElement;
56import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportTraceGroupElement;
2bdf0193 57import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
b9c84b9c
BH
58import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
59import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
60import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
2bdf0193
AM
61import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException;
62import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
63import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
b9c84b9c 64import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
2bdf0193
AM
65import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
66import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
67import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
68import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
69import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
70import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
71import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
291cbdbf
BH
72import org.eclipse.ui.IWorkbenchPage;
73import org.eclipse.ui.IWorkbenchWindow;
74import org.eclipse.ui.PlatformUI;
75
76/**
291cbdbf 77 * <p>
6fd3c6e9
MAL
78 * Command handler implementation to import traces from a (remote) session to a
79 * tracing project.
291cbdbf 80 * </p>
cfdb727a 81 *
dbd4432d 82 * @author Bernd Hufmann
291cbdbf
BH
83 */
84public class ImportHandler extends BaseControlViewHandler {
85
cd9821de
BH
86 // ------------------------------------------------------------------------
87 // Constants
88 // ------------------------------------------------------------------------
81d5dc3a
MAL
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$
91
291cbdbf
BH
92 // ------------------------------------------------------------------------
93 // Attributes
94 // ------------------------------------------------------------------------
6f4e8ec0
AM
95
96 /**
97 * The command parameter
98 */
291cbdbf 99 protected CommandParameter fParam;
cfdb727a 100
291cbdbf
BH
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
cfdb727a 104
291cbdbf
BH
105 @Override
106 public Object execute(ExecutionEvent event) throws ExecutionException {
107
108 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
109
110 if (window == null) {
111 return false;
112 }
113
b9c84b9c 114 CommandParameter param;
291cbdbf
BH
115 fLock.lock();
116 try {
b9c84b9c
BH
117 param = fParam.clone();
118 } finally {
119 fLock.unlock();
120 }
121 // create default project
122 IProject project = TmfProjectRegistry.createProject(RemoteFetchLogWizardRemotePage.DEFAULT_REMOTE_PROJECT_NAME, null, null);
123
124 if (param.getSession().isLiveTrace()) {
125 importLiveTrace(new LttngRelaydConnectionInfo(param.getSession().getLiveUrl(), param.getSession().getLivePort(), param.getSession().getName()), project);
126 return null;
127 } else if (param.getSession().isStreamedTrace()) {
128
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);
291cbdbf
BH
133 }
134
b9c84b9c
BH
135 // Streamed trace
136 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
137 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
67c7236e 138
b9c84b9c
BH
139 ImportTraceWizard wizard = new ImportTraceWizard();
140 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder));
141 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
142 dialog.open();
143 return null;
144 }
cd9821de 145
b9c84b9c
BH
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();
cd9821de 152
b9c84b9c
BH
153 if (!rc.hasService(IRemoteConnectionHostService.class)) {
154 return null;
155 }
cd9821de 156
b9c84b9c
BH
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();
89730b51 162
b9c84b9c
BH
163 URI remoteUri;
164 try {
165 remoteUri = new URI(scheme, user, address, port, null, null, null);
166 } catch (URISyntaxException e) {
167 return false;
291cbdbf 168 }
b9c84b9c
BH
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$
176
177 RemoteFetchLogWizard wizard = new RemoteFetchLogWizard(profile);
178 wizard.init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
179 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
180 dialog.open();
181
291cbdbf
BH
182 return null;
183 }
184
291cbdbf
BH
185 @Override
186 public boolean isEnabled() {
187 // Get workbench page for the Control View
188 IWorkbenchPage page = getWorkbenchPage();
189 if (page == null) {
190 return false;
191 }
192
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();) {
cfdb727a 199 Object element = iterator.next();
291cbdbf 200 if (element instanceof TraceSessionComponent) {
6fd3c6e9
MAL
201 // Add only TraceSessionComponents that are inactive and not
202 // destroyed
291cbdbf 203 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
6fd3c6e9 204 if ((tmpSession.isSnapshotSession() || tmpSession.isLiveTrace() || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
291cbdbf
BH
205 session = tmpSession;
206 }
207 }
208 }
209 }
210 boolean isEnabled = session != null;
211
212 fLock.lock();
213 try {
214 fParam = null;
215 if (isEnabled) {
216 fParam = new CommandParameter(session);
217 }
218 } finally {
219 fLock.unlock();
220 }
221 return isEnabled;
222 }
cfdb727a 223
291cbdbf
BH
224 // ------------------------------------------------------------------------
225 // Helper methods
226 // ------------------------------------------------------------------------
11252342 227
b9c84b9c 228 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo, final IProject project) {
6fd3c6e9
MAL
229 Job job = new Job(Messages.TraceControl_ImportJob) {
230
231 @Override
232 protected IStatus run(final IProgressMonitor monitor) {
233 try {
234 // We initiate the connection first so that we can retrieve the trace path
235 LttngRelaydConsumer lttngRelaydConsumer = LttngRelaydConnectionManager.getInstance().getConsumer(connectionInfo);
236 try {
237 lttngRelaydConsumer.connect();
238 } catch (CoreException e) {
92fe6900 239 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.Messages.LttngRelaydConnectionManager_ConnectionError, e);
6fd3c6e9
MAL
240 }
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);
245 }
246 }
247
248 };
249 job.setSystem(true);
250 job.schedule();
251 }
252
253
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);
259 if (result.isOK()) {
260 traceFolder.createLink(location, IResource.REPLACE, new NullProgressMonitor());
261 } else {
262 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, result.getMessage()));
263 }
264
265 TraceTypeHelper selectedTraceType = TmfTraceTypeUIUtils.selectTraceType(location.toOSString(), null, null);
266 // No trace type was determined.
267 TmfTraceTypeUIUtils.setTraceType(traceFolder, selectedTraceType);
268
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())) {
275 found = candidate;
276 }
277 }
278
279 if (found == null) {
280 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportHandler_LiveTraceElementError));
281 }
282
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());
287
288 final TmfTraceElement finalTrace = found;
289 Display.getDefault().syncExec(new Runnable() {
290
291 @Override
292 public void run() {
293 TmfOpenTraceHelper.openTraceFromElement(finalTrace);
294 }
295 });
296 }
291cbdbf 297}
This page took 0.092449 seconds and 5 git commands to generate.