org.entityfs.util.properties
Class FileBackedProperties

java.lang.Object
  extended by org.entityfs.util.properties.FileBackedProperties
All Implemented Interfaces:
Map<String,String>, Properties

public class FileBackedProperties
extends Object
implements Properties

This is an implementation of Properties that is backed by a file.

Properties written to this object are also written to the file, either directly when the property is written if autocommit is used, or when commit() is called.

The reload() method can be used to reload properties if the backing file has been modified.

An instance of this class is not thread safe. If it is to be used concurrently by several threads, the client code must make sure that it is properly protected by locks.

Since:
1.0
Author:
Karl Gustafsson

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
 
Fields inherited from interface org.entityfs.util.properties.Properties
STRING_ARRAY_DELIMITER, STRING_ARRAY_SPLIT_REGEXP
 
Constructor Summary
FileBackedProperties(ReadWritableFile f)
          Create a file-based properties object.
FileBackedProperties(ReadWritableFile f, boolean autoCommit)
           
 
Method Summary
 void clear()
           
 void commit()
          Commit all modified properties to the backing file.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<String,String>> entrySet()
           
 String get(Object key)
           
 Properties getAsJavaProperties()
          Get the contents of this properties object in a Properties object.
 boolean getBooleanValue(String key)
          Get a boolean property value.
 boolean getBooleanValue(String key, boolean def)
          Get a boolean property value.
 double getDoubleValue(String key)
          Get a double property value.
 double getDoubleValue(String key, double def)
          Get a double property value.
 float getFloatValue(String key)
          Get a float property value.
 float getFloatValue(String key, float def)
          Get a float property value.
 int getIntValue(String key)
          Get an int property value.
 int getIntValue(String key, int def)
          Get an int property value.
 long getLongValue(String key)
          Get a long property value.
 long getLongValue(String key, long def)
          Get a long property value.
 Serializable getObjectValue(String key)
          Get an Object (Serializable) property value.
 Serializable getObjectValue(String key, Serializable def)
          Get an Object (Serializable) property value.
 PropertiesImpl getPropertiesMatching(Glob g)
          Get all properties whose names match the supplied glob pattern.
 PropertiesImpl getPropertiesMatching(Pattern p)
          Get all properties whose names match the supplied regular expression pattern.
 PropertyValue getPropertyValue(String key)
          Return a property value as a PropertyValue object.
 PropertyValue getPropertyValue(String key, PropertyValue defaultVal)
          Get a property value.
 String[] getSplitStringArrayValue(String key, String regexp)
          Return a String property value converted to a String array by splitting it with the provided regular expression.
 String[] getSplitStringArrayValue(String key, String regexp, String[] def)
          Return a comma-separated String property value converted to a String array by splitting it with the provided regular expression.
 String[] getStringArrayValue(String key)
          Return a comma-separated String property value converted to a String array.
 String[] getStringArrayValue(String key, String[] def)
          Return a comma-separated String property value converted to a String array.
 String getStringValue(String key)
          Get a String property value.
 String getStringValue(String key, String def)
          Get a String property value.
 boolean isEmpty()
           
 Set<String> keySet()
           
 Set<Map.Entry<String,PropertyValue>> propertyEntrySet()
          Get an entry set.
 Collection<PropertyValue> propertyValues()
          Get a collection of property values.
 String put(String key, String value)
           
 void putAll(Map<? extends String,? extends String> m)
           
 void putAll(Properties p)
          Add all properties from the supplied Properties object.
 String putBooleanValue(String key, boolean value)
          Set a property with a boolean value.
 String putDoubleValue(String key, double value)
          Set a property with a double value.
 String putFloatValue(String key, float value)
          Set a property with a float value.
 String putIntValue(String key, int value)
          Set a property with an int value.
 String putLongValue(String key, long value)
          Set a property with a long value.
 String putObjectValue(String key, Serializable value)
          Set a property with an Object (Serializable) value.
 String putPropertyValue(String key, PropertyValue defaultVal)
          Set a property value.
 String putStringArrayValue(String key, String[] value)
          Set a property with a String array value.
 String putStringArrayValue(String key, String[] value, String delimiter)
          Set a property with a String array value.
 String putStringValue(String key, String value)
          Set a property with a String value.
 void reload()
          Reload properties from the backing file.
 String remove(Object key)
           
 int size()
           
 Collection<String> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

