Commit | Line | Data |
---|---|---|
c068a752 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
c068a752 GB |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.analysis; |
c068a752 GB |
14 | |
15 | import java.util.ArrayList; | |
16 | import java.util.Collections; | |
17 | import java.util.HashMap; | |
18 | import java.util.List; | |
19 | import java.util.Map; | |
20 | ||
ba27dd38 GB |
21 | import org.eclipse.jdt.annotation.NonNull; |
22 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
23 | import org.eclipse.jdt.annotation.Nullable; | |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
25 | import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisModuleSources; | |
26 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
c068a752 GB |
27 | |
28 | /** | |
29 | * Manages the available analysis helpers from different sources and their | |
30 | * parameter providers. | |
31 | * | |
c068a752 GB |
32 | * @author Geneviève Bastien |
33 | * @since 3.0 | |
34 | */ | |
ba27dd38 | 35 | @NonNullByDefault |
c068a752 GB |
36 | public class TmfAnalysisManager { |
37 | ||
a4524c1b AM |
38 | private static final Map<String, IAnalysisModuleHelper> fAnalysisModules = new HashMap<>(); |
39 | private static final Map<String, List<Class<? extends IAnalysisParameterProvider>>> fParameterProviders = new HashMap<>(); | |
40 | private static final Map<Class<? extends IAnalysisParameterProvider>, IAnalysisParameterProvider> fParamProviderInstances = new HashMap<>(); | |
b3b03da0 | 41 | private static final List<IAnalysisModuleSource> fSources = new ArrayList<>(); |
3a26292f | 42 | private static final List<ITmfNewAnalysisModuleListener> fListeners = new ArrayList<>(); |
b3b03da0 | 43 | |
7eb0d2dc GB |
44 | /** |
45 | * Constructor, not to be used | |
46 | * TODO This class is not meant to be instantiated, put me private in next major release | |
47 | * @deprecated It was never meant to be public | |
48 | */ | |
49 | @Deprecated | |
50 | public TmfAnalysisManager() { | |
51 | ||
52 | } | |
53 | ||
b3b03da0 GB |
54 | /** |
55 | * Registers a new source of modules | |
56 | * | |
57 | * @param source | |
58 | * A {@link IAnalysisModuleSource} instance | |
59 | */ | |
41ad3673 GB |
60 | public static synchronized void registerModuleSource(IAnalysisModuleSource source) { |
61 | fSources.add(source); | |
62 | refreshModules(); | |
b3b03da0 GB |
63 | } |
64 | ||
65 | /** | |
b63291e6 | 66 | * Initializes sources and new module listeners from the extension point |
b3b03da0 | 67 | */ |
41ad3673 GB |
68 | public static synchronized void initialize() { |
69 | fSources.clear(); | |
70 | fListeners.clear(); | |
b63291e6 GB |
71 | initializeModuleSources(); |
72 | initializeNewModuleListeners(); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Cleans the module sources list and initialize it from the extension point | |
77 | */ | |
41ad3673 GB |
78 | private static synchronized void initializeModuleSources() { |
79 | for (IAnalysisModuleSource source : TmfAnalysisModuleSources.getSources()) { | |
80 | fSources.add(source); | |
b3b03da0 GB |
81 | } |
82 | } | |
c068a752 | 83 | |
b63291e6 GB |
84 | /** |
85 | * Cleans the new module listeners list and initialize it from the extension | |
86 | * point | |
87 | */ | |
41ad3673 GB |
88 | private static synchronized void initializeNewModuleListeners() { |
89 | for (ITmfNewAnalysisModuleListener output : TmfAnalysisModuleOutputs.getOutputListeners()) { | |
90 | fListeners.add(output); | |
b63291e6 GB |
91 | } |
92 | } | |
93 | ||
41ad3673 GB |
94 | /** |
95 | * Add a new module listener to the list of listeners | |
96 | * | |
97 | * @param listener | |
98 | * The new module listener | |
99 | */ | |
100 | public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener) { | |
101 | fListeners.add(listener); | |
102 | } | |
103 | ||
c068a752 GB |
104 | /** |
105 | * Gets all available analysis module helpers | |
106 | * | |
107 | * This map is read-only | |
108 | * | |
109 | * @return The map of available {@link IAnalysisModuleHelper} | |
110 | */ | |
41ad3673 GB |
111 | public static synchronized Map<String, IAnalysisModuleHelper> getAnalysisModules() { |
112 | if (fAnalysisModules.isEmpty()) { | |
113 | for (IAnalysisModuleSource source : fSources) { | |
114 | for (IAnalysisModuleHelper helper : source.getAnalysisModules()) { | |
115 | fAnalysisModules.put(helper.getId(), helper); | |
b3b03da0 | 116 | } |
c068a752 GB |
117 | } |
118 | } | |
ba27dd38 GB |
119 | @SuppressWarnings("null") |
120 | @NonNull Map<String, IAnalysisModuleHelper> map = Collections.unmodifiableMap(fAnalysisModules); | |
121 | return map; | |
c068a752 GB |
122 | } |
123 | ||
124 | /** | |
125 | * Gets all analysis module helpers that apply to a given trace type | |
126 | * | |
127 | * This map is read-only | |
128 | * | |
129 | * @param traceclass | |
130 | * The trace class to get modules for | |
131 | * @return The map of available {@link IAnalysisModuleHelper} | |
132 | */ | |
133 | public static Map<String, IAnalysisModuleHelper> getAnalysisModules(Class<? extends ITmfTrace> traceclass) { | |
134 | Map<String, IAnalysisModuleHelper> allModules = getAnalysisModules(); | |
a4524c1b | 135 | Map<String, IAnalysisModuleHelper> map = new HashMap<>(); |
c068a752 GB |
136 | for (IAnalysisModuleHelper module : allModules.values()) { |
137 | if (module.appliesToTraceType(traceclass)) { | |
138 | map.put(module.getId(), module); | |
139 | } | |
140 | } | |
ba27dd38 GB |
141 | @SuppressWarnings("null") |
142 | @NonNull Map<String, IAnalysisModuleHelper> retMap = Collections.unmodifiableMap(map); | |
143 | return retMap; | |
c068a752 GB |
144 | } |
145 | ||
146 | /** | |
147 | * Gets an analysis module helper identified by an id | |
148 | * | |
149 | * @param id | |
150 | * Id of the analysis module to get | |
151 | * @return The {@link IAnalysisModuleHelper} | |
152 | */ | |
ba27dd38 | 153 | public static @Nullable IAnalysisModuleHelper getAnalysisModule(String id) { |
c068a752 GB |
154 | Map<String, IAnalysisModuleHelper> map = getAnalysisModules(); |
155 | return map.get(id); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Register a new parameter provider for an analysis | |
160 | * | |
161 | * @param analysisId | |
162 | * The id of the analysis | |
163 | * @param paramProvider | |
164 | * The class of the parameter provider | |
165 | */ | |
166 | public static void registerParameterProvider(String analysisId, Class<? extends IAnalysisParameterProvider> paramProvider) { | |
167 | synchronized (fParameterProviders) { | |
168 | if (!fParameterProviders.containsKey(analysisId)) { | |
169 | fParameterProviders.put(analysisId, new ArrayList<Class<? extends IAnalysisParameterProvider>>()); | |
170 | } | |
171 | fParameterProviders.get(analysisId).add(paramProvider); | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * Get a parameter provider that applies to the requested trace | |
177 | * | |
178 | * @param module | |
179 | * Analysis module | |
180 | * @param trace | |
181 | * The trace | |
182 | * @return A parameter provider if one applies to the trace, null otherwise | |
183 | */ | |
184 | public static List<IAnalysisParameterProvider> getParameterProviders(IAnalysisModule module, ITmfTrace trace) { | |
a4524c1b | 185 | List<IAnalysisParameterProvider> providerList = new ArrayList<>(); |
c068a752 GB |
186 | synchronized (fParameterProviders) { |
187 | if (!fParameterProviders.containsKey(module.getId())) { | |
188 | return providerList; | |
189 | } | |
190 | for (Class<? extends IAnalysisParameterProvider> providerClass : fParameterProviders.get(module.getId())) { | |
191 | try { | |
192 | IAnalysisParameterProvider provider = fParamProviderInstances.get(providerClass); | |
193 | if (provider == null) { | |
194 | provider = providerClass.newInstance(); | |
195 | fParamProviderInstances.put(providerClass, provider); | |
196 | } | |
197 | if (provider != null) { | |
198 | if (provider.appliesToTrace(trace)) { | |
199 | providerList.add(provider); | |
200 | } | |
201 | } | |
202 | } catch (IllegalArgumentException e) { | |
203 | Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e); | |
204 | } catch (SecurityException e) { | |
205 | Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e); | |
206 | } catch (InstantiationException e) { | |
207 | Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e); | |
208 | } catch (IllegalAccessException e) { | |
209 | Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e); | |
210 | } | |
211 | } | |
212 | } | |
213 | return providerList; | |
214 | } | |
215 | ||
b3b03da0 GB |
216 | /** |
217 | * Clear the list of modules so that next time, it is computed again from | |
218 | * sources | |
219 | */ | |
41ad3673 GB |
220 | public static synchronized void refreshModules() { |
221 | fAnalysisModules.clear(); | |
b3b03da0 GB |
222 | } |
223 | ||
3a26292f GB |
224 | /** |
225 | * This method should be called when new analysis modules have been created | |
226 | * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can | |
227 | * be executed on the module instance. | |
228 | * | |
229 | * @param module | |
230 | * The newly created analysis module | |
231 | */ | |
41ad3673 GB |
232 | public static synchronized void analysisModuleCreated(IAnalysisModule module) { |
233 | for (ITmfNewAnalysisModuleListener listener : fListeners) { | |
234 | listener.moduleCreated(module); | |
3a26292f GB |
235 | } |
236 | } | |
237 | ||
c068a752 | 238 | } |