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