ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/Configuration.java
Revision: 1.13
Committed: Tue Feb 25 22:14:20 2003 UTC (21 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.12: +3 -3 lines
Log Message:
Fixed a bug in Conient - removed the TCP heartbeat watching stuff.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 //---PACKAGE DECLARATION---
22 package uk.org.iscream.cms.conient;
23
24 //---IMPORTS---
25 import javax.swing.JOptionPane;
26 import javax.swing.JFileChooser;
27 import javax.swing.SwingUtilities;
28 import javax.swing.filechooser.FileFilter;
29 import java.awt.Frame;
30 import java.util.Properties;
31 import java.io.*;
32
33 /**
34 * Provides configuration details to Conient
35 * This class is a Singleton class. It handles all the configuration
36 * for Conient. Once a connection has been made, it is told that
37 * it can get configuration from the server, thus allowing other components
38 * access to the servers configuration. It also shows the ConfigurationDialog
39 * on request, which allows local configuration options to be changed.
40 *
41 * It also has support for a variety of methods of saving the config in
42 * different files and in the default file.
43 *
44 * @author $Author: tdb $
45 * @version $Id: Configuration.java,v 1.12 2002/05/21 16:47:10 tdb Exp $
46 */
47 public class Configuration {
48
49 //---FINAL ATTRIBUTES---
50
51 /**
52 * The current CVS revision of this class
53 */
54 public static final String REVISION = "$Revision: 1.12 $";
55
56 public static final String CONFIG_HEADER =
57 "!!! Conient Local Configuration File !!!\n" +
58 "#-----------------------------------------\n" +
59 "# This file was auto-generated by Conient.\n" +
60 "# It is recommended that you use the GUI\n" +
61 "# configuration facility to make changes\n" +
62 "# to this file.\n#";
63
64 //---STATIC METHODS---
65
66 /**
67 * Creates and initialises a the Configuration
68 * system for Conient. This calls the private
69 * constructor and ensures this class is a singleton.
70 *
71 * @param configFile the path to the file that this configuration should load
72 */
73 public static void initialise(String configFile) {
74 _instance = new Configuration(configFile);
75 }
76
77 /**
78 * Returns the singleton instance of this
79 * class.
80 * This will throw a runtime exception if it
81 * is called at the wrong time!
82 *
83 * @return the singleton instance
84 */
85 public static Configuration getInstance() {
86 if (_instance == null) {
87 throw new RuntimeException("Configuration class requested but HASN'T been initialised!");
88 }
89 return _instance;
90 }
91
92 //---CONSTRUCTORS---
93
94 /**
95 * The private constructor, ensures that this is a singleton.
96 * Simply reads in the local configuration from the file.
97 * Then sets the current properties to be this configuration.
98 * Later the system will obtain further configuration from the
99 * server.
100 *
101 * @param configFile the path to the file that this configuration should load
102 */
103 private Configuration(String configFile) {
104 _configFile = new File(configFile);
105 _defaultConfigFile = _configFile;
106 try {
107 _properties = readFileConfiguration(_configFile);
108 } catch (FileNotFoundException e) {
109 JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE);
110 System.exit(1);
111 } catch (IOException e) {
112 JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE);
113 System.exit(1);
114 }
115 }
116
117 //---PUBLIC METHODS---
118
119 /**
120 * This method is called by any part of the system
121 * that requires configuration from the server.
122 *
123 * It should be passed the configuration name:
124 * eg, "Host.raptor.ukc.ac.uk"
125 * and the property required:
126 * eg, "Host.UDPUpdateTime"
127 *
128 * This method will then ask the ConnectionHandler to
129 * talk to the server and return the property. If the server
130 * fails to get the property, or the ConnectionHandler
131 * is not started, this method returns null.
132 */
133 public String getServerProperty(String configName, String propertyName) {
134 String property = null;
135 if (_connectionHandler != null) {
136 property = _connectionHandler.getConfigFromServer(configName, propertyName);
137 }
138 return property;
139 }
140
141 /**
142 * Tells the configuration class that it should
143 * perform user re-configuration through displaying
144 * a gui. This passes control to a dialog to
145 * handle re-configuration. Basically this is
146 * the nicer alternative to hacking the config
147 * file.
148 * The ConientConfiguration class is responsible
149 * for handling the re-configuration.
150 */
151 public void GUIReconfiguration() {
152 ConfigurationDialog conf = new ConfigurationDialog();
153 }
154
155 /**
156 * Prompts the user for a box to save the configuration
157 * to a specific filename.
158 */
159 public void saveNewConfiguration() {
160 fc.setCurrentDirectory(_configFile.getParentFile());
161
162 int returnVal = fc.showSaveDialog(Conient.getFrame());
163 if (returnVal == JFileChooser.APPROVE_OPTION) {
164 _configFile = fc.getSelectedFile();
165 _usingSpecificConfig = true;
166 saveConfiguration();
167 }
168 }
169
170 /**
171 * This method saves the currently saves the current
172 * config, if the config has not been saved before
173 * at it is not the default, then it prompts for a
174 * file name by calling saveNewConfiguration().
175 */
176 public void saveConfiguration() {
177 saveFileConfiguration(_configFile);
178 }
179
180 /**
181 * This method saves the currently loaded config as
182 * the default config
183 */
184 public void saveDefaultConfiguration() {
185 saveFileConfiguration(_defaultConfigFile);
186 }
187
188 /**
189 * Loads in a configuration that is chosen by the
190 * user after displaying a dialog.
191 */
192 public void loadConfiguration() {
193 fc.setCurrentDirectory(_configFile.getParentFile());
194 int returnVal = fc.showOpenDialog(Conient.getFrame());
195 if (returnVal == JFileChooser.APPROVE_OPTION) {
196 _configFile = fc.getSelectedFile();
197 _usingSpecificConfig = true;
198 try {
199 _properties = readFileConfiguration(_configFile);
200 JOptionPane.showMessageDialog(null, "Configuration sucessfully read in - " + _configFile.getName(), "Configuration Loaded", JOptionPane.INFORMATION_MESSAGE);
201 } catch (FileNotFoundException e) {
202 JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE);
203 } catch (IOException e) {
204 JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE);
205 }
206 }
207 }
208
209 //---PRIVATE METHODS---
210
211 /**
212 * Reads in the specified file and parses
213 * its properties.
214 *
215 * @param configFile the path of the file to be read
216 * @return the parsed properties
217 */
218 private Properties readFileConfiguration(File inputFile) throws FileNotFoundException, IOException {
219 Properties configHolder = new Properties();
220 configHolder.load(new FileInputStream(inputFile));
221 return configHolder;
222 }
223
224 /**
225 * This method writes out the current configuration
226 * to a file using the Properties.store() method.
227 *
228 * It uses the CONFIG_HEADER attribute to head the file.
229 *
230 * @param outputfile the file to write the configurtion to
231 */
232 private void saveFileConfiguration(File outputFile) {
233 try {
234 _properties.store(new FileOutputStream(outputFile), CONFIG_HEADER);
235 JOptionPane.showMessageDialog(null, "Configuration written out - " + _configFile.getName(), "Configuration Saved", JOptionPane.INFORMATION_MESSAGE);
236 } catch (IOException e) {
237 JOptionPane.showMessageDialog(null, "Unable to write default configuration file - " + outputFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE);
238 }
239 }
240
241 //---ACCESSOR/MUTATOR METHODS---
242
243 /**
244 * A wrapper for java.util.Properties.getProperty
245 * When given a key it returns the value of that key
246 * ie, key = value
247 *
248 * @param key the key the value of which is wanted
249 */
250 public String getProperty(String key) {
251 String property = _properties.getProperty(key);
252 if (property == null) {
253 return "";
254 }
255 return property;
256 }
257
258 /**
259 * A wrapper for java.util.Properties.setProperty
260 * When given a key and a value, it writes it to the
261 * current properties.
262 *
263 * @param key the key to write
264 * @param value the value to assign it
265 */
266 public void setProperty(String key, String value) {
267 _properties.setProperty(key, value);
268 }
269
270 /**
271 * Returns whether a specific config is in use
272 * rather than a default one
273 *
274 * @return yay or nay
275 */
276 public boolean getUsingSpecificConfig() {
277 return _usingSpecificConfig;
278 }
279
280 /**
281 * When the connection handler class starts up it notifys this class
282 * that it is up, so that we can obtain configuration through the
283 * open connections, should there be any.
284 *
285 * @param connectionHandler a handle on the instance of the connnection handler
286 */
287 public void setConnectionHandler(ConnectionHandler connectionHandler) {
288 _connectionHandler = connectionHandler;
289 }
290
291 //---ATTRIBUTES---
292
293 /**
294 * The current configuration in use
295 */
296 private Properties _properties = null;
297
298 /**
299 * A reference to the ConnectionHandler, this allows
300 * this class to obtain configuration from the server
301 */
302 private ConnectionHandler _connectionHandler = null;
303
304 /**
305 * A value to indicate whether a specific config is in use
306 * or not
307 */
308 private boolean _usingSpecificConfig = false;
309
310 /**
311 * The file that the current configuration is loaded from
312 */
313 private File _configFile;
314
315 /**
316 * The file containing the default configuration
317 */
318 private File _defaultConfigFile;
319
320 /**
321 * A file filter for the file chooser dialog boxes
322 */
323 private final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files");
324
325 /**
326 * A file chooser to prompt for file names when saving configuration
327 */
328 private final JFileChooser fc = new JFileChooser();
329 {
330 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
331 fc.setMultiSelectionEnabled(false);
332 fc.setFileFilter(new FileFilter() {
333 public boolean accept(File f) {
334 return (f != null) && (f.isDirectory() || filter.accept(f));
335 }
336 public String getDescription() {
337 return "Folders and Conient Configuration Files";
338 }
339 });
340 fc.addChoosableFileFilter(filter);
341 }
342
343 //---STATIC ATTRIBUTES---
344
345 /**
346 * The reference to the singleton instance of this class
347 */
348 private static Configuration _instance = null;
349
350 //---INNER CLASSES---
351
352 /**
353 * An inner class for the file filter.
354 * This allows filters to be defined for files based on their
355 * suffix.
356 */
357 private class SuffixFileFilter extends FileFilter {
358 public SuffixFileFilter(String suffix, String description) {
359 if (suffix.charAt(0) != '.') {
360 _suffix = '.' + suffix;
361 } else {
362 _suffix = suffix;
363 }
364 _description = description;
365 }
366
367 public boolean accept(File f) {
368 boolean ok = false;
369 if (f != null) {
370 final String name = f.getName();
371 if (name != null) {
372 final int nameLength = name.length();
373 int dotIndex = name.lastIndexOf(".");
374 ok = (dotIndex >= 1) &&
375 getSuffix().equalsIgnoreCase(name.substring(dotIndex));
376 }
377 }
378 return ok;
379 }
380
381 public String getDescription() {
382 return _description;
383 }
384
385 public String getSuffix() {
386 return _suffix;
387 }
388
389 private final String _suffix, _description;
390 }
391
392 }