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
(Generate patch)

Comparing projects/cms/source/conient/uk/org/iscream/cms/conient/Configuration.java (file contents):
Revision 1.5 by ajm, Mon Feb 26 18:40:25 2001 UTC vs.
Revision 1.10 by tdb, Tue May 29 17:41:32 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.conient;
2 > package uk.org.iscream.cms.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
15 > * 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 + *
24   * @author  $Author$
25   * @version $Id$
26   */
# Line 24 | Line 33 | public class Configuration {
33       */
34      public static final String REVISION = "$Revision$";
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 +    
44   //---STATIC METHODS---
45  
46      /**
# Line 64 | Line 81 | public class Configuration {
81       * @param configFile the path to the file that this configuration should load
82       */
83      private Configuration(String configFile) {
84 +        _configFile = new File(configFile);
85 +        _defaultConfigFile = _configFile;
86          try {
87 <            _properties = readFileConfiguration(configFile);
87 >            _properties = readFileConfiguration(_configFile);
88          } catch (FileNotFoundException e) {
89 <            JOptionPane.showMessageDialog(null, "Configuration file not found - " + configFile, "Configuration Error", JOptionPane.ERROR_MESSAGE);
89 >            JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE);
90              System.exit(1);
91          } catch (IOException e) {
92 <            JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + e.getMessage(), "Configuration Error", JOptionPane.ERROR_MESSAGE);
92 >            JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE);
93              System.exit(1);
94          }
95      }
# Line 78 | Line 97 | public class Configuration {
97   //---PUBLIC METHODS---
98  
99      /**
100 <     * This routine asks the server for all configuration
101 <     * options that are needed to be obtained from the
83 <     * server.  It takes hooks to the I/O streams for the
84 <     * control link socket in order that it can request
85 <     * the attributes.  It is assumed that the connection
86 <     * has been negotiated to a stage that the server is
87 <     * ready to server attributes.  Once this method returns
88 <     * the calling class should tell the server that configuration
89 <     * has terminated.
100 >     * This method is called by any part of the system
101 >     * that requires configuration from the server.
102       *
103 <     * @param in the input stream of the control link
104 <     * @param out the output stream of the control link
105 <     * @throws IOException if there is an error communicating
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 void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException {
114 <        String response = null;
115 <        out.println("Host.UDPUpdateTime");
116 <        out.flush();
99 <        response = in.readLine();
100 <        
101 <        if (!response.equals("ERROR")) {
102 <            _properties.setProperty("Host.UDPUpdateTime", response);
113 >    public String getServerProperty(String configName, String propertyName) {
114 >        String property = null;
115 >        if (_connectionHandler != null) {
116 >            property = _connectionHandler.getConfigFromServer(configName, propertyName);
117          }
118 <        out.println("Host.TCPUpdateTime");
105 <        out.flush();
106 <        response = in.readLine();
107 <        
108 <        if (!response.equals("ERROR")) {
109 <            _properties.setProperty("Host.TCPUpdateTime", response);
110 <        }
118 >        return property;
119      }
120      
121      /**
# Line 123 | Line 131 | public class Configuration {
131      public void GUIReconfiguration() {
132          ConfigurationDialog conf = new ConfigurationDialog();
133      }
134 +    
135 +    /**
136 +     * Prompts the user for a box to save the configuration
137 +     * to a specific filename.
138 +     */
139 +    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 +    /**
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 +    public void saveConfiguration() {
157 +        saveFileConfiguration(_configFile);
158 +    }
159 +    
160 +    /**
161 +     * This method saves the currently loaded config as
162 +     * the default config
163 +     */
164 +    public void saveDefaultConfiguration() {
165 +        saveFileConfiguration(_defaultConfigFile);
166 +    }
167 +    
168 +    /**
169 +     * Loads in a configuration that is chosen by the
170 +     * user after displaying a dialog.
171 +     */    
172 +    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   //---PRIVATE METHODS---
190  
191      /**
# Line 132 | Line 195 | public class Configuration {
195       * @param configFile the path of the file to be read
196       * @return the parsed properties
197       */
198 <    private Properties readFileConfiguration(String configFile) throws FileNotFoundException, IOException {
136 <        File file = new File(configFile);
198 >    private Properties readFileConfiguration(File inputFile) throws FileNotFoundException, IOException {
199          Properties configHolder = new Properties();
200 <        configHolder.load(new FileInputStream(file));
200 >        configHolder.load(new FileInputStream(inputFile));
201          return configHolder;
202      }
203 +    
204 +    /**
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 +    private void saveFileConfiguration(File outputFile) {
213 +        try {
214 +            _properties.store(new FileOutputStream(outputFile), CONFIG_HEADER);
215 +            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  
221   //---ACCESSOR/MUTATOR METHODS---
222  
# Line 149 | Line 228 | public class Configuration {
228       * @param key the key the value of which is wanted
229       */
230      public String getProperty(String key) {
231 <        return _properties.getProperty(key);
231 >        String property = _properties.getProperty(key);
232 >        if (property == null) {
233 >            return "";
234 >        }
235 >        return property;
236      }
237 +    
238 +    /**
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 +    public void setProperty(String key, String value) {
247 +        _properties.setProperty(key, value);
248 +    }
249 +    
250 +    /**
251 +     * Returns whether a specific config is in use
252 +     * rather than a default one
253 +     *
254 +     * @return yay or nay
255 +     */
256 +    public boolean getUsingSpecificConfig() {
257 +        return _usingSpecificConfig;
258 +    }
259  
260 +    /**
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   //---ATTRIBUTES---
272  
273      /**
274       * The current configuration in use
275       */
276      private Properties _properties = null;
277 <
277 >    
278 >    /**
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 >    private boolean _usingSpecificConfig = false;
289 >    
290 >    /**
291 >     * The file that the current configuration is loaded from
292 >     */
293 >    private File _configFile;
294 >    
295 >    /**
296 >     * The file containing the default configuration
297 >     */
298 >    private File _defaultConfigFile;
299 >    
300 >    /**
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 >    {
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   //---STATIC ATTRIBUTES---
324  
325      /**
326       * The reference to the singleton instance of this class
327       */
328      private static Configuration _instance = null;
329 +    
330 + //---INNER CLASSES---
331 +
332 +    /**
333 +     * An inner class for the file filter.
334 +     * This allows filters to be defined for files based on their
335 +     * suffix.
336 +     */
337 +    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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines