| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 É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 | |
| 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; |
| 22 | import org.eclipse.linuxtools.internal.tmf.core.analysis.TmfAnalysisModuleSources; |
| 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 | * |
| 29 | * @author Geneviève Bastien |
| 30 | * @since 3.0 |
| 31 | */ |
| 32 | public class TmfAnalysisManager { |
| 33 | |
| 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<>(); |
| 39 | |
| 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 | |
| 50 | /** |
| 51 | * Registers a new source of modules |
| 52 | * |
| 53 | * @param source |
| 54 | * A {@link IAnalysisModuleSource} instance |
| 55 | */ |
| 56 | public static synchronized void registerModuleSource(IAnalysisModuleSource source) { |
| 57 | fSources.add(source); |
| 58 | refreshModules(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Initializes sources and new module listeners from the extension point |
| 63 | */ |
| 64 | public static synchronized void initialize() { |
| 65 | fSources.clear(); |
| 66 | fListeners.clear(); |
| 67 | initializeModuleSources(); |
| 68 | initializeNewModuleListeners(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Cleans the module sources list and initialize it from the extension point |
| 73 | */ |
| 74 | private static synchronized void initializeModuleSources() { |
| 75 | for (IAnalysisModuleSource source : TmfAnalysisModuleSources.getSources()) { |
| 76 | fSources.add(source); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Cleans the new module listeners list and initialize it from the extension |
| 82 | * point |
| 83 | */ |
| 84 | private static synchronized void initializeNewModuleListeners() { |
| 85 | for (ITmfNewAnalysisModuleListener output : TmfAnalysisModuleOutputs.getOutputListeners()) { |
| 86 | fListeners.add(output); |
| 87 | } |
| 88 | } |
| 89 | |
| 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 | |
| 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 | */ |
| 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); |
| 112 | } |
| 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(); |
| 129 | Map<String, IAnalysisModuleHelper> map = new HashMap<>(); |
| 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) { |
| 177 | List<IAnalysisParameterProvider> providerList = new ArrayList<>(); |
| 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 | |
| 208 | /** |
| 209 | * Clear the list of modules so that next time, it is computed again from |
| 210 | * sources |
| 211 | */ |
| 212 | public static synchronized void refreshModules() { |
| 213 | fAnalysisModules.clear(); |
| 214 | } |
| 215 | |
| 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 | */ |
| 224 | public static synchronized void analysisModuleCreated(IAnalysisModule module) { |
| 225 | for (ITmfNewAnalysisModuleListener listener : fListeners) { |
| 226 | listener.moduleCreated(module); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | } |