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
.linuxtools
.tmf
.core
.synchronization
;
16 import java
.io
.IOException
;
18 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.matching
.ITmfEventMatching
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.matching
.TmfNetworkEventMatching
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
25 * This abstract manager class handles loading trace synchronization data or
26 * otherwise their calculation.
28 * @author Geneviève Bastien
31 public abstract class SynchronizationManager
extends TmfComponent
{
34 * Function called to synchronize traces using the fully incremental
35 * synchronization algorithm
38 * The target name of the synchronization file. If it exists, it
39 * will be opened, otherwise it will be created and data from
40 * this synchro run will be saved there
42 * The list of traces to synchronize
44 * Whether to actually synchronize or just try opening a sync
46 * @return The synchronization object
48 public static SynchronizationAlgorithm
synchronizeTraces(final File syncFile
, final ITmfTrace
[] traces
, boolean doSync
) {
50 SynchronizationAlgorithm syncAlgo
;
52 syncAlgo
= synchronize(syncFile
, traces
, new SyncAlgorithmFullyIncremental());
54 syncAlgo
= openExisting(syncFile
);
55 if (syncAlgo
== null) {
56 syncAlgo
= new SyncAlgorithmFullyIncremental();
63 * Function called to synchronize traces with a specific synchronization
64 * algorithm. If a synchronization already exists, but is not the requested
65 * algorithm, the synchronization is done again using the new algorithm
68 * The target name of the synchronization file. If it exists, it
69 * will be opened, otherwise it will be created and data from
70 * this synchro run will be saved there
72 * The list of traces to synchronize
74 * A synchronization algorithm object to determine the algorithm
75 * used to synchronization.
77 * Whether to actually synchronize or just try opening a sync
79 * @return The synchronization object
81 public static SynchronizationAlgorithm
synchronizeTraces(final File syncFile
, final ITmfTrace
[] traces
, SynchronizationAlgorithm algo
, boolean doSync
) {
83 SynchronizationAlgorithm syncAlgo
;
85 syncAlgo
= synchronize(syncFile
, traces
, algo
);
87 syncAlgo
= openExisting(syncFile
);
88 if (syncAlgo
== null || (syncAlgo
.getClass() != algo
.getClass())) {
92 syncAlgo
= new SyncAlgorithmFullyIncremental();
100 private static SynchronizationAlgorithm
openExisting(final File syncFile
) {
101 if ((syncFile
!= null) && syncFile
.exists()) {
102 /* Load an existing history */
104 SynchronizationBackend syncBackend
= new SynchronizationBackend(syncFile
);
105 SynchronizationAlgorithm algo
= syncBackend
.openExistingSync();
107 } catch (IOException e
) {
109 * There was an error opening the existing file. Perhaps it was
110 * corrupted, perhaps it's an old version? We'll just
111 * fall-through and try to build a new one from scratch instead.
113 Activator
.logInfo("Problem opening existing trace synchronization file", e
); //$NON-NLS-1$
119 private static SynchronizationAlgorithm
synchronize(final File syncFile
, final ITmfTrace
[] traces
, SynchronizationAlgorithm syncAlgo
) {
120 ITmfEventMatching matching
= new TmfNetworkEventMatching(traces
, syncAlgo
);
121 matching
.matchEvents();
123 SynchronizationBackend syncBackend
;
125 syncBackend
= new SynchronizationBackend(syncFile
, false);
126 syncBackend
.saveSync(syncAlgo
);
127 } catch (IOException e
) {
128 Activator
.logError("Error while saving trace synchronization file", e
); //$NON-NLS-1$