1 | |
package net.sf.jolene.util; |
2 | |
|
3 | |
import net.sf.jolene.constants.Prefs; |
4 | |
import org.apache.log4j.LogManager; |
5 | |
import org.apache.log4j.Logger; |
6 | |
|
7 | |
import java.io.IOException; |
8 | |
import java.io.InputStream; |
9 | |
import java.net.URL; |
10 | |
import java.util.Iterator; |
11 | |
import java.util.Properties; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
public class PrefsReader { |
20 | |
|
21 | 0 | private static final Logger log = LogManager.getLogger(PrefsReader.class); |
22 | |
|
23 | 0 | private PrefsReader() { |
24 | 0 | } |
25 | |
|
26 | |
public static void init() { |
27 | 0 | Object o = new Object(); |
28 | 0 | synchronized (o) { |
29 | 0 | Properties properties = new Properties(); |
30 | 0 | InputStream input = null; |
31 | |
try { |
32 | 0 | URL resource = PrefsReader.class.getResource("/jolene.properties"); |
33 | 0 | if (resource == null) { |
34 | 0 | log.info("jolene.properties not found. Default prefs will be used"); |
35 | 0 | return; |
36 | |
} |
37 | |
|
38 | 0 | log.info("Reading jolene.properties from " + resource.getFile()); |
39 | 0 | input = PrefsReader.class.getResourceAsStream("/jolene.properties"); |
40 | 0 | properties.load(input); |
41 | 0 | } catch (IOException e) { |
42 | 0 | log.warn(e.getMessage(), e); |
43 | 0 | } |
44 | |
|
45 | 0 | Iterator it = properties.keySet().iterator(); |
46 | 0 | while (it.hasNext()) { |
47 | |
Object key; |
48 | 0 | key = it.next(); |
49 | |
|
50 | 0 | Prefs pref = Prefs.valueOf(key.toString()); |
51 | 0 | if (pref != null) { |
52 | 0 | log.info("Assigning jolene preference " + key + " to " + properties.get(key)); |
53 | 0 | pref.setValue(properties.get(key)); |
54 | |
} else { |
55 | 0 | log.warn("Unknown PrefsReader preference in properties file: " + key); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | 0 | if (input != null) { |
60 | |
try { |
61 | 0 | input.close(); |
62 | 0 | } catch (IOException e) { |
63 | 0 | log.warn(e.getMessage()); |
64 | 0 | } |
65 | |
} |
66 | |
|
67 | 0 | } |
68 | 0 | } |
69 | |
} |