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.3 2001/02/04 23:45:10 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.3 $"; |
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 |
/** |
79 |
* This routine asks the server for all configuration |
80 |
* options that are needed to be obtained from the |
81 |
* server. It takes hooks to the I/O streams for the |
82 |
* control link socket in order that it can request |
83 |
* the attributes. It is assumed that the connection |
84 |
* has been negotiated to a stage that the server is |
85 |
* ready to server attributes. Once this method returns |
86 |
* the calling class should tell the server that configuration |
87 |
* has terminated. |
88 |
* |
89 |
* @param in the input stream of the control link |
90 |
* @param out the output stream of the control link |
91 |
* @throws IOException if there is an error communicating |
92 |
*/ |
93 |
public void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException { |
94 |
String response = null; |
95 |
out.println("Host.UDPUpdateTime"); |
96 |
out.flush(); |
97 |
response = in.readLine(); |
98 |
|
99 |
if (!response.equals("ERROR")) { |
100 |
_properties.setProperty("Host.UDPUpdateTime", response); |
101 |
} |
102 |
out.println("Host.TCPUpdateTime"); |
103 |
out.flush(); |
104 |
response = in.readLine(); |
105 |
|
106 |
if (!response.equals("ERROR")) { |
107 |
_properties.setProperty("Host.TCPUpdateTime", response); |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* Tells the configuration class that it should |
113 |
* perform user re-configuration through displaying |
114 |
* a gui. This passes control to a dialog to |
115 |
* handle re-configuration. Basically this is |
116 |
* the nicer alternative to hacking the config |
117 |
* file. |
118 |
* The ConientConfiguration class is responsible |
119 |
* for handling the re-configuration. |
120 |
*/ |
121 |
public void GUIReconfiguration() { |
122 |
//ConientConfiguration conf = new ConientConfiguration(); |
123 |
} |
124 |
//---PRIVATE METHODS--- |
125 |
|
126 |
/** |
127 |
* Reads in the specified file and parses |
128 |
* its properties. |
129 |
* |
130 |
* @param configFile the path of the file to be read |
131 |
* @return the parsed properties |
132 |
*/ |
133 |
private Properties readFileConfiguration(String configFile) throws FileNotFoundException, IOException { |
134 |
File file = new File(configFile); |
135 |
Properties configHolder = new Properties(); |
136 |
configHolder.load(new FileInputStream(file)); |
137 |
return configHolder; |
138 |
} |
139 |
|
140 |
//---ACCESSOR/MUTATOR METHODS--- |
141 |
|
142 |
/** |
143 |
* A wrapper for java.util.Properties.getProperty |
144 |
* When given a key it returns the value of that key |
145 |
* ie, key = value |
146 |
* |
147 |
* @param key the key the value of which is wanted |
148 |
*/ |
149 |
public String getProperty(String key) { |
150 |
return _properties.getProperty(key); |
151 |
} |
152 |
|
153 |
//---ATTRIBUTES--- |
154 |
|
155 |
/** |
156 |
* The current configuration in use |
157 |
*/ |
158 |
private Properties _properties = null; |
159 |
|
160 |
//---STATIC ATTRIBUTES--- |
161 |
|
162 |
/** |
163 |
* The reference to the singleton instance of this class |
164 |
*/ |
165 |
private static Configuration _instance = null; |
166 |
} |