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.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146