1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.conient; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import javax.swing.JOptionPane; |
6 |
import javax.swing.JFileChooser; |
7 |
import javax.swing.SwingUtilities; |
8 |
import javax.swing.filechooser.FileFilter; |
9 |
import java.awt.Frame; |
10 |
import java.util.Properties; |
11 |
import java.io.*; |
12 |
|
13 |
/** |
14 |
* Provides configuration details to Conient |
15 |
* This class is a Singleton class and |
16 |
* |
17 |
* @author $Author: ajm4 $ |
18 |
* @version $Id: Configuration.java,v 1.7 2001/02/27 23:31:32 ajm4 Exp $ |
19 |
*/ |
20 |
public class Configuration { |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public static final String REVISION = "$Revision: 1.7 $"; |
28 |
|
29 |
//---STATIC METHODS--- |
30 |
|
31 |
/** |
32 |
* Creates and initialises a the Configuration |
33 |
* system for Conient. This calls the private |
34 |
* constructor and ensures this class is a singleton. |
35 |
* |
36 |
* @param configFile the path to the file that this configuration should load |
37 |
*/ |
38 |
public static void initialise(String configFile) { |
39 |
_instance = new Configuration(configFile); |
40 |
} |
41 |
|
42 |
/** |
43 |
* Returns the singleton instance of this |
44 |
* class. |
45 |
* This will throw a runtime exception if it |
46 |
* is called at the wrong time! |
47 |
* |
48 |
* @return the singleton instance |
49 |
*/ |
50 |
public static Configuration getInstance() { |
51 |
if (_instance == null) { |
52 |
throw new RuntimeException("Configuration class requested but HASN'T been initialised!"); |
53 |
} |
54 |
return _instance; |
55 |
} |
56 |
|
57 |
//---CONSTRUCTORS--- |
58 |
|
59 |
/** |
60 |
* The private constructor, ensures that this is a singleton. |
61 |
* Simply reads in the local configuration from the file. |
62 |
* Then sets the current properties to be this configuration. |
63 |
* Later the system will obtain further configuration from the |
64 |
* server. |
65 |
* |
66 |
* @param configFile the path to the file that this configuration should load |
67 |
*/ |
68 |
private Configuration(String configFile) { |
69 |
_configFile = new File(configFile); |
70 |
_defaultConfigFile = _configFile; |
71 |
try { |
72 |
_properties = readFileConfiguration(_configFile); |
73 |
} catch (FileNotFoundException e) { |
74 |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
75 |
System.exit(1); |
76 |
} catch (IOException e) { |
77 |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
78 |
System.exit(1); |
79 |
} |
80 |
} |
81 |
|
82 |
//---PUBLIC METHODS--- |
83 |
|
84 |
/** |
85 |
* This routine asks the server for all configuration |
86 |
* 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. |
94 |
* |
95 |
* @param in the input stream of the control link |
96 |
* @param out the output stream of the control link |
97 |
* @throws IOException if there is an error communicating |
98 |
*/ |
99 |
public void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException { |
100 |
String response = null; |
101 |
out.println("Host.UDPUpdateTime"); |
102 |
out.flush(); |
103 |
response = in.readLine(); |
104 |
|
105 |
if (!response.equals("ERROR")) { |
106 |
_properties.setProperty("Host.UDPUpdateTime", response); |
107 |
} |
108 |
out.println("Host.TCPUpdateTime"); |
109 |
out.flush(); |
110 |
response = in.readLine(); |
111 |
|
112 |
if (!response.equals("ERROR")) { |
113 |
_properties.setProperty("Host.TCPUpdateTime", response); |
114 |
} |
115 |
} |
116 |
|
117 |
/** |
118 |
* Tells the configuration class that it should |
119 |
* perform user re-configuration through displaying |
120 |
* a gui. This passes control to a dialog to |
121 |
* handle re-configuration. Basically this is |
122 |
* the nicer alternative to hacking the config |
123 |
* file. |
124 |
* The ConientConfiguration class is responsible |
125 |
* for handling the re-configuration. |
126 |
*/ |
127 |
public void GUIReconfiguration() { |
128 |
ConfigurationDialog conf = new ConfigurationDialog(); |
129 |
} |
130 |
|
131 |
public void saveNewConfiguration() { |
132 |
fc.setCurrentDirectory(_configFile.getParentFile()); |
133 |
|
134 |
int returnVal = fc.showSaveDialog(Conient.getFrame()); |
135 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
136 |
_configFile = fc.getSelectedFile(); |
137 |
_usingSpecificConfig = true; |
138 |
saveConfiguration(); |
139 |
} |
140 |
} |
141 |
|
142 |
public void saveConfiguration() { |
143 |
saveFileConfiguration(_configFile); |
144 |
} |
145 |
|
146 |
public void saveDefaultConfiguration() { |
147 |
saveFileConfiguration(_defaultConfigFile); |
148 |
} |
149 |
|
150 |
public void loadConfiguration() { |
151 |
fc.setCurrentDirectory(_configFile.getParentFile()); |
152 |
int returnVal = fc.showOpenDialog(Conient.getFrame()); |
153 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
154 |
_configFile = fc.getSelectedFile(); |
155 |
_usingSpecificConfig = true; |
156 |
try { |
157 |
_properties = readFileConfiguration(_configFile); |
158 |
JOptionPane.showMessageDialog(null, "Configuration sucessfully read in - " + _configFile.getName(), "Configuration Loaded", JOptionPane.INFORMATION_MESSAGE); |
159 |
} catch (FileNotFoundException e) { |
160 |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
161 |
} catch (IOException e) { |
162 |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
//---PRIVATE METHODS--- |
168 |
|
169 |
/** |
170 |
* Reads in the specified file and parses |
171 |
* its properties. |
172 |
* |
173 |
* @param configFile the path of the file to be read |
174 |
* @return the parsed properties |
175 |
*/ |
176 |
private Properties readFileConfiguration(File inputFile) throws FileNotFoundException, IOException { |
177 |
Properties configHolder = new Properties(); |
178 |
configHolder.load(new FileInputStream(inputFile)); |
179 |
return configHolder; |
180 |
} |
181 |
|
182 |
private void saveFileConfiguration(File outputFile) { |
183 |
try { |
184 |
_properties.store(new FileOutputStream(outputFile), "!!!IN TIME A NICE DESCRIPTIVE HEADER WILL BE HERE!!!"); |
185 |
JOptionPane.showMessageDialog(null, "Configuration written out - " + _configFile.getName(), "Configuration Saved", JOptionPane.INFORMATION_MESSAGE); |
186 |
} catch (IOException e) { |
187 |
JOptionPane.showMessageDialog(null, "Unable to write default configuration file - " + outputFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
188 |
} |
189 |
} |
190 |
|
191 |
//---ACCESSOR/MUTATOR METHODS--- |
192 |
|
193 |
/** |
194 |
* A wrapper for java.util.Properties.getProperty |
195 |
* When given a key it returns the value of that key |
196 |
* ie, key = value |
197 |
* |
198 |
* @param key the key the value of which is wanted |
199 |
*/ |
200 |
public String getProperty(String key) { |
201 |
String property = _properties.getProperty(key); |
202 |
if (property == null) { |
203 |
return ""; |
204 |
} |
205 |
return property; |
206 |
} |
207 |
|
208 |
public void setProperty(String key, String value) { |
209 |
_properties.setProperty(key, value); |
210 |
} |
211 |
|
212 |
public boolean getUsingSpecificConfig() { |
213 |
return _usingSpecificConfig; |
214 |
} |
215 |
|
216 |
//---ATTRIBUTES--- |
217 |
|
218 |
/** |
219 |
* The current configuration in use |
220 |
*/ |
221 |
private Properties _properties = null; |
222 |
|
223 |
private boolean _usingSpecificConfig = false; |
224 |
|
225 |
private File _configFile; |
226 |
|
227 |
private File _defaultConfigFile; |
228 |
|
229 |
final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files"); |
230 |
final JFileChooser fc = new JFileChooser(); |
231 |
{ |
232 |
fc.setFileSelectionMode(JFileChooser.FILES_ONLY); |
233 |
fc.setMultiSelectionEnabled(false); |
234 |
fc.setFileFilter(new FileFilter() { |
235 |
public boolean accept(File f) { |
236 |
return (f != null) && (f.isDirectory() || filter.accept(f)); |
237 |
} |
238 |
public String getDescription() { |
239 |
return "Folders and Conient Configuration Files"; |
240 |
} |
241 |
}); |
242 |
fc.addChoosableFileFilter(filter); |
243 |
} |
244 |
|
245 |
//---STATIC ATTRIBUTES--- |
246 |
|
247 |
/** |
248 |
* The reference to the singleton instance of this class |
249 |
*/ |
250 |
private static Configuration _instance = null; |
251 |
//---INNER CLASSES--- |
252 |
|
253 |
private class SuffixFileFilter extends FileFilter { |
254 |
public SuffixFileFilter(String suffix, String description) { |
255 |
if (suffix.charAt(0) != '.') { |
256 |
_suffix = '.' + suffix; |
257 |
} else { |
258 |
_suffix = suffix; |
259 |
} |
260 |
_description = description; |
261 |
} |
262 |
|
263 |
public boolean accept(File f) { |
264 |
boolean ok = false; |
265 |
if (f != null) { |
266 |
final String name = f.getName(); |
267 |
if (name != null) { |
268 |
final int nameLength = name.length(); |
269 |
int dotIndex = name.lastIndexOf("."); |
270 |
ok = (dotIndex >= 1) && |
271 |
getSuffix().equalsIgnoreCase(name.substring(dotIndex)); |
272 |
} |
273 |
} |
274 |
return ok; |
275 |
} |
276 |
|
277 |
public String getDescription() { |
278 |
return _description; |
279 |
} |
280 |
|
281 |
public String getSuffix() { |
282 |
return _suffix; |
283 |
} |
284 |
|
285 |
private final String _suffix, _description; |
286 |
} |
287 |
|
288 |
} |