TMF: Add new analysis module listener called when module are being instantiated
[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 /**
57 * Cleans the module source list and initialize it from the extension point
58 */
59 public static void initializeModuleSources() {
60 synchronized (fSources) {
61 fSources.clear();
62 for (IAnalysisModuleSource source : TmfAnalysisModuleSources.getSources()) {
63 fSources.add(source);
64 }
65 }
66 }
c068a752
GB
67
68 /**
69 * Gets all available analysis module helpers
70 *
71 * This map is read-only
72 *
73 * @return The map of available {@link IAnalysisModuleHelper}
74 */
75 public static Map<String, IAnalysisModuleHelper> getAnalysisModules() {
76 synchronized (fAnalysisModules) {
77 if (fAnalysisModules.isEmpty()) {
b3b03da0
GB
78 for (IAnalysisModuleSource source : fSources) {
79 for (IAnalysisModuleHelper helper : source.getAnalysisModules()) {
80 fAnalysisModules.put(helper.getId(), helper);
81 }
82 }
c068a752
GB
83 }
84 }
85 return Collections.unmodifiableMap(fAnalysisModules);
86 }
87
88 /**
89 * Gets all analysis module helpers that apply to a given trace type
90 *
91 * This map is read-only
92 *
93 * @param traceclass
94 * The trace class to get modules for
95 * @return The map of available {@link IAnalysisModuleHelper}
96 */
97 public static Map<String, IAnalysisModuleHelper> getAnalysisModules(Class<? extends ITmfTrace> traceclass) {
98 Map<String, IAnalysisModuleHelper> allModules = getAnalysisModules();
a4524c1b 99 Map<String, IAnalysisModuleHelper> map = new HashMap<>();
c068a752
GB
100 for (IAnalysisModuleHelper module : allModules.values()) {
101 if (module.appliesToTraceType(traceclass)) {
102 map.put(module.getId(), module);
103 }
104 }
105 return Collections.unmodifiableMap(map);
106 }
107
108 /**
109 * Gets an analysis module helper identified by an id
110 *
111 * @param id
112 * Id of the analysis module to get
113 * @return The {@link IAnalysisModuleHelper}
114 */
115 public static IAnalysisModuleHelper getAnalysisModule(String id) {
116 Map<String, IAnalysisModuleHelper> map = getAnalysisModules();
117 return map.get(id);
118 }
119
120 /**
121 * Register a new parameter provider for an analysis
122 *
123 * @param analysisId
124 * The id of the analysis
125 * @param paramProvider
126 * The class of the parameter provider
127 */
128 public static void registerParameterProvider(String analysisId, Class<? extends IAnalysisParameterProvider> paramProvider) {
129 synchronized (fParameterProviders) {
130 if (!fParameterProviders.containsKey(analysisId)) {
131 fParameterProviders.put(analysisId, new ArrayList<Class<? extends IAnalysisParameterProvider>>());
132 }
133 fParameterProviders.get(analysisId).add(paramProvider);
134 }
135 }
136
137 /**
138 * Get a parameter provider that applies to the requested trace
139 *
140 * @param module
141 * Analysis module
142 * @param trace
143 * The trace
144 * @return A parameter provider if one applies to the trace, null otherwise
145 */
146 public static List<IAnalysisParameterProvider> getParameterProviders(IAnalysisModule module, ITmfTrace trace) {
a4524c1b 147 List<IAnalysisParameterProvider> providerList = new ArrayList<>();
c068a752
GB
148 synchronized (fParameterProviders) {
149 if (!fParameterProviders.containsKey(module.getId())) {
150 return providerList;
151 }
152 for (Class<? extends IAnalysisParameterProvider> providerClass : fParameterProviders.get(module.getId())) {
153 try {
154 IAnalysisParameterProvider provider = fParamProviderInstances.get(providerClass);
155 if (provider == null) {
156 provider = providerClass.newInstance();
157 fParamProviderInstances.put(providerClass, provider);
158 }
159 if (provider != null) {
160 if (provider.appliesToTrace(trace)) {
161 providerList.add(provider);
162 }
163 }
164 } catch (IllegalArgumentException e) {
165 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
166 } catch (SecurityException e) {
167 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
168 } catch (InstantiationException e) {
169 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
170 } catch (IllegalAccessException e) {
171 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
172 }
173 }
174 }
175 return providerList;
176 }
177
b3b03da0
GB
178 /**
179 * Clear the list of modules so that next time, it is computed again from
180 * sources
181 */
182 public static void refreshModules() {
183 synchronized (fAnalysisModules) {
184 fAnalysisModules.clear();
185 }
186 }
187
3a26292f
GB
188 /**
189 * This method should be called when new analysis modules have been created
190 * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can
191 * be executed on the module instance.
192 *
193 * @param module
194 * The newly created analysis module
195 */
196 public static void analysisModuleCreated(IAnalysisModule module) {
197 synchronized (fListeners) {
198 for (ITmfNewAnalysisModuleListener listener : fListeners) {
199 listener.moduleCreated(module);
200 }
201 }
202 }
203
c068a752 204}
This page took 0.059988 seconds and 5 git commands to generate.