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