1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.conient; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.Properties; |
6 |
import java.io.File; |
7 |
import java.io.FileInputStream; |
8 |
import java.io.IOException; |
9 |
import java.io.FileNotFoundException; |
10 |
|
11 |
/** |
12 |
* Provides configuration details to Conient |
13 |
* This class is a Singleton class and |
14 |
* |
15 |
* @author $Author: tdb1 $ |
16 |
* @version $Id: TemplateClass.java,v 1.10 2001/01/18 23:14:39 tdb1 Exp $ |
17 |
*/ |
18 |
public class Configuration { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public static final String REVISION = "$Revision: 1.10 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
/** |
30 |
* Creates and initialises a the Configuration |
31 |
* system for Conient. This calls the private |
32 |
* constructor and ensures this class is a singleton. |
33 |
* |
34 |
* @param configFile the path to the file that this configuration should load |
35 |
*/ |
36 |
public static void initialise(String configFile) { |
37 |
_instance = new Configuration(configFile); |
38 |
} |
39 |
|
40 |
/** |
41 |
* Returns the singleton instance of this |
42 |
* class. |
43 |
* This will throw a runtime exception if it |
44 |
* is called at the wrong time! |
45 |
* |
46 |
* @return the singleton instance |
47 |
*/ |
48 |
public static Configuration getInstance() { |
49 |
if (_instance == null) { |
50 |
throw new RuntimeException("Configuration class requested but HASN'T been initialised!"); |
51 |
} |
52 |
return _instance; |
53 |
} |
54 |
|
55 |
//---CONSTRUCTORS--- |
56 |
|
57 |
/** |
58 |
* The private constructor, ensures that this is a singleton. |
59 |
* Simply reads in the local configuration from the file. |
60 |
* Then sets the current properties to be this configuration. |
61 |
* Later the system will obtain further configuration from the |
62 |
* server. |
63 |
* |
64 |
* @param configFile the path to the file that this configuration should load |
65 |
*/ |
66 |
private Configuration(String configFile) { |
67 |
try { |
68 |
_properties = readFileConfiguration(configFile); |
69 |
} catch (FileNotFoundException e) { |
70 |
// should show a box! |
71 |
System.err.println("ERROR{configuration}: file not found - " + e.getMessage()); |
72 |
} catch (IOException e) { |
73 |
// should show a box! |
74 |
System.err.println("ERROR{configuration}: i/o error - " + e.getMessage()); |
75 |
} |
76 |
} |
77 |
|
78 |
//---PUBLIC METHODS--- |
79 |
|
80 |
//---PRIVATE METHODS--- |
81 |
|
82 |
/** |
83 |
* Reads in the specified file and parses |
84 |
* its properties. |
85 |
* |
86 |
* @param configFile the path of the file to be read |
87 |
* @return the parsed properties |
88 |
*/ |
89 |
private Properties readFileConfiguration(String configFile) throws FileNotFoundException, IOException { |
90 |
File file = new File(configFile); |
91 |
Properties configHolder = new Properties(); |
92 |
configHolder.load(new FileInputStream(file)); |
93 |
return configHolder; |
94 |
} |
95 |
|
96 |
//---ACCESSOR/MUTATOR METHODS--- |
97 |
|
98 |
/** |
99 |
* A wrapper for java.util.Properties.getProperty |
100 |
* When given a key it returns the value of that key |
101 |
* ie, key = value |
102 |
* |
103 |
* @param key the key the value of which is wanted |
104 |
*/ |
105 |
public String getProperty(String key) { |
106 |
return _properties.getProperty(key); |
107 |
} |
108 |
|
109 |
//---ATTRIBUTES--- |
110 |
|
111 |
/** |
112 |
* The current configuration in use |
113 |
*/ |
114 |
private Properties _properties = null; |
115 |
|
116 |
//---STATIC ATTRIBUTES--- |
117 |
|
118 |
/** |
119 |
* The reference to the singleton instance of this class |
120 |
*/ |
121 |
private static Configuration _instance = null; |
122 |
} |