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.util;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.io.FileNotFoundException;
44  import java.io.InputStream;
45  
46  import java.text.SimpleDateFormat;
47  import java.util.Calendar;
48  import java.util.Locale;
49  
50  /**
51   * Some handy utils for the QALab maven 2 plugin.
52   * @author <a href="http://www.davesag.com">Dave Sag</a>.
53   */
54  public final class Utils {
55  
56      /**
57       * private contructor as this is a utils class.
58       */
59      private Utils() {
60          // does nothing.
61      }
62  
63      /**
64       * performs a simple check that the file is real and readable.
65       * @param aFile the file to check
66       * @param aParamName the name of the input param - used for error reporting.
67       * @throws IOException if the file was null or not readable.
68       */
69      public static void checkFile(final File aFile, final String aParamName)
70               throws IOException {
71          if (aFile == null) {
72              throw new FileNotFoundException(aParamName
73                      + " is mandatory");
74          }
75  
76          if (!aFile.exists()) {
77              throw new FileNotFoundException(aFile
78                      + " does not exist.");
79          }
80  
81          if (!aFile.canRead()) {
82              throw new IOException("Unable to read from '"
83                      + aFile + "' while processing value for param "
84                      + aParamName);
85          }
86  
87      }
88  
89      /**
90       * extract an input stream from the supplied resource file. The resource is
91       * presumed to be somewhere on the classpath for this app.
92       * @param aResourcePath the path to the resource we need the
93       *        actual system path for. Must not be empty or null.
94       * @return an input stream pointing at the named resource.
95       * @throws IOException if the resource path was not readable.
96       */
97      public static InputStream extractAsInputStream(final String aResourcePath)
98              throws IOException {
99  
100         assert aResourcePath != null : "The supplied path was null";
101         assert !"".equals(aResourcePath) : "The supplied path was empty";
102 
103         final ClassLoader l = Utils.class.getClassLoader();
104         final InputStream res = l.getResourceAsStream(aResourcePath);
105         if (res == null) {
106             throw new FileNotFoundException(
107                     "could not find resource at " + aResourcePath);
108         }
109         return res;
110     }
111 
112     /**
113      * Generate a yyyy-MM-dd date based on now less the number of hours
114      * to offset.
115      * @param anOffset the number of hours to subtract from now.
116      * @return a yyyy-MM-dd formatted string representing now less the offset.
117      */
118     public static String formatDateBasedOnOffset(final String anOffset) {
119         assert anOffset != null : "offset supplied must not be null";
120         final int hours = Integer.parseInt(anOffset);
121         assert hours > 0 : "offset supplied must be positive";
122 
123         final Calendar c = Calendar.getInstance();
124         c.add(Calendar.HOUR, -hours);
125 
126         final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
127                 Locale.getDefault());
128         return df.format(c.getTime());
129     }
130 }
131 /*
132  *                   ObjectLab is sponsoring QALab
133  * 
134  * Based in London, we are world leaders in the design and development 
135  * of bespoke applications for the securities financing markets.
136  * 
137  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
138  *           ___  _     _           _   _          _
139  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
140  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
141  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
142  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
143  *                   |__/
144  *
145  *                     www.ObjectLab.co.uk
146  */