1
2
3
4
5
6
7 package org.astrogrid.config;
8
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.net.URL;
13
14 /***
15 * A static singleton for easy one-line access to an application-global
16 * configuration file.
17 * <p>
18 * Use like this:
19 * SimpleConfig.getSingleton().getProperty(key);
20 * <p>
21 * A static singleton so that all packages have easy access to the *same*
22 * configuration set in one runtime environment, although this may be loaded
23 * from several files. So although this configuration is a common access point, it can be used to
24 * access several configuration files. Any application can call the 'load'
25 * methods and properties will be loaded from the given file/url/etc.
26 * <p/>
27 * This does mean that packages using the configuration file must make sure their
28 * keys are unique. The most reliable way to do this is to prefix each key with
29 * the package name (ie the code namespace) but this doesn't produce very nice
30 * property files for humans to edit.
31 * <p/>
32 * @todo work out a nice way of ensuring keys are unique or throw exceptions if not.
33 * <p/>
34 * Not entirely happy with the mixed static/instanceness of this - maybe ought
35 * to break it into two but don't see it's necessary yet...
36 *
37 * @author M Hill
38 */
39
40 public abstract class SimpleConfig
41 {
42 /*** Singleton Instance used to provide load methods */
43 private static final Config instance = ConfigFactory.getConfig(SimpleConfig.class);
44
45 /***
46 * Returns the instance for applications to work with - saves having to
47 * mirror-implement all the instance methods here */
48 public static Config getSingleton() {
49 return instance;
50 }
51
52 /***
53 * Static access to load from a file at the given filepath
54 */
55 public static void load(String filepath) throws IOException
56 {
57 File f = new File(filepath);
58 load(f.toURL());
59 }
60
61 /***
62 * Static access to load from a url
63 */
64 public static void load(URL url) throws IOException
65 {
66 instance.loadFromUrl(url);
67 }
68
69 /***
70 * Static access to the instance method
71 */
72 public static String loadedFrom()
73 {
74 return instance.loadedFrom();
75 }
76
77 /***
78 * Sets the given property - useful for tests.
79 */
80 public static void setProperty(String key, String value)
81 {
82 instance.setProperty(key, value);
83 }
84
85 /***
86 * Returns the property value of the given key; throws an exception if the
87 * key is not set
88 */
89 public static String getProperty(String key)
90 {
91 return instance.getString(key);
92 }
93
94 /***
95 * Returns the property value of the given key, or the given default if
96 * the key is not found or the properties file has not been created (eg
97 * this has not been initialised)
98 */
99 public static String getProperty(String key, Object defaultValue)
100 {
101 if (defaultValue == null) {
102 return instance.getString(key, null);
103 }
104 else {
105 return instance.getString(key, defaultValue.toString());
106 }
107 }
108
109 }
110