1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.analysis
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.analysis
.TmfAnalysisModuleSources
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
26 * Manages the available analysis helpers from different sources and their
27 * parameter providers.
29 * @author Geneviève Bastien
32 public class TmfAnalysisManager
{
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
<>();
37 private static final List
<IAnalysisModuleSource
> fSources
= new ArrayList
<>();
38 private static final List
<ITmfNewAnalysisModuleListener
> fListeners
= new ArrayList
<>();
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
46 public TmfAnalysisManager() {
51 * Registers a new source of modules
54 * A {@link IAnalysisModuleSource} instance
56 public static synchronized void registerModuleSource(IAnalysisModuleSource source
) {
62 * Initializes sources and new module listeners from the extension point
64 public static synchronized void initialize() {
67 initializeModuleSources();
68 initializeNewModuleListeners();
72 * Cleans the module sources list and initialize it from the extension point
74 private static synchronized void initializeModuleSources() {
75 for (IAnalysisModuleSource source
: TmfAnalysisModuleSources
.getSources()) {
81 * Cleans the new module listeners list and initialize it from the extension
84 private static synchronized void initializeNewModuleListeners() {
85 for (ITmfNewAnalysisModuleListener output
: TmfAnalysisModuleOutputs
.getOutputListeners()) {
86 fListeners
.add(output
);
91 * Add a new module listener to the list of listeners
94 * The new module listener
96 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener
) {
97 fListeners
.add(listener
);
101 * Gets all available analysis module helpers
103 * This map is read-only
105 * @return The map of available {@link IAnalysisModuleHelper}
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
);
115 return Collections
.unmodifiableMap(fAnalysisModules
);
119 * Gets all analysis module helpers that apply to a given trace type
121 * This map is read-only
124 * The trace class to get modules for
125 * @return The map of available {@link IAnalysisModuleHelper}
127 public static Map
<String
, IAnalysisModuleHelper
> getAnalysisModules(Class
<?
extends ITmfTrace
> traceclass
) {
128 Map
<String
, IAnalysisModuleHelper
> allModules
= getAnalysisModules();
129 Map
<String
, IAnalysisModuleHelper
> map
= new HashMap
<>();
130 for (IAnalysisModuleHelper module
: allModules
.values()) {
131 if (module
.appliesToTraceType(traceclass
)) {
132 map
.put(module
.getId(), module
);
135 return Collections
.unmodifiableMap(map
);
139 * Gets an analysis module helper identified by an id
142 * Id of the analysis module to get
143 * @return The {@link IAnalysisModuleHelper}
145 public static IAnalysisModuleHelper
getAnalysisModule(String id
) {
146 Map
<String
, IAnalysisModuleHelper
> map
= getAnalysisModules();
151 * Register a new parameter provider for an analysis
154 * The id of the analysis
155 * @param paramProvider
156 * The class of the parameter provider
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
>>());
163 fParameterProviders
.get(analysisId
).add(paramProvider
);
168 * Get a parameter provider that applies to the requested trace
174 * @return A parameter provider if one applies to the trace, null otherwise
176 public static List
<IAnalysisParameterProvider
> getParameterProviders(IAnalysisModule module
, ITmfTrace trace
) {
177 List
<IAnalysisParameterProvider
> providerList
= new ArrayList
<>();
178 synchronized (fParameterProviders
) {
179 if (!fParameterProviders
.containsKey(module
.getId())) {
182 for (Class
<?
extends IAnalysisParameterProvider
> providerClass
: fParameterProviders
.get(module
.getId())) {
184 IAnalysisParameterProvider provider
= fParamProviderInstances
.get(providerClass
);
185 if (provider
== null) {
186 provider
= providerClass
.newInstance();
187 fParamProviderInstances
.put(providerClass
, provider
);
189 if (provider
!= null) {
190 if (provider
.appliesToTrace(trace
)) {
191 providerList
.add(provider
);
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
);
209 * Clear the list of modules so that next time, it is computed again from
212 public static synchronized void refreshModules() {
213 fAnalysisModules
.clear();
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.
222 * The newly created analysis module
224 public static synchronized void analysisModuleCreated(IAnalysisModule module
) {
225 for (ITmfNewAnalysisModuleListener listener
: fListeners
) {
226 listener
.moduleCreated(module
);