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