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