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 java.util.ArrayList; | |
16 | import java.util.Collections; | |
17 | import java.util.HashMap; | |
18 | import java.util.List; | |
19 | import java.util.Map; | |
20 | import java.util.concurrent.CountDownLatch; | |
21 | import java.util.concurrent.TimeUnit; | |
22 | ||
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
24 | import org.eclipse.core.runtime.IStatus; | |
25 | import org.eclipse.core.runtime.Status; | |
26 | import org.eclipse.core.runtime.jobs.Job; | |
27 | import org.eclipse.linuxtools.internal.tmf.core.Activator; | |
28 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; | |
29 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
30 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; | |
31 | import org.eclipse.linuxtools.tmf.core.signal.TmfStartAnalysisSignal; | |
32 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal; | |
33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
34 | import org.eclipse.osgi.util.NLS; | |
35 | ||
36 | /** | |
37 | * Base class that analysis modules main class may extend. It provides default | |
38 | * behavior to some methods of the analysis module | |
39 | * | |
40 | * @author Geneviève Bastien | |
41 | * @since 3.0 | |
42 | */ | |
43 | public abstract class TmfAbstractAnalysisModule extends TmfComponent implements IAnalysisModule { | |
44 | ||
45 | private String fName, fId; | |
46 | private boolean fAutomatic = false, fStarted = false; | |
47 | private ITmfTrace fTrace; | |
48 | private final Map<String, Object> fParameters = new HashMap<String, Object>(); | |
49 | private final List<String> fParameterNames = new ArrayList<String>(); | |
50 | private final List<IAnalysisOutput> fOutputs = new ArrayList<IAnalysisOutput>(); | |
51 | private List<IAnalysisParameterProvider> fParameterProviders = new ArrayList<IAnalysisParameterProvider>(); | |
52 | private Job fJob = null; | |
53 | ||
54 | private final Object syncObj = new Object(); | |
55 | ||
56 | /* Latch tracking if the analysis is completed or not */ | |
57 | private CountDownLatch fFinishedLatch = new CountDownLatch(0); | |
58 | ||
59 | private boolean fAnalysisCancelled = false; | |
60 | ||
61 | @Override | |
62 | public boolean isAutomatic() { | |
63 | return fAutomatic; | |
64 | } | |
65 | ||
66 | @Override | |
67 | public String getName() { | |
68 | return fName; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public void setName(String name) { | |
73 | fName = name; | |
74 | } | |
75 | ||
76 | @Override | |
77 | public void setId(String id) { | |
78 | fId = id; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public String getId() { | |
83 | return fId; | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void setAutomatic(boolean auto) { | |
88 | fAutomatic = auto; | |
89 | } | |
90 | ||
91 | @Override | |
92 | public void setTrace(ITmfTrace trace) throws TmfAnalysisException { | |
93 | if (fTrace != null) { | |
94 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_TraceSetMoreThanOnce, getName())); | |
95 | } | |
96 | ||
97 | /* Check that analysis can be executed */ | |
98 | if (!canExecute(trace)) { | |
99 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, getName())); | |
100 | } | |
101 | fTrace = trace; | |
102 | /* Get the parameter providers for this trace */ | |
103 | fParameterProviders = TmfAnalysisManager.getParameterProviders(this, fTrace); | |
104 | for (IAnalysisParameterProvider provider : fParameterProviders) { | |
105 | provider.registerModule(this); | |
106 | } | |
107 | resetAnalysis(); | |
108 | fStarted = false; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Gets the trace | |
113 | * | |
114 | * @return The trace | |
115 | */ | |
116 | protected ITmfTrace getTrace() { | |
117 | return fTrace; | |
118 | } | |
119 | ||
120 | @Override | |
121 | public void addParameter(String name) { | |
122 | fParameterNames.add(name); | |
123 | } | |
124 | ||
125 | @Override | |
126 | public synchronized void setParameter(String name, Object value) { | |
127 | if (!fParameterNames.contains(name)) { | |
128 | throw new RuntimeException(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, name, getName())); | |
129 | } | |
130 | Object oldValue = fParameters.get(name); | |
131 | fParameters.put(name, value); | |
132 | if ((value != null) && !(value.equals(oldValue))) { | |
133 | parameterChanged(name); | |
134 | } | |
135 | } | |
136 | ||
137 | @Override | |
138 | public synchronized void notifyParameterChanged(String name) { | |
139 | if (!fParameterNames.contains(name)) { | |
140 | throw new RuntimeException(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, name, getName())); | |
141 | } | |
142 | Object oldValue = fParameters.get(name); | |
143 | Object value = getParameter(name); | |
144 | if ((value != null) && !(value.equals(oldValue))) { | |
145 | parameterChanged(name); | |
146 | } | |
147 | } | |
148 | ||
149 | /** | |
150 | * Used to indicate that a parameter value has been changed | |
151 | * | |
152 | * @param name | |
153 | * The name of the modified parameter | |
154 | */ | |
155 | protected void parameterChanged(String name) { | |
156 | ||
157 | } | |
158 | ||
159 | @Override | |
160 | public Object getParameter(String name) { | |
161 | Object paramValue = fParameters.get(name); | |
162 | /* The parameter is not set, maybe it can be provided by someone else */ | |
163 | if ((paramValue == null) && (fTrace != null)) { | |
164 | for (IAnalysisParameterProvider provider : fParameterProviders) { | |
165 | paramValue = provider.getParameter(name); | |
166 | if (paramValue != null) { | |
167 | break; | |
168 | } | |
169 | } | |
170 | } | |
171 | return paramValue; | |
172 | } | |
173 | ||
174 | @Override | |
175 | public boolean canExecute(ITmfTrace trace) { | |
176 | return true; | |
177 | } | |
178 | ||
179 | /** | |
180 | * Set the countdown latch back to 1 so the analysis can be executed again | |
181 | */ | |
182 | protected void resetAnalysis() { | |
183 | fFinishedLatch = new CountDownLatch(1); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Actually executes the analysis itself | |
188 | * | |
189 | * @param monitor | |
190 | * Progress monitor | |
191 | * @return Whether the analysis was completed successfully or not | |
192 | * @throws TmfAnalysisException | |
193 | * Method may throw an analysis exception | |
194 | */ | |
195 | protected abstract boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException; | |
196 | ||
197 | /** | |
198 | * Indicate the analysis has been canceled. It is abstract to force | |
199 | * implementing class to cleanup what they are running. This is called by | |
200 | * the job's canceling. It does not need to be called directly. | |
201 | */ | |
202 | protected abstract void canceling(); | |
203 | ||
204 | /** | |
205 | * To be called when the analysis is completed, whether normally or because | |
206 | * it was cancelled or for any other reason. | |
207 | * | |
208 | * It has to be called inside a synchronized block | |
209 | */ | |
210 | private void setAnalysisCompleted() { | |
211 | fStarted = false; | |
212 | fJob = null; | |
213 | fFinishedLatch.countDown(); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Cancels the analysis if it is executing | |
218 | */ | |
219 | @Override | |
220 | public final void cancel() { | |
221 | synchronized (syncObj) { | |
222 | if (fJob != null) { | |
223 | if (fJob.cancel()) { | |
224 | fAnalysisCancelled = true; | |
225 | setAnalysisCompleted(); | |
226 | } | |
227 | } | |
228 | fStarted = false; | |
229 | } | |
230 | } | |
231 | ||
232 | private void execute() { | |
233 | ||
234 | /* | |
235 | * TODO: The analysis in a job should be done at the analysis manager | |
236 | * level instead of depending on this abstract class implementation, | |
237 | * otherwise another analysis implementation may block the main thread | |
238 | */ | |
239 | ||
240 | /* Do not execute if analysis has already run */ | |
241 | if (fFinishedLatch.getCount() == 0) { | |
242 | return; | |
243 | } | |
244 | ||
245 | /* Do not execute if analysis already running */ | |
246 | synchronized (syncObj) { | |
247 | if (fStarted) { | |
248 | return; | |
249 | } | |
250 | fStarted = true; | |
251 | } | |
252 | ||
253 | /* | |
254 | * Actual analysis will be run on a separate thread | |
255 | */ | |
256 | fJob = new Job(NLS.bind(Messages.TmfAbstractAnalysisModule_RunningAnalysis, getName())) { | |
257 | @Override | |
258 | protected IStatus run(final IProgressMonitor monitor) { | |
259 | try { | |
260 | monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$ | |
261 | broadcast(new TmfStartAnalysisSignal(TmfAbstractAnalysisModule.this, TmfAbstractAnalysisModule.this)); | |
262 | fAnalysisCancelled = !executeAnalysis(monitor); | |
263 | } catch (TmfAnalysisException e) { | |
264 | Activator.logError("Error executing analysis with trace " + getTrace().getName(), e); //$NON-NLS-1$ | |
265 | } finally { | |
266 | synchronized (syncObj) { | |
267 | monitor.done(); | |
268 | setAnalysisCompleted(); | |
269 | } | |
270 | } | |
271 | if (!fAnalysisCancelled) { | |
272 | return Status.OK_STATUS; | |
273 | } | |
274 | return Status.CANCEL_STATUS; | |
275 | } | |
276 | ||
277 | @Override | |
278 | protected void canceling() { | |
279 | TmfAbstractAnalysisModule.this.canceling(); | |
280 | } | |
281 | ||
282 | }; | |
283 | fJob.schedule(); | |
284 | } | |
285 | ||
286 | @Override | |
287 | public IStatus schedule() { | |
288 | if (fTrace == null) { | |
289 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, String.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$ | |
290 | } | |
291 | execute(); | |
292 | ||
293 | return Status.OK_STATUS; | |
294 | } | |
295 | ||
296 | @Override | |
297 | public List<IAnalysisOutput> getOutputs() { | |
298 | return Collections.unmodifiableList(fOutputs); | |
299 | } | |
300 | ||
301 | @Override | |
302 | public void registerOutput(IAnalysisOutput output) { | |
303 | if (!fOutputs.contains(output)) { | |
304 | fOutputs.add(output); | |
305 | } | |
306 | } | |
307 | ||
308 | @Override | |
309 | public boolean waitForCompletion(IProgressMonitor monitor) { | |
310 | try { | |
311 | while (!fFinishedLatch.await(1, TimeUnit.MILLISECONDS)) { | |
312 | if (monitor.isCanceled()) { | |
313 | fAnalysisCancelled = true; | |
314 | return false; | |
315 | } | |
316 | } | |
317 | } catch (InterruptedException e) { | |
318 | Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$ | |
319 | } | |
320 | return !fAnalysisCancelled; | |
321 | } | |
322 | ||
323 | /** | |
324 | * Signal handler for trace closing | |
325 | * | |
326 | * @param signal | |
327 | * Trace closed signal | |
328 | */ | |
329 | @TmfSignalHandler | |
330 | public void traceClosed(TmfTraceClosedSignal signal) { | |
331 | /* Is the closing trace the one that was requested? */ | |
332 | if (signal.getTrace() == fTrace) { | |
333 | cancel(); | |
334 | fTrace = null; | |
335 | } | |
336 | } | |
337 | ||
338 | /** | |
339 | * Returns a full help text to display | |
340 | * | |
341 | * @return Full help text for the module | |
342 | */ | |
343 | protected String getFullHelpText() { | |
344 | return NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisModule, getName()); | |
345 | } | |
346 | ||
347 | /** | |
348 | * Gets a short help text, to display as header to other help text | |
349 | * | |
350 | * @param trace | |
351 | * The trace to show help for | |
352 | * | |
353 | * @return Short help text describing the module | |
354 | */ | |
355 | protected String getShortHelpText(ITmfTrace trace) { | |
356 | return NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisForTrace, getName(), trace.getName()); | |
357 | } | |
358 | ||
359 | /** | |
360 | * Gets the help text specific for a trace who does not have required | |
361 | * characteristics for module to execute | |
362 | * | |
363 | * @param trace | |
364 | * The trace to apply the analysis to | |
365 | * @return Help text | |
366 | */ | |
367 | protected String getTraceCannotExecuteHelpText(ITmfTrace trace) { | |
368 | return Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute; | |
369 | } | |
370 | ||
371 | @Override | |
372 | public String getHelpText() { | |
373 | return getFullHelpText(); | |
374 | } | |
375 | ||
376 | @Override | |
377 | public String getHelpText(ITmfTrace trace) { | |
378 | if (trace == null) { | |
379 | return getHelpText(); | |
380 | } | |
381 | String text = getShortHelpText(trace); | |
382 | if (!canExecute(trace)) { | |
383 | text = text + getTraceCannotExecuteHelpText(trace); | |
384 | } | |
385 | return text; | |
386 | } | |
387 | ||
388 | } |