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