1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.testtools.common;
17
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30
31
32
33
34
35
36
37
38
39 public class PropertiesUtils {
40
41
42
43
44
45
46
47
48
49
50 public Properties loadProperties(InputStream inputStream) throws IOException {
51 Properties props = new Properties();
52
53 if(inputStream != null) {
54 props.load(inputStream);
55 }
56
57 return props;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
74 Properties props = null;
75 InputStream in = null;
76 if(fileLocation != null) {
77 try {
78 in = new FileInputStream(fileLocation);
79 } catch (FileNotFoundException fio) {
80 System.out.println(fio.getMessage() + " trying to read as resource.");
81 if (resourceLocation != null) {
82 System.out.println("Trying to read as " + resourceLocation+ " as a resource.");
83 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
84 }
85 }
86 } else {
87 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
88 if (in == null) {
89 System.out.println("Unable to read " + fileLocation + " as a file or " + resourceLocation + " as a resource stream");
90 }
91 }
92 if(in != null) {
93 props = loadProperties(in);
94 in.close();
95 }
96
97 return props;
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 public Properties loadProperties(String location) throws IOException {
112 Properties props = null;
113 InputStream in = null;
114 try {
115 in = new FileInputStream(location);
116 } catch (FileNotFoundException fio) {
117 System.out.println(fio.getMessage() + " trying to read as resource.");
118 in = getClass().getClassLoader().getResourceAsStream(location);
119 if (in == null) {
120 System.out.println("Unable to read " + location + " as a resource stream");
121 }
122 }
123 if(in != null) {
124 props = loadProperties(in);
125 in.close();
126 }
127
128 return props;
129 }
130
131 public Properties loadPropertiesWithSystemAndOverrides(String location) throws IOException {
132 Properties properties = loadProperties(location);
133 return systemPropertiesAndOverride(properties);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 public Properties loadPropertiesWithSystemAndOverridesIntoSystem(String location) throws IOException {
148 Properties properties = loadProperties(location);
149 properties = systemPropertiesAndOverride(properties);
150
151 Iterator propKeys = properties.keySet().iterator();
152 while (propKeys.hasNext()) {
153 String key = (String)propKeys.next();
154 if (System.getProperty(key) == null) {
155 System.setProperty(key, properties.getProperty(key));
156 }
157 }
158 return properties;
159 }
160
161 public Properties loadPropertiesWithSystemOverrides(String location) throws IOException {
162 Properties properties = loadProperties(location);
163 return systemPropertiesOverride(properties);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 public Properties loadPropertiesWithSystemOverrides(InputStream inputStream) throws IOException {
178 Properties props = loadProperties(inputStream);
179 props = systemPropertiesOverride(props);
180 return props;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public Properties loadPropertiesWithSystemOverridesAndNumberedPropertiesToList(InputStream inputStream) throws IOException {
197 Properties props = loadProperties(inputStream);
198 props = systemPropertiesOverride(props);
199 props = transformNumberedPropertiesToList(props);
200 return props;
201 }
202
203
204
205
206
207
208
209
210
211 public String removeNumber(final String numberedKey) {
212 String unnumberedKey = numberedKey;
213 int firstNumberIndex = unnumberedKey.length() - 1;
214 while (Character.isDigit(unnumberedKey.charAt(firstNumberIndex))) {
215 firstNumberIndex--;
216 }
217 unnumberedKey = unnumberedKey.substring(0, firstNumberIndex + 1);
218
219 return unnumberedKey;
220 }
221
222 public Properties systemPropertiesAndOverride(Properties props) {
223 return systemPropertiesAndOverride(props, null);
224 }
225
226
227
228
229
230
231
232
233 public Properties systemPropertiesOverride(Properties props) {
234 return systemPropertiesOverride(props, null);
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 public Properties systemPropertiesAndOverride(Properties props, String arg) {
251 PropertiesUtils propUtils = new PropertiesUtils();
252 props = propUtils.systemPropertiesOverride(props, arg);
253
254 Iterator iter = System.getProperties().stringPropertyNames().iterator();
255 while (iter.hasNext()) {
256 String key = (String) iter.next();
257 if (arg == null || arg.equals("")) {
258 if (!props.contains(key)) {
259 props.setProperty(key, System.getProperty(key));
260 }
261 } else {
262 if (key.startsWith(arg) && !props.contains(key)) {
263 props.setProperty(key, System.getProperty(key));
264 }
265 }
266 }
267 return props;
268 }
269
270
271
272
273
274
275
276
277
278
279
280 public Properties systemPropertiesOverride(Properties props, String arg) {
281 Enumeration<?> names = props.propertyNames();
282 Object nameObject;
283 String key;
284 while (names.hasMoreElements()) {
285
286 nameObject = names.nextElement();
287 if (nameObject instanceof String) {
288
289 key = (String)nameObject;
290 if (arg == null || arg.isEmpty()) {
291 props.setProperty(key, System.getProperty(key, props.getProperty(key)));
292 } else {
293 props.setProperty(key, System.getProperty(arg + "." + key, props.getProperty(key)));
294 }
295 }
296 }
297 return props;
298 }
299
300
301
302
303
304
305
306
307
308 public Properties transformNumberedPropertiesToList(Properties props) {
309 String key = null;
310 String unnumberedKey = null;
311 List<String> keyList = null;
312 List<String> removeKeys = new LinkedList<String>();
313
314
315 Iterator keys = props.keySet().iterator();
316 Map<String, List<String>> keysLists = new HashMap<String, List<String>>();
317 while (keys.hasNext()) {
318 key = (String)keys.next();
319 if (Character.isDigit(key.charAt(key.length()-1))) {
320 unnumberedKey = removeNumber(key);
321 if (keysLists.get(unnumberedKey) == null) {
322 keyList = new ArrayList<String>();
323 keyList.add(props.getProperty(key));
324 keysLists.put(unnumberedKey, keyList);
325 removeKeys.add(key);
326 } else {
327 keyList = keysLists.get(unnumberedKey);
328 keyList.add(props.getProperty(key));
329 keysLists.put(unnumberedKey, keyList);
330 removeKeys.add(key);
331 }
332 }
333 }
334
335
336 Iterator removeKey = removeKeys.iterator();
337 while (removeKey.hasNext()) {
338 key = (String)removeKey.next();
339 props.remove(key);
340 }
341
342
343 Iterator newKeys = keysLists.keySet().iterator();
344 String newKey = null;
345 while (newKeys.hasNext()) {
346 newKey = (String)newKeys.next();
347 props.put(newKey + "s", keysLists.get(newKey));
348 }
349 return props;
350 }
351 }