org.apache.felix.prefs
Class PreferencesImpl

java.lang.Object
  extended by org.apache.felix.prefs.PreferencesImpl
All Implemented Interfaces:
Preferences

public class PreferencesImpl
extends Object
implements Preferences

This is an implementation of the preferences. The access to the preferences is synchronized on the instance by making (nearly) all public methods synchronized. This avoids the heavy management of a separate read/write lock. Such a lock is too heavy for the simple operations preferences support. The various getXX and putXX methods are not synchronized as they all use the get/put methods which are synchronized.


Field Summary
protected  ChangeSet changeSet
          The change set keeps track of all changes.
protected  Map children
          The child nodes.
protected  PreferencesDescription description
          The description for this preferences.
protected  String name
          The name of the properties.
protected  PreferencesImpl parent
          The parent.
protected  Map properties
          The properties.
protected  BackingStoreManager storeManager
          The backing store manager.
protected  boolean valid
          Has this node been removed?
 
Constructor Summary
PreferencesImpl(PreferencesDescription d, BackingStoreManager storeManager)
          Construct the root node of the tree.
PreferencesImpl(PreferencesImpl p, String name)
          Construct a child node.
 
Method Summary
 String absolutePath()
           
 void applyChanges(PreferencesImpl prefs)
          Apply the changes done to the passed preferences object.
protected  void checkKey(String key)
          The key is not allowed to be null.
protected  void checkValidity()
          Check if this node is still valid.
protected  void checkValue(Object value)
          The value is not allowed to be null.
 String[] childrenNames()
           
 void clear()
           
 void flush()
           
 String get(String key, String def)
           
 BackingStoreManager getBackingStoreManager()
          Return the backing store manager.
 boolean getBoolean(String key, boolean def)
           
 byte[] getByteArray(String key, byte[] def)
           
 ChangeSet getChangeSet()
          Return the change set.
 Collection getChildren()
          Return all children or an empty collection.
 PreferencesDescription getDescription()
          Return the preferences description.
 double getDouble(String key, double def)
           
 float getFloat(String key, float def)
           
 int getInt(String key, int def)
           
 long getLong(String key, long def)
           
protected  PreferencesImpl getNode(String path, boolean saveNewlyCreatedNode, boolean create)
          Get a relative node.
 PreferencesImpl getOrCreateNode(String pathName)
          Get or create the node.
 Map getProperties()
          Return the properties set.
 PreferencesImpl getRoot()
          Get the root preferences.
 boolean isValid()
           
 String[] keys()
           
 String name()
           
 Preferences node(String pathName)
          We do not synchronize this method to avoid dead locks as this method might call another preferences object in the hierarchy.
 boolean nodeExists(String pathName)
          We do not synchronize this method to avoid dead locks as this method might call another preferences object in the hierarchy.
 Preferences parent()
           
 void put(String key, String value)
           
 void putBoolean(String key, boolean value)
           
 void putByteArray(String key, byte[] value)
           
 void putDouble(String key, double value)
           
 void putFloat(String key, float value)
           
 void putInt(String key, int value)
           
 void putLong(String key, long value)
           
 void remove(String key)
           
protected  void removeChild(PreferencesImpl child)
           
 void removeNode()
           
protected  void safelyRemoveNode()
          Safely remove a node by resetting all properties and calling this method on all children recursively.
 void sync()
           
 void update(PreferencesImpl impl)
          Update from the preferences impl.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

properties

protected final Map properties
The properties.


valid

protected boolean valid
Has this node been removed?


parent

protected final PreferencesImpl parent
The parent.


children

protected final Map children
The child nodes.


name

protected final String name
The name of the properties.


description

protected final PreferencesDescription description
The description for this preferences.


storeManager

protected final BackingStoreManager storeManager
The backing store manager.


changeSet

protected final ChangeSet changeSet
The change set keeps track of all changes.

Constructor Detail

PreferencesImpl

public PreferencesImpl(PreferencesDescription d,
                       BackingStoreManager storeManager)
Construct the root node of the tree.

Parameters:
d - The unique description.
storeManager - The backing store.

PreferencesImpl

public PreferencesImpl(PreferencesImpl p,
                       String name)
Construct a child node.

Parameters:
p - The parent node.
name - The node name
Method Detail

getChangeSet

public ChangeSet getChangeSet()
Return the change set.


getDescription

public PreferencesDescription getDescription()
Return the preferences description.

Returns:
The preferences description.

getRoot

public PreferencesImpl getRoot()
Get the root preferences.


getChildren

public Collection getChildren()
Return all children or an empty collection.

Returns:
A collection containing the children.

getProperties

public Map getProperties()
Return the properties set.


getBackingStoreManager

public BackingStoreManager getBackingStoreManager()
Return the backing store manager.


checkValidity

protected void checkValidity()
                      throws IllegalStateException
Check if this node is still valid. It gets invalid if it has been removed.

Throws:
IllegalStateException

checkKey

