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
.osgi
.framework
.Bundle
;
35 * Analysis module helper for modules provided by a plugin's configuration
38 * @author Geneviève Bastien
41 public class TmfAnalysisModuleHelperConfigElement
implements IAnalysisModuleHelper
{
43 private final IConfigurationElement fCe
;
49 * The source {@link IConfigurationElement} of this module helper
51 public TmfAnalysisModuleHelperConfigElement(IConfigurationElement ce
) {
55 // ----------------------------------------
56 // Wrappers to {@link IAnalysisModule} methods
57 // ----------------------------------------
60 public String
getId() {
61 String id
= fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.ID_ATTR
);
63 throw new IllegalStateException();
69 public String
getName() {
70 String name
= fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.NAME_ATTR
);
72 throw new IllegalStateException();
78 public boolean isAutomatic() {
79 return Boolean
.parseBoolean(fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.AUTOMATIC_ATTR
));
83 public String
getHelpText() {
85 * FIXME: No need to externalize this. A better solution will be found
86 * soon and this string is just temporary
88 return new String("The trace must be opened to get the help message"); //$NON-NLS-1$
92 public String
getIcon() {
93 return fCe
.getAttribute(TmfAnalysisModuleSourceConfigElement
.ICON_ATTR
);
97 public Bundle
getBundle() {
98 return ContributorFactoryOSGi
.resolve(fCe
.getContributor());
102 public boolean appliesToTraceType(Class
<?
extends ITmfTrace
> traceclass
) {
103 boolean applies
= false;
105 /* Get the module's applying tracetypes */
106 final IConfigurationElement
[] tracetypeCE
= fCe
.getChildren(TmfAnalysisModuleSourceConfigElement
.TRACETYPE_ELEM
);
107 for (IConfigurationElement element
: tracetypeCE
) {
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
);
117 applies
|= applyclass
.isAssignableFrom(traceclass
);
119 /* If the trace type does not apply, reset the applies variable to false */
120 if (applyclass
.isAssignableFrom(traceclass
)) {
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$
134 public Iterable
<Class
<?
extends ITmfTrace
>> getValidTraceTypes() {
135 Set
<Class
<?
extends ITmfTrace
>> traceTypes
= new HashSet
<>();
137 for (TraceTypeHelper tth
: TmfTraceType
.getTraceTypeHelpers()) {
138 if (appliesToTraceType(tth
.getTraceClass())) {
139 traceTypes
.add(tth
.getTraceClass());
147 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
148 IAnalysisModule module
= createModule();
149 if (module
!= null) {
150 return module
.getAnalysisRequirements();
152 return Collections
.EMPTY_SET
;
156 // ---------------------------------------
158 // ---------------------------------------
160 private IAnalysisModule
createModule() {
161 IAnalysisModule module
= null;
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$
173 public IAnalysisModule
newModule(ITmfTrace trace
) throws TmfAnalysisException
{
175 /* Check that analysis can be executed */
176 if (!appliesToTraceType(trace
.getClass())) {
177 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAnalysisModuleHelper_AnalysisDoesNotApply
, getName()));
180 IAnalysisModule module
= createModule();
181 if (module
== null) {
185 module
.setAutomatic(isAutomatic());
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) {
194 module
.addParameter(paramName
);
195 String defaultValue
= element
.getAttribute(TmfAnalysisModuleSourceConfigElement
.DEFAULT_VALUE_ATTR
);
196 if (defaultValue
!= null) {
197 module
.setParameter(paramName
, defaultValue
);
200 module
.setTrace(trace
);
201 TmfAnalysisManager
.analysisModuleCreated(module
);
208 public String
getHelpText(@NonNull ITmfTrace trace
) {
209 IAnalysisModule module
= createModule();
210 if (module
!= null) {
211 String ret
= module
.getHelpText(trace
);
215 return getHelpText();