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