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