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.linuxtools.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.linuxtools.internal.tmf.core.Activator;
26
import org.eclipse.linuxtools.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement;
27
import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
28
import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
29
import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
30
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31
import org.eclipse.osgi.util.NLS;
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
return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ID_ATTR);
65
public String getName() {
66
return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
70
public boolean isAutomatic() {
71
return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.AUTOMATIC_ATTR));
75
public String getHelpText() {
77
* FIXME: No need to externalize this. A better solution will be found
78
* soon and this string is just temporary
80
return new String("The trace must be opened to get the help message"); //$NON-NLS-1$
84
public String getIcon() {
85
return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ICON_ATTR);
89
public Bundle getBundle() {
90
return ContributorFactoryOSGi.resolve(fCe.getContributor());
94
public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) {
95
boolean applies = false;
97
/* Get the module's applying tracetypes */
98
final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.TRACETYPE_ELEM);
99
for (IConfigurationElement element : tracetypeCE) {
102
applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisModuleSourceConfigElement.CLASS_ATTR));
103
String classAppliesVal = element.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_ATTR);
104
boolean classApplies = true;
105
if (classAppliesVal != null) {
106
classApplies = Boolean.parseBoolean(classAppliesVal);
109
applies |= applyclass.isAssignableFrom(traceclass);
111
/* If the trace type does not apply, reset the applies variable to false */
112
if (applyclass.isAssignableFrom(traceclass)) {
116
} catch (ClassNotFoundException e) {
117
Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
118
} catch (InvalidRegistryObjectException e) {
119
Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
126
public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
127
Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>();
129
for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) {
130
if (appliesToTraceType(tth.getTraceClass())) {
131
traceTypes.add(tth.getTraceClass());
139
public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
140
IAnalysisModule module = createModule();
141
if (module != null) {
142
return module.getAnalysisRequirements();
144
return Collections.EMPTY_SET;
148
// ---------------------------------------
150
// ---------------------------------------
152
private IAnalysisModule createModule() {
153
IAnalysisModule module = null;
155
module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisModuleSourceConfigElement.ANALYSIS_MODULE_ATTR);
156
module.setName(getName());
157
module.setId(getId());
158
} catch (CoreException e) {
159
Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$
165
public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
167
/* Check that analysis can be executed */
168
if (!appliesToTraceType(trace.getClass())) {
169
throw new TmfAnalysisException(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, getName()));
172
IAnalysisModule module = createModule();
173
if (module == null) {
177
module.setAutomatic(isAutomatic());
179
/* Get the module's parameters */
180
final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.PARAMETER_ELEM);
181
for (IConfigurationElement element : parametersCE) {
182
module.addParameter(element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR));
183
String defaultValue = element.getAttribute(TmfAnalysisModuleSourceConfigElement.DEFAULT_VALUE_ATTR);
184
if (defaultValue != null) {
185
module.setParameter(element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR), defaultValue);
188
module.setTrace(trace);
189
TmfAnalysisManager.analysisModuleCreated(module);
196
public String getHelpText(@NonNull ITmfTrace trace) {
197
IAnalysisModule module = createModule();
198
if (module != null) {
199
String ret = module.getHelpText(trace);
203
return getHelpText();