12 |
|
|
13 |
|
/** |
14 |
|
* Provides configuration details to Conient |
15 |
< |
* This class is a Singleton class and |
15 |
> |
* This class is a Singleton class. It handles all the configuration |
16 |
> |
* for Conient. Once a connection has been made, it is told that |
17 |
> |
* it can get configuration from the server, thus allowing other components |
18 |
> |
* access to the servers configuration. It also shows the ConfigurationDialog |
19 |
> |
* on request, which allows local configuration options to be changed. |
20 |
|
* |
21 |
+ |
* It also has support for a variety of methods of saving the config in |
22 |
+ |
* different files and in the default file. |
23 |
+ |
* |
24 |
|
* @author $Author$ |
25 |
|
* @version $Id$ |
26 |
|
*/ |
33 |
|
*/ |
34 |
|
public static final String REVISION = "$Revision$"; |
35 |
|
|
36 |
+ |
public static final String CONFIG_HEADER = |
37 |
+ |
"!!! Conient Local Configuration File !!!\n" + |
38 |
+ |
"#-----------------------------------------\n" + |
39 |
+ |
"# This file was auto-generated by Conient.\n" + |
40 |
+ |
"# It is recommended that you use the GUI\n" + |
41 |
+ |
"# configuration facility to make changes\n" + |
42 |
+ |
"# to this file.\n#"; |
43 |
+ |
|
44 |
|
//---STATIC METHODS--- |
45 |
|
|
46 |
|
/** |
97 |
|
//---PUBLIC METHODS--- |
98 |
|
|
99 |
|
/** |
100 |
< |
* This routine asks the server for all configuration |
101 |
< |
* options that are needed to be obtained from the |
87 |
< |
* server. It takes hooks to the I/O streams for the |
88 |
< |
* control link socket in order that it can request |
89 |
< |
* the attributes. It is assumed that the connection |
90 |
< |
* has been negotiated to a stage that the server is |
91 |
< |
* ready to server attributes. Once this method returns |
92 |
< |
* the calling class should tell the server that configuration |
93 |
< |
* has terminated. |
100 |
> |
* This method is called by any part of the system |
101 |
> |
* that requires configuration from the server. |
102 |
|
* |
103 |
< |
* @param in the input stream of the control link |
104 |
< |
* @param out the output stream of the control link |
105 |
< |
* @throws IOException if there is an error communicating |
103 |
> |
* It should be passed the configuration name: |
104 |
> |
* eg, "Host.raptor.ukc.ac.uk" |
105 |
> |
* and the property required: |
106 |
> |
* eg, "Host.TCPUpdateTime" |
107 |
> |
* |
108 |
> |
* This method will then ask the ConnectionHandler to |
109 |
> |
* talk to the server and return the property. If the server |
110 |
> |
* fails to get the property, or the ConnectionHandler |
111 |
> |
* is not started, this method returns null. |
112 |
|
*/ |
113 |
< |
public void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException { |
114 |
< |
String response = null; |
115 |
< |
out.println("Host.UDPUpdateTime"); |
116 |
< |
out.flush(); |
103 |
< |
response = in.readLine(); |
104 |
< |
|
105 |
< |
if (!response.equals("ERROR")) { |
106 |
< |
_properties.setProperty("Host.UDPUpdateTime", response); |
113 |
> |
public String getServerProperty(String configName, String propertyName) { |
114 |
> |
String property = null; |
115 |
> |
if (_connectionHandler != null) { |
116 |
> |
property = _connectionHandler.getConfigFromServer(configName, propertyName); |
117 |
|
} |
118 |
< |
out.println("Host.TCPUpdateTime"); |
109 |
< |
out.flush(); |
110 |
< |
response = in.readLine(); |
111 |
< |
|
112 |
< |
if (!response.equals("ERROR")) { |
113 |
< |
_properties.setProperty("Host.TCPUpdateTime", response); |
114 |
< |
} |
118 |
> |
return property; |
119 |
|
} |
120 |
|
|
121 |
|
/** |
132 |
|
ConfigurationDialog conf = new ConfigurationDialog(); |
133 |
|
} |
134 |
|
|
135 |
+ |
/** |
136 |
+ |
* Prompts the user for a box to save the configuration |
137 |
+ |
* to a specific filename. |
138 |
+ |
*/ |
139 |
|
public void saveNewConfiguration() { |
140 |
|
fc.setCurrentDirectory(_configFile.getParentFile()); |
141 |
|
|
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
+ |
/** |
151 |
+ |
* This method saves the currently saves the current |
152 |
+ |
* config, if the config has not been saved before |
153 |
+ |
* at it is not the default, then it prompts for a |
154 |
+ |
* file name by calling saveNewConfiguration(). |
155 |
+ |
*/ |
156 |
|
public void saveConfiguration() { |
157 |
|
saveFileConfiguration(_configFile); |
158 |
|
} |
159 |
|
|
160 |
+ |
/** |
161 |
+ |
* This method saves the currently loaded config as |
162 |
+ |
* the default config |
163 |
+ |
*/ |
164 |
|
public void saveDefaultConfiguration() { |
165 |
|
saveFileConfiguration(_defaultConfigFile); |
166 |
|
} |
167 |
|
|
168 |
+ |
/** |
169 |
+ |
* Loads in a configuration that is chosen by the |
170 |
+ |
* user after displaying a dialog. |
171 |
+ |
*/ |
172 |
|
public void loadConfiguration() { |
173 |
|
fc.setCurrentDirectory(_configFile.getParentFile()); |
174 |
|
int returnVal = fc.showOpenDialog(Conient.getFrame()); |
201 |
|
return configHolder; |
202 |
|
} |
203 |
|
|
204 |
+ |
/** |
205 |
+ |
* This method writes out the current configuration |
206 |
+ |
* to a file using the Properties.store() method. |
207 |
+ |
* |
208 |
+ |
* It uses the CONFIG_HEADER attribute to head the file. |
209 |
+ |
* |
210 |
+ |
* @param outputfile the file to write the configurtion to |
211 |
+ |
*/ |
212 |
|
private void saveFileConfiguration(File outputFile) { |
213 |
|
try { |
214 |
< |
_properties.store(new FileOutputStream(outputFile), "!!!IN TIME A NICE DESCRIPTIVE HEADER WILL BE HERE!!!"); |
214 |
> |
_properties.store(new FileOutputStream(outputFile), CONFIG_HEADER); |
215 |
|
JOptionPane.showMessageDialog(null, "Configuration written out - " + _configFile.getName(), "Configuration Saved", JOptionPane.INFORMATION_MESSAGE); |
216 |
|
} catch (IOException e) { |
217 |
|
JOptionPane.showMessageDialog(null, "Unable to write default configuration file - " + outputFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
235 |
|
return property; |
236 |
|
} |
237 |
|
|
238 |
+ |
/** |
239 |
+ |
* A wrapper for java.util.Properties.setProperty |
240 |
+ |
* When given a key and a value, it writes it to the |
241 |
+ |
* current properties. |
242 |
+ |
* |
243 |
+ |
* @param key the key to write |
244 |
+ |
* @param value the value to assign it |
245 |
+ |
*/ |
246 |
|
public void setProperty(String key, String value) { |
247 |
|
_properties.setProperty(key, value); |
248 |
|
} |
249 |
|
|
250 |
+ |
/** |
251 |
+ |
* Returns whether a specific config is in use |
252 |
+ |
* rather than a default one |
253 |
+ |
* |
254 |
+ |
* @return yay or nay |
255 |
+ |
*/ |
256 |
|
public boolean getUsingSpecificConfig() { |
257 |
|
return _usingSpecificConfig; |
258 |
|
} |
259 |
|
|
260 |
+ |
/** |
261 |
+ |
* When the connection handler class starts up it notifys this class |
262 |
+ |
* that it is up, so that we can obtain configuration through the |
263 |
+ |
* open connections, should there be any. |
264 |
+ |
* |
265 |
+ |
* @param connectionHandler a handle on the instance of the connnection handler |
266 |
+ |
*/ |
267 |
+ |
public void setConnectionHandler(ConnectionHandler connectionHandler) { |
268 |
+ |
_connectionHandler = connectionHandler; |
269 |
+ |
} |
270 |
+ |
|
271 |
|
//---ATTRIBUTES--- |
272 |
|
|
273 |
|
/** |
275 |
|
*/ |
276 |
|
private Properties _properties = null; |
277 |
|
|
278 |
+ |
/** |
279 |
+ |
* A reference to the ConnectionHandler, this allows |
280 |
+ |
* this class to obtain configuration from the server |
281 |
+ |
*/ |
282 |
+ |
private ConnectionHandler _connectionHandler = null; |
283 |
+ |
|
284 |
+ |
/** |
285 |
+ |
* A value to indicate whether a specific config is in use |
286 |
+ |
* or not |
287 |
+ |
*/ |
288 |
|
private boolean _usingSpecificConfig = false; |
289 |
|
|
290 |
+ |
/** |
291 |
+ |
* The file that the current configuration is loaded from |
292 |
+ |
*/ |
293 |
|
private File _configFile; |
294 |
|
|
295 |
+ |
/** |
296 |
+ |
* The file containing the default configuration |
297 |
+ |
*/ |
298 |
|
private File _defaultConfigFile; |
299 |
|
|
300 |
< |
final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files"); |
301 |
< |
final JFileChooser fc = new JFileChooser(); |
300 |
> |
/** |
301 |
> |
* A file filter for the file chooser dialog boxes |
302 |
> |
*/ |
303 |
> |
private final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files"); |
304 |
> |
|
305 |
> |
/** |
306 |
> |
* A file chooser to prompt for file names when saving configuration |
307 |
> |
*/ |
308 |
> |
private final JFileChooser fc = new JFileChooser(); |
309 |
|
{ |
310 |
|
fc.setFileSelectionMode(JFileChooser.FILES_ONLY); |
311 |
|
fc.setMultiSelectionEnabled(false); |
326 |
|
* The reference to the singleton instance of this class |
327 |
|
*/ |
328 |
|
private static Configuration _instance = null; |
329 |
+ |
|
330 |
|
//---INNER CLASSES--- |
331 |
|
|
332 |
+ |
/** |
333 |
+ |
* An inner class for the file filter. |
334 |
+ |
* This allows filters to be defined for files based on their |
335 |
+ |
* suffix. |
336 |
+ |
*/ |
337 |
|
private class SuffixFileFilter extends FileFilter { |
338 |
|
public SuffixFileFilter(String suffix, String description) { |
339 |
|
if (suffix.charAt(0) != '.') { |