public abstract class AbstractPreferences extends Preferences
Preferences
class, greatly easing the task of implementing it.
This class is for Preferences
implementers only.
Normal users of the Preferences
facility should have no need to
consult this documentation. The Preferences
documentation
should suffice.
Implementors must override the nine abstract service-provider interface
(SPI) methods: getSpi(String)
, putSpi(String,String)
,
removeSpi(String)
, childSpi(String)
, removeNodeSpi()
, keysSpi()
, childrenNamesSpi()
, syncSpi()
and flushSpi()
. All of the concrete methods specify
precisely how they are implemented atop these SPI methods. The implementor
may, at his discretion, override one or more of the concrete methods if the
default implementation is unsatisfactory for any reason, such as
performance.
The SPI methods fall into three groups concerning exception
behavior. The getSpi
method should never throw exceptions, but it
doesn't really matter, as any exception thrown by this method will be
intercepted by get(String,String)
, which will return the specified
default value to the caller. The removeNodeSpi, keysSpi,
childrenNamesSpi, syncSpi
and flushSpi
methods are specified
to throw BackingStoreException
, and the implementation is required
to throw this checked exception if it is unable to perform the operation.
The exception propagates outward, causing the corresponding API method
to fail.
The remaining SPI methods putSpi(String,String)
, removeSpi(String)
and childSpi(String)
have more complicated
exception behavior. They are not specified to throw
BackingStoreException
, as they can generally obey their contracts
even if the backing store is unavailable. This is true because they return
no information and their effects are not required to become permanent until
a subsequent call to Preferences.flush()
or
Preferences.sync()
. Generally speaking, these SPI methods should not
throw exceptions. In some implementations, there may be circumstances
under which these calls cannot even enqueue the requested operation for
later processing. Even under these circumstances it is generally better to
simply ignore the invocation and return, rather than throwing an
exception. Under these circumstances, however, subsequently invoking
flush()
or sync
would not imply that all previous
operations had successfully been made permanent.
There is one circumstance under which putSpi, removeSpi and
childSpi
should throw an exception: if the caller lacks
sufficient privileges on the underlying operating system to perform the
requested operation. This will, for instance, occur on most systems
if a non-privileged user attempts to modify system preferences.
(The required privileges will vary from implementation to
implementation. On some implementations, they are the right to modify the
contents of some directory in the file system; on others they are the right
to modify contents of some key in a registry.) Under any of these
circumstances, it would generally be undesirable to let the program
continue executing as if these operations would become permanent at a later
time. While implementations are not required to throw an exception under
these circumstances, they are encouraged to do so. A SecurityException
would be appropriate.
Most of the SPI methods require the implementation to read or write information at a preferences node. The implementor should beware of the fact that another VM may have concurrently deleted this node from the backing store. It is the implementation's responsibility to recreate the node if it has been deleted.
Implementation note: In Sun's default Preferences
implementations, the user's identity is inherited from the underlying
operating system and does not change for the lifetime of the virtual
machine. It is recognized that server-side Preferences
implementations may have the user identity change from request to request,
implicitly passed to Preferences
methods via the use of a
static ThreadLocal
instance. Authors of such implementations are
strongly encouraged to determine the user at the time preferences
are accessed (for example by the get(String,String)
or put(String,String)
method) rather than permanently associating a user
with each Preferences
instance. The latter behavior conflicts
with normal Preferences
usage and would lead to great confusion.
- Since:
- 1.4
- See Also:
Preferences
-
Field Summary
Fields Modifier and Type Field Description protected Object
lock
An object whose monitor is used to lock this node.protected boolean
newNode
This field should betrue
if this node did not exist in the backing store prior to the creation of this object.Fields declared in class java.util.prefs.Preferences
MAX_KEY_LENGTH, MAX_NAME_LENGTH, MAX_VALUE_LENGTH
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractPreferences(AbstractPreferences parent, String name)
Creates a preference node with the specified parent and the specified name relative to its parent. -
Method Summary
Modifier and Type Method Description String
absolutePath()
Implements theabsolutePath
method as per the specification inPreferences.absolutePath()
.protected AbstractPreferences[]
cachedChildren()
Returns all known unremoved children of this node.String[]
childrenNames()
Implements thechildren
method as per the specification inPreferences.childrenNames()
.protected abstract String[]
childrenNamesSpi()
Returns the names of the children of this preference node.protected abstract AbstractPreferences
childSpi(String name)
Returns the named child of this preference node, creating it if it does not already exist.void
clear()
Implements theclear
method as per the specification inPreferences.clear()
.void
exportNode(OutputStream os)
Implements theexportNode
method as per the specification inPreferences.exportNode(OutputStream)
.void
exportSubtree(OutputStream os)
Implements theexportSubtree
method as per the specification inPreferences.exportSubtree(OutputStream)
.void
flush()
Implements theflush
method as per the specification inPreferences.flush()
.protected abstract void
flushSpi()
This method is invoked with this node locked.String
get(String key, String def)
Implements theget
method as per the specification inPreferences.get(String,String)
.boolean
getBoolean(String key, boolean def)
Implements thegetBoolean
method as per the specification inPreferences.getBoolean(String,boolean)
.byte[]
getByteArray(String key, byte[] def)
Implements thegetByteArray
method as per the specification inPreferences.getByteArray(String,byte[])
.protected AbstractPreferences
getChild(String nodeName)
Returns the named child if it exists, ornull
if it does not.double
getDouble(String key, double def)
Implements thegetDouble
method as per the specification inPreferences.getDouble(String,double)
.float
getFloat(String key, float def)
Implements thegetFloat
method as per the specification inPreferences.getFloat(String,float)
.int
getInt(String key, int def)
Implements thegetInt
method as per the specification inPreferences.getInt(String,int)
.long
getLong(String key, long def)
Implements thegetLong
method as per the specification inPreferences.getLong(String,long)
.protected abstract String
getSpi(String key)
Return the value associated with the specified key at this preference node, ornull
if there is no association for this key, or the association cannot be determined at this time.protected boolean
isRemoved()
Returnstrue
iff this node (or an ancestor) has been removed with theremoveNode()
method.boolean
isUserNode()
Implements theisUserNode
method as per the specification inPreferences.isUserNode()
.String[]
keys()
Implements thekeys
method as per the specification inPreferences.keys()
.protected abstract String[]
keysSpi()
Returns all of the keys that have an associated value in this preference node.String
name()
Implements thename
method as per the specification inPreferences.name()
.Preferences
node(String path)
Implements thenode
method as per the specification inPreferences.node(String)
.boolean
nodeExists(String path)
Implements thenodeExists
method as per the specification inPreferences.nodeExists(String)
.Preferences
parent()
Implements theparent
method as per the specification inPreferences.parent()
.void
put(String key, String value)
Implements theput
method as per the specification inPreferences.put(String,String)
.void
putBoolean(String key, boolean value)
Implements theputBoolean
method as per the specification inPreferences.putBoolean(String,boolean)
.void
putByteArray(String key, byte[] value)
Implements theputByteArray
method as per the specification inPreferences.putByteArray(String,byte[])
.void
putDouble(String key, double value)
Implements theputDouble
method as per the specification inPreferences.putDouble(String,double)
.void
putFloat(String key, float value)
Implements theputFloat
method as per the specification inPreferences.putFloat(String,float)
.void
putInt(String key, int value)
Implements theputInt
method as per the specification inPreferences.putInt(String,int)
.void
putLong(String key, long value)
Implements theputLong
method as per the specification inPreferences.putLong(String,long)
.protected abstract void
putSpi(String key, String value)
Put the given key-value association into this preference node.void
remove(String key)
Implements theremove(String)
method as per the specification inPreferences.remove(String)
.void
removeNode()
Implements theremoveNode()
method as per the specification inPreferences.removeNode()
.protected abstract void
removeNodeSpi()
Removes this preference node, invalidating it and any preferences that it contains.protected abstract void
removeSpi(String key)
Remove the association (if any) for the specified key at this preference node.void
sync()
Implements thesync
method as per the specification inPreferences.sync()
.protected abstract void
syncSpi()
This method is invoked with this node locked.String
toString()
Returns the absolute path name of this preferences node.Methods declared in class java.util.prefs.Preferences
addNodeChangeListener, addPreferenceChangeListener, importPreferences, removeNodeChangeListener, removePreferenceChangeListener, systemNodeForPackage, systemRoot, userNodeForPackage, userRoot
-
Field Details
-
newNode
protected boolean newNodeThis field should betrue
if this node did not exist in the backing store prior to the creation of this object. The field is initialized to false, but may be set to true by a subclass constructor (and should not be modified thereafter). This field indicates whether a node change event should be fired when creation is complete. -
lock
An object whose monitor is used to lock this node. This object is used in preference to the node itself to reduce the likelihood of intentional or unintentional denial of service due to a locked node. To avoid deadlock, a node is never locked by a thread that holds a lock on a descendant of that node.
-
-
Constructor Details
-
AbstractPreferences
Creates a preference node with the specified parent and the specified name relative to its parent.- Parameters:
parent
- the parent of this preference node, or null if this is the root.name
- the name of this preference node, relative to its parent, or""
if this is the root.- Throws:
IllegalArgumentException
- ifname
contains a slash ('/'
), orparent
isnull
and name isn't""
.
-
-
Method Details
-
put
Implements theput
method as per the specification inPreferences.put(String,String)
.This implementation checks that the key and value are legal, obtains this preference node's lock, checks that the node has not been removed, invokes
putSpi(String,String)
, and if there are any preference change listeners, enqueues a notification event for processing by the event dispatch thread.- Specified by:
put
in classPreferences
- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.- Throws:
NullPointerException
- if key or value isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
or ifvalue.length
exceedsMAX_VALUE_LENGTH
.IllegalArgumentException
- if either key or value contain the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.
-
get
Implements theget
method as per the specification inPreferences.get(String,String)
.This implementation first checks to see if
key
isnull
throwing aNullPointerException
if this is the case. Then it obtains this preference node's lock, checks that the node has not been removed, invokesgetSpi(String)
, and returns the result, unless thegetSpi
invocation returnsnull
or throws an exception, in which case this invocation returnsdef
.- Specified by:
get
in classPreferences
- Parameters:
key
- 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 withkey
.- Returns:
- the value associated with
key
, ordef
if no value is associated withkey
. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- if key isnull
. (Anull
default is permitted.)IllegalArgumentException
- if key contains the null control character, code point U+0000.
-
remove
Implements theremove(String)
method as per the specification inPreferences.remove(String)
.This implementation obtains this preference node's lock, checks that the node has not been removed, invokes
removeSpi(String)
and if there are any preference change listeners, enqueues a notification event for processing by the event dispatch thread.- Specified by:
remove
in classPreferences
- Parameters:
key
- key whose mapping is to be removed from the preference node.- Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.IllegalArgumentException
- if key contains the null control character, code point U+0000.NullPointerException
- ifkey
isnull
..
-
clear
Implements theclear
method as per the specification inPreferences.clear()
.This implementation obtains this preference node's lock, invokes
keys()
to obtain an array of keys, and iterates over the array invokingremove(String)
on each key.- Specified by:
clear
in classPreferences
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.removeNode()
-
putInt
Implements theputInt
method as per the specification inPreferences.putInt(String,int)
.This implementation translates
value
to a string withInteger.toString(int)
and invokesput(String,String)
on the result.- Specified by:
putInt
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getInt(String,int)
-
getInt
Implements thegetInt
method as per the specification inPreferences.getInt(String,int)
.This implementation invokes
get(key, null)
. If the return value is non-null, the implementation attempts to translate it to anint
withInteger.parseInt(String)
. If the attempt succeeds, the return value is returned by this method. Otherwise,def
is returned.- Specified by:
getInt
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as an int.- Returns:
- the int value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as an int. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
.IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.putInt(String,int)
,Preferences.get(String,String)
-
putLong
Implements theputLong
method as per the specification inPreferences.putLong(String,long)
.This implementation translates
value
to a string withLong.toString(long)
and invokesput(String,String)
on the result.- Specified by:
putLong
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getLong(String,long)
-
getLong
Implements thegetLong
method as per the specification inPreferences.getLong(String,long)
.This implementation invokes
get(key, null)
. If the return value is non-null, the implementation attempts to translate it to along
withLong.parseLong(String)
. If the attempt succeeds, the return value is returned by this method. Otherwise,def
is returned.- Specified by:
getLong
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as a long.- Returns:
- the long value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as a long. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
.IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.putLong(String,long)
,Preferences.get(String,String)
-
putBoolean
Implements theputBoolean
method as per the specification inPreferences.putBoolean(String,boolean)
.This implementation translates
value
to a string withString.valueOf(boolean)
and invokesput(String,String)
on the result.- Specified by:
putBoolean
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getBoolean(String,boolean)
,Preferences.get(String,String)
-
getBoolean
Implements thegetBoolean
method as per the specification inPreferences.getBoolean(String,boolean)
.This implementation invokes
get(key, null)
. If the return value is non-null, it is compared with"true"
usingString.equalsIgnoreCase(String)
. If the comparison returnstrue
, this invocation returnstrue
. Otherwise, the original return value is compared with"false"
, again usingString.equalsIgnoreCase(String)
. If the comparison returnstrue
, this invocation returnsfalse
. Otherwise, this invocation returnsdef
.- Specified by:
getBoolean
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as a boolean.- Returns:
- the boolean value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as a boolean. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
.IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.get(String,String)
,Preferences.putBoolean(String,boolean)
-
putFloat
Implements theputFloat
method as per the specification inPreferences.putFloat(String,float)
.This implementation translates
value
to a string withFloat.toString(float)
and invokesput(String,String)
on the result.- Specified by:
putFloat
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getFloat(String,float)
-
getFloat
Implements thegetFloat
method as per the specification inPreferences.getFloat(String,float)
.This implementation invokes
get(key, null)
. If the return value is non-null, the implementation attempts to translate it to anfloat
withFloat.parseFloat(String)
. If the attempt succeeds, the return value is returned by this method. Otherwise,def
is returned.- Specified by:
getFloat
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as a float.- Returns:
- the float value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as a float. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
.IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.putFloat(String,float)
,Preferences.get(String,String)
-
putDouble
Implements theputDouble
method as per the specification inPreferences.putDouble(String,double)
.This implementation translates
value
to a string withDouble.toString(double)
and invokesput(String,String)
on the result.- Specified by:
putDouble
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key isnull
.IllegalArgumentException
- ifkey.length()
exceedsMAX_KEY_LENGTH
.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getDouble(String,double)
-
getDouble
Implements thegetDouble
method as per the specification inPreferences.getDouble(String,double)
.This implementation invokes
get(key, null)
. If the return value is non-null, the implementation attempts to translate it to andouble
withDouble.parseDouble(String)
. If the attempt succeeds, the return value is returned by this method. Otherwise,def
is returned.- Specified by:
getDouble
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as a double.- Returns:
- the double value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as a double. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
.IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.putDouble(String,double)
,Preferences.get(String,String)
-
putByteArray
Implements theputByteArray
method as per the specification inPreferences.putByteArray(String,byte[])
.- Specified by:
putByteArray
in classPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
- value whose string form is to be associated with key.- Throws:
NullPointerException
- if key or value isnull
.IllegalArgumentException
- if key.length() exceeds MAX_KEY_LENGTH or if value.length exceeds MAX_VALUE_LENGTH*3/4.IllegalArgumentException
- if key contains the null control character, code point U+0000.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.getByteArray(String,byte[])
,Preferences.get(String,String)
-
getByteArray
Implements thegetByteArray
method as per the specification inPreferences.getByteArray(String,byte[])
.- Specified by:
getByteArray
in classPreferences
- Parameters:
key
- 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 withkey
or the associated value cannot be interpreted as a byte array.- Returns:
- the byte array value represented by the string associated with
key
in this preference node, ordef
if the associated value does not exist or cannot be interpreted as a byte array. - Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.NullPointerException
- ifkey
isnull
. (Anull
value fordef
is permitted.)IllegalArgumentException
- if key contains the null control character, code point U+0000.- See Also:
Preferences.get(String,String)
,Preferences.putByteArray(String,byte[])
-
keys
Implements thekeys
method as per the specification inPreferences.keys()
.This implementation obtains this preference node's lock, checks that the node has not been removed and invokes
keysSpi()
.- Specified by:
keys
in classPreferences
- Returns:
- an array of the keys that have an associated value in this preference node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.
-
childrenNames
Implements thechildren
method as per the specification inPreferences.childrenNames()
.This implementation obtains this preference node's lock, checks that the node has not been removed, constructs a
TreeSet
initialized to the names of children already cached (the children in this node's "child-cache"), invokeschildrenNamesSpi()
, and adds all of the returned child-names into the set. The elements of the tree set are dumped into aString
array using thetoArray
method, and this array is returned.- Specified by:
childrenNames
in classPreferences
- Returns:
- the names of the children of this preference node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
cachedChildren()
-
cachedChildren
Returns all known unremoved children of this node.- Returns:
- all known unremoved children of this node.
-
parent
Implements theparent
method as per the specification inPreferences.parent()
.This implementation obtains this preference node's lock, checks that the node has not been removed and returns the parent value that was passed to this node's constructor.
- Specified by:
parent
in classPreferences
- Returns:
- the parent of this preference node.
- Throws:
IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.
-
node
Implements thenode
method as per the specification inPreferences.node(String)
.This implementation obtains this preference node's lock and checks that the node has not been removed. If
path
is""
, this node is returned; ifpath
is"/"
, this node's root is returned. If the first character inpath
is not'/'
, the implementation breakspath
into tokens and recursively traverses the path from this node to the named node, "consuming" a name and a slash frompath
at each step of the traversal. At each step, the current node is locked and the node's child-cache is checked for the named node. If it is not found, the name is checked to make sure its length does not exceedMAX_NAME_LENGTH
. Then thechildSpi(String)
method is invoked, and the result stored in this node's child-cache. If the newly createdPreferences
object'snewNode
field istrue
and there are any node change listeners, a notification event is enqueued for processing by the event dispatch thread.When there are no more tokens, the last value found in the child-cache or returned by
childSpi
is returned by this method. If during the traversal, two"/"
tokens occur consecutively, or the final token is"/"
(rather than a name), an appropriateIllegalArgumentException
is thrown.If the first character of
path
is'/'
(indicating an absolute path name) this preference node's lock is dropped prior to breakingpath
into tokens, and this method recursively traverses the path starting from the root (rather than starting from this node). The traversal is otherwise identical to the one described for relative path names. Dropping the lock on this node prior to commencing the traversal at the root node is essential to avoid the possibility of deadlock, as per thelocking invariant
.- Specified by:
node
in classPreferences
- Parameters:
path
- the path name of the preference node to return.- Returns:
- the specified preference node.
- Throws:
IllegalArgumentException
- if the path name is invalid (i.e., it contains multiple consecutive slash characters, or ends with a slash character and is more than one character long).IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
Preferences.flush()
-
nodeExists
Implements thenodeExists
method as per the specification inPreferences.nodeExists(String)
.This implementation is very similar to
node(String)
, except thatgetChild(String)
is used instead ofchildSpi(String)
.- Specified by:
nodeExists
in classPreferences
- Parameters:
path
- the path name of the node whose existence is to be checked.- Returns:
- true if the specified node exists.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.IllegalArgumentException
- if the path name is invalid (i.e., it contains multiple consecutive slash characters, or ends with a slash character and is more than one character long).IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method andpathname
is not the empty string (""
).
-
removeNode
Implements theremoveNode()
method as per the specification inPreferences.removeNode()
.This implementation checks to see that this node is the root; if so, it throws an appropriate exception. Then, it locks this node's parent, and calls a recursive helper method that traverses the subtree rooted at this node. The recursive method locks the node on which it was called, checks that it has not already been removed, and then ensures that all of its children are cached: The
childrenNamesSpi()
method is invoked and each returned child name is checked for containment in the child-cache. If a child is not already cached, thechildSpi(String)
method is invoked to create aPreferences
instance for it, and this instance is put into the child-cache. Then the helper method calls itself recursively on each node contained in its child-cache. Next, it invokesremoveNodeSpi()
, marks itself as removed, and removes itself from its parent's child-cache. Finally, if there are any node change listeners, it enqueues a notification event for processing by the event dispatch thread.Note that the helper method is always invoked with all ancestors up to the "closest non-removed ancestor" locked.
- Specified by:
removeNode
in classPreferences
- Throws:
IllegalStateException
- if this node (or an ancestor) has already been removed with theremoveNode()
method.UnsupportedOperationException
- if this method is invoked on the root node.BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
Preferences.flush()
-
name
Implements thename
method as per the specification inPreferences.name()
.This implementation merely returns the name that was passed to this node's constructor.
- Specified by:
name
in classPreferences
- Returns:
- this preference node's name, relative to its parent.
-
absolutePath
Implements theabsolutePath
method as per the specification inPreferences.absolutePath()
.This implementation merely returns the absolute path name that was computed at the time that this node was constructed (based on the name that was passed to this node's constructor, and the names that were passed to this node's ancestors' constructors).
- Specified by:
absolutePath
in classPreferences
- Returns:
- this preference node's absolute path name.
-
isUserNode
public boolean isUserNode()Implements theisUserNode
method as per the specification inPreferences.isUserNode()
.This implementation compares this node's root node (which is stored in a private field) with the value returned by
Preferences.userRoot()
. If the two object references are identical, this method returns true.- Specified by:
isUserNode
in classPreferences
- Returns:
true
if this preference node is in the user preference tree,false
if it's in the system preference tree.
-
putSpi
Put the given key-value association into this preference node. It is guaranteed thatkey
andvalue
are non-null and of legal length. Also, it is guaranteed that this node has not been removed. (The implementor needn't check for any of these things.)This method is invoked with the lock on this node held.
- Parameters:
key
- the keyvalue
- the value
-
getSpi
Return the value associated with the specified key at this preference node, ornull
if there is no association for this key, or the association cannot be determined at this time. It is guaranteed thatkey
is non-null. Also, it is guaranteed that this node has not been removed. (The implementor needn't check for either of these things.)Generally speaking, this method should not throw an exception under any circumstances. If, however, if it does throw an exception, the exception will be intercepted and treated as a
null
return value.This method is invoked with the lock on this node held.
- Parameters:
key
- the key- Returns:
- the value associated with the specified key at this preference
node, or
null
if there is no association for this key, or the association cannot be determined at this time.
-
removeSpi
Remove the association (if any) for the specified key at this preference node. It is guaranteed thatkey
is non-null. Also, it is guaranteed that this node has not been removed. (The implementor needn't check for either of these things.)This method is invoked with the lock on this node held.
- Parameters:
key
- the key
-
removeNodeSpi
Removes this preference node, invalidating it and any preferences that it contains. The named child will have no descendants at the time this invocation is made (i.e., thePreferences.removeNode()
method invokes this method repeatedly in a bottom-up fashion, removing each of a node's descendants before removing the node itself).This method is invoked with the lock held on this node and its parent (and all ancestors that are being removed as a result of a single invocation to
Preferences.removeNode()
).The removal of a node needn't become persistent until the
flush
method is invoked on this node (or an ancestor).If this node throws a
BackingStoreException
, the exception will propagate out beyond the enclosingremoveNode()
invocation.- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
keysSpi
Returns all of the keys that have an associated value in this preference node. (The returned array will be of size zero if this node has no preferences.) It is guaranteed that this node has not been removed.This method is invoked with the lock on this node held.
If this node throws a
BackingStoreException
, the exception will propagate out beyond the enclosingkeys()
invocation.- Returns:
- an array of the keys that have an associated value in this preference node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
childrenNamesSpi
Returns the names of the children of this preference node. (The returned array will be of size zero if this node has no children.) This method need not return the names of any nodes already cached, but may do so without harm.This method is invoked with the lock on this node held.
If this node throws a
BackingStoreException
, the exception will propagate out beyond the enclosingchildrenNames()
invocation.- Returns:
- an array containing the names of the children of this preference node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
getChild
Returns the named child if it exists, ornull
if it does not. It is guaranteed thatnodeName
is non-null, non-empty, does not contain the slash character ('/'), and is no longer thanPreferences.MAX_NAME_LENGTH
characters. Also, it is guaranteed that this node has not been removed. (The implementor needn't check for any of these things if he chooses to override this method.)Finally, it is guaranteed that the named node has not been returned by a previous invocation of this method or
childSpi(java.lang.String)
after the last time that it was removed. In other words, a cached value will always be used in preference to invoking this method. (The implementor needn't maintain his own cache of previously returned children if he chooses to override this method.)This implementation obtains this preference node's lock, invokes
childrenNames()
to get an array of the names of this node's children, and iterates over the array comparing the name of each child with the specified node name. If a child node has the correct name, thechildSpi(String)
method is invoked and the resulting node is returned. If the iteration completes without finding the specified name,null
is returned.- Parameters:
nodeName
- name of the child to be searched for.- Returns:
- the named child if it exists, or null if it does not.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
childSpi
Returns the named child of this preference node, creating it if it does not already exist. It is guaranteed thatname
is non-null, non-empty, does not contain the slash character ('/'), and is no longer thanPreferences.MAX_NAME_LENGTH
characters. Also, it is guaranteed that this node has not been removed. (The implementor needn't check for any of these things.)Finally, it is guaranteed that the named node has not been returned by a previous invocation of this method or
getChild(String)
after the last time that it was removed. In other words, a cached value will always be used in preference to invoking this method. Subclasses need not maintain their own cache of previously returned children.The implementer must ensure that the returned node has not been removed. If a like-named child of this node was previously removed, the implementer must return a newly constructed
AbstractPreferences
node; once removed, anAbstractPreferences
node cannot be "resuscitated."If this method causes a node to be created, this node is not guaranteed to be persistent until the
flush
method is invoked on this node or one of its ancestors (or descendants).This method is invoked with the lock on this node held.
- Parameters:
name
- The name of the child node to return, relative to this preference node.- Returns:
- The named child node.
-
toString
Returns the absolute path name of this preferences node.- Specified by:
toString
in classPreferences
- Returns:
- a string representation of the object.
-
sync
Implements thesync
method as per the specification inPreferences.sync()
.This implementation calls a recursive helper method that locks this node, invokes syncSpi() on it, unlocks this node, and recursively invokes this method on each "cached child." A cached child is a child of this node that has been created in this VM and not subsequently removed. In effect, this method does a depth first traversal of the "cached subtree" rooted at this node, calling syncSpi() on each node in the subTree while only that node is locked. Note that syncSpi() is invoked top-down.
- Specified by:
sync
in classPreferences
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.IllegalStateException
- if this node (or an ancestor) has been removed with theremoveNode()
method.- See Also:
flush()
-
syncSpi
This method is invoked with this node locked. The contract of this method is to synchronize any cached preferences stored at this node with any stored in the backing store. (It is perfectly possible that this node does not exist on the backing store, either because it has been deleted by another VM, or because it has not yet been created.) Note that this method should not synchronize the preferences in any subnodes of this node. If the backing store naturally syncs an entire subtree at once, the implementer is encouraged to override sync(), rather than merely overriding this method.If this node throws a
BackingStoreException
, the exception will propagate out beyond the enclosingsync()
invocation.- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
flush
Implements theflush
method as per the specification inPreferences.flush()
.This implementation calls a recursive helper method that locks this node, invokes flushSpi() on it, unlocks this node, and recursively invokes this method on each "cached child." A cached child is a child of this node that has been created in this VM and not subsequently removed. In effect, this method does a depth first traversal of the "cached subtree" rooted at this node, calling flushSpi() on each node in the subTree while only that node is locked. Note that flushSpi() is invoked top-down.
If this method is invoked on a node that has been removed with the
removeNode()
method, flushSpi() is invoked on this node, but not on others.- Specified by:
flush
in classPreferences
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
flush()
-
flushSpi
This method is invoked with this node locked. The contract of this method is to force any cached changes in the contents of this preference node to the backing store, guaranteeing their persistence. (It is perfectly possible that this node does not exist on the backing store, either because it has been deleted by another VM, or because it has not yet been created.) Note that this method should not flush the preferences in any subnodes of this node. If the backing store naturally flushes an entire subtree at once, the implementer is encouraged to override flush(), rather than merely overriding this method.If this node throws a
BackingStoreException
, the exception will propagate out beyond the enclosingflush()
invocation.- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
isRemoved
protected boolean isRemoved()Returnstrue
iff this node (or an ancestor) has been removed with theremoveNode()
method. This method locks this node prior to returning the contents of the private field used to track this state.- Returns:
true
iff this node (or an ancestor) has been removed with theremoveNode()
method.
-
exportNode
Implements theexportNode
method as per the specification inPreferences.exportNode(OutputStream)
.- Specified by:
exportNode
in classPreferences
- Parameters:
os
- the output stream on which to emit the XML document.- Throws:
IOException
- if writing to the specified output stream results in anIOException
.BackingStoreException
- if preference data cannot be read from backing store.- See Also:
Preferences.importPreferences(InputStream)
-
exportSubtree
Implements theexportSubtree
method as per the specification inPreferences.exportSubtree(OutputStream)
.- Specified by:
exportSubtree
in classPreferences
- Parameters:
os
- the output stream on which to emit the XML document.- Throws:
IOException
- if writing to the specified output stream results in anIOException
.BackingStoreException
- if preference data cannot be read from backing store.- See Also:
Preferences.importPreferences(InputStream)
,Preferences.exportNode(OutputStream)
-