1 package org.jdiseq.util;
2
3 import java.util.Properties;
4 import java.util.MissingResourceException;
5 import java.io.InputStream;
6 import java.io.IOException;
7 import java.io.FileInputStream;
8 import java.io.BufferedInputStream;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 /***
14 * Class for fetching all application settings.
15 * <p>
16 * @author Trond Andersen <trondandersen@c2i.net>
17 * @version $Id: SettingUtil.java,v 1.2 2003/08/17 13:15:53 trondandersen Exp $
18 * @since version 1.0
19 */
20 public class SettingUtil {
21 private static final Log log = LogFactory.getLog(SettingUtil.class);
22 /*** Name of the property file containing all application settings */
23 private static final String SETTINGS_FILE_NAME = "settings.properties";
24 /*** Singleton instance */
25 private static SettingUtil instance;
26 /*** Application settings */
27 private Properties settings;
28
29
30 /***
31 * Singleton implementation
32 * @return Singleton instance of {@link SettingUtil}
33 */
34 public static synchronized SettingUtil getInstance() {
35 if (instance == null) {
36 instance = new SettingUtil();
37 }
38 return instance;
39 }
40
41 /***
42 * Retrieves the application and system setting.
43 * @param settingKey used to look up settings
44 * @return the setting for the given key
45 * @throws RuntimeException if setting doesn't exists
46 * @precondition settingKey != null
47 * @precondition settingKey.length > 0
48 */
49 public String getSetting(final String settingKey) {
50 return getProperty(settingKey);
51 }
52
53 /***
54 * @param settingKey key to setting defined in property file
55 * @param defaultValue default value if the key isn't found
56 * @return String representation of the property
57 */
58 public String getSetting(final String settingKey, final String defaultValue) {
59 return settings.getProperty(settingKey, defaultValue);
60 }
61
62 /***
63 * @param settingKey key to setting defined in property file
64 * @return int representation of the value
65 * @throws NumberFormatException Failed to parse an integer value
66 */
67 public int getIntSetting(final String settingKey) {
68 return Integer.parseInt(getProperty(settingKey));
69 }
70
71 /***
72 * @param settingKey key to setting defined in property file
73 * @return boolean representation of the value
74 *
75 */
76 public boolean getBooleanSetting(final String settingKey) {
77 String bool = getProperty(settingKey);
78 if (bool.equalsIgnoreCase("true") || bool.equalsIgnoreCase("false")) {
79 return Boolean.valueOf(bool).booleanValue();
80 } else {
81 throw new RuntimeException("Boolean property written wrongly");
82 }
83 }
84
85 /***
86 * Creates an <code>InputStream</code> to read the property file containing all the
87 * settings defined for the applicaton.
88 * @return Stream to the file containing all the settings
89 * @throws IOException when reading the file fails.
90 */
91 protected InputStream getSettingStream() throws IOException {
92 InputStream is = new FileInputStream(SETTINGS_FILE_NAME);
93
94 return new BufferedInputStream(is);
95 }
96
97 /***
98 * constructor to load configuration files
99 */
100 SettingUtil() {
101 settings = new Properties();
102 try {
103 settings.load(getSettingStream());
104 } catch (IOException e) {
105 log.info("Failed to load the setting from filesystem. Trying the classpath instead");
106 //Try loading from classpath
107 try {
108 settings.load(this.getClass().getClassLoader().getResourceAsStream(SETTINGS_FILE_NAME));
109 } catch (IOException ioEx) {
110 log.error("Failed to load setting from both filesystem and classpath");
111 throw new MissingResourceException(e.getMessage(), "Settings property file", SETTINGS_FILE_NAME);
112 }
113 }
114 }
115
116 private String getProperty(String key) {
117 assert (key != null);
118 assert (key.length() > 0);
119 String value = settings.getProperty(key);
120 if (value == null || value.length() <= 0) {
121 throw new RuntimeException("Setting key \'" + key + "\' isn't defined");
122 }
123 return value;
124 }
125 }
This page was automatically generated by Maven