1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.8 |
package uk.org.iscream.conient; |
3 |
ajm |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.3 |
import javax.swing.JOptionPane; |
6 |
ajm |
1.6 |
import javax.swing.JFileChooser; |
7 |
ajm |
1.5 |
import javax.swing.SwingUtilities; |
8 |
ajm |
1.6 |
import javax.swing.filechooser.FileFilter; |
9 |
ajm |
1.5 |
import java.awt.Frame; |
10 |
ajm |
1.1 |
import java.util.Properties; |
11 |
ajm |
1.2 |
import java.io.*; |
12 |
ajm |
1.1 |
|
13 |
|
|
/** |
14 |
|
|
* Provides configuration details to Conient |
15 |
ajm |
1.9 |
* 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 |
ajm |
1.1 |
* |
24 |
ajm |
1.2 |
* @author $Author: ajm4 $ |
25 |
ajm |
1.9 |
* @version $Id: Configuration.java,v 1.8 2001/03/15 01:05:46 ajm4 Exp $ |
26 |
ajm |
1.1 |
*/ |
27 |
|
|
public class Configuration { |
28 |
|
|
|
29 |
|
|
//---FINAL ATTRIBUTES--- |
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* The current CVS revision of this class |
33 |
|
|
*/ |
34 |
ajm |
1.9 |
public static final String REVISION = "$Revision: 1.8 $"; |
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 |
ajm |
1.1 |
|
44 |
|
|
//---STATIC METHODS--- |
45 |
|
|
|
46 |
|
|
/** |
47 |
|
|
* Creates and initialises a the Configuration |
48 |
|
|
* system for Conient. This calls the private |
49 |
|
|
* constructor and ensures this class is a singleton. |
50 |
|
|
* |
51 |
|
|
* @param configFile the path to the file that this configuration should load |
52 |
|
|
*/ |
53 |
|
|
public static void initialise(String configFile) { |
54 |
|
|
_instance = new Configuration(configFile); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* Returns the singleton instance of this |
59 |
|
|
* class. |
60 |
|
|
* This will throw a runtime exception if it |
61 |
|
|
* is called at the wrong time! |
62 |
|
|
* |
63 |
|
|
* @return the singleton instance |
64 |
|
|
*/ |
65 |
|
|
public static Configuration getInstance() { |
66 |
|
|
if (_instance == null) { |
67 |
|
|
throw new RuntimeException("Configuration class requested but HASN'T been initialised!"); |
68 |
|
|
} |
69 |
|
|
return _instance; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
//---CONSTRUCTORS--- |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* The private constructor, ensures that this is a singleton. |
76 |
|
|
* Simply reads in the local configuration from the file. |
77 |
|
|
* Then sets the current properties to be this configuration. |
78 |
|
|
* Later the system will obtain further configuration from the |
79 |
|
|
* server. |
80 |
|
|
* |
81 |
|
|
* @param configFile the path to the file that this configuration should load |
82 |
|
|
*/ |
83 |
|
|
private Configuration(String configFile) { |
84 |
ajm |
1.6 |
_configFile = new File(configFile); |
85 |
|
|
_defaultConfigFile = _configFile; |
86 |
ajm |
1.1 |
try { |
87 |
ajm |
1.6 |
_properties = readFileConfiguration(_configFile); |
88 |
ajm |
1.1 |
} catch (FileNotFoundException e) { |
89 |
ajm |
1.6 |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
90 |
ajm |
1.3 |
System.exit(1); |
91 |
ajm |
1.1 |
} catch (IOException e) { |
92 |
ajm |
1.6 |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
93 |
ajm |
1.3 |
System.exit(1); |
94 |
ajm |
1.1 |
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
//---PUBLIC METHODS--- |
98 |
|
|
|
99 |
ajm |
1.4 |
/** |
100 |
ajm |
1.9 |
* This method is called by any part of the system |
101 |
|
|
* that requires configuration from the server. |
102 |
|
|
* |
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 String getServerProperty(String configName, String propertyName) { |
114 |
|
|
String property = null; |
115 |
|
|
if (_connectionHandler != null) { |
116 |
|
|
property = _connectionHandler.getConfigFromServer(configName, propertyName); |
117 |
ajm |
1.2 |
} |
118 |
ajm |
1.9 |
return property; |
119 |
ajm |
1.2 |
} |
120 |
|
|
|
121 |
ajm |
1.4 |
/** |
122 |
|
|
* Tells the configuration class that it should |
123 |
|
|
* perform user re-configuration through displaying |
124 |
|
|
* a gui. This passes control to a dialog to |
125 |
|
|
* handle re-configuration. Basically this is |
126 |
|
|
* the nicer alternative to hacking the config |
127 |
|
|
* file. |
128 |
|
|
* The ConientConfiguration class is responsible |
129 |
|
|
* for handling the re-configuration. |
130 |
|
|
*/ |
131 |
|
|
public void GUIReconfiguration() { |
132 |
ajm |
1.5 |
ConfigurationDialog conf = new ConfigurationDialog(); |
133 |
ajm |
1.4 |
} |
134 |
ajm |
1.6 |
|
135 |
ajm |
1.9 |
/** |
136 |
|
|
* Prompts the user for a box to save the configuration |
137 |
|
|
* to a specific filename. |
138 |
|
|
*/ |
139 |
ajm |
1.6 |
public void saveNewConfiguration() { |
140 |
|
|
fc.setCurrentDirectory(_configFile.getParentFile()); |
141 |
|
|
|
142 |
|
|
int returnVal = fc.showSaveDialog(Conient.getFrame()); |
143 |
|
|
if (returnVal == JFileChooser.APPROVE_OPTION) { |
144 |
|
|
_configFile = fc.getSelectedFile(); |
145 |
|
|
_usingSpecificConfig = true; |
146 |
|
|
saveConfiguration(); |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
ajm |
1.9 |
/** |
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 |
ajm |
1.6 |
public void saveConfiguration() { |
157 |
|
|
saveFileConfiguration(_configFile); |
158 |
|
|
} |
159 |
|
|
|
160 |
ajm |
1.9 |
/** |
161 |
|
|
* This method saves the currently loaded config as |
162 |
|
|
* the default config |
163 |
|
|
*/ |
164 |
ajm |
1.6 |
public void saveDefaultConfiguration() { |
165 |
|
|
saveFileConfiguration(_defaultConfigFile); |
166 |
|
|
} |
167 |
|
|
|
168 |
ajm |
1.9 |
/** |
169 |
|
|
* Loads in a configuration that is chosen by the |
170 |
|
|
* user after displaying a dialog. |
171 |
|
|
*/ |
172 |
ajm |
1.6 |
public void loadConfiguration() { |
173 |
|
|
fc.setCurrentDirectory(_configFile.getParentFile()); |
174 |
|
|
int returnVal = fc.showOpenDialog(Conient.getFrame()); |
175 |
|
|
if (returnVal == JFileChooser.APPROVE_OPTION) { |
176 |
|
|
_configFile = fc.getSelectedFile(); |
177 |
|
|
_usingSpecificConfig = true; |
178 |
|
|
try { |
179 |
|
|
_properties = readFileConfiguration(_configFile); |
180 |
|
|
JOptionPane.showMessageDialog(null, "Configuration sucessfully read in - " + _configFile.getName(), "Configuration Loaded", JOptionPane.INFORMATION_MESSAGE); |
181 |
|
|
} catch (FileNotFoundException e) { |
182 |
|
|
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
183 |
|
|
} catch (IOException e) { |
184 |
|
|
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
ajm |
1.1 |
//---PRIVATE METHODS--- |
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* Reads in the specified file and parses |
193 |
|
|
* its properties. |
194 |
|
|
* |
195 |
|
|
* @param configFile the path of the file to be read |
196 |
|
|
* @return the parsed properties |
197 |
|
|
*/ |
198 |
ajm |
1.6 |
private Properties readFileConfiguration(File inputFile) throws FileNotFoundException, IOException { |
199 |
ajm |
1.1 |
Properties configHolder = new Properties(); |
200 |
ajm |
1.6 |
configHolder.load(new FileInputStream(inputFile)); |
201 |
ajm |
1.1 |
return configHolder; |
202 |
|
|
} |
203 |
ajm |
1.6 |
|
204 |
ajm |
1.9 |
/** |
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 |
ajm |
1.6 |
private void saveFileConfiguration(File outputFile) { |
213 |
|
|
try { |
214 |
ajm |
1.9 |
_properties.store(new FileOutputStream(outputFile), CONFIG_HEADER); |
215 |
ajm |
1.6 |
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); |
218 |
|
|
} |
219 |
|
|
} |
220 |
ajm |
1.1 |
|
221 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
222 |
|
|
|
223 |
|
|
/** |
224 |
|
|
* A wrapper for java.util.Properties.getProperty |
225 |
|
|
* When given a key it returns the value of that key |
226 |
|
|
* ie, key = value |
227 |
|
|
* |
228 |
|
|
* @param key the key the value of which is wanted |
229 |
|
|
*/ |
230 |
|
|
public String getProperty(String key) { |
231 |
ajm |
1.7 |
String property = _properties.getProperty(key); |
232 |
|
|
if (property == null) { |
233 |
|
|
return ""; |
234 |
|
|
} |
235 |
|
|
return property; |
236 |
ajm |
1.1 |
} |
237 |
ajm |
1.6 |
|
238 |
ajm |
1.9 |
/** |
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 |
ajm |
1.6 |
public void setProperty(String key, String value) { |
247 |
|
|
_properties.setProperty(key, value); |
248 |
|
|
} |
249 |
|
|
|
250 |
ajm |
1.9 |
/** |
251 |
|
|
* Returns whether a specific config is in use |
252 |
|
|
* rather than a default one |
253 |
|
|
* |
254 |
|
|
* @return yay or nay |
255 |
|
|
*/ |
256 |
ajm |
1.6 |
public boolean getUsingSpecificConfig() { |
257 |
|
|
return _usingSpecificConfig; |
258 |
|
|
} |
259 |
ajm |
1.1 |
|
260 |
ajm |
1.9 |
/** |
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 |
ajm |
1.1 |
//---ATTRIBUTES--- |
272 |
|
|
|
273 |
|
|
/** |
274 |
|
|
* The current configuration in use |
275 |
|
|
*/ |
276 |
|
|
private Properties _properties = null; |
277 |
ajm |
1.6 |
|
278 |
ajm |
1.9 |
/** |
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 |
ajm |
1.6 |
private boolean _usingSpecificConfig = false; |
289 |
|
|
|
290 |
ajm |
1.9 |
/** |
291 |
|
|
* The file that the current configuration is loaded from |
292 |
|
|
*/ |
293 |
ajm |
1.6 |
private File _configFile; |
294 |
|
|
|
295 |
ajm |
1.9 |
/** |
296 |
|
|
* The file containing the default configuration |
297 |
|
|
*/ |
298 |
ajm |
1.6 |
private File _defaultConfigFile; |
299 |
|
|
|
300 |
ajm |
1.9 |
/** |
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 |
ajm |
1.6 |
{ |
310 |
|
|
fc.setFileSelectionMode(JFileChooser.FILES_ONLY); |
311 |
|
|
fc.setMultiSelectionEnabled(false); |
312 |
|
|
fc.setFileFilter(new FileFilter() { |
313 |
|
|
public boolean accept(File f) { |
314 |
|
|
return (f != null) && (f.isDirectory() || filter.accept(f)); |
315 |
|
|
} |
316 |
|
|
public String getDescription() { |
317 |
|
|
return "Folders and Conient Configuration Files"; |
318 |
|
|
} |
319 |
|
|
}); |
320 |
|
|
fc.addChoosableFileFilter(filter); |
321 |
|
|
} |
322 |
|
|
|
323 |
ajm |
1.1 |
//---STATIC ATTRIBUTES--- |
324 |
|
|
|
325 |
|
|
/** |
326 |
|
|
* The reference to the singleton instance of this class |
327 |
|
|
*/ |
328 |
|
|
private static Configuration _instance = null; |
329 |
ajm |
1.9 |
|
330 |
ajm |
1.6 |
//---INNER CLASSES--- |
331 |
|
|
|
332 |
ajm |
1.9 |
/** |
333 |
|
|
* An inner class for the file filter. |
334 |
|
|
* This allows filters to be defined for files based on their |
335 |
|
|
* suffix. |
336 |
|
|
*/ |
337 |
ajm |
1.6 |
private class SuffixFileFilter extends FileFilter { |
338 |
|
|
public SuffixFileFilter(String suffix, String description) { |
339 |
|
|
if (suffix.charAt(0) != '.') { |
340 |
|
|
_suffix = '.' + suffix; |
341 |
|
|
} else { |
342 |
|
|
_suffix = suffix; |
343 |
|
|
} |
344 |
|
|
_description = description; |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
public boolean accept(File f) { |
348 |
|
|
boolean ok = false; |
349 |
|
|
if (f != null) { |
350 |
|
|
final String name = f.getName(); |
351 |
|
|
if (name != null) { |
352 |
|
|
final int nameLength = name.length(); |
353 |
|
|
int dotIndex = name.lastIndexOf("."); |
354 |
|
|
ok = (dotIndex >= 1) && |
355 |
|
|
getSuffix().equalsIgnoreCase(name.substring(dotIndex)); |
356 |
|
|
} |
357 |
|
|
} |
358 |
|
|
return ok; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
public String getDescription() { |
362 |
|
|
return _description; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
public String getSuffix() { |
366 |
|
|
return _suffix; |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
private final String _suffix, _description; |
370 |
|
|
} |
371 |
|
|
|
372 |
ajm |
1.1 |
} |