9a710a5bff3aa8c959897c76451360b535771aac
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / TmfAnalysisModuleHelperConfigElement.java
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 * Mathieu Rail - Added functionality for getting a module's requirements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.analysis;
15
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.eclipse.core.runtime.ContributorFactoryOSGi;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.InvalidRegistryObjectException;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.tracecompass.internal.tmf.core.Activator;
26 import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement;
27 import org.eclipse.tracecompass.tmf.core.analysis.requirements.TmfAbstractAnalysisRequirement;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
29 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
30 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
33 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
34 import org.osgi.framework.Bundle;
35
36 /**
37 * Analysis module helper for modules provided by a plugin's configuration
38 * elements.
39 *
40 * @author Geneviève Bastien
41 */
42 public class TmfAnalysisModuleHelperConfigElement implements IAnalysisModuleHelper {
43
44 private final IConfigurationElement fCe;
45
46 /**
47 * Constructor
48 *
49 * @param ce
50 * The source {@link IConfigurationElement} of this module helper
51 */
52 public TmfAnalysisModuleHelperConfigElement(IConfigurationElement ce) {
53 fCe = ce;
54 }
55
56 // ----------------------------------------
57 // Wrappers to {@link IAnalysisModule} methods
58 // ----------------------------------------
59
60 @Override
61 public String getId() {
62 String id = fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ID_ATTR);
63 if (id == null) {
64 throw new IllegalStateException();
65 }
66 return id;
67 }
68
69 @Override
70 public String getName() {
71 String name = fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
72 if (name == null) {
73 throw new IllegalStateException();
74 }
75 return name;
76 }
77
78 @Override
79 public boolean isAutomatic() {
80 return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.AUTOMATIC_ATTR));
81 }
82
83 /**
84 * @since 1.0
85 */
86 @Override
87 public boolean appliesToExperiment() {
88 return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_EXP_ATTR));
89 }
90
91 @Override
92 public String getHelpText() {
93 /*
94 * FIXME: No need to externalize this. A better solution will be found
95 * soon and this string is just temporary
96 */
97 return "The trace must be opened to get the help message"; //$NON-NLS-1$
98 }
99
100 @Override
101 public String getIcon() {
102 return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ICON_ATTR);
103 }
104
105 @Override
106 public Bundle getBundle() {
107 return ContributorFactoryOSGi.resolve(fCe.getContributor());
108 }
109
110 @Override
111 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) {
112 boolean applies = false;
113
114 /* Get the module's applying tracetypes */
115 final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.TRACETYPE_ELEM);
116 for (IConfigurationElement element : tracetypeCE) {
117 Class<?> applyclass;
118 try {
119 applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisModuleSourceConfigElement.CLASS_ATTR));
120 String classAppliesVal = element.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_ATTR);
121 boolean classApplies = true;
122 if (classAppliesVal != null) {
123 classApplies = Boolean.parseBoolean(classAppliesVal);
124 }
125 if (classApplies) {
126 applies |= applyclass.isAssignableFrom(traceclass);
127 } else {
128 /*
129 * If the trace type does not apply, reset the applies
130 * variable to false
131 */
132 if (applyclass.isAssignableFrom(traceclass)) {
133 applies = false;
134 }
135 }
136 } catch (ClassNotFoundException | InvalidRegistryObjectException e) {
137 Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
138 }
139 }
140 return applies;
141 }
142
143 @Override
144 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
145 Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>();
146
147 for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) {
148 if (appliesToTraceType(tth.getTraceClass())) {
149 traceTypes.add(tth.getTraceClass());
150 }
151 }
152
153 return traceTypes;
154 }
155
156 @Override
157 public Iterable<TmfAbstractAnalysisRequirement> getAnalysisRequirements() {
158 IAnalysisModule module = createModule();
159 if (module != null) {
160 return module.getAnalysisRequirements();
161 }
162 return Collections.EMPTY_SET;
163
164 }
165
166 // ---------------------------------------
167 // Functionalities
168 // ---------------------------------------
169
170 private IAnalysisModule createModule() {
171 IAnalysisModule module = null;
172 try {
173 module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisModuleSourceConfigElement.ANALYSIS_MODULE_ATTR);
174 module.setName(getName());
175 module.setId(getId());
176 } catch (CoreException e) {
177 Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$
178 }
179 return module;
180 }
181
182 @Override
183 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
184
185 /* Check if it applies to trace itself */
186 boolean applies = appliesToTraceType(trace.getClass());
187 /*
188 * If the trace is an experiment, check if this module would apply to an
189 * experiment should it apply to one of its traces.
190 */
191 if (!applies && (trace instanceof TmfExperiment) && appliesToExperiment()) {
192 for (ITmfTrace expTrace : TmfTraceManager.getTraceSet(trace)) {
193 if (appliesToTraceType(expTrace.getClass())) {
194 applies = true;
195 break;
196 }
197 }
198 }
199
200 if (!applies) {
201 return null;
202 }
203
204 IAnalysisModule module = createModule();
205 if (module == null) {
206 return null;
207 }
208
209 module.setAutomatic(isAutomatic());
210
211 /* Get the module's parameters */
212 final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.PARAMETER_ELEM);
213 for (IConfigurationElement element : parametersCE) {
214 String paramName = element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
215 if (paramName == null) {
216 continue;
217 }
218 module.addParameter(paramName);
219 String defaultValue = element.getAttribute(TmfAnalysisModuleSourceConfigElement.DEFAULT_VALUE_ATTR);
220 if (defaultValue != null) {
221 module.setParameter(paramName, defaultValue);
222 }
223 }
224 if (module.setTrace(trace)) {
225 TmfAnalysisManager.analysisModuleCreated(module);
226 } else {
227 module.dispose();
228 module = null;
229 }
230
231 return module;
232
233 }
234
235 @Override
236 public String getHelpText(@NonNull ITmfTrace trace) {
237 IAnalysisModule module = createModule();
238 if (module != null) {
239 String ret = module.getHelpText(trace);
240 module.dispose();
241 return ret;
242 }
243 return getHelpText();
244
245 }
246 }
This page took 0.043992 seconds and 4 git commands to generate.