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