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.11
Committed: Sat May 18 18:15:56 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.10: +22 -3 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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