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.core.ant.listener;
|
|
18 |
|
|
19 |
import java.io.DataOutputStream;
|
628
|
20 |
import java.io.File;
|
587
|
21 |
import java.io.FileOutputStream;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
import java.util.Date;
|
|
24 |
import java.util.List;
|
|
25 |
import java.util.concurrent.TimeUnit;
|
|
26 |
|
|
27 |
import org.apache.tools.ant.BuildEvent;
|
|
28 |
import org.apache.tools.ant.BuildListener;
|
|
29 |
import org.apache.tools.ant.Project;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* <code>TargetTimesGeneratorListener</code> is a build listener used to
|
|
33 |
* generate a comma separated value file of targets and the total time the
|
|
34 |
* targets took to run.
|
|
35 |
*
|
|
36 |
* Note: To generate a target times csv file make an entry of this listener in
|
|
37 |
* the hlm.bat file.
|
|
38 |
*
|
|
39 |
*/
|
|
40 |
public class TargetTimesLogGeneratorListener implements BuildListener {
|
|
41 |
|
|
42 |
private String targetTimesLogCsv;
|
|
43 |
private List<String> targetTimesTable;
|
|
44 |
|
|
45 |
private Project project;
|
|
46 |
private Date targetStartTime;
|
|
47 |
private boolean isInitialized;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Method initializes the listener on build start.
|
|
51 |
*
|
|
52 |
* @param event
|
|
53 |
* is the build start event.
|
|
54 |
*/
|
|
55 |
public void buildStarted(BuildEvent event) {
|
|
56 |
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Initialize the listener.
|
|
61 |
*
|
|
62 |
* @param project
|
|
63 |
* is the current ant project running.
|
|
64 |
*/
|
|
65 |
private void initialize(Project project) {
|
|
66 |
this.project = project;
|
|
67 |
targetTimesLogCsv = project.getProperty("target.times.log.file");
|
|
68 |
targetTimesTable = new ArrayList<String>();
|
|
69 |
isInitialized = true;
|
|
70 |
if (targetTimesLogCsv == null) {
|
|
71 |
this.project.log("TargetTimesLog csv file will not be generated. "
|
|
72 |
+ "Reason: Property 'target.times.log.file' not set");
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Method generates the comma separated value file of targets and their
|
|
78 |
* times.
|
|
79 |
*
|
|
80 |
* @param event
|
|
81 |
* is the build finish event.
|
|
82 |
*/
|
|
83 |
public void buildFinished(BuildEvent event) {
|
|
84 |
if (targetTimesLogCsv != null) {
|
|
85 |
DataOutputStream timesLogOut = null;
|
|
86 |
try {
|
|
87 |
File dir = new File(targetTimesLogCsv).getParentFile();
|
|
88 |
dir.mkdirs();
|
|
89 |
if (dir.exists())
|
|
90 |
{
|
|
91 |
FileOutputStream timesLogFileStream = new FileOutputStream(
|
|
92 |
targetTimesLogCsv, true);
|
|
93 |
timesLogOut = new DataOutputStream(timesLogFileStream);
|
|
94 |
// Display (sorted) hashtable.
|
628
|
95 |
for (String targetTime : targetTimesTable) {
|
588
|
96 |
timesLogOut.writeBytes(targetTime + "\n");
|
628
|
97 |
}
|
587
|
98 |
timesLogOut.close();
|
|
99 |
}
|
|
100 |
} catch (java.io.FileNotFoundException ex) {
|
|
101 |
// We are Ignoring the errors as no need to fail the build.
|
|
102 |
project.log("Exception has occurred", ex, Project.MSG_WARN);
|
|
103 |
ex.printStackTrace();
|
|
104 |
} catch (java.io.IOException ex) {
|
|
105 |
// We are Ignoring the errors as no need to fail the build.
|
|
106 |
project.log("Exception has occurred", ex, Project.MSG_WARN);
|
|
107 |
ex.printStackTrace();
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Method records the target start time.
|
|
114 |
*
|
|
115 |
* @param event
|
|
116 |
* is the target start event.
|
|
117 |
*/
|
|
118 |
public void targetStarted(BuildEvent event) {
|
|
119 |
if (!isInitialized) {
|
|
120 |
initialize(event.getProject());
|
|
121 |
}
|
|
122 |
targetStartTime = new Date();
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* Method records the total time of the target.
|
|
127 |
*
|
|
128 |
* @param event
|
|
129 |
* is the target finish event.
|
|
130 |
*/
|
|
131 |
public void targetFinished(BuildEvent event) {
|
|
132 |
String targetName = event.getTarget().getName();
|
|
133 |
long time = getTargetRunTime();
|
|
134 |
targetTimesTable.add(targetName + "," + time);
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Returns the total time the target run.
|
|
139 |
*
|
|
140 |
* @return total target run time in seconds.
|
|
141 |
*/
|
|
142 |
private long getTargetRunTime() {
|
|
143 |
Date targetFinishTime = new Date();
|
|
144 |
long targetLengthMSecs = targetFinishTime.getTime()
|
|
145 |
- targetStartTime.getTime();
|
|
146 |
return TimeUnit.MILLISECONDS.toSeconds(targetLengthMSecs);
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* {@inheritDoc}
|
|
151 |
*/
|
|
152 |
public void messageLogged(BuildEvent event) {
|
|
153 |
// ignore
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* {@inheritDoc}
|
|
158 |
*/
|
|
159 |
public void taskFinished(BuildEvent event) {
|
|
160 |
// ignore
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* {@inheritDoc}
|
|
165 |
*/
|
|
166 |
public void taskStarted(BuildEvent event) {
|
|
167 |
// ignore
|
|
168 |
}
|
|
169 |
}
|