1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package net.objectlab.qalab.ant;
40
41 import net.objectlab.qalab.parser.BuildStatMoverHandler;
42 import net.objectlab.qalab.util.QALabTags;
43 import net.objectlab.qalab.util.TaskLogger;
44
45 import org.apache.tools.ant.BuildException;
46 import org.apache.tools.ant.Task;
47 import org.xml.sax.InputSource;
48 import org.xml.sax.SAXException;
49
50 import javax.xml.parsers.ParserConfigurationException;
51
52 import java.io.File;
53 import java.io.FileReader;
54 import java.io.FileWriter;
55 import java.io.IOException;
56
57 import java.text.ParseException;
58
59 /**
60 * An Ant task to create the mover XML file given some parameters.
61 *
62 * @author Paramjit Rehinsi
63 * @version $Revision: 187 $
64 */
65 public class BuildStatMoverTask extends Task implements TaskLogger {
66 /** size of the string based on ISO 8601 date format. */
67 private static final int MIN_DATE_SIZE = 18;
68
69 /**
70 * the inputFile, typically qalab.xml.
71 */
72 private File inputFile = null;
73
74 /**
75 * number of hours to define the time window from now.
76 */
77 private String startTimeHoursOffset;
78
79 /**
80 * Statistic types to be taken into account
81 * (checkstyle|findbugs|pms|simian). Or comma separated combinations.
82 */
83 private String types;
84
85 /**
86 * The output XML file with ups and downs.
87 */
88 private File outputXMLfile;
89
90 /**
91 * if true, no output log.
92 */
93 private boolean quiet = false;
94
95 /**
96 * if true, adjust for weekend ie add 48h if start time falls on a weekend.
97 */
98 private boolean weekendAdjustment = false;
99
100 /**
101 * Start Time Window (instead of using startTimeHoursOffset).
102 */
103 private String startTimeWindow = null;
104
105 /**
106 * End Time Window (instead of assuming "now").
107 */
108 private String endTimeWindow = null;
109
110 /**
111 * offset in hours to determine if the stat has fallen to 0 over the last n
112 * hours from the endDateCutoff.
113 */
114 private Integer hoursOffsetForLastRun = null;
115
116 /**
117 * @return true if no log enabled.
118 */
119 public final boolean isQuiet() {
120 return quiet;
121 }
122
123 /**
124 * @param noLog
125 * true and the system will not output debug info.
126 */
127 public final void setQuiet(final boolean noLog) {
128 this.quiet = noLog;
129 }
130
131 /**
132 * Method invoked by the ant framework to execute the compilation of
133 * statistics movers withing the time window.
134 */
135 public final void execute() {
136 validate();
137
138 if (!quiet) {
139 log("inputFile='" + inputFile.getPath() + "', type='" + getTypes() + "'" + " output='" + getOutputXMLfile().getPath()
140 + "' adjustForWeekend='" + isWeekendAdjustment() + "'" + " startTimeOffset='" + getStartTimeHoursOffset()
141 + "'" + " startTimeWindow='" + getStartTimeWindow() + "'" + " endTimeWindow='" + getEndTimeWindow() + "'");
142 }
143
144 try {
145 final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(getInputFile())),
146 getTypes(), new FileWriter(getOutputXMLfile()), isWeekendAdjustment(), this, quiet);
147
148 if (startTimeWindow != null) {
149 handler.setStartTimeWindow(QALabTags.DEFAULT_DATETIME_FORMAT.parse(startTimeWindow));
150 }
151
152 if (endTimeWindow != null) {
153 handler.setEndTimeWindow(QALabTags.DEFAULT_DATETIME_FORMAT.parse(endTimeWindow));
154 }
155
156 if (getStartTimeHoursOffset() != null) {
157 handler.setOffsetHours(getStartTimeHoursOffset());
158 }
159
160 if (getHoursOffsetForLastRun() != null) {
161 handler.setHoursOffsetForLastRun(hoursOffsetForLastRun.intValue());
162 }
163
164 handler.process();
165 } catch (ParserConfigurationException e) {
166 log(e.toString());
167 } catch (SAXException e) {
168 log(e.toString());
169 } catch (IOException e) {
170 log(e.toString());
171 } catch (ParseException e) {
172 throw new BuildException("Date Parse issue", e);
173 }
174 }
175
176 /**
177 * Do some basic parameter validation.
178 */
179 private void validate() {
180 if (inputFile == null) {
181 throw new BuildException("inputFile is mandatory");
182 }
183
184 if ((startTimeHoursOffset != null) && (startTimeWindow != null)) {
185 throw new BuildException("use startTimeHoursOffset OR " + "startTimeWindow, not both.");
186 }
187 }
188
189 /**
190 * uses the super log from the conventional Ant Task log.
191 *
192 * @param text
193 * the text to log
194 */
195 public final void log(final String text) {
196 super.log(text);
197 }
198
199 /**
200 * @return Return the offset time in hours.
201 */
202 public final String getStartTimeHoursOffset() {
203 return startTimeHoursOffset;
204 }
205
206 /**
207 * Set the offset time in hours to define start window.
208 *
209 * @param hours
210 * offset in hours
211 */
212 public final void setStartTimeHoursOffset(final String hours) {
213 startTimeHoursOffset = hours;
214 }
215
216 /**
217 * @return Return List of types handled (checkstyle|findbugs|pmd|simian).
218 */
219 public final String getTypes() {
220 return types;
221 }
222
223 /**
224 * Sets the types on which the report is based, checkstyle|findbugs|pmd
225 * |simian can be combined in a comma separated format.
226 *
227 * @param type
228 * single or multiple types.
229 */
230 public final void setTypes(final String type) {
231 this.types = type;
232 }
233
234 /**
235 * @return Return the destination XML file for the up/down stats.
236 */
237 public final File getOutputXMLfile() {
238 return outputXMLfile;
239 }
240
241 /**
242 * Set the destination XML file for the up/down stats.
243 *
244 * @param outputFile
245 * XML destination file.
246 */
247 public final void setOutputXMLfile(final File outputFile) {
248 outputXMLfile = outputFile;
249 }
250
251 /**
252 * @return the input base file xml document, typically qalab.xml.
253 */
254 public final File getInputFile() {
255 return inputFile;
256 }
257
258 /**
259 * Set the base file xml document, typically qalab.xml.
260 *
261 * @param inputXML
262 * the qalab.xml.
263 */
264 public final void setInputFile(final File inputXML) {
265 inputFile = inputXML;
266 }
267
268 /**
269 * @param wkendadj
270 * true will deduce a further 48 hours to the start window in
271 * order to take into account weekend if the start window fell on
272 * a Saturday or Sunday.
273 */
274 public final void setWeekendAdjustment(final boolean wkendadj) {
275 weekendAdjustment = wkendadj;
276 }
277
278 /**
279 * @return true if system is supposed to adjust for weekend.
280 */
281 public final boolean isWeekendAdjustment() {
282 return weekendAdjustment;
283 }
284
285 /**
286 * @param endTime
287 * The endTimeWindow to set.
288 */
289 public final void setEndTimeWindow(final String endTime) {
290 this.endTimeWindow = endTime.trim();
291
292 if (endTimeWindow.length() < MIN_DATE_SIZE) {
293 endTimeWindow += " 00:00:00";
294 }
295 }
296
297 /**
298 * @return Returns the endTimeWindow.
299 */
300 public final String getEndTimeWindow() {
301 return endTimeWindow;
302 }
303
304 /**
305 * @param startTime
306 * The startTimeWindow to set.
307 */
308 public final void setStartTimeWindow(final String startTime) {
309 this.startTimeWindow = startTime.trim();
310
311 if (startTimeWindow.length() < MIN_DATE_SIZE) {
312 startTimeWindow += " 00:00:00";
313 }
314 }
315
316 /**
317 * @return Returns the startTimeWindow.
318 */
319 public final String getStartTimeWindow() {
320 return startTimeWindow;
321 }
322
323 public Integer getHoursOffsetForLastRun() {
324 return hoursOffsetForLastRun;
325 }
326
327 public void setHoursOffsetForLastRun(Integer hoursOffsetForLastRun) {
328 this.hoursOffsetForLastRun = hoursOffsetForLastRun;
329 }
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346