lttng: Rename lttng2 feature/plugins to lttng2.control
[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.TmfTraceType;
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.wizards.importtrace.BatchImportTraceWizard;
56 import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
57 import org.eclipse.rse.services.files.IFileService;
58 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
59 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem;
60 import org.eclipse.ui.IWorkbenchPage;
61 import org.eclipse.ui.IWorkbenchWindow;
62 import org.eclipse.ui.PlatformUI;
63
64 /**
65 * <p>
66 * Command handler implementation to import traces from a (remote) session to a tracing project.
67 * </p>
68 *
69 * @author Bernd Hufmann
70 */
71 public class ImportHandler extends BaseControlViewHandler {
72
73 // ------------------------------------------------------------------------
74 // Constants
75 // ------------------------------------------------------------------------
76 /** Trace Type ID for LTTng Kernel traces */
77 private static final String LTTNG_KERNEL_TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype"; //$NON-NLS-1$
78 /** Trace Type ID for Generic CTF traces */
79 private static final String GENERIC_CTF_TRACE_TYPE = "org.eclipse.linuxtools.tmf.ui.type.ctf"; //$NON-NLS-1$
80 /** Name of default project to import traces to */
81 public static final String DEFAULT_REMOTE_PROJECT_NAME = "Remote"; //$NON-NLS-1$
82
83 // ------------------------------------------------------------------------
84 // Attributes
85 // ------------------------------------------------------------------------
86
87 /**
88 * The command parameter
89 */
90 protected CommandParameter fParam;
91
92 // ------------------------------------------------------------------------
93 // Operations
94 // ------------------------------------------------------------------------
95
96 @Override
97 public Object execute(ExecutionEvent event) throws ExecutionException {
98
99 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
100
101 if (window == null) {
102 return false;
103 }
104
105 fLock.lock();
106 try {
107 final CommandParameter param = fParam.clone();
108
109 // create default project
110 IProject project = TmfProjectRegistry.createProject(DEFAULT_REMOTE_PROJECT_NAME, null, null);
111
112 if (param.getSession().isStreamedTrace()) {
113 // Streamed trace
114 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
115 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
116
117 BatchImportTraceWizard wizard = new BatchImportTraceWizard();
118 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder));
119 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
120 dialog.open();
121 return null;
122 }
123
124 // Remote trace
125 final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog();
126 dialog.setSession(param.getSession());
127 dialog.setDefaultProject(DEFAULT_REMOTE_PROJECT_NAME);
128
129 if (dialog.open() != Window.OK) {
130 return null;
131 }
132
133 Job job = new Job(Messages.TraceControl_ImportJob) {
134 @Override
135 protected IStatus run(IProgressMonitor monitor) {
136
137 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, Messages.TraceControl_ImportFailure, null);
138 List<ImportFileInfo> traces = dialog.getTracePathes();
139 IProject selectedProject = dialog.getProject();
140 for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) {
141 try {
142 ImportFileInfo remoteFile = iterator.next();
143
144 downloadTrace(remoteFile, selectedProject, monitor);
145
146 // Set trace type
147 IFolder traceFolder = selectedProject.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
148
149 if (monitor.isCanceled()) {
150 status.add(Status.CANCEL_STATUS);
151 break;
152 }
153
154 IResource file = traceFolder.findMember(remoteFile.getLocalTraceName());
155
156 TraceTypeHelper helper = null;
157
158 if (remoteFile.isKernel()) {
159 helper = TmfTraceType.getInstance().getTraceType(LTTNG_KERNEL_TRACE_TYPE);
160 } else {
161 helper = TmfTraceType.getInstance().getTraceType(GENERIC_CTF_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 } catch (ExecutionException e) {
182 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e));
183 } catch (CoreException e) {
184 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e));
185 }
186 }
187 return status;
188 }
189 };
190 job.setUser(true);
191 job.schedule();
192 } finally {
193 fLock.unlock();
194 }
195 return null;
196 }
197
198 @Override
199 public boolean isEnabled() {
200 // Get workbench page for the Control View
201 IWorkbenchPage page = getWorkbenchPage();
202 if (page == null) {
203 return false;
204 }
205
206 // Check if one or more session are selected
207 ISelection selection = page.getSelection(ControlView.ID);
208 TraceSessionComponent session = null;
209 if (selection instanceof StructuredSelection) {
210 StructuredSelection structered = ((StructuredSelection) selection);
211 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
212 Object element = iterator.next();
213 if (element instanceof TraceSessionComponent) {
214 // Add only TraceSessionComponents that are inactive and not destroyed
215 TraceSessionComponent tmpSession = (TraceSessionComponent) element;
216 if (((tmpSession.isSnapshotSession()) || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
217 session = tmpSession;
218 }
219 }
220 }
221 }
222 boolean isEnabled = session != null;
223
224 fLock.lock();
225 try {
226 fParam = null;
227 if (isEnabled) {
228 fParam = new CommandParameter(session);
229 }
230 } finally {
231 fLock.unlock();
232 }
233 return isEnabled;
234 }
235
236 // ------------------------------------------------------------------------
237 // Helper methods
238 // ------------------------------------------------------------------------
239
240 /**
241 * Downloads a trace from the remote host to the given project.
242 *
243 * @param trace
244 * - trace information of trace to import
245 * @param project
246 * - project to import to
247 * @param monitor
248 * - a progress monitor
249 * @throws ExecutionException
250 */
251 private static void downloadTrace(ImportFileInfo trace, IProject project, IProgressMonitor monitor)
252 throws ExecutionException {
253 try {
254 IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem();
255
256 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
257 if (!traceFolder.exists()) {
258 throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$
259 }
260
261 String traceName = trace.getLocalTraceName();
262 IFolder folder = traceFolder.getFolder(traceName);
263 if (folder.exists()) {
264 if(!trace.isOverwrite()) {
265 throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$
266 }
267 } else {
268 folder.create(true, true, null);
269 }
270
271 IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor());
272 SubMonitor subMonitor = SubMonitor.convert(monitor, sources.length);
273 subMonitor.beginTask(Messages.TraceControl_DownloadTask, sources.length);
274
275 for (int i = 0; i < sources.length; i++) {
276 if (subMonitor.isCanceled()) {
277 monitor.setCanceled(true);
278 return;
279 }
280 String destination = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString();
281 subMonitor.setTaskName(Messages.TraceControl_DownloadTask + ' ' + traceName + '/' +sources[i].getName());
282 fsss.download(sources[i], destination, null, subMonitor.newChild(1));
283 }
284 } catch (SystemMessageException e) {
285 throw new ExecutionException(e.toString(), e);
286 } catch (CoreException e) {
287 throw new ExecutionException(e.toString(), e);
288 }
289 }
290 }
This page took 0.037061 seconds and 6 git commands to generate.