FileBackedProperties

public FileBackedProperties(ReadWritableFile f)
Create a file-based properties object. Properties that are written to this object are automatically committed to the backing file when they are written. (The property file will be locked for writing while the properties are being written.)

Parameters:
f - The property file.

FileBackedProperties

public FileBackedProperties(ReadWritableFile f,
                            boolean autoCommit)
Method Detail

getLongValue

public long getLongValue(String key)
                  throws NoSuchPropertyException,
                         NumberFormatException
Description copied from interface: Properties
Get a long property value.

Specified by:
getLongValue in interface Properties
Parameters:
key - The property key.
Returns:
The long value of the property.
Throws:
NoSuchPropertyException - If the property is missing.
NumberFormatException - If the property value cannot be converted to a long.

getLongValue

public long getLongValue(String key,
                         long def)
                  throws NumberFormatException
Description copied from interface: Properties
Get a long property value. If the property is missing, return a default value.

Specified by:
getLongValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing.
Returns:
The long value of the property or the default value if the property is missing.
Throws:
NumberFormatException - If the property value cannot be converted to a long.

putLongValue

public String putLongValue(String key,
                           long value)
Description copied from interface: Properties
Set a property with a long value.

Specified by:
putLongValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getIntValue

public int getIntValue(String key)
                throws NoSuchPropertyException,
                       NumberFormatException
Description copied from interface: Properties
Get an int property value.

Specified by:
getIntValue in interface Properties
Parameters:
key - The property key.
Returns:
The int value of the property.
Throws:
NoSuchPropertyException - If the property is missing.
NumberFormatException - If the property value cannot be converted to an int.

getIntValue

public int getIntValue(String key,
                       int def)
                throws NumberFormatException
Description copied from interface: Properties
Get an int property value. If the property is missing, return a default value.

Specified by:
getIntValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing.
Returns:
The int value of the property or the default value if the property is missing.
Throws:
NumberFormatException - If the property value cannot be converted to an int.

putIntValue

public String putIntValue(String key,
                          int value)
Description copied from interface: Properties
Set a property with an int value.

Specified by:
putIntValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getFloatValue

public float getFloatValue(String key)
                    throws NoSuchPropertyException,
                           NumberFormatException
Description copied from interface: Properties
Get a float property value.

Specified by:
getFloatValue in interface Properties
Parameters:
key - The property key.
Returns:
The float value of the property.
Throws:
NoSuchPropertyException - If the property is missing.
NumberFormatException - If the property value cannot be converted to a float.

getFloatValue

public float getFloatValue(String key,
                           float def)
                    throws NumberFormatException
Description copied from interface: Properties
Get a float property value. If the property is missing, return a default value.

Specified by:
getFloatValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing.
Returns:
The float value of the property or the default value if the property is missing.
Throws:
NumberFormatException - If the property value cannot be converted to float.

putFloatValue

public String putFloatValue(String key,
                            float value)
Description copied from interface: Properties
Set a property with a float value.

Specified by:
putFloatValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getDoubleValue

public double getDoubleValue(String key)
                      throws NoSuchPropertyException,
                             NumberFormatException
Description copied from interface: Properties
Get a double property value.

Specified by:
getDoubleValue in interface Properties
Parameters:
key - The property key.
Returns:
The double value of the property.
Throws:
NoSuchPropertyException - If the property is missing.
NumberFormatException - If the property value cannot be converted to a double.

getDoubleValue

public double getDoubleValue(String key,
                             double def)
                      throws NumberFormatException
Description copied from interface: Properties
Get a double property value. If the property is missing, return a default value.

Specified by:
getDoubleValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing.
Returns:
The double value of the property or the default value if the property is missing.
Throws:
NumberFormatException - If the property value cannot be converted to double.

putDoubleValue

public String putDoubleValue(String key,
                             double value)
Description copied from interface: Properties
Set a property with a double value.

Specified by:
putDoubleValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getBooleanValue

public boolean getBooleanValue(String key)
                        throws NoSuchPropertyException
Description copied from interface: Properties
Get a boolean property value.

Specified by:
getBooleanValue in interface Properties
Parameters:
key - The property key.
Returns:
The boolean value of the property.
Throws:
NoSuchPropertyException - If the property is missing.

getBooleanValue

public boolean getBooleanValue(String key,
                               boolean def)
