net.grinder.common
Class GrinderProperties

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<java.lang.Object,java.lang.Object>
          extended by java.util.Properties
              extended by net.grinder.common.GrinderProperties
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.Object,java.lang.Object>

public class GrinderProperties
extends java.util.Properties

Extend Properties to add type safe accessors. Has an optional associated file.

Author:
Philip Aston
See Also:
Grinder.ScriptContext.getProperties(), Serialized Form

Nested Class Summary
static class GrinderProperties.PersistenceException
          Exception indicating a problem in persisting properties.
 
Field Summary
static java.lang.String CONSOLE_HOST
          Key to use for the console host property.
static java.lang.String CONSOLE_PORT
          Key to use for the console host property.
static java.io.File DEFAULT_PROPERTIES
          Default file name for properties.
static java.io.File DEFAULT_SCRIPT
          Default script file name.
static java.lang.String LOG_DIRECTORY
          Key to use for the log directory property.
static java.lang.String SCRIPT
          Key to use for the script property.
 
Fields inherited from class java.util.Properties
defaults
 
Constructor Summary
GrinderProperties()
          Construct an empty GrinderProperties with no associated file.
GrinderProperties(java.io.File file)
          Construct a GrinderProperties, reading initial values from the specified file.
 
Method Summary
 java.io.File getAssociatedFile()
          Get the associated file.
 boolean getBoolean(java.lang.String propertyName, boolean defaultValue)
          Get the value of the property with the given name, return the value as a boolean.
 double getDouble(java.lang.String propertyName, double defaultValue)
          Get the value of the property with the given name, return the value as a double.
 java.io.File getFile(java.lang.String propertyName, java.io.File defaultValue)
          Get the value of the property with the given name, return the value as a File.
 int getInt(java.lang.String propertyName, int defaultValue)
          Get the value of the property with the given name, return the value as an int.
 long getLong(java.lang.String propertyName, long defaultValue)
          Get the value of the property with the given name, return the value as a long.
 GrinderProperties getPropertySubset(java.lang.String prefix)
          Return a new GrinderProperties that contains the subset of our Properties which begin with the specified prefix.
 short getShort(java.lang.String propertyName, short defaultValue)
          Get the value of the property with the given name, return the value as a short.
 java.io.File resolveRelativeFile(java.io.File file)
          Returns a File representing the combined path of our associated property file directory and the passed file.
 void save()
          Save our properties to our file.
 void saveSingleProperty(java.lang.String name)
          Save a single property to our file.
 void setAssociatedFile(java.io.File file)
          Set the associated file.
 void setBoolean(java.lang.String propertyName, boolean value)
          Set the property with the given name to a boolean value.
 void setDouble(java.lang.String propertyName, double value)
          Set the property with the given name to a double value.
 void setErrorWriter(java.io.PrintWriter writer)
          Set a writer to report warnings to.
 void setFile(java.lang.String propertyName, java.io.File value)
          Set the property with the given name to a File value.
 void setInt(java.lang.String propertyName, int value)
          Set the property with the given name to an int value.
 void setLong(java.lang.String propertyName, long value)
          Set the property with the given name to a long value.
 void setShort(java.lang.String propertyName, short value)
          Set the property with the given name to a short value.
 
Methods inherited from class java.util.Properties
getProperty, getProperty, list, list, load, load, loadFromXML, propertyNames, save, setProperty, store, store, storeToXML, storeToXML, stringPropertyNames
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SCRIPT

public static final java.lang.String SCRIPT
Key to use for the script property.

See Also:
Constant Field Values

LOG_DIRECTORY

public static final java.lang.String LOG_DIRECTORY
Key to use for the log directory property.

See Also:
Constant Field Values

CONSOLE_HOST

public static final java.lang.String CONSOLE_HOST
Key to use for the console host property.

See Also:
Constant Field Values

CONSOLE_PORT

public static final java.lang.String CONSOLE_PORT
Key to use for the console host property.

See Also:
Constant Field Values

DEFAULT_PROPERTIES

public static final java.io.File DEFAULT_PROPERTIES
Default file name for properties.


DEFAULT_SCRIPT

public static final java.io.File DEFAULT_SCRIPT
Default script file name.

Constructor Detail

GrinderProperties

