View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //
3   //                  ObjectLab is sponsoring QALab
4   //
5   // Based in London, we are world leaders in the design and development
6   // of bespoke applications for the Securities Financing markets.
7   //
8   // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
9   //           ___  _     _           _   _          _
10  //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
11  //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
12  //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
13  //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
14  //                   |__/
15  //
16  //                   http://www.ObjectLab.co.uk
17  // ---------------------------------------------------------------------------
18  //
19  //QALab is released under the GNU General Public License.
20  //
21  //QALab: Collects QA Statistics from your build over time.
22  //2005+, ObjectLab Ltd
23  //
24  //This library is free software; you can redistribute it and/or
25  //modify it under the terms of the GNU General Public
26  //License as published by the Free Software Foundation; either
27  //version 2.1 of the License, or (at your option) any later version.
28  //
29  //This library is distributed in the hope that it will be useful,
30  //but WITHOUT ANY WARRANTY; without even the implied warranty of
31  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  //General Public License for more details.
33  //
34  //You should have received a copy of the GNU General Public
35  //License along with this library; if not, write to the Free Software
36  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  //
38  ////////////////////////////////////////////////////////////////////////////////
39  package net.objectlab.qalab.m2;
40  
41  import net.objectlab.qalab.parser.BuildStatMoverHandler;
42  import net.objectlab.qalab.util.QALabTags;
43  import net.objectlab.qalab.util.TaskLogger;
44  import net.objectlab.qalab.m2.util.Utils;
45  import net.objectlab.qalab.m2.util.Maven2TaskLogger;
46  
47  import org.apache.maven.plugin.AbstractMojo;
48  import org.apache.maven.plugin.MojoExecutionException;
49  
50  import org.xml.sax.InputSource;
51  import org.xml.sax.SAXException;
52  
53  import javax.xml.parsers.ParserConfigurationException;
54  
55  import java.io.File;
56  import java.io.FileReader;
57  import java.io.FileWriter;
58  import java.io.IOException;
59  
60  import java.text.ParseException;
61  
62  /**
63   * Goal to create the mover XML file given the correct parameters. The movers
64   * report shows what has changed over the last "x" hours.
65   * 
66   * @author <a href="http://www.davesag.com">Dave Sag</a>.
67   * @goal movers
68   * @phase deploy
69   */
70  public class BuildStatMoversMojo extends AbstractMojo {
71  
72      // ~ Static final fields ---------------------------------------------------
73      /** The size of the string based on ISO 8601 date format. */
74      private static final int MIN_DATE_SIZE = 18;
75  
76      /**
77       * The qalabFile, typically qalab.xml.
78       * 
79       * @parameter expression="${project.basedir}/qalab.xml"
80       */
81      private File qalabFile = null;
82  
83      /**
84       * The number of hours to define the time window from now.
85       * 
86       * @parameter default-value="54"
87       */
88      private String startTimeHoursOffset = null;
89  
90      /**
91       * Statistic types to be taken into account
92       * (checkstyle|findbugs|pmd|simian|pmd-cpd|cobertura-line|cobertura-branch).
93       * Or comma separated combinations. Default is checkstyle, pmd and findbugs. Please note
94       * that there is no point of adding pmd-cpd or Simian as no statistics are
95       * kept at file level for those.
96       * 
97       * @parameter default-value="checkstyle,pmd,findbugs,cobertura-line,cobertura-branch"
98       */
99      private String types = "checkstyle,pmd,findbugs,cobertura-line,cobertura-branch";
100 
101     /**
102      * The output XML file with ups and downs.
103      * 
104      * @parameter expression="${project.build.directory}/qalab/qalab-movers.xml"
105      */
106     private File qalabMoversOutputFile;
107 
108     /**
109      * If true then any debug logging output will be suppressed.
110      * 
111      * @parameter default-value=false
112      */
113     private boolean quiet = false;
114 
115     /**
116      * if true, adjust for weekend ie add 48h if start time falls on a weekend.
117      * 
118      * @parameter default-value=true
119      */
120     private boolean weekendAdjustment = true;
121 
122     /**
123      * Start Time Window (instead of using startTimeHoursOffset). 
124      * "parameter
125      */
126     private String startTimeWindow = null;
127 
128     /**
129      * End Time Window (instead of assuming "now"). 
130      * "parameter
131      */
132     private String endTimeWindow = null;
133 
134     /**
135      * Method invoked by the maven 2 framework to execute the compilation of
136      * statistics movers withing the time window.
137      * 
138      * @throws MojoExecutionException
139      *             if anything went wrong.
140      */
141     public final void execute() throws MojoExecutionException {
142         validate();
143 
144         if (!quiet) {
145             getLog().info("QALAB Movers ==> qalabFile='" + qalabFile.getPath());
146             getLog().info("type=" + types);
147             getLog().info("output=" + qalabMoversOutputFile.getPath());
148             getLog().info("adjustForWeekend=" + weekendAdjustment);
149             getLog().info("startTimeOffset=" + startTimeHoursOffset);
150             getLog().info("startTimeWindow=" + startTimeWindow);
151             getLog().info("endTimeWindow=" + endTimeWindow);
152         }
153 
154         try {
155             final TaskLogger logger = new Maven2TaskLogger(this);
156 
157             final BuildStatMoverHandler handler = new BuildStatMoverHandler(new InputSource(new FileReader(qalabFile)), types,
158                     new FileWriter(qalabMoversOutputFile), weekendAdjustment, logger, quiet);
159 
160             if (startTimeWindow != null) {
161                 handler.setStartTimeWindow(QALabTags.DEFAULT_DATETIME_FORMAT.parse(fixTime(startTimeWindow)));
162             }
163 
164             if (endTimeWindow != null) {
165                 handler.setEndTimeWindow(QALabTags.DEFAULT_DATETIME_FORMAT.parse(fixTime(endTimeWindow)));
166             }
167 
168             if (startTimeHoursOffset != null) {
169                 handler.setOffsetHours(startTimeHoursOffset);
170             }
171 
172             handler.process();
173         } catch (ParserConfigurationException pcex) {
174             getLog().error(pcex.toString());
175         } catch (SAXException sex) {
176             getLog().error(sex.toString());
177         } catch (IOException ioex) {
178             getLog().error(ioex.toString());
179         } catch (ParseException pex) {
180             throw new MojoExecutionException("Date Parse issue", pex);
181         }
182     }
183 
184     /**
185      * Do some basic parameter validation.
186      * 
187      * @throws MojoExecutionException
188      *             if anything goes wrong.
189      */
190     private void validate() throws MojoExecutionException {
191         try {
192             Utils.checkFile(qalabFile, "qalabFile");
193         } catch (IOException ioex) {
194             throw new MojoExecutionException(ioex.getMessage(), ioex);
195         }
196 
197         if ((startTimeHoursOffset == null) && (startTimeWindow == null)) {
198             throw new MojoExecutionException("Please set startTimeHoursOffset or startTimeWindow.");
199         }
200 
201         if ((startTimeHoursOffset != null) && (startTimeWindow != null)) {
202             throw new MojoExecutionException("Use startTimeHoursOffset OR startTimeWindow, not both.");
203         }
204 
205         final File dir = qalabMoversOutputFile.getParentFile();
206         if (!dir.exists()) {
207             dir.mkdirs();
208         }
209     }
210 
211     /**
212      * @param aTime
213      *            The time string to adjust.
214      * @return the adjusted time string - now with full hh:mm:ss
215      */
216     private String fixTime(final String aTime) {
217         String result = aTime.trim();
218 
219         if (result.length() < MIN_DATE_SIZE) {
220             result += " 00:00:00";
221         }
222 
223         return result;
224     }
225 
226     /**
227      * @return the endTimeWindow
228      */
229     public String getEndTimeWindow() {
230         return endTimeWindow;
231     }
232 
233     /**
234      * @param endTimeWindow the endTimeWindow to set
235      */
236     public void setEndTimeWindow(String endTimeWindow) {
237         this.endTimeWindow = endTimeWindow;
238     }
239 
240     /**
241      * @return the qalabFile
242      */
243     public File getQalabFile() {
244         return qalabFile;
245     }
246 
247     /**
248      * @param qalabFile the qalabFile to set
249      */
250     public void setQalabFile(File qalabFile) {
251         this.qalabFile = qalabFile;
252     }
253 
254     /**
255      * @return the qalabMoversOutputFile
256      */
257     public File getQalabMoversOutputFile() {
258         return qalabMoversOutputFile;
259     }
260 
261     /**
262      * @param qalabMoversOutputFile the qalabMoversOutputFile to set
263      */
264     public void setQalabMoversOutputFile(File qalabMoversOutputFile) {
265         this.qalabMoversOutputFile = qalabMoversOutputFile;
266     }
267 
268     /**
269      * @return the quiet
270      */
271     public boolean isQuiet() {
272         return quiet;
273     }
274 
275     /**
276      * @param quiet the quiet to set
277      */
278     public void setQuiet(boolean quiet) {
279         this.quiet = quiet;
280     }
281 
282     /**
283      * @return the startTimeHoursOffset
284      */
285     public String getStartTimeHoursOffset() {
286         return startTimeHoursOffset;
287     }
288 
289     /**
290      * @param startTimeHoursOffset the startTimeHoursOffset to set
291      */
292     public void setStartTimeHoursOffset(String startTimeHoursOffset) {
293         this.startTimeHoursOffset = startTimeHoursOffset;
294     }
295 
296     /**
297      * @return the startTimeWindow
298      */
299     public String getStartTimeWindow() {
300         return startTimeWindow;
301     }
302 
303     /**
304      * @param startTimeWindow the startTimeWindow to set
305      */
306     public void setStartTimeWindow(String startTimeWindow) {
307         this.startTimeWindow = startTimeWindow;
308     }
309 
310     /**
311      * @return the types
312      */
313     public String getTypes() {
314         return types;
315     }
316 
317     /**
318      * @param types the types to set
319      */
320     public void setTypes(String types) {
321         this.types = types;
322     }
323 
324     /**
325      * @return the weekendAdjustment
326      */
327     public boolean isWeekendAdjustment() {
328         return weekendAdjustment;
329     }
330 
331     /**
332      * @param weekendAdjustment the weekendAdjustment to set
333      */
334     public void setWeekendAdjustment(boolean weekendAdjustment) {
335         this.weekendAdjustment = weekendAdjustment;
336     }
337 }
338 /*
339  *                   ObjectLab is sponsoring QALab
340  * 
341  * Based in London, we are world leaders in the design and development 
342  * of bespoke applications for the securities financing markets.
343  * 
344  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
345  *           ___  _     _           _   _          _
346  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
347  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
348  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
349  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
350  *                   |__/
351  *
352  *                     www.ObjectLab.co.uk
353  */