Description copied from interface: Properties
Get a boolean property value. If the property is missing, return a default value.

Specified by:
getBooleanValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing.
Returns:
The boolean value of the property or the default value if the property is missing.

putBooleanValue

public String putBooleanValue(String key,
                              boolean value)
Description copied from interface: Properties
Set a property with a boolean value.

Specified by:
putBooleanValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getPropertyValue

public PropertyValue getPropertyValue(String key)
                               throws NoSuchPropertyException
Description copied from interface: Properties
Return a property value as a PropertyValue object.

Specified by:
getPropertyValue in interface Properties
Parameters:
key - The property key.
Returns:
The property value.
Throws:
NoSuchPropertyException - If the property is missing.
See Also:
Properties.getPropertyValue(String, PropertyValue)

getPropertyValue

public PropertyValue getPropertyValue(String key,
                                      PropertyValue defaultVal)
Description copied from interface: Properties
Get a property value. If the property is not set, return a default value.

Specified by:
getPropertyValue in interface Properties
Parameters:
key - The property key.
defaultVal - The default value to return if the property is not set. This may be null.
Returns:
The property value, or a default value if the property is not set.
See Also:
Properties.getPropertyValue(String)

putPropertyValue

public String putPropertyValue(String key,
                               PropertyValue defaultVal)
Description copied from interface: Properties
Set a property value.

Specified by:
putPropertyValue in interface Properties
Parameters:
key - The property name.
defaultVal - The property value.
Returns:
The previous value for the property, or null if it was not set.

getObjectValue

public Serializable getObjectValue(String key)
                            throws NoSuchPropertyException,
                                   WrappedIOException
Description copied from interface: Properties
Get an Object (Serializable) property value.

Specified by:
getObjectValue in interface Properties
Parameters:
key - The property key.
Returns:
The Object value of the property.
Throws:
NoSuchPropertyException - If the property is missing.
WrappedIOException - On errors when deserializing the property value.

getObjectValue

public Serializable getObjectValue(String key,
                                   Serializable def)
                            throws WrappedIOException
Description copied from interface: Properties
Get an Object (Serializable) property value. If the property is missing, return a default value.

Specified by:
getObjectValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing. The value may be null.
Returns:
The boolean value of the property or the default value if the property is missing.
Throws:
WrappedIOException - On errors when deserializing the property value.

putObjectValue

public String putObjectValue(String key,
                             Serializable value)
Description copied from interface: Properties
Set a property with an Object (Serializable) value.

Specified by:
putObjectValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getStringValue

public String getStringValue(String key)
                      throws NoSuchPropertyException
Description copied from interface: Properties
Get a String property value.

Specified by:
getStringValue in interface Properties
Parameters:
key - The property key.
Returns:
The String value of the property.
Throws:
NoSuchPropertyException - If the property is missing.

getStringValue

public String getStringValue(String key,
                             String def)
Description copied from interface: Properties
Get a String property value. If the property is missing, return a default value.

Specified by:
getStringValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing. The value may be null.
Returns:
The String value of the property or the default value if the property is missing.

putStringValue

public String putStringValue(String key,
                             String value)
Description copied from interface: Properties
Set a property with a String value.

Specified by:
putStringValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getStringArrayValue

public String[] getStringArrayValue(String key)
                             throws NoSuchPropertyException
Description copied from interface: Properties
Return a comma-separated String property value converted to a String array.

If the property value is empty, an array of length 0 is returned.

Specified by:
getStringArrayValue in interface Properties
Parameters:
key - The property key.
Returns:
The property value, split around the regexp "\\s*,\\s*".
Throws:
NoSuchPropertyException - If the property is missing

getStringArrayValue

public String[] getStringArrayValue(String key,
                                    String[] def)
Description copied from interface: Properties
Return a comma-separated String property value converted to a String array. If the property is missing, return a default value.

If the property value is empty, an array of length 0 is returned.

Specified by:
getStringArrayValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing. The value may be null.
Returns:
The property value, split around the regexp "\\s*,\\s*" or the default value if the property is missing.

getSplitStringArrayValue

public String[] getSplitStringArrayValue(String key,
                                         String regexp)
                                  throws NoSuchPropertyException
Description copied from interface: Properties
Return a String property value converted to a String array by splitting it with the provided regular expression.

If the property value is empty, an array of length 0 is returned.

