1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.synchronization
;
16 import java
.io
.IOException
;
17 import java
.util
.Collection
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfComponent
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.matching
.ITmfEventMatching
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.event
.matching
.TmfNetworkEventMatching
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
26 * This abstract manager class handles loading trace synchronization data or
27 * otherwise their calculation.
29 * @author Geneviève Bastien
32 public abstract class SynchronizationManager
extends TmfComponent
{
35 * Function called to synchronize traces using the fully incremental
36 * synchronization algorithm
39 * The target name of the synchronization file. If it exists, it
40 * will be opened, otherwise it will be created and data from
41 * this synchro run will be saved there
43 * The list of traces to synchronize
45 * Whether to actually synchronize or just try opening a sync
47 * @return The synchronization object
49 public static SynchronizationAlgorithm
synchronizeTraces(final File syncFile
, final Collection
<ITmfTrace
> traces
, boolean doSync
) {
51 SynchronizationAlgorithm syncAlgo
;
53 syncAlgo
= synchronize(syncFile
, traces
, SynchronizationAlgorithmFactory
.getDefaultAlgorithm());
55 syncAlgo
= openExisting(syncFile
);
56 if (syncAlgo
== null) {
57 syncAlgo
= SynchronizationAlgorithmFactory
.getDefaultAlgorithm();
64 * Function called to synchronize traces with a specific synchronization
65 * algorithm. If a synchronization already exists, but is not the requested
66 * algorithm, the synchronization is done again using the new algorithm
69 * The target name of the synchronization file. If it exists, it
70 * will be opened, otherwise it will be created and data from
71 * this synchro run will be saved there
73 * The list of traces to synchronize
75 * A synchronization algorithm object to determine the algorithm
76 * used to synchronization.
78 * Whether to actually synchronize or just try opening a sync
80 * @return The synchronization object
82 public static SynchronizationAlgorithm
synchronizeTraces(final File syncFile
, final Collection
<ITmfTrace
> traces
, SynchronizationAlgorithm algo
, boolean doSync
) {
84 SynchronizationAlgorithm syncAlgo
;
86 syncAlgo
= synchronize(syncFile
, traces
, algo
);
88 syncAlgo
= openExisting(syncFile
);
89 if (syncAlgo
== null || (syncAlgo
.getClass() != algo
.getClass())) {
93 syncAlgo
= SynchronizationAlgorithmFactory
.getDefaultAlgorithm();
101 private static SynchronizationAlgorithm
openExisting(final File syncFile
) {
102 if ((syncFile
!= null) && syncFile
.exists()) {
103 /* Load an existing history */
105 SynchronizationBackend syncBackend
= new SynchronizationBackend(syncFile
);
106 SynchronizationAlgorithm algo
= syncBackend
.openExistingSync();
108 } catch (IOException e
) {
110 * There was an error opening the existing file. Perhaps it was
111 * corrupted, perhaps it's an old version? We'll just
112 * fall-through and try to build a new one from scratch instead.
114 Activator
.logInfo("Problem opening existing trace synchronization file", e
); //$NON-NLS-1$
120 private static SynchronizationAlgorithm
synchronize(final File syncFile
, final Collection
<ITmfTrace
> traces
, SynchronizationAlgorithm syncAlgo
) {
121 ITmfEventMatching matching
= new TmfNetworkEventMatching(traces
, syncAlgo
);
122 matching
.matchEvents();
124 SynchronizationBackend syncBackend
;
126 syncBackend
= new SynchronizationBackend(syncFile
, false);
127 syncBackend
.saveSync(syncAlgo
);
128 } catch (IOException e
) {
129 Activator
.logError("Error while saving trace synchronization file", e
); //$NON-NLS-1$