|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.prefs.Preferences
org.tentackle.db.DbPreferences
public class DbPreferences
Database backend for the Preferences API.
Tentackle preferences are stored in a database rather than a file(unix) or registry(windows).
The persistent classes are:
DbPreferencesNode: the nodes representing the preferences hierarchy.DbPreferencesKey: the key/value pairs corresponding to the nodes.ModificationThread by use of the tableSerial.
Notice that events in the local JVM are enqueued at the time of change (i.e. put(),
remove(), node() or removeNode()) while other JVMs will become aware of the changes
only after the changes are flush()ed to persistent storage.
The underlying mechanism works as follows:
ModificationCounter for details).
AbstractPreferences (which we can not simply extend
due to various design issues), the DbPreferences differ slightly in the following aspects:
| Field Summary | |
|---|---|
static String |
TX_FLUSH
transaction name for flush |
static String |
TX_REMOVE_NODE
transaction name for remove node |
| Fields inherited from class java.util.prefs.Preferences |
|---|
MAX_KEY_LENGTH, MAX_NAME_LENGTH, MAX_VALUE_LENGTH |
| Constructor Summary | |
|---|---|
protected |
DbPreferences(boolean userMode)
Special constructor for roots (both user and system). |
protected |
DbPreferences(DbPreferences parent,
DbPreferencesNode node)
Constructs DbPreferences from a DbPreferencesNode. |
|
DbPreferences(DbPreferences parent,
String name)
Creates a preference node with the specified parent and the specified name relative to its parent. |
| Method Summary | |
|---|---|
String |
absolutePath()
Returns this preference node's absolute path name. |
void |
addNodeChangeListener(NodeChangeListener ncl)
Registers the specified listener to receive node change events for this node. |
void |
addPreferenceChangeListener(PreferenceChangeListener pcl)
Registers the specified listener to receive preference change events for this preference node. |
String[] |
childrenNames()
Returns the names of the children of this preference node, relative to this node. |
void |
clear()
Removes all of the preferences (key-value associations) in this preference node. |
static void |
expireKeys(Db db,
long maxSerial)
Expire the preferences keys. Invoked from the ModificationThread (registered in DbPreferencesKey) |
static void |
expireNodes(Db db,
long maxSerial)
Expires the preferences nodes. Invoked from ModificationThread (registered in DbPreferencesNode) |
void |
exportNode(OutputStream os)
Emits on the specified output stream an XML document representing all of the preferences contained in this node (but not its descendants). |
void |
exportSubtree(OutputStream os)
Emits an XML document representing all of the preferences contained in this node and all of its descendants. |
void |
flush()
Forces any changes in the contents of this preference node and its descendants to the persistent store. |
String |
get(String key,
String def)
Returns the value associated with the specified key in this preference node. |
boolean |
getBoolean(String key,
boolean def)
Returns the boolean value represented by the string associated with the specified key in this preference node. |
byte[] |
getByteArray(String key,
byte[] def)
Returns the byte array value represented by the string associated with the specified key in this preference node. |
static Db |
getDb()
Gets the database connection. |
double |
getDouble(String key,
double def)
Returns the double value represented by the string associated with the specified key in this preference node. |
float |
getFloat(String key,
float def)
Returns the float value represented by the string associated with the specified key in this preference node. |
int |
getInt(String key,
int def)
Returns the int value represented by the string associated with the specified key in this preference node. |
long |
getLong(String key,
long def)
Returns the long value represented by the string associated with the specified key in this preference node. |
static void |
importPreferences(InputStream is)
Imports all of the preferences represented by the XML document as in Preferences. |
static void |
initialize(Db prefDb,
boolean sync)
initialize the preferences. |
static boolean |
isAutoSync()
Determines whether the preferences are automatically sync'd to the database. |
boolean |
isReadOnly()
Determines whether preferences are read only. |
boolean |
isUserNode()
Returns true if this preference node is in the user preference tree, false if it's in the system preference tree. |
String[] |
keys()
Returns all of the keys that have an associated value in this preference node. |
String |
name()
Returns this preference node's name, relative to its parent. |
Preferences |
node(String path)
Returns the named preference node in the same tree as this node, creating it and any of its ancestors if they do not already exist. |
boolean |
nodeExists(String pathName)
Returns true if the named preference node exists in the same tree as this node. |
Preferences |
parent()
Returns the parent of this preference node, or null if this is the root. |
void |
put(String key,
String value)
Associates the specified value with the specified key in this preference node. |
void |
putBoolean(String key,
boolean value)
Associates a string representing the specified boolean value with the specified key in this preference node. |
void |
putByteArray(String key,
byte[] value)
Associates a string representing the specified byte array with the specified key in this preference node. |
void |
putDouble(String key,
double value)
Associates a string representing the specified double value with the specified key in this preference node. |
void |
putFloat(String key,
float value)
Associates a string representing the specified float value with the specified key in this preference node. |
void |
putInt(String key,
int value)
Associates a string representing the specified int value with the specified key in this preference node. |
void |
putLong(String key,
long value)
Associates a string representing the specified long value with the specified key in this preference node. |
void |
remove(String key)
Removes the value associated with the specified key in this preference node, if any. |
void |
removeNode()
Removes this preference node and all of its descendants, invalidating any preferences contained in the removed nodes. |
void |
removeNodeChangeListener(NodeChangeListener ncl)
Removes the specified NodeChangeListener, so it no longer receives change events. |
void |
removePreferenceChangeListener(PreferenceChangeListener pcl)
Removes the specified preference change listener, so it no longer receives preference change events. |
static void |
setReadOnly(boolean readOnly)
Sets the preferences to read only globally. |
void |
sync()
Ensures that future reads from this preference node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to the sync invocation. |
String |
toString()
Returns a string representation of this preferences node. if node is deleted: "<absolutePath> [deleted]" user node: "<user>:<absolutePath>" system node: "<system>:<absolutePath>" |
protected void |
updateParentNode(DbPreferencesNode node)
Updates the serial and tableserial of a parent node. |
| Methods inherited from class java.util.prefs.Preferences |
|---|
systemNodeForPackage, systemRoot, userNodeForPackage, userRoot |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final String TX_FLUSH
public static final String TX_REMOVE_NODE
| Constructor Detail |
|---|
public DbPreferences(DbPreferences parent,
String name)
parent - the parent of this preference nodename - the name of this preference node, relative to its parent
IllegalArgumentException - if name contains a slash
('/'), or parent is null and
name isn't "".protected DbPreferences(boolean userMode)
userMode - true if user mode, else system mode
protected DbPreferences(DbPreferences parent,
DbPreferencesNode node)
throws BackingStoreException
parent - the parent preferencesnode - the node
BackingStoreException| Method Detail |
|---|
public static void setReadOnly(boolean readOnly)
readOnly - true for read only, false for read/write.public boolean isReadOnly()
public static void initialize(Db prefDb,
boolean sync)
prefDb - the database connectionsync - true for autoSync, else syncObject() must be invoked explicitlypublic static Db getDb()
public static boolean isAutoSync()
public static void expireKeys(Db db,
long maxSerial)
db - is the Modthreads db connectionmaxSerial - is the current tableserial
public static void expireNodes(Db db,
long maxSerial)
db - is the Modthreads db connectionmaxSerial - is the current tableserial
protected void updateParentNode(DbPreferencesNode node)
throws BackingStoreException
node - the parent node to update
BackingStoreException - if update failedpublic String toString()
toString in class Preferencespublic String name()
java.util.prefs.Preferences
name in class Preferencespublic String absolutePath()
java.util.prefs.Preferences
absolutePath in class Preferencespublic boolean isUserNode()
java.util.prefs.Preferences
isUserNode in class Preferences
public void put(String key,
String value)
java.util.prefs.Preferences
put in class Preferenceskey - key with which the specified value is to be associated.value - value to be associated with the specified key.
public String get(String key,
String def)
java.util.prefs.PreferencesSome implementations may store default values in their backing stores. If there is no value associated with the specified key but there is such a stored default, it is returned in preference to the specified default.
get in class Preferenceskey - key whose associated value is to be returned.def - the value to be returned in the event that this
preference node has no value associated with key.
public void remove(String key)
java.util.prefs.PreferencesIf this implementation supports stored defaults, and there is such a default for the specified preference, the stored default will be "exposed" by this call, in the sense that it will be returned by a succeeding call to get.
remove in class Preferenceskey - key whose mapping is to be removed from the preference node.
public void clear()
throws BackingStoreException
java.util.prefs.PreferencesIf this implementation supports stored defaults, and this node in the preferences hierarchy contains any such defaults, the stored defaults will be "exposed" by this call, in the sense that they will be returned by succeeding calls to get.
clear in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.Preferences.removeNode()
public void sync()
throws BackingStoreException
java.util.prefs.Preferences
sync in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.Preferences.flush()
public void flush()
throws BackingStoreException
java.util.prefs.PreferencesImplementations are free to flush changes into the persistent store at any time. They do not need to wait for this method to be called.
When a flush occurs on a newly created node, it is made persistent, as are any ancestors (and descendants) that have yet to be made persistent. Note however that any preference value changes in ancestors are not guaranteed to be made persistent.
If this method is invoked on a node that has been removed with
the Preferences.removeNode() method, flushSpi() is invoked on this node,
but not on others.
flush in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.Preferences.sync()
public void putInt(String key,
int value)
java.util.prefs.PreferencesInteger.toString(int). This method is intended for use in
conjunction with Preferences.getInt(java.lang.String, int).
putInt in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getInt(String,int)
public int getInt(String key,
int def)
java.util.prefs.PreferencesInteger.parseInt(String). Returns the
specified default if there is no value associated with the key,
the backing store is inaccessible, or if
Integer.parseInt(String) would throw a NumberFormatException if the associated value were passed. This
method is intended for use in conjunction with Preferences.putInt(java.lang.String, int).
If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to an int with Integer.parseInt, this int is returned in preference to the specified default.
getInt in class Preferenceskey - key whose associated value is to be returned as an int.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as an int,
or the backing store is inaccessible.
Preferences.putInt(String,int),
Preferences.get(String,String)
public void putLong(String key,
long value)
java.util.prefs.PreferencesLong.toString(long). This method is intended for use in
conjunction with Preferences.getLong(java.lang.String, long).
putLong in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getLong(String,long)
public long getLong(String key,
long def)
java.util.prefs.PreferencesLong.parseLong(String). Returns the
specified default if there is no value associated with the key,
the backing store is inaccessible, or if
Long.parseLong(String) would throw a NumberFormatException if the associated value were passed. This
method is intended for use in conjunction with Preferences.putLong(java.lang.String, long).
If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a long with Long.parseLong, this long is returned in preference to the specified default.
getLong in class Preferenceskey - key whose associated value is to be returned as a long.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as a long,
or the backing store is inaccessible.
Preferences.putLong(String,long),
Preferences.get(String,String)
public void putBoolean(String key,
boolean value)
java.util.prefs.PreferencesPreferences.getBoolean(java.lang.String, boolean).
putBoolean in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getBoolean(String,boolean),
Preferences.get(String,String)
public boolean getBoolean(String key,
boolean def)
java.util.prefs.PreferencesPreferences.putBoolean(java.lang.String, boolean).
Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or if the associated value is something other than "true" or "false", ignoring case.
If the implementation supports stored defaults and such a default exists and is accessible, it is used in preference to the specified default, unless the stored default is something other than "true" or "false", ignoring case, in which case the specified default is used.
getBoolean in class Preferenceskey - key whose associated value is to be returned as a boolean.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as a boolean,
or the backing store is inaccessible.
Preferences.get(String,String),
Preferences.putBoolean(String,boolean)
public void putFloat(String key,
float value)
java.util.prefs.PreferencesFloat.toString(float). This method is intended for use in
conjunction with Preferences.getFloat(java.lang.String, float).
putFloat in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getFloat(String,float)
public float getFloat(String key,
float def)
java.util.prefs.PreferencesFloat.parseFloat(String). Returns the specified
default if there is no value associated with the key, the backing store
is inaccessible, or if Float.parseFloat(String) would throw a
NumberFormatException if the associated value were passed.
This method is intended for use in conjunction with Preferences.putFloat(java.lang.String, float).
If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a float with Float.parseFloat, this float is returned in preference to the specified default.
getFloat in class Preferenceskey - key whose associated value is to be returned as a float.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as a float,
or the backing store is inaccessible.
Preferences.putFloat(String,float),
Preferences.get(String,String)
public void putDouble(String key,
double value)
java.util.prefs.PreferencesDouble.toString(double). This method is intended for use in
conjunction with Preferences.getDouble(java.lang.String, double).
putDouble in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getDouble(String,double)
public double getDouble(String key,
double def)
java.util.prefs.PreferencesDouble.parseDouble(String). Returns the specified
default if there is no value associated with the key, the backing store
is inaccessible, or if Double.parseDouble(String) would throw a
NumberFormatException if the associated value were passed.
This method is intended for use in conjunction with Preferences.putDouble(java.lang.String, double).
If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a double with Double.parseDouble, this double is returned in preference to the specified default.
getDouble in class Preferenceskey - key whose associated value is to be returned as a double.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as a double,
or the backing store is inaccessible.
Preferences.putDouble(String,double),
Preferences.get(String,String)
public void putByteArray(String key,
byte[] value)
java.util.prefs.PreferencesPreferences.getByteArray(java.lang.String, byte[]).
putByteArray in class Preferenceskey - key with which the string form of value is to be associated.value - value whose string form is to be associated with key.Preferences.getByteArray(String,byte[]),
Preferences.get(String,String)
public byte[] getByteArray(String key,
byte[] def)
java.util.prefs.PreferencesPreferences.putByteArray(java.lang.String, byte[]).
Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or if the associated value is not a valid Base64 encoded byte array (as defined above).
If the implementation supports stored defaults and such a default exists and is accessible, it is used in preference to the specified default, unless the stored default is not a valid Base64 encoded byte array (as defined above), in which case the specified default is used.
getByteArray in class Preferenceskey - key whose associated value is to be returned as a byte array.def - the value to be returned in the event that this
preference node has no value associated with key
or the associated value cannot be interpreted as a byte array,
or the backing store is inaccessible.
Preferences.get(String,String),
Preferences.putByteArray(String,byte[])
public String[] keys()
throws BackingStoreException
java.util.prefs.PreferencesIf the implementation supports stored defaults and there are any such defaults at this node that have not been overridden, by explicit preferences, the defaults are returned in the array in addition to any explicit preferences.
keys in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.
public String[] childrenNames()
throws BackingStoreException
java.util.prefs.Preferences
childrenNames in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.public Preferences parent()
java.util.prefs.Preferences
parent in class Preferencespublic Preferences node(String path)
java.util.prefs.PreferencesIf the returned node did not exist prior to this call, this node and any ancestors that were created by this call are not guaranteed to become permanent until the flush method is called on the returned node (or one of its ancestors or descendants).
node in class Preferencespath - the path name of the preference node to return.
Preferences.flush()
public boolean nodeExists(String pathName)
throws BackingStoreException
If this node (or an ancestor) has already been removed with the
Preferences.removeNode() method, it is legal to invoke this method,
but only with the path name ""; the invocation will return
false. Thus, the idiom p.nodeExists("") may be
used to test whether p has been removed.
This implementation differs from AbstractPreferences because it checks the persistent storage rather than the cache, i.e. a node only created in memory but not yet written to persistent storage is considered non-existant. As a consequence it is possible to decide whether a system-preference has been overridden by a user-preference. (see CompositePreferences). Notice that "" will return true even if not written to disk to denote "not deleted" (which does not mean "saved" -- a little confusing)
nodeExists in class PreferencespathName - the path name of the node whose existence
is to be checked.
BackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.
public void removeNode()
throws BackingStoreException
java.util.prefs.PreferencesPreferences.name(),
Preferences.absolutePath(), Preferences.isUserNode(), Preferences.flush() or
nodeExists("") on the corresponding
Preferences instance will fail with an
IllegalStateException. (The methods defined on Object
can still be invoked on a node after it has been removed; they will not
throw IllegalStateException.)
The removal is not guaranteed to be persistent until the flush method is called on this node (or an ancestor).
If this implementation supports stored defaults, removing a node exposes any stored defaults at or below this node. Thus, a subsequent call to nodeExists on this node's path name may return true, and a subsequent call to node on this path name may return a (different) Preferences instance representing a non-empty collection of preferences and/or children.
removeNode in class PreferencesBackingStoreException - if this operation cannot be completed
due to a failure in the backing store, or inability to
communicate with it.Preferences.flush()public void addPreferenceChangeListener(PreferenceChangeListener pcl)
removeNode() method, which generates a node change event.
Preference change events are generated by the clear
method.)
Events are generated even for changes made outside this JVM. For the local JVM events are generated before the changes have been made persistent. For all other JVMs events are generated *after* flush()/syncObject().
addPreferenceChangeListener in class Preferencespcl - The preference change listener to add.
NullPointerException - if pcl is null.
IllegalStateException - if this node (or an ancestor) has been
removed with the removeNode() method.removePreferenceChangeListener(PreferenceChangeListener),
addNodeChangeListener(NodeChangeListener)public void removePreferenceChangeListener(PreferenceChangeListener pcl)
java.util.prefs.Preferences
removePreferenceChangeListener in class Preferencespcl - The preference change listener to remove.Preferences.addPreferenceChangeListener(PreferenceChangeListener)public void addNodeChangeListener(NodeChangeListener ncl)
removeNode()
invocation results in multiple node change events, one for every
node in the subtree rooted at the removed node.)
Events are generated even for changes made outside this JVM. For the local JVM events are generated before the changes have been made persistent. For all other JVMs events are generated *after* flush()/syncObject().
Node creation will always generate an even for the local JVM. Other JVMs get that event only in case the node is created on disk (and not updated in case another JVM already created that node).
addNodeChangeListener in class Preferencesncl - The NodeChangeListener to add.
NullPointerException - if ncl is null.
IllegalStateException - if this node (or an ancestor) has been
removed with the removeNode() method.removeNodeChangeListener(NodeChangeListener),
addPreferenceChangeListener(PreferenceChangeListener)public void removeNodeChangeListener(NodeChangeListener ncl)
java.util.prefs.Preferences
removeNodeChangeListener in class Preferencesncl - The NodeChangeListener to remove.Preferences.addNodeChangeListener(NodeChangeListener)
public void exportNode(OutputStream os)
throws IOException,
BackingStoreException
java.util.prefs.PreferencesThe XML document will have the following DOCTYPE declaration:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">The UTF-8 character encoding will be used.
This method is an exception to the general rule that the results of concurrently executing multiple methods in this class yields results equivalent to some serial execution. If the preferences at this node are modified concurrently with an invocation of this method, the exported preferences comprise a "fuzzy snapshot" of the preferences contained in the node; some of the concurrent modifications may be reflected in the exported data while others may not.
exportNode in class Preferencesos - the output stream on which to emit the XML document.
IOException - if writing to the specified output stream
results in an IOException.
BackingStoreException - if preference data cannot be read from
backing store.Preferences.importPreferences(InputStream)
public void exportSubtree(OutputStream os)
throws IOException,
BackingStoreException
java.util.prefs.PreferencesThe XML document will have the following DOCTYPE declaration:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">The UTF-8 character encoding will be used.
This method is an exception to the general rule that the results of concurrently executing multiple methods in this class yields results equivalent to some serial execution. If the preferences or nodes in the subtree rooted at this node are modified concurrently with an invocation of this method, the exported preferences comprise a "fuzzy snapshot" of the subtree; some of the concurrent modifications may be reflected in the exported data while others may not.
exportSubtree in class Preferencesos - the output stream on which to emit the XML document.
IOException - if writing to the specified output stream
results in an IOException.
BackingStoreException - if preference data cannot be read from
backing store.Preferences.importPreferences(InputStream),
Preferences.exportNode(OutputStream)
public static void importPreferences(InputStream is)
throws IOException,
InvalidPreferencesFormatException
CAUTION: As in Preferences this method is static, i.e. this one does *NOT* override Preferences.importPreferences()! YOU MUST INVOKE DbPreferences.importPreferences() directly!!! This is due to the design by Sun. Sun's implementation of Preferences.importPreferences() should better invoke a method in PreferencesFactory according to the property "java.util.prefs.PreferencesFactory". Instead it invokes (package scoped) XmlSupport :-(
is - the input stream from which to read the XML document.
IOException - if reading from the specified output stream
results in an IOException.
InvalidPreferencesFormatException - Data on input stream does not
constitute a valid XML document with the mandated document type.
SecurityException - If a security manager is present and
it denies RuntimePermission("preferences").RuntimePermission
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||