-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.java
More file actions
58 lines (49 loc) · 1.78 KB
/
Configuration.java
File metadata and controls
58 lines (49 loc) · 1.78 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package unimelb.bitbox.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
/**
* Simple wrapper for using Properties(). Example:
* <pre>
* {@code
* int port = Integer.parseInt(Configuration.getConfigurationValue("port"));
* String[] peers = Configuration.getConfigurationValue("peers").split(",");
* }
* </pre>
* @author aaron
*
*/
public class Configuration {
private static Logger log = Logger.getLogger(Configuration.class.getName());
// the configuration file is stored in the root of the class path as a .properties file
private static final String CONFIGURATION_FILE = "configuration.properties";
private static final Properties properties;
// use static initializer to read the configuration file when the class is loaded
static {
properties = new Properties();
try (InputStream inputStream = new FileInputStream(CONFIGURATION_FILE)) {
properties.load(inputStream);
} catch (IOException e) {
log.warning("Could not read file " + CONFIGURATION_FILE);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, String> getConfiguration() {
// ugly workaround to get String as generics
Map temp = properties;
Map<String, String> map = new HashMap<String, String>(temp);
// prevent the returned configuration from being modified
return Collections.unmodifiableMap(map);
}
public static String getConfigurationValue(String key) {
return properties.getProperty(key);
}
// private constructor to prevent initialization
private Configuration() {
}
}