tmf: Use Apache Common Compress for importing from archive
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / tracepkg / importexport / TracePackageImportOperation.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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 * Marc-Andre Laperle - Initial API and implementation
11 * Patrick Tasse - Add support for source location
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.importexport;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.lang.reflect.InvocationTargetException;
22 import java.net.URI;
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IFolder;
32 import org.eclipse.core.resources.IMarker;
33 import org.eclipse.core.resources.IResource;
34 import org.eclipse.core.runtime.CoreException;
35 import org.eclipse.core.runtime.IPath;
36 import org.eclipse.core.runtime.IProgressMonitor;
37 import org.eclipse.core.runtime.IStatus;
38 import org.eclipse.core.runtime.Path;
39 import org.eclipse.core.runtime.Status;
40 import org.eclipse.core.runtime.SubMonitor;
41 import org.eclipse.core.runtime.URIUtil;
42 import org.eclipse.jdt.annotation.NonNull;
43 import org.eclipse.jface.operation.ModalContext;
44 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
45 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation;
46 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement;
47 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
48 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
49 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement;
50 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFilesElement;
51 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
52 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
53 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException;
54 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
55 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
56 import org.eclipse.tracecompass.tmf.core.util.Pair;
57 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
58 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
59 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
60 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
61 import org.eclipse.ui.dialogs.IOverwriteQuery;
62 import org.eclipse.ui.ide.IDE;
63 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
64 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
65
66 /**
67 * An operation that imports a trace package from an archive
68 *
69 * @author Marc-Andre Laperle
70 */
71 public class TracePackageImportOperation extends AbstractTracePackageOperation implements IOverwriteQuery {
72
73 private final TracePackageElement[] fImportTraceElements;
74 private final TmfTraceFolder fTmfTraceFolder;
75
76 /**
77 * Constructs a new import operation
78 *
79 * @param importTraceElements
80 * the trace element to be imported
81 * @param fileName
82 * the output file name
83 * @param tmfTraceFolder
84 * the destination folder
85 */
86 public TracePackageImportOperation(String fileName, TracePackageElement[] importTraceElements, TmfTraceFolder tmfTraceFolder) {
87 super(fileName);
88 fImportTraceElements = importTraceElements;
89 fTmfTraceFolder = tmfTraceFolder;
90 }
91
92 private class ImportProvider implements IImportStructureProvider {
93
94 private Exception fException;
95
96 @Override
97 public List getChildren(Object element) {
98 return null;
99 }
100
101 @Override
102 public InputStream getContents(Object element) {
103 InputStream inputStream = null;
104 // We can add throws
105 try {
106 inputStream = ((ArchiveProviderElement) element).getContents();
107 } catch (IOException e) {
108 fException = e;
109 }
110 return inputStream;
111 }
112
113 @Override
114 public String getFullPath(Object element) {
115 return ((ArchiveProviderElement) element).getFullPath();
116 }
117
118 @Override
119 public String getLabel(Object element) {
120 return ((ArchiveProviderElement) element).getLabel();
121 }
122
123 @Override
124 public boolean isFolder(Object element) {
125 return ((ArchiveProviderElement) element).isFolder();
126 }
127
128 public Exception getException() {
129 return fException;
130 }
131 }
132
133 private class ArchiveProviderElement {
134
135 private final String fPath;
136 private final String fLabel;
137
138 private ArchiveFile fArchiveFile;
139 private ArchiveEntry fEntry;
140
141 public ArchiveProviderElement(String destinationPath, String label, ArchiveFile archiveFile, ArchiveEntry entry) {
142 fPath = destinationPath;
143 fLabel = label;
144 this.fArchiveFile = archiveFile;
145 this.fEntry = entry;
146 }
147
148 public InputStream getContents() throws IOException {
149 return fArchiveFile.getInputStream(fEntry);
150 }
151
152 public String getFullPath() {
153 return fPath;
154 }
155
156 public String getLabel() {
157 return fLabel;
158 }
159
160 public boolean isFolder() {
161 return false;
162 }
163 }
164
165 /**
166 * Run the operation. The status (result) of the operation can be obtained
167 * with {@link #getStatus}
168 *
169 * @param progressMonitor
170 * the progress monitor to use to display progress and receive
171 * requests for cancellation
172 */
173 @Override
174 public void run(IProgressMonitor progressMonitor) {
175 int totalWork = getNbCheckedElements(fImportTraceElements) * 2;
176 progressMonitor.beginTask(Messages.TracePackageImportOperation_ImportingPackage, totalWork);
177 doRun(progressMonitor);
178 progressMonitor.done();
179 }
180
181 private void doRun(IProgressMonitor progressMonitor) {
182 try {
183 setStatus(deleteExistingTraces(progressMonitor));
184 if (getStatus().getSeverity() != IStatus.OK) {
185 return;
186 }
187
188 TracePackageFilesElement traceFilesElement = null;
189 for (TracePackageElement packageElement : fImportTraceElements) {
190 TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement;
191 if (!isFilesChecked(packageElement)) {
192 continue;
193 }
194
195 TracePackageElement[] children = traceElement.getChildren();
196 for (TracePackageElement element : children) {
197 ModalContext.checkCanceled(progressMonitor);
198
199 if (element instanceof TracePackageFilesElement) {
200 traceFilesElement = (TracePackageFilesElement) element;
201 setStatus(importTraceFiles(traceFilesElement, traceElement, progressMonitor));
202
203 } else if (element instanceof TracePackageSupplFilesElement) {
204 TracePackageSupplFilesElement suppFilesElement = (TracePackageSupplFilesElement) element;
205 setStatus(importSupplFiles(suppFilesElement, traceElement, progressMonitor));
206 }
207
208 if (getStatus().getSeverity() != IStatus.OK) {
209 return;
210 }
211 }
212 }
213
214 } catch (InterruptedException e) {
215 setStatus(Status.CANCEL_STATUS);
216 }
217 }
218
219 /**
220 * Returns whether or not the Files element is checked under the given trace
221 * package element
222 *
223 * @param tracePackageElement
224 * the trace package element
225 * @return whether or not the Files element is checked under the given trace
226 * package element
227 */
228 public static boolean isFilesChecked(TracePackageElement tracePackageElement) {
229 for (TracePackageElement element : tracePackageElement.getChildren()) {
230 if (element instanceof TracePackageFilesElement) {
231 return element.isChecked();
232 }
233 }
234
235 return false;
236 }
237
238 /**
239 * Return the matching TmfTraceElement for a given trace element.
240 */
241 private TmfTraceElement getMatchingTraceElement(TracePackageTraceElement tracePackageElement) {
242 IPath tracePath = fTmfTraceFolder.getPath().append(tracePackageElement.getDestinationElementPath());
243 List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
244 for (TmfTraceElement t : traces) {
245 if (t.getPath().equals(tracePath)) {
246 return t;
247 }
248 }
249
250 return null;
251 }
252
253 private IStatus deleteExistingTraces(IProgressMonitor progressMonitor) {
254 for (TracePackageElement packageElement : fImportTraceElements) {
255 TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement;
256 if (!isFilesChecked(traceElement)) {
257 continue;
258 }
259
260 TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
261 if (existingTrace != null) {
262 try {
263 existingTrace.delete(SubMonitor.convert(progressMonitor));
264 } catch (CoreException e) {
265 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
266 }
267 }
268 }
269
270 return Status.OK_STATUS;
271 }
272
273 private void importBookmarks(IResource traceRes, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
274 for (TracePackageElement o : traceElement.getChildren()) {
275 if (o instanceof TracePackageBookmarkElement && o.isChecked()) {
276
277 // Get element
278 IFile bookmarksFile = null;
279 TmfTraceElement tmfTraceElement = getMatchingTraceElement(traceElement);
280 if (tmfTraceElement != null) {
281 try {
282 bookmarksFile = tmfTraceElement.createBookmarksFile();
283
284 // Make sure that if a bookmark is double-clicked first
285 // before opening the trace, it opens the right editor
286
287 // Get the editor id from the extension point
288 String traceEditorId = tmfTraceElement.getEditorId();
289 final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
290 IDE.setDefaultEditor(bookmarksFile, editorId);
291
292 } catch (CoreException e) {
293 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmarkFile, traceRes.getName()), e);
294 }
295 }
296
297 if (bookmarksFile == null) {
298 break;
299 }
300
301 TracePackageBookmarkElement bookmarkElement = (TracePackageBookmarkElement) o;
302
303 List<Map<String, String>> bookmarks = bookmarkElement.getBookmarks();
304 for (Map<String, String> attrs : bookmarks) {
305 IMarker createMarker = null;
306 try {
307 createMarker = bookmarksFile.createMarker(IMarker.BOOKMARK);
308 } catch (CoreException e) {
309 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
310 }
311 if (createMarker != null && createMarker.exists()) {
312 try {
313 for (Entry<String, String> entry : attrs.entrySet()) {
314 String key = entry.getKey();
315 String value = entry.getValue();
316 if (key.equals(IMarker.LOCATION)) {
317 try {
318 /* try location as an integer for backward compatibility */
319 createMarker.setAttribute(IMarker.LOCATION, Integer.parseInt(value));
320 } catch (NumberFormatException e) {
321 createMarker.setAttribute(IMarker.LOCATION, value);
322 }
323 } else {
324 createMarker.setAttribute(key, value);
325 }
326 }
327 } catch (CoreException e) {
328 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
329 }
330 }
331 }
332 }
333 }
334
335 monitor.worked(1);
336 }
337
338 private IStatus importTraceFiles(TracePackageFilesElement traceFilesElement, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
339 List<Pair<String, String>> fileNameAndLabelPairs = new ArrayList<>();
340
341 String sourceName = checkNotNull(traceFilesElement.getFileName());
342 String destinationName = checkNotNull(traceElement.getImportName());
343
344 fileNameAndLabelPairs.add(new Pair<>(sourceName, destinationName));
345
346 IPath containerPath = fTmfTraceFolder.getPath();
347 IStatus status = importFiles(getSpecifiedArchiveFile(), fileNameAndLabelPairs, containerPath, Path.EMPTY, monitor);
348 if (getStatus().getSeverity() != IStatus.OK) {
349 return status;
350 }
351
352 // We need to set the trace type before importing the supplementary files so we do it here
353 IResource traceRes = fTmfTraceFolder.getResource().findMember(traceElement.getDestinationElementPath());
354 if (traceRes == null || !traceRes.exists()) {
355 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorFindingImportedTrace, destinationName));
356 }
357
358 TraceTypeHelper traceType = null;
359 String traceTypeStr = traceElement.getTraceType();
360 if (traceTypeStr != null) {
361 traceType = TmfTraceType.getTraceType(traceTypeStr);
362 if (traceType == null) {
363 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName));
364 }
365 } else {
366 try {
367 monitor.subTask(MessageFormat.format(Messages.TracePackageImportOperation_DetectingTraceType, destinationName));
368 traceType = TmfTraceTypeUIUtils.selectTraceType(traceRes.getLocation().toOSString(), null, null);
369 } catch (TmfTraceImportException e) {
370 // Could not figure out the type
371 }
372 }
373
374 if (traceType != null) {
375 try {
376 TmfTraceTypeUIUtils.setTraceType(traceRes, traceType);
377 } catch (CoreException e) {
378 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName), e);
379 }
380 }
381
382 importBookmarks(traceRes, traceElement, monitor);
383
384 try {
385 URI uri = new File(getFileName()).toURI();
386 IPath entryPath = new Path(traceFilesElement.getFileName());
387 if (traceRes instanceof IFolder) {
388 entryPath = entryPath.addTrailingSeparator();
389 }
390 String sourceLocation = URIUtil.toUnencodedString(URIUtil.toJarURI(uri, entryPath));
391 traceRes.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
392 } catch (CoreException e) {
393 }
394
395 return status;
396 }
397
398 private IStatus importSupplFiles(TracePackageSupplFilesElement suppFilesElement, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
399 List<Pair<String, String>> fileNameAndLabelPairs = new ArrayList<>();
400 for (TracePackageElement child : suppFilesElement.getChildren()) {
401 if (child.isChecked()) {
402 TracePackageSupplFileElement supplFile = (TracePackageSupplFileElement) child;
403 fileNameAndLabelPairs.add(new Pair<>(checkNotNull(supplFile.getText()), checkNotNull(new Path(supplFile.getText()).lastSegment())));
404 }
405 }
406
407 if (!fileNameAndLabelPairs.isEmpty()) {
408 TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
409 if (existingTrace != null) {
410 ArchiveFile archiveFile = getSpecifiedArchiveFile();
411 existingTrace.refreshSupplementaryFolder();
412 // Project/Traces/A/B -> A/B
413 IPath traceFolderRelativePath = fTmfTraceFolder.getPath().makeRelativeTo(fTmfTraceFolder.getProject().getTracesFolder().getPath());
414 // Project/.tracing/A/B/
415 IFolder traceSupplementaryFolder = fTmfTraceFolder.getTraceSupplementaryFolder(traceFolderRelativePath.toString());
416 IPath destinationContainerPath = traceSupplementaryFolder.getFullPath();
417 // Remove the .tracing segment at the beginnin so that a file in folder .tracing/A/B/ imports destinationContainerPath/A/B/
418 Path baseSourcePath = new Path(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME);
419 return importFiles(archiveFile, fileNameAndLabelPairs, destinationContainerPath, baseSourcePath, monitor);
420 }
421 }
422
423 return Status.OK_STATUS;
424 }
425
426 private IStatus importFiles(ArchiveFile archiveFile, List<Pair<String, String>> fileNameAndLabelPairs, IPath destinationContainerPath, IPath baseSourcePath, IProgressMonitor monitor) {
427 List<ArchiveProviderElement> objects = new ArrayList<>();
428 Enumeration<@NonNull ?> entries = archiveFile.entries();
429 while (entries.hasMoreElements()) {
430 ArchiveEntry entry = (ArchiveEntry) entries.nextElement();
431 String entryName = entry.getName();
432 IPath fullArchivePath = new Path(entryName);
433 if (fullArchivePath.hasTrailingSeparator()) {
434 // We only care about file entries as the folders will get created by the ImportOperation
435 continue;
436 }
437
438 for (Pair<String, String> fileNameAndLabel : fileNameAndLabelPairs) {
439
440 // Examples: Traces/aaa/kernel/ .tracing/aaa/testtexttrace.txt/statistics.ht
441 IPath searchedArchivePath = new Path(fileNameAndLabel.getFirst());
442
443 // Check if this archive entry matches the searched file name at this archive location
444 boolean fileMatch = entryName.equalsIgnoreCase(searchedArchivePath.toString());
445 // For example Traces/aaa/kernel/metadata matches Traces/aaa/kernel/
446 boolean folderMatch = entryName.startsWith(searchedArchivePath + "/"); //$NON-NLS-1$
447
448 if (fileMatch || folderMatch) {
449 // .tracing/aaa/testtexttrace.txt/statistics.ht -> aaa/testtexttrace.txt/statistics.ht
450 IPath destinationPath = fullArchivePath.makeRelativeTo(baseSourcePath);
451
452 // metadata statistics.ht
453 // We don't use the label when the entry is a folder match because the labels for individual files
454 // under the folder are not specified in the manifest so just use the last segment.
455 String resourceLabel = folderMatch ? fullArchivePath.lastSegment() : fileNameAndLabel.getSecond();
456
457 ArchiveProviderElement pe = new ArchiveProviderElement(destinationPath.toString(), resourceLabel, archiveFile, entry);
458 objects.add(pe);
459 break;
460 }
461 }
462 }
463
464 ImportProvider provider = new ImportProvider();
465
466 ImportOperation operation = new ImportOperation(destinationContainerPath,
467 null, provider, this,
468 objects);
469 operation.setCreateContainerStructure(true);
470 operation.setOverwriteResources(true);
471
472 try {
473 operation.run(SubMonitor.convert(monitor));
474 archiveFile.close();
475 } catch (InvocationTargetException e) {
476 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
477 } catch (InterruptedException e) {
478 return Status.CANCEL_STATUS;
479 } catch (IOException e) {
480 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
481 }
482
483 if (provider.getException() != null) {
484 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, provider.getException());
485 }
486
487 return operation.getStatus();
488 }
489
490 @Override
491 public String queryOverwrite(String pathString) {
492 // We always overwrite once we reach this point
493 return null;
494 }
495 }
This page took 0.063175 seconds and 5 git commands to generate.