lttng-control: Use legacy import wizard for network traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / 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 **********************************************************************/
14 package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers;
15
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.MultiStatus;
31 import org.eclipse.core.runtime.NullProgressMonitor;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.core.runtime.SubMonitor;
34 import org.eclipse.core.runtime.URIUtil;
35 import org.eclipse.core.runtime.jobs.Job;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.window.Window;
39 import org.eclipse.jface.wizard.WizardDialog;
40 import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState;
41 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
42 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
43 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.IImportDialog;
44 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.ImportFileInfo;
45 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
46 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
47 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
48 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
49 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException;
50 import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
51 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
52 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
53 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
54 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils;
55 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTracesFolder;
56 import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils;
57 import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
58 import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
59 import org.eclipse.rse.services.files.IFileService;
60 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
61 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem;
62 import org.eclipse.ui.IWorkbenchPage;
63 import org.eclipse.ui.IWorkbenchWindow;
64 import org.eclipse.ui.PlatformUI;
65
66 /**
67 * <p>
68 * Command handler implementation to import traces from a (remote) session to a tracing project.
69 * </p>
70 *
71 * @author Bernd Hufmann
72 */
73 public class ImportHandler extends BaseControlViewHandler {
74
75 // ------------------------------------------------------------------------
76 // Constants
77 // ------------------------------------------------------------------------
78 /** Name of default project to import traces to */
79 public static final String DEFAULT_REMOTE_PROJECT_NAME = "Remote"; //$NON-NLS-1$
80
81 // ------------------------------------------------------------------------
82 // Attributes
83 // ------------------------------------------------------------------------
84
85 /**
86 * The command parameter
87 */
88 protected CommandParameter fParam;
89
90 // ------------------------------------------------------------------------
91 // Operations
92 // ------------------------------------------------------------------------
93
94 @Override
95 public Object execute(ExecutionEvent event) throws ExecutionException {
96
97 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
98
99 if (window == null) {
100 return false;
101 }
102
103 fLock.lock();
104 try {
105 final CommandParameter param = fParam.clone();
106
107 // create default project
108 IProject project = TmfProjectRegistry.createProject(DEFAULT_REMOTE_PROJECT_NAME, null, null);
109
110 if (param.getSession().isStreamedTrace()) {
111 // Streamed trace
112 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
113 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
114
115 ImportTraceWizard wizard = new ImportTraceWizard();
116 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder));
117 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
118 dialog.open();
119 return null;
120 }
121
122 // Remote trace
123 final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog();
124 dialog.setSession(param.getSession());
125 dialog.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME);
126
127 if (dialog.open() != Window.OK) {
128 return null;
129 }
130
131 Job job = new Job(Messages.TraceControl_ImportJob) {
132 @Override
133 protected IStatus run(IProgressMonitor monitor) {
134
135 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, Messages.TraceControl_ImportFailure, null);
136 List<ImportFileInfo> traces = dialog.getTracePathes();
137 IProject selectedProject = dialog.getProject();
138 for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) {
139 try {
140
141 if (monitor.isCanceled()) {
142 status.add(Status.CANCEL_STATUS);
143 break;
144 }
145
146 ImportFileInfo remoteFile = iterator.next();
147
148 downloadTrace(remoteFile, selectedProject, monitor);
149
150 // Set trace type
151 IFolder traceFolder = remoteFile.getDestinationFolder();
152
153 IResource file = traceFolder.findMember(remoteFile.getLocalTraceName());
154
155 if (file != null) {
156 TraceTypeHelper helper = null;
157
158 try {
159 helper = TmfTraceTypeUIUtils.selectTraceType(file.getLocationURI().getPath(), null, null);
160 } catch (TmfTraceImportException e) {
161 // the trace did not match any trace type
162 }
163
164 if (helper != null) {
165 status.add(TmfTraceTypeUIUtils.setTraceType(file, helper));
166 }
167
168 try {
169 final String scheme = "sftp"; //$NON-NLS-1$
170 String host = remoteFile.getImportFile().getHost().getName();
171 int port = remoteFile.getImportFile().getParentRemoteFileSubSystem().getConnectorService().getPort();
172 String path = remoteFile.getImportFile().getAbsolutePath();
173 if (file instanceof IFolder) {
174 path += IPath.SEPARATOR;
175 }
176 URI uri = new URI(scheme, null, host, port, path, null, null);
177 String sourceLocation = URIUtil.toUnencodedString(uri);
178 file.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
179 } catch (URISyntaxException e) {
180 }
181 }
182 } catch (ExecutionException e) {
183 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e));
184 } catch (CoreException e) {
185 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e));
186 }
187 }
188 return status;
189 }
190 };
191 job.setUser(true);
192 job.schedule();
193 } finally {
194 fLock.unlock();
195 }
196 return null;
197 }
198
199 @Override
200 public boolean isEnabled() {
201 // Get workbench page for the Control View
202 IWorkbenchPage page = getWorkbenchPage();
203 if (page == null) {
204 return false;
205 }
206
207 // Check if one or more session are selected
208 ISelection selection = page.getSelection(ControlView.ID);
209 TraceSessionComponent session = null;
210 if (selection instanceof StructuredSelection) {
211 StructuredSelection structered = ((StructuredSelection) selection);
212 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
213 Object element = iterator.next();
214 if (element instanceof TraceSessionComponent) {
215 // Add only TraceSessionComponents that are inactive and not destroyed
216 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
217 if (((tmpSession.isSnapshotSession()) || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
218 session = tmpSession;
219 }
220 }
221 }
222 }
223 boolean isEnabled = session != null;
224
225 fLock.lock();
226 try {
227 fParam = null;
228 if (isEnabled) {
229 fParam = new CommandParameter(session);
230 }
231 } finally {
232 fLock.unlock();
233 }
234 return isEnabled;
235 }
236
237 // ------------------------------------------------------------------------
238 // Helper methods
239 // ------------------------------------------------------------------------
240
241 /**
242 * Downloads a trace from the remote host to the given project.
243 *
244 * @param trace
245 * - trace information of trace to import
246 * @param project
247 * - project to import to
248 * @param monitor
249 * - a progress monitor
250 * @throws ExecutionException
251 */
252 private static void downloadTrace(ImportFileInfo trace, IProject project, IProgressMonitor monitor)
253 throws ExecutionException {
254 try {
255 IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem();
256
257 IFolder traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
258 if (!traceFolder.exists()) {
259 throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTracesFolder.TRACES_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$
260 }
261
262 IFolder destinationFolder = trace.getDestinationFolder();
263 TraceUtils.createFolder(destinationFolder, monitor);
264
265 String traceName = trace.getLocalTraceName();
266 IFolder folder = destinationFolder.getFolder(traceName);
267 if (folder.exists()) {
268 if(!trace.isOverwrite()) {
269 throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$
270 }
271 } else {
272 folder.create(true, true, null);
273 }
274
275 IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor());
276 SubMonitor subMonitor = SubMonitor.convert(monitor, sources.length);
277 subMonitor.beginTask(Messages.TraceControl_DownloadTask, sources.length);
278
279 for (int i = 0; i < sources.length; i++) {
280 if (subMonitor.isCanceled()) {
281 monitor.setCanceled(true);
282 return;
283 }
284 String destination = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString();
285 subMonitor.setTaskName(Messages.TraceControl_DownloadTask + ' ' + traceName + '/' +sources[i].getName());
286 fsss.download(sources[i], destination, null, subMonitor.newChild(1));
287 }
288 } catch (SystemMessageException e) {
289 throw new ExecutionException(e.toString(), e);
290 } catch (CoreException e) {
291 throw new ExecutionException(e.toString(), e);
292 }
293 }
294 }
This page took 0.03961 seconds and 5 git commands to generate.