org.xnap.commons.settings
Class PropertyResource

java.lang.Object
  extended by org.xnap.commons.settings.PropertyResource
All Implemented Interfaces:
SettingResource

public class PropertyResource
extends Object
implements SettingResource

This class provides a default implementation for a preferences framework. Methods are provided that can read and write native types, arrays and a few custom types.


Field Summary
protected  boolean changedFlag
          Determines if preferences need to be saved.
protected static org.apache.commons.logging.Log logger
           
protected  String namespace
          The namespace is prefixed to each key.
protected  int oldVersion
          Version of database when read.
protected  Properties properties
          Preferences.
protected  PropertyChangeSupport propertyChange
           
protected  int version
          Version of database format.
 
Constructor Summary
PropertyResource()
           
PropertyResource(int version, String namespace)
          Constructs a PreferencesSupport object.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener l)
          Adds a preferences listener.
 void addPropertyChangeListener(String key, PropertyChangeListener l)
          Adds a preferences listener for a specific key.
protected  void convert(int oldVersion)
          Invoked by load(File) to converts preferences from oldVersion to current version.
 boolean equals(Object o)
          Returns true if o is the same object or if its version number, namespace and properties map are equal to this' fields.
 void firePropertyChange(String key, Object oldValue, Object newValue)
          Fires PropertyChangeEvent without namespace.
 String get(String key, String defaultValue)
          Returns the value to which the specified key is mapped.
 int getOldVersion()
          Returns the version of the preferences in the file at the point of the last read operation.
 int getVersion()
          Returns the current version of the preferences.
 void load(File prefsFile)
          Reads preferences from prefsFile.
 void put(String key, String newValue)
           
 void remove(String key)
          Ignores namespace.
 void removePropertyChangeListener(PropertyChangeListener l)
          Removes a preferences listener.
 void removePropertyChangeListener(String key, PropertyChangeListener l)
           
 void renameProperty(String oldKey, String newKey)
          Renames a property, used for conversion of property file formats.
 boolean store(File prefsFile)
          Writes preferences to default preferences file.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static org.apache.commons.logging.Log logger

propertyChange

protected transient PropertyChangeSupport propertyChange

changedFlag

protected boolean changedFlag
Determines if preferences need to be saved.


version

protected int version
Version of database format.


oldVersion

protected int oldVersion
Version of database when read.


namespace

protected String namespace
The namespace is prefixed to each key.


properties

protected Properties properties
Preferences.

Constructor Detail

PropertyResource

public PropertyResource(int version,
                        String namespace)
Constructs a PreferencesSupport object.

Parameters:
version - the current version of the preferences, this version can be more recent than the version of the file
namespace - the namespace, used as a prefix for all keys

PropertyResource

public PropertyResource()
Method Detail

load

public void load(File prefsFile)
          throws IOException
Reads preferences from prefsFile.

Throws:
IOException

store

public boolean store(File prefsFile)
              throws IOException
Writes preferences to default preferences file. If preferences have not changed since the last read or write operation, no action is taken.

Returns:
true, if file is written successfully or preferences were not changed; false, if an IOException has occured
Throws:
IOException

equals

public boolean equals(Object o)
Returns true if o is the same object or if its version number, namespace and properties map are equal to this' fields.

Overrides:
equals in class Object

getOldVersion

public int getOldVersion()
Returns the version of the preferences in the file at the point of the last read operation.


getVersion

public int getVersion()
Returns the current version of the preferences.


convert

protected void convert(int oldVersion)
Invoked by load(File) to converts preferences from oldVersion to current version.


addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener l)
Adds a preferences listener.


addPropertyChangeListener

public void addPropertyChangeListener(String key,
                                      PropertyChangeListener l)
Adds a preferences listener for a specific key.

Specified by:
addPropertyChangeListener in interface SettingResource

firePropertyChange

public void firePropertyChange(String key,
                               Object oldValue,
                               Object newValue)
Fires PropertyChangeEvent without namespace.

Specified by:
firePropertyChange in interface SettingResource

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener l)
Removes a preferences listener.


removePropertyChangeListener

public void removePropertyChangeListener(String key,
                                         PropertyChangeListener l)
Specified by:
removePropertyChangeListener in interface SettingResource

get

public String get(String key,
                  String defaultValue)
Description copied from interface: SettingResource
Returns the value to which the specified key is mapped.

Specified by:
get in interface SettingResource

put

public void put(String key,
                String newValue)
Specified by:
put in interface SettingResource

remove

public void remove(String key)
Ignores namespace.

Specified by:
remove in interface SettingResource

renameProperty

public void renameProperty(String oldKey,
                           String newKey)
Renames a property, used for conversion of property file formats. Ignores namespace. Does not fire change event.



Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.