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
1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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.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;
75
76 /**
77 * <p>
78 * Command handler implementation to import traces from a (remote) session to a
79 * tracing project.
80 * </p>
81 *
82 * @author Bernd Hufmann
83 */
84 public class ImportHandler extends BaseControlViewHandler {
85
86 // ------------------------------------------------------------------------
87 // Constants
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$
91
92 // ------------------------------------------------------------------------
93 // Attributes
94 // ------------------------------------------------------------------------
95
96 /**
97 * The command parameter
98 */
99 protected CommandParameter fParam;
100
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
104
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
114 CommandParameter param;
115 fLock.lock();
116 try {
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);
133 }
134
135 // Streamed trace
136 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
137 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
138
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 }
145
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();
152
153 if (!rc.hasService(IRemoteConnectionHostService.class)) {
154 return null;
155 }
156
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();
162
163 URI remoteUri;
164 try {
165 remoteUri = new URI(scheme, user, address, port, null, null, null);
166 } catch (URISyntaxException e) {
167 return false;
168 }
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
182 return null;
183 }
184
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();) {
199 Object element = iterator.next();
200 if (element instanceof TraceSessionComponent) {
201 // Add only TraceSessionComponents that are inactive and not
202 // destroyed
203 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
204 if ((tmpSession.isSnapshotSession() || tmpSession.isLiveTrace() || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
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 }
223
224 // ------------------------------------------------------------------------
225 // Helper methods
226 // ------------------------------------------------------------------------
227
228 private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo, final IProject project) {
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) {
239 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.Messages.LttngRelaydConnectionManager_ConnectionError, e);
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 }
297 }
This page took 0.038737 seconds and 5 git commands to generate.