tmf: Make TmfTraceType static
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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
14package org.eclipse.linuxtools.tmf.core.analysis;
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;
c068a752 25import org.eclipse.linuxtools.internal.tmf.core.Activator;
b3b03da0 26import org.eclipse.linuxtools.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement;
c068a752 27import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
8c736b3c
MR
28import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
29import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
c068a752
GB
30import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31import org.eclipse.osgi.util.NLS;
32import 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
c4767854 39 * @since 3.0
c068a752 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() {
b3b03da0 61 return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ID_ATTR);
c068a752
GB
62 }
63
64 @Override
65 public String getName() {
b3b03da0 66 return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
c068a752
GB
67 }
68
69 @Override
70 public boolean isAutomatic() {
0126a8ca 71 return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.AUTOMATIC_ATTR));
c068a752
GB
72 }
73
74 @Override
75 public String getHelpText() {
4bc53929
GB
76 /*
77 * FIXME: No need to externalize this. A better solution will be found
78 * soon and this string is just temporary
79 */
80 return new String("The trace must be opened to get the help message"); //$NON-NLS-1$
c068a752
GB
81 }
82
83 @Override
84 public String getIcon() {
b3b03da0 85 return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ICON_ATTR);
c068a752
GB
86 }
87
88 @Override
89 public Bundle getBundle() {
90 return ContributorFactoryOSGi.resolve(fCe.getContributor());
91 }
92
93 @Override
94 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) {
95 boolean applies = false;
96
97 /* Get the module's applying tracetypes */
b3b03da0 98 final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.TRACETYPE_ELEM);
c068a752
GB
99 for (IConfigurationElement element : tracetypeCE) {
100 Class<?> applyclass;
101 try {
b3b03da0
GB
102 applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisModuleSourceConfigElement.CLASS_ATTR));
103 String classAppliesVal = element.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_ATTR);
c068a752
GB
104 boolean classApplies = true;
105 if (classAppliesVal != null) {
0126a8ca 106 classApplies = Boolean.parseBoolean(classAppliesVal);
c068a752
GB
107 }
108 if (classApplies) {
109 applies = applyclass.isAssignableFrom(traceclass);
110 } else {
111 applies = !applyclass.isAssignableFrom(traceclass);
112 }
113 } catch (ClassNotFoundException e) {
114 Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
115 } catch (InvalidRegistryObjectException e) {
116 Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
117 }
118 }
119 return applies;
120 }
121
63c43609
MR
122 @Override
123 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
8c736b3c
MR
124 Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>();
125
a4a116c3 126 for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) {
8c736b3c
MR
127 if (appliesToTraceType(tth.getTraceClass())) {
128 traceTypes.add(tth.getTraceClass());
129 }
130 }
131
132 return traceTypes;
63c43609
MR
133 }
134
135 @Override
136 public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
54eae41f
GB
137 IAnalysisModule module = createModule();
138 if (module != null) {
139 return module.getAnalysisRequirements();
8c736b3c 140 }
63c43609
MR
141 return Collections.EMPTY_SET;
142 }
143
c068a752
GB
144 // ---------------------------------------
145 // Functionalities
146 // ---------------------------------------
147
54eae41f
GB
148 private IAnalysisModule createModule() {
149 IAnalysisModule module = null;
150 try {
151 module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisModuleSourceConfigElement.ANALYSIS_MODULE_ATTR);
152 module.setName(getName());
153 module.setId(getId());
154 } catch (CoreException e) {
155 Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$
156 }
157 return module;
158 }
159
c068a752
GB
160 @Override
161 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
162
163 /* Check that analysis can be executed */
164 if (!appliesToTraceType(trace.getClass())) {
165 throw new TmfAnalysisException(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, getName()));
166 }
167
54eae41f
GB
168 IAnalysisModule module = createModule();
169 if (module == null) {
170 return null;
171 }
172
173 module.setAutomatic(isAutomatic());
174
175 /* Get the module's parameters */
176 final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.PARAMETER_ELEM);
177 for (IConfigurationElement element : parametersCE) {
178 module.addParameter(element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR));
179 String defaultValue = element.getAttribute(TmfAnalysisModuleSourceConfigElement.DEFAULT_VALUE_ATTR);
180 if (defaultValue != null) {
181 module.setParameter(element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR), defaultValue);
c068a752 182 }
c068a752 183 }
54eae41f
GB
184 module.setTrace(trace);
185 TmfAnalysisManager.analysisModuleCreated(module);
186
c068a752
GB
187 return module;
188
189 }
54eae41f
GB
190
191 @Override
192 public String getHelpText(@NonNull ITmfTrace trace) {
193 IAnalysisModule module = createModule();
194 if (module != null) {
195 String ret = module.getHelpText(trace);
196 module.dispose();
197 return ret;
198 }
199 return getHelpText();
200 }
c068a752 201}
This page took 0.041198 seconds and 5 git commands to generate.