Commit | Line | Data |
---|---|---|
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 | ||
14 | package org.eclipse.linuxtools.tmf.core.analysis; | |
15 | ||
63c43609 | 16 | import java.util.Collections; |
8c736b3c MR |
17 | import java.util.HashSet; |
18 | import java.util.Set; | |
63c43609 | 19 | |
c068a752 GB |
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; | |
54eae41f | 24 | import org.eclipse.jdt.annotation.NonNull; |
c068a752 | 25 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
b3b03da0 | 26 | import org.eclipse.linuxtools.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement; |
c068a752 | 27 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; |
8c736b3c MR |
28 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
29 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
c068a752 GB |
30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
31 | import org.eclipse.osgi.util.NLS; | |
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 | |
c4767854 | 39 | * @since 3.0 |
c068a752 | 40 | */ |
b3b03da0 | 41 | public 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) { | |
04da5385 | 109 | applies |= applyclass.isAssignableFrom(traceclass); |
c068a752 | 110 | } else { |
04da5385 GB |
111 | /* If the trace type does not apply, reset the applies variable to false */ |
112 | if (applyclass.isAssignableFrom(traceclass)) { | |
113 | applies = false; | |
114 | } | |
c068a752 GB |
115 | } |
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$ | |
120 | } | |
121 | } | |
122 | return applies; | |
123 | } | |
124 | ||
63c43609 MR |
125 | @Override |
126 | public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() { | |
8c736b3c MR |
127 | Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>(); |
128 | ||
a4a116c3 | 129 | for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) { |
8c736b3c MR |
130 | if (appliesToTraceType(tth.getTraceClass())) { |
131 | traceTypes.add(tth.getTraceClass()); | |
132 | } | |
133 | } | |
134 | ||
135 | return traceTypes; | |
63c43609 MR |
136 | } |
137 | ||
138 | @Override | |
139 | public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() { | |
54eae41f GB |
140 | IAnalysisModule module = createModule(); |
141 | if (module != null) { | |
142 | return module.getAnalysisRequirements(); | |
8c736b3c | 143 | } |
63c43609 | 144 | return Collections.EMPTY_SET; |
04da5385 | 145 | |
63c43609 MR |
146 | } |
147 | ||
c068a752 GB |
148 | // --------------------------------------- |
149 | // Functionalities | |
150 | // --------------------------------------- | |
151 | ||
54eae41f GB |
152 | private IAnalysisModule createModule() { |
153 | IAnalysisModule module = null; | |
154 | try { | |
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$ | |
160 | } | |
161 | return module; | |
162 | } | |
163 | ||
c068a752 GB |
164 | @Override |
165 | public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException { | |
166 | ||
167 | /* Check that analysis can be executed */ | |
168 | if (!appliesToTraceType(trace.getClass())) { | |
169 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, getName())); | |
170 | } | |
171 | ||
54eae41f GB |
172 | IAnalysisModule module = createModule(); |
173 | if (module == null) { | |
174 | return null; | |
175 | } | |
176 | ||
177 | module.setAutomatic(isAutomatic()); | |
178 | ||
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); | |
c068a752 | 186 | } |
c068a752 | 187 | } |
54eae41f GB |
188 | module.setTrace(trace); |
189 | TmfAnalysisManager.analysisModuleCreated(module); | |
190 | ||
c068a752 GB |
191 | return module; |
192 | ||
193 | } | |
54eae41f GB |
194 | |
195 | @Override | |
196 | public String getHelpText(@NonNull ITmfTrace trace) { | |
197 | IAnalysisModule module = createModule(); | |
198 | if (module != null) { | |
199 | String ret = module.getHelpText(trace); | |
200 | module.dispose(); | |
201 | return ret; | |
202 | } | |
203 | return getHelpText(); | |
04da5385 | 204 | |
54eae41f | 205 | } |
c068a752 | 206 | } |