| 1 |
+ |
/* |
| 2 |
+ |
* i-scream central monitoring system |
| 3 |
+ |
* http://www.i-scream.org |
| 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.ac.ukc.iscream.conient; |
| 22 |
> |
package uk.org.iscream.cms.conient; |
| 23 |
|
|
| 24 |
|
//---IMPORTS--- |
| 25 |
|
import javax.swing.JOptionPane; |
| 32 |
|
|
| 33 |
|
/** |
| 34 |
|
* Provides configuration details to Conient |
| 35 |
< |
* This class is a Singleton class and |
| 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$ |
| 45 |
|
* @version $Id$ |
| 46 |
|
*/ |
| 53 |
|
*/ |
| 54 |
|
public static final String REVISION = "$Revision$"; |
| 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 |
|
/** |
| 117 |
|
//---PUBLIC METHODS--- |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
< |
* This routine asks the server for all configuration |
| 121 |
< |
* 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. |
| 120 |
> |
* This method is called by any part of the system |
| 121 |
> |
* that requires configuration from the server. |
| 122 |
|
* |
| 123 |
< |
* @param in the input stream of the control link |
| 124 |
< |
* @param out the output stream of the control link |
| 125 |
< |
* @throws IOException if there is an error communicating |
| 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 void readServerConfiguration(BufferedReader in, PrintWriter out) throws IOException { |
| 134 |
< |
String response = null; |
| 135 |
< |
out.println("Host.UDPUpdateTime"); |
| 136 |
< |
out.flush(); |
| 103 |
< |
response = in.readLine(); |
| 104 |
< |
|
| 105 |
< |
if (!response.equals("ERROR")) { |
| 106 |
< |
_properties.setProperty("Host.UDPUpdateTime", response); |
| 133 |
> |
public String getServerProperty(String configName, String propertyName) { |
| 134 |
> |
String property = null; |
| 135 |
> |
if (_connectionHandler != null) { |
| 136 |
> |
property = _connectionHandler.getConfigFromServer(configName, propertyName); |
| 137 |
|
} |
| 138 |
< |
out.println("Host.TCPUpdateTime"); |
| 109 |
< |
out.flush(); |
| 110 |
< |
response = in.readLine(); |
| 111 |
< |
|
| 112 |
< |
if (!response.equals("ERROR")) { |
| 113 |
< |
_properties.setProperty("Host.TCPUpdateTime", response); |
| 114 |
< |
} |
| 138 |
> |
return property; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/** |
| 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 |
|
|
| 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()); |
| 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), "!!!IN TIME A NICE DESCRIPTIVE HEADER WILL BE HERE!!!"); |
| 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); |
| 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 |
|
/** |
| 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 |
< |
final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files"); |
| 321 |
< |
final JFileChooser fc = new JFileChooser(); |
| 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); |
| 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) != '.') { |