01: // Copyright (C) 2006 by SolutionsIQ. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.util;
04:
05: import java.util.Properties;
06:
07: public class PropertiesUtil {
08: private static String NOT_FOUND_VALUE = "No value found for the requested property key.";
09:
10: public static String getPropertyOrFail(String propertyKey) {
11: return getPropertyOrFail(
12: propertyKey,
13: "Please specify a value for property \""
14: + propertyKey
15: + "\". Property values can be specified as environment variables, using the -D runtime directive, or in the storytestiq.properties property file.");
16: }
17:
18: public static String getPropertyOrFail(String propertyKey,
19: String errorMessage) {
20: String propertyValue = getProperty(propertyKey, NOT_FOUND_VALUE);
21: if (propertyValue == NOT_FOUND_VALUE) {
22: throw new IllegalArgumentException(errorMessage);
23: }
24: return propertyValue;
25: }
26:
27: public static String getProperty(String propertyKey) {
28: return getProperty(propertyKey, "");
29: }
30:
31: public static String getProperty(String propertyKey,
32: String defaultValue) {
33: try {
34: String propertyValue = System.getProperty(propertyKey);
35: if (propertyValue == null) {
36: Properties props = new Properties();
37: props.load(PropertiesUtil.class.getClassLoader()
38: .getResourceAsStream("storytestiq.properties"));
39: propertyValue = props.getProperty(propertyKey);
40: if (propertyValue == null) {
41: return defaultValue;
42: }
43: }
44: return propertyValue;
45: } catch (Exception e) {
46: return defaultValue;
47: }
48:
49: }
50: }
|