1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.importexport
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.text
.MessageFormat
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Enumeration
;
21 import java
.util
.List
;
24 import org
.eclipse
.core
.resources
.IFile
;
25 import org
.eclipse
.core
.resources
.IMarker
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.runtime
.CoreException
;
28 import org
.eclipse
.core
.runtime
.IPath
;
29 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Path
;
32 import org
.eclipse
.core
.runtime
.Status
;
33 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
34 import org
.eclipse
.jface
.operation
.ModalContext
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.AbstractTracePackageOperation
;
37 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageBookmarkElement
;
38 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
39 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
40 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFileElement
;
41 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFilesElement
;
42 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfNavigatorContentProvider
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceTypeHelper
;
50 import org
.eclipse
.ui
.dialogs
.IOverwriteQuery
;
51 import org
.eclipse
.ui
.ide
.IDE
;
52 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.TarException
;
53 import org
.eclipse
.ui
.wizards
.datatransfer
.IImportStructureProvider
;
54 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
57 * An operation that imports a trace package from an archive
59 * @author Marc-Andre Laperle
61 @SuppressWarnings("restriction")
62 public class TracePackageImportOperation
extends AbstractTracePackageOperation
implements IOverwriteQuery
{
64 private final TracePackageTraceElement fImportTraceElement
;
65 private final TmfTraceFolder fTmfTraceFolder
;
67 // Result of reading the manifest
68 private TracePackageElement fResultElement
;
71 * Constructs a new import operation
73 * @param importTraceElement
74 * the trace element to be imported
76 * the output file name
77 * @param tmfTraceFolder
78 * the destination folder
80 public TracePackageImportOperation(String fileName
, TracePackageTraceElement importTraceElement
, TmfTraceFolder tmfTraceFolder
) {
82 fImportTraceElement
= importTraceElement
;
83 fTmfTraceFolder
= tmfTraceFolder
;
86 private class ImportProvider
implements IImportStructureProvider
{
88 private Exception fException
;
91 public List
getChildren(Object element
) {
96 public InputStream
getContents(Object element
) {
97 InputStream inputStream
= null;
100 inputStream
= ((ArchiveProviderElement
) element
).getContents();
101 } catch (IOException e
) {
103 } catch (TarException e
) {
110 public String
getFullPath(Object element
) {
111 return ((ArchiveProviderElement
) element
).getFullPath();
115 public String
getLabel(Object element
) {
116 return ((ArchiveProviderElement
) element
).getLabel();
120 public boolean isFolder(Object element
) {
121 return ((ArchiveProviderElement
) element
).isFolder();
124 public Exception
getException() {
129 private class ArchiveProviderElement
{
131 private final String fPath
;
132 private final String fLabel
;
134 private ArchiveFile fArchiveFile
;
135 private ArchiveEntry fEntry
;
137 public ArchiveProviderElement(String destinationPath
, String label
, ArchiveFile archiveFile
, ArchiveEntry entry
) {
138 fPath
= destinationPath
;
140 this.fArchiveFile
= archiveFile
;
144 public InputStream
getContents() throws TarException
, IOException
{
145 return fArchiveFile
.getInputStream(fEntry
);
148 public String
getFullPath() {
152 public String
getLabel() {
156 public boolean isFolder() {
162 * Run the operation. The status (result) of the operation can be obtained
163 * with {@link #getStatus}
165 * @param progressMonitor
166 * the progress monitor to use to display progress and receive
167 * requests for cancellation
170 public void run(IProgressMonitor progressMonitor
) {
171 int totalWork
= getNbCheckedElements(new TracePackageElement
[] { fImportTraceElement
}) * 2;
172 progressMonitor
.beginTask(Messages
.TracePackageImportOperation_ImportingPackage
, totalWork
);
173 doRun(progressMonitor
);
174 progressMonitor
.done();
177 private void doRun(IProgressMonitor progressMonitor
) {
179 setStatus(deleteExistingTrace(progressMonitor
));
180 if (getStatus().getSeverity() != IStatus
.OK
) {
184 TracePackageElement
[] children
= fImportTraceElement
.getChildren();
185 for (TracePackageElement element
: children
) {
186 ModalContext
.checkCanceled(progressMonitor
);
188 if (element
instanceof TracePackageFilesElement
) {
189 TracePackageFilesElement traceFilesElement
= (TracePackageFilesElement
) element
;
190 setStatus(importTraceFiles(progressMonitor
, traceFilesElement
));
192 } else if (element
instanceof TracePackageSupplFilesElement
) {
193 TracePackageSupplFilesElement suppFilesElement
= (TracePackageSupplFilesElement
) element
;
194 setStatus(importSupplFiles(progressMonitor
, suppFilesElement
));
197 if (getStatus().getSeverity() != IStatus
.OK
) {
202 String traceName
= fImportTraceElement
.getText();
203 IResource traceRes
= fTmfTraceFolder
.getResource().findMember(traceName
);
204 if (traceRes
== null || !traceRes
.exists()) {
205 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, MessageFormat
.format(Messages
.ImportTracePackageWizardPage_ErrorFindingImportedTrace
, traceName
)));
209 TraceTypeHelper traceType
= TmfTraceType
.getInstance().getTraceType(fImportTraceElement
.getTraceType());
210 if (traceType
== null) {
211 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, MessageFormat
.format(Messages
.ImportTracePackageWizardPage_ErrorSettingTraceType
, fImportTraceElement
.getTraceType(), traceName
)));
216 TmfTraceType
.setTraceType(traceRes
.getFullPath(), traceType
);
217 } catch (CoreException e
) {
218 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, MessageFormat
.format(Messages
.ImportTracePackageWizardPage_ErrorSettingTraceType
, fImportTraceElement
.getTraceType(), traceName
), e
));
221 importBookmarks(traceRes
, progressMonitor
);
223 } catch (InterruptedException e
) {
224 setStatus(Status
.CANCEL_STATUS
);
228 private IStatus
deleteExistingTrace(IProgressMonitor progressMonitor
) {
229 List
<TmfTraceElement
> traces
= fTmfTraceFolder
.getTraces();
230 TmfTraceElement existingTrace
= null;
232 for (TmfTraceElement t
: traces
) {
233 if (t
.getName().equals(fImportTraceElement
.getText())) {
239 if (existingTrace
!= null) {
241 existingTrace
.delete(new SubProgressMonitor(progressMonitor
, 1, SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
));
242 } catch (CoreException e
) {
243 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.Messages
.TracePackage_ErrorOperation
, e
);
247 return Status
.OK_STATUS
;
250 private void importBookmarks(IResource traceRes
, IProgressMonitor monitor
) {
251 for (TracePackageElement o
: fImportTraceElement
.getChildren()) {
252 if (o
instanceof TracePackageBookmarkElement
&& o
.isChecked()) {
255 IFile bookmarksFile
= null;
256 List
<TmfTraceElement
> traces
= fTmfTraceFolder
.getTraces();
257 for (TmfTraceElement t
: traces
) {
258 if (t
.getName().equals(traceRes
.getName())) {
260 bookmarksFile
= t
.createBookmarksFile();
262 // Make sure that if a bookmark is double-clicked first
263 // before opening the trace, it opens the right editor
265 // Get the editor id from the extension point
266 String traceEditorId
= t
.getEditorId();
267 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
268 IDE
.setDefaultEditor(bookmarksFile
, editorId
);
270 } catch (CoreException e
) {
271 Activator
.getDefault().logError(MessageFormat
.format(Messages
.TracePackageImportOperation_ErrorCreatingBookmarkFile
, traceRes
.getName()), e
);
277 if (bookmarksFile
== null) {
281 TracePackageBookmarkElement bookmarkElement
= (TracePackageBookmarkElement
) o
;
283 List
<Map
<String
, String
>> bookmarks
= bookmarkElement
.getBookmarks();
284 for (Map
<String
, String
> attrs
: bookmarks
) {
285 IMarker createMarker
= null;
287 createMarker
= bookmarksFile
.createMarker(IMarker
.BOOKMARK
);
288 } catch (CoreException e
) {
289 Activator
.getDefault().logError(MessageFormat
.format(Messages
.TracePackageImportOperation_ErrorCreatingBookmark
, traceRes
.getName()), e
);
291 if (createMarker
!= null && createMarker
.exists()) {
293 for (String key
: attrs
.keySet()) {
294 String value
= attrs
.get(key
);
295 if (key
.equals(IMarker
.LOCATION
)) {
296 createMarker
.setAttribute(IMarker
.LOCATION
, Integer
.valueOf(value
).intValue());
298 createMarker
.setAttribute(key
, value
);
301 } catch (CoreException e
) {
302 Activator
.getDefault().logError(MessageFormat
.format(Messages
.TracePackageImportOperation_ErrorCreatingBookmark
, traceRes
.getName()), e
);
312 private static boolean fileNameMatches(String fileName
, String entryName
) {
313 boolean fileMatch
= entryName
.equalsIgnoreCase(fileName
);
314 boolean folderMatch
= entryName
.startsWith(fileName
+ "/"); //$NON-NLS-1$
315 return fileMatch
|| folderMatch
;
318 private IStatus
importTraceFiles(IProgressMonitor monitor
, TracePackageFilesElement traceFilesElement
) {
319 List
<String
> fileNames
= new ArrayList
<String
>();
320 IPath prefix
= new Path(TmfTraceFolder
.TRACE_FOLDER_NAME
);
321 fileNames
.add(traceFilesElement
.getFileName());
322 IPath containerPath
= fTmfTraceFolder
.getPath();
323 IStatus status
= importFiles(getSpecifiedArchiveFile(), fileNames
, prefix
, containerPath
, monitor
);
325 new TmfNavigatorContentProvider().getChildren(fTmfTraceFolder
);
330 private IStatus
importSupplFiles(IProgressMonitor monitor
, TracePackageSupplFilesElement suppFilesElement
) {
331 List
<String
> fileNames
= new ArrayList
<String
>();
332 for (TracePackageElement child
: suppFilesElement
.getChildren()) {
333 TracePackageSupplFileElement supplFile
= (TracePackageSupplFileElement
) child
;
334 fileNames
.add(supplFile
.getText());
337 if (!fileNames
.isEmpty()) {
338 List
<TmfTraceElement
> traces
= fTmfTraceFolder
.getTraces();
339 TmfTraceElement traceElement
= null;
340 for (TmfTraceElement t
: traces
) {
341 if (t
.getName().equals(fImportTraceElement
.getText())) {
347 if (traceElement
!= null) {
348 ArchiveFile archiveFile
= getSpecifiedArchiveFile();
349 traceElement
.refreshSupplementaryFolder();
350 String traceName
= traceElement
.getResource().getName();
351 // Project/.tracing/tracename
352 IPath destinationContainerPath
= traceElement
.getTraceSupplementaryFolder(traceName
).getFullPath();
353 // .tracing/tracename
354 IPath pathInArchive
= new Path(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
).append(traceName
);
355 return importFiles(archiveFile
, fileNames
, pathInArchive
, destinationContainerPath
, monitor
);
359 return Status
.OK_STATUS
;
362 private IStatus
importFiles(ArchiveFile archiveFile
, List
<String
> fileNames
, IPath pathInArchive
, IPath destinationContainerPath
, IProgressMonitor monitor
) {
363 List
<ArchiveProviderElement
> objects
= new ArrayList
<ArchiveProviderElement
>();
364 Enumeration
<?
> entries
= archiveFile
.entries();
365 while (entries
.hasMoreElements()) {
366 ArchiveEntry entry
= (ArchiveEntry
) entries
.nextElement();
367 String entryName
= entry
.getName();
368 IPath fullArchivePath
= new Path(entryName
);
369 if (fullArchivePath
.hasTrailingSeparator()) {
370 // We only care about file entries as the folders will get created by the ImportOperation
374 for (String fileName
: fileNames
) {
375 // Check if this archive entry matches the searched file name at this archive location
376 IPath searchedArchivePath
= pathInArchive
.append(fileName
);
377 if (fileNameMatches(searchedArchivePath
.toString(), entryName
)) {
378 // Traces/kernel/metadata
379 // kernel/metadata, the ImportOperation will take care of creating the kernel folder
380 IPath destinationPath
= fullArchivePath
.removeFirstSegments(pathInArchive
.segmentCount());
382 String resourceLabel
= fullArchivePath
.lastSegment();
384 ArchiveProviderElement pe
= new ArchiveProviderElement(destinationPath
.toString(), resourceLabel
, archiveFile
, entry
);
391 ImportProvider provider
= new ImportProvider();
393 ImportOperation operation
= new ImportOperation(destinationContainerPath
,
394 null, provider
, this,
396 operation
.setCreateContainerStructure(true);
397 operation
.setOverwriteResources(true);
400 operation
.run(new SubProgressMonitor(monitor
, fileNames
.size(), SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
));
402 } catch (InvocationTargetException e
) {
403 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.Messages
.TracePackage_ErrorOperation
, e
);
404 } catch (InterruptedException e
) {
405 return Status
.CANCEL_STATUS
;
406 } catch (IOException e
) {
407 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.Messages
.TracePackage_ErrorOperation
, e
);
410 if (provider
.getException() != null) {
411 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.Messages
.TracePackage_ErrorOperation
, provider
.getException());
414 return operation
.getStatus();
418 public String
queryOverwrite(String pathString
) {
419 // We always overwrite once we reach this point
424 * Get the resulting element from extracting the manifest from the archive
426 * @return the resulting element
428 public TracePackageElement
getResultElement() {
429 return fResultElement
;