1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.conient; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.3 |
import javax.swing.JOptionPane; |
6 |
ajm |
1.5 |
import javax.swing.SwingUtilities; |
7 |
|
|
import java.awt.Frame; |
8 |
ajm |
1.1 |
import java.util.Properties; |
9 |
ajm |
1.2 |
import java.io.*; |
10 |
ajm |
1.1 |
|
11 |
|
|
/** |
12 |
|
|
* Provides configuration details to Conient |
13 |
|
|
* This class is a Singleton class and |
14 |
|
|
* |
15 |
ajm |
1.2 |
* @author $Author: ajm4 $ |
16 |
ajm |
1.5 |
* @version $Id: Configuration.java,v 1.4 2001/02/16 16:24:08 ajm4 Exp $ |
17 |
ajm |
1.1 |
*/ |
18 |
|
|
public class Configuration { |
19 |
|
|
|
20 |
|
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* The current CVS revision of this class |
24 |
|
|
*/ |
25 |
ajm |
1.5 |
public static final String REVISION = "$Revision: 1.4 $"; |
26 |
ajm |
1.1 |
|
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 |
ajm |
1.3 |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + configFile, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
71 |
|
|
System.exit(1); |
72 |
ajm |
1.1 |
} catch (IOException e) { |
73 |
ajm |
1.3 |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + e.getMessage(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
74 |
|
|
System.exit(1); |
75 |
ajm |
1.1 |
} |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
//---PUBLIC METHODS--- |
79 |
|
|
|
80 |
ajm |
1.4 |
/** |
81 |
|
|
* This routine asks the server for all configuration |
82 |
|
|
* options that are needed to be obtained from the |
83 |
|
|
* server. It takes hooks to the I/O streams for the |
84 |
|
|
* control link socket in order that it can request |
85 |
|
|
* the attributes. It is assumed that the connection |
86 |
|
|
* has been negotiated to a stage that the server is |
87 |
|
|
* ready to server attributes. Once this method returns |
88 |
|
|
* the calling class should tell the server that configuration |
89 |
|
|
* has terminated. |
90 |
|
|
* |
91 |
|
|
* @param in the input stream of the control link |
92 |
|
|
* @param out the output stream of the control link |
93 |
|
|
* @throws IOException if there is an error communicating |
94 |
|
|
*/ |
95 |
ajm |
1.2 |
public void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException { |
96 |
|
|
String response = null; |
97 |
|
|
out.println("Host.UDPUpdateTime"); |
98 |
|
|
out.flush(); |
99 |
|
|
response = in.readLine(); |
100 |
|
|
|
101 |
|
|
if (!response.equals("ERROR")) { |
102 |
|
|
_properties.setProperty("Host.UDPUpdateTime", response); |
103 |
|
|
} |
104 |
|
|
out.println("Host.TCPUpdateTime"); |
105 |
|
|
out.flush(); |
106 |
|
|
response = in.readLine(); |
107 |
|
|
|
108 |
|
|
if (!response.equals("ERROR")) { |
109 |
|
|
_properties.setProperty("Host.TCPUpdateTime", response); |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
ajm |
1.4 |
/** |
114 |
|
|
* Tells the configuration class that it should |
115 |
|
|
* perform user re-configuration through displaying |
116 |
|
|
* a gui. This passes control to a dialog to |
117 |
|
|
* handle re-configuration. Basically this is |
118 |
|
|
* the nicer alternative to hacking the config |
119 |
|
|
* file. |
120 |
|
|
* The ConientConfiguration class is responsible |
121 |
|
|
* for handling the re-configuration. |
122 |
|
|
*/ |
123 |
|
|
public void GUIReconfiguration() { |
124 |
ajm |
1.5 |
ConfigurationDialog conf = new ConfigurationDialog(); |
125 |
ajm |
1.4 |
} |
126 |
ajm |
1.1 |
//---PRIVATE METHODS--- |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* Reads in the specified file and parses |
130 |
|
|
* its properties. |
131 |
|
|
* |
132 |
|
|
* @param configFile the path of the file to be read |
133 |
|
|
* @return the parsed properties |
134 |
|
|
*/ |
135 |
|
|
private Properties readFileConfiguration(String configFile) throws FileNotFoundException, IOException { |
136 |
|
|
File file = new File(configFile); |
137 |
|
|
Properties configHolder = new Properties(); |
138 |
|
|
configHolder.load(new FileInputStream(file)); |
139 |
|
|
return configHolder; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* A wrapper for java.util.Properties.getProperty |
146 |
|
|
* When given a key it returns the value of that key |
147 |
|
|
* ie, key = value |
148 |
|
|
* |
149 |
|
|
* @param key the key the value of which is wanted |
150 |
|
|
*/ |
151 |
|
|
public String getProperty(String key) { |
152 |
|
|
return _properties.getProperty(key); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
//---ATTRIBUTES--- |
156 |
|
|
|
157 |
|
|
/** |
158 |
|
|
* The current configuration in use |
159 |
|
|
*/ |
160 |
|
|
private Properties _properties = null; |
161 |
|
|
|
162 |
|
|
//---STATIC ATTRIBUTES--- |
163 |
|
|
|
164 |
|
|
/** |
165 |
|
|
* The reference to the singleton instance of this class |
166 |
|
|
*/ |
167 |
|
|
private static Configuration _instance = null; |
168 |
|
|
} |