29f972d0d9e750faa9c9092b1c9d538574e659e6
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / synchronization / SynchronizationManager.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.synchronization;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Collection;
18
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.TmfEventMatching;
23 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
24
25 /**
26 * This abstract manager class handles loading trace synchronization data or
27 * otherwise their calculation.
28 *
29 * @author Geneviève Bastien
30 */
31 public abstract class SynchronizationManager extends TmfComponent {
32
33 /**
34 * Function called to synchronize traces using the fully incremental
35 * synchronization algorithm
36 *
37 * @param syncFile
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
41 * @param traces
42 * The list of traces to synchronize
43 * @param doSync
44 * Whether to actually synchronize or just try opening a sync
45 * file
46 * @return The synchronization object
47 */
48 public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection<ITmfTrace> traces, boolean doSync) {
49
50 SynchronizationAlgorithm syncAlgo;
51 if (doSync) {
52 syncAlgo = synchronize(syncFile, traces, SynchronizationAlgorithmFactory.getDefaultAlgorithm());
53 } else {
54 syncAlgo = openExisting(syncFile);
55 if (syncAlgo == null) {
56 syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm();
57 }
58 }
59 return syncAlgo;
60 }
61
62 /**
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
66 *
67 * @param syncFile
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
71 * @param traces
72 * The list of traces to synchronize
73 * @param algo
74 * A synchronization algorithm object to determine the algorithm
75 * used to synchronization.
76 * @param doSync
77 * Whether to actually synchronize or just try opening a sync
78 * file
79 * @return The synchronization object
80 */
81 public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection<ITmfTrace> traces, SynchronizationAlgorithm algo, boolean doSync) {
82
83 SynchronizationAlgorithm syncAlgo;
84 if (doSync) {
85 syncAlgo = synchronize(syncFile, traces, algo);
86 } else {
87 syncAlgo = openExisting(syncFile);
88 if (syncAlgo == null || (syncAlgo.getClass() != algo.getClass())) {
89 if (algo != null) {
90 syncAlgo = algo;
91 } else {
92 syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm();
93 }
94 }
95 }
96
97 return syncAlgo;
98 }
99
100 private static SynchronizationAlgorithm openExisting(final File syncFile) {
101 if ((syncFile != null) && syncFile.exists()) {
102 /* Load an existing history */
103 try {
104 SynchronizationBackend syncBackend = new SynchronizationBackend(syncFile);
105 SynchronizationAlgorithm algo = syncBackend.openExistingSync();
106 return algo;
107 } catch (IOException e) {
108 /*
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.
112 */
113 Activator.logInfo("Problem opening existing trace synchronization file", e); //$NON-NLS-1$
114 }
115 }
116 return null;
117 }
118
119 private static SynchronizationAlgorithm synchronize(final File syncFile, final Collection<ITmfTrace> traces, SynchronizationAlgorithm syncAlgo) {
120 ITmfEventMatching matching = new TmfEventMatching(traces, syncAlgo);
121 matching.matchEvents();
122
123 SynchronizationBackend syncBackend;
124 try {
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$
129 }
130 return syncAlgo;
131 }
132
133 }
This page took 0.0352 seconds and 4 git commands to generate.