tmf: Fix concurrent data access violations
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / TmfAnalysisManager.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 API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.analysis;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.tracecompass.internal.tmf.core.Activator;
26 import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisModuleSources;
27 import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisParameterProviders;
28 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
29
30 import com.google.common.collect.HashMultimap;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.ImmutableMultimap;
33 import com.google.common.collect.Multimap;
34
35 /**
36 * Manages the available analysis helpers from different sources and their
37 * parameter providers.
38 *
39 * @author Geneviève Bastien
40 */
41 @NonNullByDefault
42 public class TmfAnalysisManager {
43
44 private static final Multimap<String, IAnalysisModuleHelper> fAnalysisModules = HashMultimap.create();
45 private static final Map<String, List<Class<? extends IAnalysisParameterProvider>>> fParameterProviders = new HashMap<>();
46 private static final Map<Class<? extends IAnalysisParameterProvider>, IAnalysisParameterProvider> fParamProviderInstances = new HashMap<>();
47 private static final List<IAnalysisModuleSource> fSources = new ArrayList<>();
48 private static final List<ITmfNewAnalysisModuleListener> fListeners = new ArrayList<>();
49
50 /**
51 * Constructor, not to be used
52 */
53 private TmfAnalysisManager() {
54
55 }
56
57 /**
58 * Disposes the analysis manager
59 *
60 * @since 2.3
61 */
62 public static void dispose() {
63 TmfAnalysisParameterProviders.dispose();
64 synchronized (fParameterProviders) {
65 fParamProviderInstances.values().forEach(provider -> provider.dispose());
66 }
67 }
68
69 /**
70 * Registers a new source of modules
71 *
72 * @param source
73 * A {@link IAnalysisModuleSource} instance
74 */
75 public static synchronized void registerModuleSource(IAnalysisModuleSource source) {
76 fSources.add(source);
77 refreshModules();
78 }
79
80 /**
81 * Initializes sources and new module listeners from the extension point
82 */
83 public static synchronized void initialize() {
84 fSources.clear();
85 fListeners.clear();
86 initializeModuleSources();
87 initializeNewModuleListeners();
88 }
89
90 /**
91 * Cleans the module sources list and initialize it from the extension point
92 */
93 private static synchronized void initializeModuleSources() {
94 for (IAnalysisModuleSource source : TmfAnalysisModuleSources.getSources()) {
95 fSources.add(source);
96 }
97 }
98
99 /**
100 * Cleans the new module listeners list and initialize it from the extension
101 * point
102 */
103 private static synchronized void initializeNewModuleListeners() {
104 for (ITmfNewAnalysisModuleListener output : TmfAnalysisModuleOutputs.getOutputListeners()) {
105 fListeners.add(output);
106 }
107 }
108
109 /**
110 * Add a new module listener to the list of listeners
111 *
112 * @param listener
113 * The new module listener
114 */
115 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener) {
116 fListeners.add(listener);
117 }
118
119 /**
120 * Gets all available analysis module helpers
121 *
122 * This map is read-only
123 *
124 * @return The map of available {@link IAnalysisModuleHelper}
125 * @since 1.0
126 */
127 public static synchronized Multimap<String, IAnalysisModuleHelper> getAnalysisModules() {
128 if (fAnalysisModules.isEmpty()) {
129 for (IAnalysisModuleSource source : fSources) {
130 for (IAnalysisModuleHelper helper : source.getAnalysisModules()) {
131 fAnalysisModules.put(helper.getId(), helper);
132 }
133 }
134 }
135 return checkNotNull(ImmutableMultimap.copyOf(fAnalysisModules));
136 }
137
138 /**
139 * Gets all analysis module helpers that apply to a given trace type. For
140 * each analysis ID, only one helper will be returned if more than one
141 * applies.
142 *
143 * This map is read-only
144 *
145 * TODO: This method is only used to populate the project view in the UI. It
146 * should be deprecated eventually, after some UI rework, so that the trace
147 * type does not drive whether the analysis module applies or not to a
148 * trace, but rather the content of the trace or experiment (once it is
149 * opened)
150 *
151 * @param traceclass
152 * The trace class to get modules for
153 * @return The map of available {@link IAnalysisModuleHelper}
154 */
155 public static Map<String, IAnalysisModuleHelper> getAnalysisModules(Class<? extends ITmfTrace> traceclass) {
156 Multimap<String, IAnalysisModuleHelper> allModules = getAnalysisModules();
157 Map<String, IAnalysisModuleHelper> map = new HashMap<>();
158 for (IAnalysisModuleHelper module : allModules.values()) {
159 if (module.appliesToTraceType(traceclass)) {
160 map.put(module.getId(), module);
161 }
162 }
163 return ImmutableMap.copyOf(map);
164 }
165
166 /**
167 * Register a new parameter provider for an analysis
168 *
169 * @param analysisId
170 * The id of the analysis
171 * @param paramProvider
172 * The class of the parameter provider
173 */
174 public static void registerParameterProvider(String analysisId, Class<? extends IAnalysisParameterProvider> paramProvider) {
175 synchronized (fParameterProviders) {
176 if (!fParameterProviders.containsKey(analysisId)) {
177 fParameterProviders.put(analysisId, new ArrayList<Class<? extends IAnalysisParameterProvider>>());
178 }
179 /* We checked via containsKey() above, get() should not return null */
180 checkNotNull(fParameterProviders.get(analysisId)).add(paramProvider);
181 }
182 }
183
184 /**
185 * Get the parameter providers that apply to the requested trace
186 *
187 * @param module
188 * Analysis module
189 * @param trace
190 * The trace
191 * @return The set of parameter providers that apply to a trace for this module
192 * @deprecated Use the
193 * {@link #getParameterProvidersForModule(IAnalysisModule, ITmfTrace)}
194 * method that returns a set instead.
195 */
196 @Deprecated
197 public static List<IAnalysisParameterProvider> getParameterProviders(IAnalysisModule module, ITmfTrace trace) {
198 /* Call the method that returns a set */
199 Set<IAnalysisParameterProvider> providerList = getParameterProvidersForModule(module, trace);
200 return new ArrayList<>(providerList);
201 }
202
203 /**
204 * Get the parameter providers that apply to the requested trace
205 *
206 * @param module
207 * Analysis module
208 * @param trace
209 * The trace
210 * @return The set of parameter providers that apply to a trace for this module
211 * @since 2.0
212 */
213 public static Set<IAnalysisParameterProvider> getParameterProvidersForModule(IAnalysisModule module, ITmfTrace trace) {
214 /* First, get the parameter providers from the extension point */
215 Set<IAnalysisParameterProvider> providerSet = TmfAnalysisParameterProviders.getParameterProvidersFor(module.getId());
216 /* Then add any new parameter provider coming from other sources */
217 synchronized (fParameterProviders) {
218 if (!fParameterProviders.containsKey(module.getId())) {
219 return providerSet;
220 }
221 /* We checked via containsKey, get() should not return null */
222 List<Class<? extends IAnalysisParameterProvider>> parameterProviders = checkNotNull(fParameterProviders.get(module.getId()));
223 for (Class<? extends IAnalysisParameterProvider> providerClass : parameterProviders) {
224 try {
225 IAnalysisParameterProvider provider = fParamProviderInstances.get(providerClass);
226 if (provider == null) {
227 provider = providerClass.newInstance();
228 fParamProviderInstances.put(providerClass, provider);
229 }
230 if (provider.appliesToTrace(trace)) {
231 providerSet.add(provider);
232 }
233 } catch (IllegalArgumentException | SecurityException | InstantiationException | IllegalAccessException e) {
234 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
235 }
236 }
237 }
238 return Collections.unmodifiableSet(providerSet);
239 }
240
241 /**
242 * Clear the list of modules so that next time, it is computed again from
243 * sources
244 */
245 public static synchronized void refreshModules() {
246 fAnalysisModules.clear();
247 }
248
249 /**
250 * This method should be called when new analysis modules have been created
251 * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can
252 * be executed on the module instance.
253 *
254 * @param module
255 * The newly created analysis module
256 */
257 public static synchronized void analysisModuleCreated(IAnalysisModule module) {
258 for (ITmfNewAnalysisModuleListener listener : fListeners) {
259 listener.moduleCreated(module);
260 }
261 }
262
263 }
This page took 0.037801 seconds and 5 git commands to generate.