Commit | Line | Data |
---|---|---|
c068a752 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
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 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.analysis; | |
14 | ||
15 | import org.eclipse.core.runtime.ContributorFactoryOSGi; | |
16 | import org.eclipse.core.runtime.CoreException; | |
17 | import org.eclipse.core.runtime.IConfigurationElement; | |
18 | import org.eclipse.core.runtime.InvalidRegistryObjectException; | |
19 | import org.eclipse.linuxtools.internal.tmf.core.Activator; | |
20 | import org.eclipse.linuxtools.internal.tmf.core.analysis.TmfAnalysisType; | |
21 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
22 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
23 | import org.eclipse.osgi.util.NLS; | |
24 | import org.osgi.framework.Bundle; | |
25 | ||
26 | /** | |
27 | * Analysis module helper for modules provided by a plugin's configuration | |
28 | * elements. | |
29 | * | |
30 | * @author Geneviève Bastien | |
31 | */ | |
32 | public class TmfAnalysisModuleHelperCE implements IAnalysisModuleHelper { | |
33 | ||
34 | private final IConfigurationElement fCe; | |
35 | ||
36 | /** | |
37 | * Constructor | |
38 | * | |
39 | * @param ce | |
40 | * The source {@link IConfigurationElement} of this module helper | |
41 | */ | |
42 | public TmfAnalysisModuleHelperCE(IConfigurationElement ce) { | |
43 | fCe = ce; | |
44 | } | |
45 | ||
46 | // ---------------------------------------- | |
47 | // Wrappers to {@link IAnalysisModule} methods | |
48 | // ---------------------------------------- | |
49 | ||
50 | @Override | |
51 | public String getId() { | |
52 | return fCe.getAttribute(TmfAnalysisType.ID_ATTR); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public String getName() { | |
57 | return fCe.getAttribute(TmfAnalysisType.NAME_ATTR); | |
58 | } | |
59 | ||
60 | @Override | |
61 | public boolean isAutomatic() { | |
62 | return Boolean.valueOf(fCe.getAttribute(TmfAnalysisType.AUTOMATIC_ATTR)); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public String getHelpText() { | |
67 | return new String(); | |
68 | } | |
69 | ||
70 | @Override | |
71 | public String getIcon() { | |
72 | return fCe.getAttribute(TmfAnalysisType.ICON_ATTR); | |
73 | } | |
74 | ||
75 | @Override | |
76 | public Bundle getBundle() { | |
77 | return ContributorFactoryOSGi.resolve(fCe.getContributor()); | |
78 | } | |
79 | ||
80 | @Override | |
81 | public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) { | |
82 | boolean applies = false; | |
83 | ||
84 | /* Get the module's applying tracetypes */ | |
85 | final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisType.TRACETYPE_ELEM); | |
86 | for (IConfigurationElement element : tracetypeCE) { | |
87 | Class<?> applyclass; | |
88 | try { | |
89 | applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisType.CLASS_ATTR)); | |
90 | String classAppliesVal = element.getAttribute(TmfAnalysisType.APPLIES_ATTR); | |
91 | boolean classApplies = true; | |
92 | if (classAppliesVal != null) { | |
93 | classApplies = Boolean.valueOf(classAppliesVal); | |
94 | } | |
95 | if (classApplies) { | |
96 | applies = applyclass.isAssignableFrom(traceclass); | |
97 | } else { | |
98 | applies = !applyclass.isAssignableFrom(traceclass); | |
99 | } | |
100 | } catch (ClassNotFoundException e) { | |
101 | Activator.logError("Error in applies to trace", e); //$NON-NLS-1$ | |
102 | } catch (InvalidRegistryObjectException e) { | |
103 | Activator.logError("Error in applies to trace", e); //$NON-NLS-1$ | |
104 | } | |
105 | } | |
106 | return applies; | |
107 | } | |
108 | ||
109 | // --------------------------------------- | |
110 | // Functionalities | |
111 | // --------------------------------------- | |
112 | ||
113 | @Override | |
114 | public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException { | |
115 | ||
116 | /* Check that analysis can be executed */ | |
117 | if (!appliesToTraceType(trace.getClass())) { | |
118 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, getName())); | |
119 | } | |
120 | ||
121 | IAnalysisModule module = null; | |
122 | try { | |
123 | module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisType.ANALYSIS_MODULE_ATTR); | |
124 | module.setName(getName()); | |
125 | module.setId(getId()); | |
126 | module.setAutomatic(isAutomatic()); | |
127 | ||
128 | /* Get the module's parameters */ | |
129 | final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisType.PARAMETER_ELEM); | |
130 | for (IConfigurationElement element : parametersCE) { | |
131 | module.addParameter(element.getAttribute(TmfAnalysisType.NAME_ATTR)); | |
132 | String defaultValue = element.getAttribute(TmfAnalysisType.DEFAULT_VALUE_ATTR); | |
133 | if (defaultValue != null) { | |
134 | module.setParameter(element.getAttribute(TmfAnalysisType.NAME_ATTR), defaultValue); | |
135 | } | |
136 | } | |
137 | module.setTrace(trace); | |
138 | } catch (CoreException e) { | |
139 | Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$ | |
140 | } | |
141 | return module; | |
142 | ||
143 | } | |
144 | ||
145 | } |