587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.logger.ant.listener;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.StringWriter;
|
|
21 |
import java.text.DateFormat;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
import java.util.Date;
|
|
24 |
import java.util.Enumeration;
|
|
25 |
import java.util.HashMap;
|
|
26 |
import java.util.Hashtable;
|
|
27 |
import java.util.LinkedHashMap;
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.Map;
|
|
30 |
import java.util.Vector;
|
|
31 |
|
|
32 |
import org.apache.log4j.Logger;
|
|
33 |
import org.apache.tools.ant.BuildEvent;
|
|
34 |
import org.apache.tools.ant.BuildException;
|
|
35 |
import org.apache.tools.ant.Project;
|
|
36 |
import org.apache.tools.ant.Target;
|
|
37 |
import org.apache.tools.ant.util.DateUtils;
|
|
38 |
|
628
|
39 |
import com.nokia.helium.core.ant.types.Stage;
|
587
|
40 |
import com.nokia.helium.logger.ant.types.StageSummary;
|
|
41 |
|
|
42 |
import freemarker.cache.FileTemplateLoader;
|
|
43 |
import freemarker.template.Configuration;
|
|
44 |
import freemarker.template.Template;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* <code>StageStatusHandler</code> is the handler class responsible for
|
|
48 |
* displaying the summary of the various configured build stages at the end of
|
|
49 |
* build process.
|
|
50 |
*
|
|
51 |
*/
|
|
52 |
public class StageSummaryHandler implements Handler {
|
|
53 |
|
|
54 |
public static final String PASSED = "PASSED";
|
|
55 |
public static final String FAILED = "FAILED";
|
|
56 |
|
|
57 |
private Logger log = Logger.getLogger(getClass());
|
|
58 |
private boolean lookup4Stages;
|
|
59 |
private boolean summarize;
|
|
60 |
|
|
61 |
private Map<String, StageWrapper> completedStages;
|
|
62 |
private StageWrapper currentStage;
|
|
63 |
private Hashtable<String, Stage> stages;
|
|
64 |
private File template;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Create an instance of {@link StageSummaryHandler}
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
public StageSummaryHandler() {
|
|
71 |
completedStages = new LinkedHashMap<String, StageWrapper>();
|
|
72 |
log.debug("StageStatusHandler instantiated");
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* {@inheritDoc}
|
|
77 |
*/
|
|
78 |
public void handleBuildStarted(BuildEvent event) {
|
|
79 |
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* {@inheritDoc}
|
|
84 |
*/
|
|
85 |
public void handleBuildFinished(BuildEvent event) {
|
628
|
86 |
if (summarize && currentStage != null) {
|
587
|
87 |
endCurrentStage();
|
628
|
88 |
}
|
587
|
89 |
if (summarize && !completedStages.isEmpty()) {
|
|
90 |
generateSummary(event.getProject());
|
|
91 |
log.debug("Stage Summary generation completed");
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* {@inheritDoc}
|
|
97 |
*/
|
|
98 |
public void handleTargetStarted(BuildEvent event) {
|
|
99 |
Project project = event.getProject();
|
|
100 |
if (!summarize) {
|
|
101 |
StageSummary stageSummary = getStageSummary(project);
|
|
102 |
summarize = stageSummary != null
|
|
103 |
&& stageSummary.getTemplate() != null;
|
|
104 |
lookup4Stages = summarize;
|
|
105 |
template = stageSummary.getTemplate();
|
|
106 |
log.debug("Is Project configured to display Stage Summary ? "
|
|
107 |
+ summarize);
|
|
108 |
}
|
|
109 |
|
|
110 |
if (lookup4Stages) {
|
|
111 |
log.debug("Loading stages....");
|
|
112 |
parseStages(event.getProject());
|
|
113 |
log.debug("Total no of stages loaded = " + stages.size());
|
|
114 |
lookup4Stages = false;
|
|
115 |
}
|
|
116 |
|
|
117 |
log.debug("Handling target - " + event.getTarget().getName());
|
|
118 |
if (summarize && doRunTarget(event)) {
|
|
119 |
StageWrapper stage = searchNewStage(event);
|
628
|
120 |
if (stage != null) {
|
587
|
121 |
startNewStage(stage);
|
628
|
122 |
}
|
587
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* {@inheritDoc}
|
|
128 |
*/
|
|
129 |
public void handleTargetFinished(BuildEvent event) {
|
628
|
130 |
if (summarize && isCurrentStageToEnd(event)) {
|
587
|
131 |
endCurrentStage();
|
628
|
132 |
}
|
587
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Indicates whether the current stage is ending or not.
|
|
137 |
*
|
|
138 |
* @param event
|
|
139 |
* is the build event.
|
|
140 |
* @return true, if the build failed or the end target of the stage is
|
|
141 |
* reached; otherwise false.
|
|
142 |
*/
|
|
143 |
private boolean isCurrentStageToEnd(BuildEvent event) {
|
|
144 |
boolean end = false;
|
|
145 |
if (currentStage != null) {
|
|
146 |
if (event.getException() != null) {
|
|
147 |
currentStage.setError(getReason(event.getException()));
|
|
148 |
end = true;
|
628
|
149 |
} else {
|
587
|
150 |
end = currentStage.stage.isEndTarget(event.getTarget()
|
|
151 |
.getName());
|
628
|
152 |
}
|
587
|
153 |
}
|
|
154 |
return end;
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Method returns the configured {@link StageSummary}.
|
|
159 |
*
|
|
160 |
* @param project
|
|
161 |
* is the project to lookup for stageSummary.
|
|
162 |
* @return the {@link StageSummary}.
|
|
163 |
*/
|
|
164 |
@SuppressWarnings("unchecked")
|
|
165 |
private StageSummary getStageSummary(Project project) {
|
|
166 |
StageSummary stageSummary = null;
|
|
167 |
int count = 0;
|
|
168 |
Hashtable<String, Object> references = project.getReferences();
|
|
169 |
for (Enumeration<String> en = references.keys(); en.hasMoreElements();) {
|
|
170 |
Object object = references.get(en.nextElement());
|
|
171 |
if (object instanceof StageSummary) {
|
|
172 |
count++;
|
|
173 |
if (count > 1) {
|
628
|
174 |
throw new BuildException("Multiple entries of 'hlm:stagesummary' found in "
|
587
|
175 |
+ "stages_config.ant.xml.");
|
|
176 |
}
|
|
177 |
stageSummary = (StageSummary) object;
|
|
178 |
}
|
|
179 |
}
|
|
180 |
return stageSummary;
|
|
181 |
}
|
|
182 |
|
|
183 |
/**
|
|
184 |
* Start the given stage as a new build stage.
|
|
185 |
*
|
|
186 |
* @param newStage
|
|
187 |
* is the new build stage to start.
|
|
188 |
*/
|
|
189 |
private void startNewStage(StageWrapper newStage) {
|
|
190 |
endCurrentStage();
|
|
191 |
Long currTime = getCurrentTime();
|
628
|
192 |
if (!completedStages.containsKey(newStage.stageName)) {
|
587
|
193 |
newStage.setStageStartTime(getTimestamp(currTime));
|
628
|
194 |
}
|
587
|
195 |
newStage.setStartTime(currTime);
|
|
196 |
this.currentStage = newStage;
|
|
197 |
log.debug("New stage [" + newStage.stageName + "] started at "
|
|
198 |
+ getTimestamp(currTime));
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* End the current stage.
|
|
203 |
*
|
|
204 |
*/
|
|
205 |
private void endCurrentStage() {
|
|
206 |
if (currentStage != null) {
|
|
207 |
Long currTime = getCurrentTime();
|
|
208 |
if (completedStages.containsKey(currentStage.stageName)) {
|
|
209 |
StageWrapper stg = completedStages.get(currentStage.stageName);
|
|
210 |
stg.setDuration(stg.duration
|
|
211 |
+ getTimeElapsed(currentStage.startTime, currTime));
|
|
212 |
} else {
|
|
213 |
currentStage.setDuration(getTimeElapsed(currentStage.startTime,
|
|
214 |
currTime));
|
|
215 |
completedStages.put(currentStage.stageName, currentStage);
|
|
216 |
}
|
|
217 |
log.debug("Stage [" + currentStage.stageName + "] finished at "
|
|
218 |
+ getTimestamp(currTime));
|
|
219 |
currentStage = null;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Search for the new Stage based on the given build event.
|
|
225 |
*
|
|
226 |
* @param event
|
|
227 |
* is the build event fired.
|
|
228 |
* @return an instance of StageWrapper, if the build event marks the start
|
|
229 |
* of a configured Stage.
|
|
230 |
*/
|
|
231 |
private StageWrapper searchNewStage(BuildEvent event) {
|
|
232 |
StageWrapper stageWrapper = null;
|
|
233 |
String target = event.getTarget().getName();
|
|
234 |
for (String stageName : stages.keySet()) {
|
|
235 |
Stage stage = stages.get(stageName);
|
|
236 |
if (isStartingTarget(target, event.getProject(), stage)) {
|
|
237 |
stageWrapper = new StageWrapper(stageName, stage);
|
|
238 |
break;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
return stageWrapper;
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Return whether the given target is a starting target of the given stage.
|
|
246 |
*
|
|
247 |
* @param targetName
|
|
248 |
* is the target to check.
|
|
249 |
* @param project
|
|
250 |
* is the project to lookup for target
|
|
251 |
* @param stage
|
|
252 |
* is the stage to check.
|
|
253 |
* @return
|
|
254 |
*/
|
|
255 |
@SuppressWarnings("unchecked")
|
|
256 |
private boolean isStartingTarget(String targetName, Project project,
|
|
257 |
Stage stage) {
|
|
258 |
boolean bool = false;
|
|
259 |
if (project.getTargets().containsKey(stage.getStartTarget())) {
|
|
260 |
Vector<Target> dependencies = project.topoSort(stage
|
|
261 |
.getStartTarget(), project.getTargets(), false);
|
|
262 |
if (!dependencies.isEmpty()) {
|
|
263 |
Target target = dependencies.firstElement();
|
|
264 |
bool = target.getName().equals(targetName);
|
|
265 |
}
|
|
266 |
}
|
|
267 |
return bool;
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
|
271 |
* Parse and cache the stages configured.
|
|
272 |
*
|
|
273 |
* @param project
|
|
274 |
* is the project to lookup for stages.
|
|
275 |
*/
|
|
276 |
@SuppressWarnings("unchecked")
|
|
277 |
private void parseStages(Project project) {
|
|
278 |
stages = new Hashtable<String, Stage>();
|
|
279 |
Hashtable<String, Object> references = project.getReferences();
|
|
280 |
for (Enumeration<String> en = references.keys(); en.hasMoreElements();) {
|
|
281 |
String key = en.nextElement();
|
|
282 |
Object value = references.get(key);
|
|
283 |
if (value instanceof Stage) {
|
|
284 |
validateStageInformation(key, (Stage) value);
|
|
285 |
stages.put(key, (Stage) value);
|
|
286 |
}
|
|
287 |
}
|
|
288 |
}
|
|
289 |
|
|
290 |
/**
|
|
291 |
* Return the reason for build failure in String format.
|
|
292 |
*
|
|
293 |
* @param th
|
|
294 |
* is the cause of build failure if any.
|
|
295 |
* @return String representing the build failure.
|
|
296 |
*/
|
|
297 |
private String getReason(Throwable th) {
|
|
298 |
return (th != null) ? th.getMessage() : "";
|
|
299 |
}
|
|
300 |
|
|
301 |
/**
|
|
302 |
* Return the current time in milliseconds.
|
|
303 |
*
|
|
304 |
* @return the current time in milliseconds.
|
|
305 |
*/
|
|
306 |
private Long getCurrentTime() {
|
|
307 |
return System.currentTimeMillis();
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* Method is used to construct build reports for the completed stages.
|
|
312 |
*
|
|
313 |
* @return a list of build status reports.
|
|
314 |
*/
|
|
315 |
private List<BuildStatusReport> constructBuildStatusReports() {
|
|
316 |
List<BuildStatusReport> reports = new ArrayList<BuildStatusReport>();
|
|
317 |
BuildStatusReport report = null;
|
|
318 |
StageWrapper stage = null;
|
|
319 |
for (String stageName : completedStages.keySet()) {
|
|
320 |
stage = completedStages.get(stageName);
|
|
321 |
report = new BuildStatusReport(stageName, stage.stageStartTime,
|
|
322 |
format2String(stage.duration), stage.error);
|
|
323 |
reports.add(report);
|
|
324 |
}
|
|
325 |
return reports;
|
|
326 |
}
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Generate build summary.
|
|
330 |
*
|
|
331 |
*/
|
|
332 |
private void generateSummary(Project project) {
|
|
333 |
if (template != null) {
|
|
334 |
try {
|
|
335 |
Configuration cfg = new Configuration();
|
|
336 |
log.debug("Basedir: " + template.getParentFile());
|
|
337 |
cfg.setTemplateLoader(new FileTemplateLoader(template
|
|
338 |
.getParentFile()));
|
|
339 |
Template templ = cfg.getTemplate(template.getName());
|
|
340 |
StringWriter writer = new StringWriter();
|
|
341 |
templ.process(getTemplateData(), writer);
|
|
342 |
project.log(writer.toString());
|
|
343 |
} catch (freemarker.core.InvalidReferenceException ivx) {
|
|
344 |
project.log("Invalid reference in config: ", ivx,
|
|
345 |
Project.MSG_WARN);
|
|
346 |
} catch (freemarker.template.TemplateException e2) {
|
|
347 |
project.log("TemplateException: ", e2, Project.MSG_WARN);
|
|
348 |
} catch (java.io.IOException e) {
|
|
349 |
project.log("I/O Error during template conversion: "
|
|
350 |
+ e.toString(), Project.MSG_WARN);
|
|
351 |
}
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
/**
|
|
356 |
* Return the data-model to be merged with the template.
|
|
357 |
*
|
|
358 |
* @return a Map representing template data-model.
|
|
359 |
*/
|
|
360 |
private Map<String, Object> getTemplateData() {
|
|
361 |
Map<String, Object> templateMap = new HashMap<String, Object>();
|
|
362 |
List<BuildStatusReport> statusReports = constructBuildStatusReports();
|
|
363 |
templateMap.put("statusReports", statusReports);
|
|
364 |
return templateMap;
|
|
365 |
}
|
|
366 |
|
|
367 |
/**
|
|
368 |
* Get the given date as String format.
|
|
369 |
*
|
|
370 |
* @param date
|
|
371 |
* is the date to be formatted as String.
|
|
372 |
* @return given date formated as String
|
|
373 |
*/
|
|
374 |
private String getTimestamp(long date) {
|
|
375 |
Date dt = new Date(date);
|
|
376 |
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
|
|
377 |
DateFormat.SHORT);
|
|
378 |
String finishTime = formatter.format(dt);
|
|
379 |
return finishTime;
|
|
380 |
}
|
|
381 |
|
|
382 |
/**
|
|
383 |
* Get the time duration for the given start and end times.
|
|
384 |
*
|
|
385 |
* @param startTime
|
|
386 |
* is the start time.
|
|
387 |
* @param endTime
|
|
388 |
* is the end time.
|
|
389 |
* @return total time elapsed.
|
|
390 |
*/
|
|
391 |
private Long getTimeElapsed(Long startTime, Long endTime) {
|
|
392 |
return endTime - startTime;
|
|
393 |
}
|
|
394 |
|
|
395 |
/**
|
|
396 |
* Return the given elapsed time in String format.
|
|
397 |
*
|
|
398 |
* @param time
|
|
399 |
* is the time to be formatted as String.
|
|
400 |
* @return time elapsed in String format.
|
|
401 |
*/
|
|
402 |
private String format2String(Long time) {
|
|
403 |
return DateUtils.formatElapsedTime(time);
|
|
404 |
}
|
|
405 |
|
|
406 |
/**
|
|
407 |
* To validate stage information.
|
|
408 |
*
|
|
409 |
* @param stageKey
|
|
410 |
* @param stage
|
|
411 |
*/
|
|
412 |
private void validateStageInformation(String stageKey, Stage stage) {
|
|
413 |
|
|
414 |
if (stage.getStartTarget() == null) {
|
|
415 |
throw new BuildException("'starttarget' for stage '" + stageKey
|
|
416 |
+ "' should not be null.");
|
|
417 |
}
|
|
418 |
|
|
419 |
if (stage.getEndTarget() == null) {
|
|
420 |
throw new BuildException("'endtarget' for stage '" + stageKey
|
|
421 |
+ "' should not be null.");
|
|
422 |
}
|
|
423 |
}
|
|
424 |
|
|
425 |
/**
|
|
426 |
* Method indicates whether the target is to be considered for build stage
|
|
427 |
* summary or not.
|
|
428 |
*
|
|
429 |
* @param event
|
|
430 |
* is the build event.
|
|
431 |
* @return true, if the target is enabled;otherwise false
|
|
432 |
*/
|
|
433 |
private boolean doRunTarget(BuildEvent event) {
|
|
434 |
boolean doRun = true;
|
|
435 |
|
|
436 |
String ifCondition = event.getTarget().getIf();
|
|
437 |
|
|
438 |
if (ifCondition != null && !ifCondition.isEmpty()) {
|
|
439 |
String prop = event.getProject().replaceProperties(ifCondition);
|
|
440 |
doRun = event.getProject().getProperty(prop) != null;
|
|
441 |
}
|
|
442 |
|
|
443 |
String unlessCondition = event.getTarget().getUnless();
|
|
444 |
|
|
445 |
if (unlessCondition != null && !unlessCondition.isEmpty()) {
|
|
446 |
String prop = event.getProject().replaceProperties(unlessCondition);
|
|
447 |
doRun = event.getProject().getProperty(prop) == null;
|
|
448 |
}
|
|
449 |
return doRun;
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* A wrapper class for Stage.
|
|
454 |
*
|
|
455 |
*/
|
|
456 |
private class StageWrapper {
|
|
457 |
private Stage stage;
|
|
458 |
private String stageName;
|
|
459 |
private String error;
|
|
460 |
private String stageStartTime;
|
|
461 |
private Long startTime;
|
|
462 |
private Long duration;
|
|
463 |
|
|
464 |
public StageWrapper(String stageName, Stage stage) {
|
|
465 |
this.stageName = stageName;
|
|
466 |
this.stage = stage;
|
|
467 |
}
|
|
468 |
|
|
469 |
public void setStartTime(Long startTime) {
|
|
470 |
this.startTime = startTime;
|
|
471 |
}
|
|
472 |
|
|
473 |
public void setError(String error) {
|
|
474 |
this.error = error;
|
|
475 |
}
|
|
476 |
|
|
477 |
public void setDuration(Long duration) {
|
|
478 |
this.duration = duration;
|
|
479 |
}
|
|
480 |
|
|
481 |
public void setStageStartTime(String stageStartTime) {
|
|
482 |
this.stageStartTime = stageStartTime;
|
|
483 |
}
|
|
484 |
}
|
|
485 |
}
|