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 * Mathieu Rail - Added functionality for getting a module's requirements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.analysis
;
16 import java
.util
.Collections
;
17 import java
.util
.HashSet
;
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
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
34 import org
.osgi
.framework
.Bundle
;
37 * Analysis module helper for modules provided by a plugin's configuration
40 * @author Geneviève Bastien
42 public class TmfAnalysisModuleHelperConfigElement
implements IAnalysisModuleHelper
{
44 private final IConfigurationElement fCe
;
50 * The source {@link IConfigurationElement} of this module helper
52 public TmfAnalysisModuleHelperConfigElement(IConfigurationElement ce
) {
56 // ----------------------------------------
57 // Wrappers to {@link IAnalysisModule} methods
58 // ----------------------------------------
61 public String
getId() {
62 String id
= fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.ID_ATTR
);
64 throw new IllegalStateException();
70 public String
getName() {
71 String name
= fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.NAME_ATTR
);
73 throw new IllegalStateException();
79 public boolean isAutomatic() {
80 return Boolean
.parseBoolean(fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.AUTOMATIC_ATTR
));
87 public boolean appliesToExperiment() {
88 return Boolean
.parseBoolean(fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.APPLIES_EXP_ATTR
));
92 public String
getHelpText() {
94 * FIXME: No need to externalize this. A better solution will be found
95 * soon and this string is just temporary
97 return new String("The trace must be opened to get the help message"); //$NON-NLS-1$
101 public String
getIcon() {
102 return fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.ICON_ATTR
);
106 public Bundle
getBundle() {
107 return ContributorFactoryOSGi
.resolve(fCe
.getContributor());
111 public boolean appliesToTraceType(Class
<?
extends ITmfTrace
> traceclass
) {
112 boolean applies
= false;
114 /* Get the module's applying tracetypes */
115 final IConfigurationElement
[] tracetypeCE
= fCe
.getChildren(TmfAnalysisModuleSourceConfigElement
.TRACETYPE_ELEM
);
116 for (IConfigurationElement element
: tracetypeCE
) {
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
);
126 applies
|= applyclass
.isAssignableFrom(traceclass
);
129 * If the trace type does not apply, reset the applies
132 if (applyclass
.isAssignableFrom(traceclass
)) {
136 } catch (ClassNotFoundException
| InvalidRegistryObjectException e
) {
137 Activator
.logError("Error in applies to trace", e
); //$NON-NLS-1$
144 public Iterable
<Class
<?
extends ITmfTrace
>> getValidTraceTypes() {
145 Set
<Class
<?
extends ITmfTrace
>> traceTypes
= new HashSet
<>();
147 for (TraceTypeHelper tth
: TmfTraceType
.getTraceTypeHelpers()) {
148 if (appliesToTraceType(tth
.getTraceClass())) {
149 traceTypes
.add(tth
.getTraceClass());
157 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
158 IAnalysisModule module
= createModule();
159 if (module
!= null) {
160 return module
.getAnalysisRequirements();
162 return Collections
.EMPTY_SET
;
166 // ---------------------------------------
168 // ---------------------------------------
170 private IAnalysisModule
createModule() {
171 IAnalysisModule module
= null;
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$
183 public IAnalysisModule
newModule(ITmfTrace trace
) throws TmfAnalysisException
{
185 /* Check if it applies to trace itself */
186 boolean applies
= appliesToTraceType(trace
.getClass());
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.
191 if (!applies
&& (trace
instanceof TmfExperiment
) && appliesToExperiment()) {
192 for (ITmfTrace expTrace
: TmfTraceManager
.getTraceSet(trace
)) {
193 if (appliesToTraceType(expTrace
.getClass())) {
200 /* Check that analysis can be executed */
202 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAnalysisModuleHelper_AnalysisDoesNotApply
, getName()));
205 IAnalysisModule module
= createModule();
206 if (module
== null) {
210 module
.setAutomatic(isAutomatic());
212 /* Get the module's parameters */
213 final IConfigurationElement
[] parametersCE
= fCe
.getChildren(TmfAnalysisModuleSourceConfigElement
.PARAMETER_ELEM
);
214 for (IConfigurationElement element
: parametersCE
) {
215 String paramName
= element
.getAttribute(TmfAnalysisModuleSourceConfigElement
.NAME_ATTR
);
216 if (paramName
== null) {
219 module
.addParameter(paramName
);
220 String defaultValue
= element
.getAttribute(TmfAnalysisModuleSourceConfigElement
.DEFAULT_VALUE_ATTR
);
221 if (defaultValue
!= null) {
222 module
.setParameter(paramName
, defaultValue
);
225 module
.setTrace(trace
);
226 TmfAnalysisManager
.analysisModuleCreated(module
);
233 public String
getHelpText(@NonNull ITmfTrace trace
) {
234 IAnalysisModule module
= createModule();
235 if (module
!= null) {
236 String ret
= module
.getHelpText(trace
);
240 return getHelpText();