Specified by:
getSplitStringArrayValue in interface Properties
Parameters:
key - The property key.
regexp - The regular expression.
Returns:
The property value.
Throws:
NoSuchPropertyException - If the property is missing.

getSplitStringArrayValue

public String[] getSplitStringArrayValue(String key,
                                         String regexp,
                                         String[] def)
Description copied from interface: Properties
Return a comma-separated String property value converted to a String array by splitting it with the provided regular expression. If the property is missing, return a default value.

If the property value is empty, an array of length 0 is returned.

Specified by:
getSplitStringArrayValue in interface Properties
Parameters:
key - The property key.
def - The default value to return if the property is missing. The value may be null.
Returns:
The property value or the default value if the property is missing.

putStringArrayValue

public String putStringArrayValue(String key,
                                  String[] value)
Description copied from interface: Properties
Set a property with a String array value. Use the default delimiter (",") to separate the values.

Specified by:
putStringArrayValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

putStringArrayValue

public String putStringArrayValue(String key,
                                  String[] value,
                                  String delimiter)
Description copied from interface: Properties
Set a property with a String array value. Use the provided delimiter to separate the values.

Specified by:
putStringArrayValue in interface Properties
Parameters:
key - The property key.
value - The property value.
Returns:
The property's old value, or null if it was not set before.

getAsJavaProperties

public Properties getAsJavaProperties()
Description copied from interface: Properties
Get the contents of this properties object in a Properties object.

Specified by:
getAsJavaProperties in interface Properties
Returns:
A Properties object with the same contents as this object.

putAll

public void putAll(Properties p)
Description copied from interface: Properties
Add all properties from the supplied Properties object. Existing properties that have the same name as properties in the supplied object will be overwritten.

Note that, since this interface extends Map, calling putAll with a Properties object of this type will also work.

Specified by:
putAll in interface Properties
Parameters:
p - A Properties object.

clear

public void clear()
Specified by:
clear in interface Map<String,String>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<String,String>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<String,String>

entrySet

public Set<Map.Entry<String,String>> entrySet()
Specified by:
entrySet in interface Map<String,String>

get

public String get(Object key)
Specified by:
get in interface Map<String,String>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<String,String>

keySet

public Set<String> keySet()
Specified by:
keySet in interface Map<String,String>

put

public String put(String key,
                  String value)
Specified by:
put in interface Map<String,String>

putAll

public void putAll(Map<? extends String,? extends String> m)
Specified by:
putAll in interface Map<String,String>

remove

public String remove(Object key)
Specified by:
remove in interface Map<String,String>

size

public int size()
Specified by:
size in interface Map<String,String>

values

public Collection<String> values()
Specified by:
values in interface Map<String,String>

getPropertiesMatching

public PropertiesImpl getPropertiesMatching(Pattern p)
Get all properties whose names match the supplied regular expression pattern. The returned Properties object is not file-backed.

Specified by:
getPropertiesMatching in interface Properties
Parameters:
p - The regular expression pattern to match property names against.
Returns:
A Properties object containing the properties from this object whose names match the regular expression pattern. If no property names matched the pattern, this object is empty.
See Also:
Properties.getPropertiesMatching(Glob)

getPropertiesMatching

public PropertiesImpl getPropertiesMatching(Glob g)
Get all properties whose names match the supplied glob pattern. The returned Properties object is not file-backed.

Specified by:
getPropertiesMatching in interface Properties
Parameters:
g - The glob pattern to match property names against.
Returns:
A Properties object containing the properties from this object whose names match the glob pattern. If no property names matched the pattern, this object is empty.
See Also:
Properties.getPropertiesMatching(Pattern)

propertyEntrySet

public Set<Map.Entry<String,PropertyValue>> propertyEntrySet()
Description copied from interface: Properties
Get an entry set. This can be used instead of Map.entrySet() to get PropertyValue values in the entry set.

The returned set is read only.

Specified by:
propertyEntrySet in interface Properties
Returns:
The entry set.

propertyValues

public Collection<PropertyValue> propertyValues()
Description copied from interface: Properties
Get a collection of property values. This can be used instead of Map.values() to get PropertyValue objects instead of strings.

The returned collection is read only.

Specified by:
propertyValues in interface Properties
Returns:
A collection of property values.

commit

public void commit()
Commit all modified properties to the backing file.


reload

public void reload()
Reload properties from the backing file.