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.6
Committed: Tue Feb 27 03:09:58 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.5: +125 -9 lines
Log Message:
Now has fully support for configuration modification, saving and loading.

Note there are still bugs, namely the server config is NOT treated seperately
from local config, as well as concurrency issues of loading in a config as
its changing.

Also not present is support for checking all REQUIRED configuration options
are present, so that will need to be done.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.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.5 2001/02/26 18:40:25 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.5 $";
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 return _properties.getProperty(key);
202 }
203
204 public void setProperty(String key, String value) {
205 _properties.setProperty(key, value);
206 }
207
208 public boolean getUsingSpecificConfig() {
209 return _usingSpecificConfig;
210 }
211
212 //---ATTRIBUTES---
213
214 /**
215 * The current configuration in use
216 */
217 private Properties _properties = null;
218
219 private boolean _usingSpecificConfig = false;
220
221 private File _configFile;
222
223 private File _defaultConfigFile;
224
225 final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files");
226 final JFileChooser fc = new JFileChooser();
227 {
228 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
229 fc.setMultiSelectionEnabled(false);
230 fc.setFileFilter(new FileFilter() {
231 public boolean accept(File f) {
232 return (f != null) && (f.isDirectory() || filter.accept(f));
233 }
234 public String getDescription() {
235 return "Folders and Conient Configuration Files";
236 }
237 });
238 fc.addChoosableFileFilter(filter);
239 }
240
241 //---STATIC ATTRIBUTES---
242
243 /**
244 * The reference to the singleton instance of this class
245 */
246 private static Configuration _instance = null;
247 //---INNER CLASSES---
248
249 private class SuffixFileFilter extends FileFilter {
250 public SuffixFileFilter(String suffix, String description) {
251 if (suffix.charAt(0) != '.') {
252 _suffix = '.' + suffix;
253 } else {
254 _suffix = suffix;
255 }
256 _description = description;
257 }
258
259 public boolean accept(File f) {
260 boolean ok = false;
261 if (f != null) {
262 final String name = f.getName();
263 if (name != null) {
264 final int nameLength = name.length();
265 int dotIndex = name.lastIndexOf(".");
266 ok = (dotIndex >= 1) &&
267 getSuffix().equalsIgnoreCase(name.substring(dotIndex));
268 }
269 }
270 return ok;
271 }
272
273 public String getDescription() {
274 return _description;
275 }
276
277 public String getSuffix() {
278 return _suffix;
279 }
280
281 private final String _suffix, _description;
282 }
283
284 }