protected void checkKey(String key)
                 throws NullPointerException
The key is not allowed to be null.

Throws:
NullPointerException

checkValue

protected void checkValue(Object value)
                   throws NullPointerException
The value is not allowed to be null.

Throws:
NullPointerException

isValid

public boolean isValid()

put

public void put(String key,
                String value)
Specified by:
put in interface Preferences
See Also:
Preferences.put(java.lang.String, java.lang.String)

get

public String get(String key,
                  String def)
Specified by:
get in interface Preferences
See Also:
Preferences.get(java.lang.String, java.lang.String)

remove

public void remove(String key)
Specified by:
remove in interface Preferences
See Also:
Preferences.remove(java.lang.String)

clear

public void clear()
           throws BackingStoreException
Specified by:
clear in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.clear()

putInt

public void putInt(String key,
                   int value)
Specified by:
putInt in interface Preferences
See Also:
Preferences.putInt(java.lang.String, int)

getInt

public int getInt(String key,
                  int def)
Specified by:
getInt in interface Preferences
See Also:
Preferences.getInt(java.lang.String, int)

putLong

public void putLong(String key,
                    long value)
Specified by:
putLong in interface Preferences
See Also:
Preferences.putLong(java.lang.String, long)

getLong

public long getLong(String key,
                    long def)
Specified by:
getLong in interface Preferences
See Also:
Preferences.getLong(java.lang.String, long)

putBoolean

public void putBoolean(String key,
                       boolean value)
Specified by:
putBoolean in interface Preferences
See Also:
Preferences.putBoolean(java.lang.String, boolean)

getBoolean

public boolean getBoolean(String key,
                          boolean def)
Specified by:
getBoolean in interface Preferences
See Also:
Preferences.getBoolean(java.lang.String, boolean)

putFloat

public void putFloat(String key,
                     float value)
Specified by:
putFloat in interface Preferences
See Also:
Preferences.putFloat(java.lang.String, float)

getFloat

public float getFloat(String key,
                      float def)
Specified by:
getFloat in interface Preferences
See Also:
Preferences.getFloat(java.lang.String, float)

putDouble

public void putDouble(String key,
                      double value)
Specified by:
putDouble in interface Preferences
See Also:
Preferences.putDouble(java.lang.String, double)

getDouble

public double getDouble(String key,
                        double def)
Specified by:
getDouble in interface Preferences
See Also:
Preferences.getDouble(java.lang.String, double)

putByteArray

public void putByteArray(String key,
                         byte[] value)
Specified by:
putByteArray in interface Preferences
See Also:
Preferences.putByteArray(java.lang.String, byte[])

getByteArray

public byte[] getByteArray(String key,
                           byte[] def)
Specified by:
getByteArray in interface Preferences
See Also:
Preferences.getByteArray(java.lang.String, byte[])

keys

public String[] keys()
              throws BackingStoreException
Specified by:
keys in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.keys()

childrenNames

public String[] childrenNames()
                       throws BackingStoreException
Specified by:
childrenNames in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.childrenNames()

parent

public Preferences parent()
Specified by:
parent in interface Preferences
See Also:
Preferences.parent()

node

public Preferences node(String pathName)
We do not synchronize this method to avoid dead locks as this method might call another preferences object in the hierarchy.

Specified by:
node in interface Preferences
See Also:
Preferences.node(java.lang.String)

getOrCreateNode

public PreferencesImpl getOrCreateNode(String pathName)
Get or create the node. If the node already exists, it's just returned. If not it is created.

Parameters:
pathName -
Returns:
The preferences impl for the path.

getNode

protected PreferencesImpl getNode(String path,
                                  boolean saveNewlyCreatedNode,
                                  boolean create)
Get a relative node.

Parameters:
path -
Returns:

nodeExists

public boolean nodeExists(String pathName)
                   throws BackingStoreException
We do not synchronize this method to avoid dead locks as this method might call another preferences object in the hierarchy.

Specified by:
nodeExists in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.nodeExists(java.lang.String)

removeNode

public void removeNode()
                throws BackingStoreException
Specified by:
removeNode in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.removeNode()

safelyRemoveNode

protected void safelyRemoveNode()
Safely remove a node by resetting all properties and calling this method on all children recursively.


removeChild

protected void removeChild(PreferencesImpl child)

name

public String name()
Specified by:
name in interface Preferences
See Also:
Preferences.name()

absolutePath

public String absolutePath()
Specified by:
absolutePath in interface Preferences
See Also:
Preferences.absolutePath()

flush

public void flush()
           throws BackingStoreException
Specified by:
flush in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.flush()

sync

public void sync()
          throws BackingStoreException
Specified by:
sync in interface Preferences
Throws:
BackingStoreException
See Also:
Preferences.sync()

update

public void update(PreferencesImpl impl)
Update from the preferences impl.

Parameters:
impl -

applyChanges

public void applyChanges(PreferencesImpl prefs)
Apply the changes done to the passed preferences object.

Parameters:
prefs -


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.