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