public GrinderProperties()
Construct an empty GrinderProperties with no associated file.


GrinderProperties

public GrinderProperties(java.io.File file)
                  throws GrinderProperties.PersistenceException
Construct a GrinderProperties, reading initial values from the specified file. System properties beginning with "grinder."are also added to allow values to be overridden on the command line.

Parameters:
file - The file to read the properties from.
Throws:
GrinderProperties.PersistenceException - If an error occurs reading from the file.
See Also:
DEFAULT_PROPERTIES
Method Detail

getAssociatedFile

public final java.io.File getAssociatedFile()
Get the associated file.

Returns:
The associated file, or null if none.

setAssociatedFile

public final void setAssociatedFile(java.io.File file)
Set the associated file. Does not cause the properties to be re-read from the new file.

Parameters:
file - The associated file, or null if none.

save

public final void save()
                throws GrinderProperties.PersistenceException
Save our properties to our file.

Throws:
GrinderProperties.PersistenceException - If there is no file associated with this GrinderProperties or an I/O exception occurs..

saveSingleProperty

public final void saveSingleProperty(java.lang.String name)
                              throws GrinderProperties.PersistenceException
Save a single property to our file.

Parameters:
name - Property name.
Throws:
GrinderProperties.PersistenceException - If there is no file associated with this GrinderProperties or an I/O exception occurs.

setErrorWriter

public final void setErrorWriter(java.io.PrintWriter writer)
Set a writer to report warnings to.

Parameters:
writer - The writer.

getPropertySubset

public final GrinderProperties getPropertySubset(java.lang.String prefix)
Return a new GrinderProperties that contains the subset of our Properties which begin with the specified prefix.

Parameters:
prefix - The prefix.
Returns:
The subset.

getInt

public final int getInt(java.lang.String propertyName,
                        int defaultValue)
Get the value of the property with the given name, return the value as an int.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist or is not an integer.
Returns:
The value.

setInt

public final void setInt(java.lang.String propertyName,
                         int value)
Set the property with the given name to an int value.

Parameters:
propertyName - The property name.
value - The value to set.

getLong

public final long getLong(java.lang.String propertyName,
                          long defaultValue)
Get the value of the property with the given name, return the value as a long.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist or is not a long.
Returns:
The value.

setLong

public final void setLong(java.lang.String propertyName,
                          long value)
Set the property with the given name to a long value.

Parameters:
propertyName - The property name.
value - The value to set.

getShort

public final short getShort(java.lang.String propertyName,
                            short defaultValue)
Get the value of the property with the given name, return the value as a short.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist or is not a short.
Returns:
The value.

setShort

public final void setShort(java.lang.String propertyName,
                           short value)
Set the property with the given name to a short value.

Parameters:
propertyName - The property name.
value - The value to set.

getDouble

public final double getDouble(java.lang.String propertyName,
                              double defaultValue)
Get the value of the property with the given name, return the value as a double.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist or is not a double.
Returns:
The value.

setDouble

public final void setDouble(java.lang.String propertyName,
                            double value)
Set the property with the given name to a double value.

Parameters:
propertyName - The property name.
value - The value to set.

getBoolean

public final boolean getBoolean(java.lang.String propertyName,
                                boolean defaultValue)
Get the value of the property with the given name, return the value as a boolean.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist.
Returns:
The value.

setBoolean

public final void setBoolean(java.lang.String propertyName,
                             boolean value)
Set the property with the given name to a boolean value.

Parameters:
propertyName - The property name.
value - The value to set.

getFile

public final java.io.File getFile(java.lang.String propertyName,
                                  java.io.File defaultValue)
Get the value of the property with the given name, return the value as a File.

Parameters:
propertyName - The property name.
defaultValue - The value to return if a property with the given name does not exist.
Returns:
The value.

resolveRelativeFile

public final java.io.File resolveRelativeFile(java.io.File file)
Returns a File representing the combined path of our associated property file directory and the passed file.

If file is absolute, or this GrinderProperties has no associated property file, return file.

Parameters:
file - The file.
Returns:
If possible, file resolved relative to the directory of our associated properties file, or file.

setFile

public final void setFile(java.lang.String propertyName,
                          java.io.File value)
Set the property with the given name to a File value.

Parameters:
propertyName - The property name.
value - The value to set.