2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package java.util.prefs;
|
|
27 |
|
|
28 |
import java.util.*;
|
|
29 |
import java.io.*;
|
|
30 |
import java.security.AccessController;
|
|
31 |
import java.security.PrivilegedAction;
|
|
32 |
// These imports needed only as a workaround for a JavaDoc bug
|
|
33 |
import java.lang.Integer;
|
|
34 |
import java.lang.Long;
|
|
35 |
import java.lang.Float;
|
|
36 |
import java.lang.Double;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* This class provides a skeletal implementation of the {@link Preferences}
|
|
40 |
* class, greatly easing the task of implementing it.
|
|
41 |
*
|
|
42 |
* <p><strong>This class is for <tt>Preferences</tt> implementers only.
|
|
43 |
* Normal users of the <tt>Preferences</tt> facility should have no need to
|
|
44 |
* consult this documentation. The {@link Preferences} documentation
|
|
45 |
* should suffice.</strong>
|
|
46 |
*
|
|
47 |
* <p>Implementors must override the nine abstract service-provider interface
|
|
48 |
* (SPI) methods: {@link #getSpi(String)}, {@link #putSpi(String,String)},
|
|
49 |
* {@link #removeSpi(String)}, {@link #childSpi(String)}, {@link
|
|
50 |
* #removeNodeSpi()}, {@link #keysSpi()}, {@link #childrenNamesSpi()}, {@link
|
|
51 |
* #syncSpi()} and {@link #flushSpi()}. All of the concrete methods specify
|
|
52 |
* precisely how they are implemented atop these SPI methods. The implementor
|
|
53 |
* may, at his discretion, override one or more of the concrete methods if the
|
|
54 |
* default implementation is unsatisfactory for any reason, such as
|
|
55 |
* performance.
|
|
56 |
*
|
|
57 |
* <p>The SPI methods fall into three groups concerning exception
|
|
58 |
* behavior. The <tt>getSpi</tt> method should never throw exceptions, but it
|
|
59 |
* doesn't really matter, as any exception thrown by this method will be
|
|
60 |
* intercepted by {@link #get(String,String)}, which will return the specified
|
|
61 |
* default value to the caller. The <tt>removeNodeSpi, keysSpi,
|
|
62 |
* childrenNamesSpi, syncSpi</tt> and <tt>flushSpi</tt> methods are specified
|
|
63 |
* to throw {@link BackingStoreException}, and the implementation is required
|
|
64 |
* to throw this checked exception if it is unable to perform the operation.
|
|
65 |
* The exception propagates outward, causing the corresponding API method
|
|
66 |
* to fail.
|
|
67 |
*
|
|
68 |
* <p>The remaining SPI methods {@link #putSpi(String,String)}, {@link
|
|
69 |
* #removeSpi(String)} and {@link #childSpi(String)} have more complicated
|
|
70 |
* exception behavior. They are not specified to throw
|
|
71 |
* <tt>BackingStoreException</tt>, as they can generally obey their contracts
|
|
72 |
* even if the backing store is unavailable. This is true because they return
|
|
73 |
* no information and their effects are not required to become permanent until
|
|
74 |
* a subsequent call to {@link Preferences#flush()} or
|
|
75 |
* {@link Preferences#sync()}. Generally speaking, these SPI methods should not
|
|
76 |
* throw exceptions. In some implementations, there may be circumstances
|
|
77 |
* under which these calls cannot even enqueue the requested operation for
|
|
78 |
* later processing. Even under these circumstances it is generally better to
|
|
79 |
* simply ignore the invocation and return, rather than throwing an
|
|
80 |
* exception. Under these circumstances, however, all subsequent invocations
|
|
81 |
* of <tt>flush()</tt> and <tt>sync</tt> should return <tt>false</tt>, as
|
|
82 |
* returning <tt>true</tt> would imply that all previous operations had
|
|
83 |
* successfully been made permanent.
|
|
84 |
*
|
|
85 |
* <p>There is one circumstance under which <tt>putSpi, removeSpi and
|
|
86 |
* childSpi</tt> <i>should</i> throw an exception: if the caller lacks
|
|
87 |
* sufficient privileges on the underlying operating system to perform the
|
|
88 |
* requested operation. This will, for instance, occur on most systems
|
|
89 |
* if a non-privileged user attempts to modify system preferences.
|
|
90 |
* (The required privileges will vary from implementation to
|
|
91 |
* implementation. On some implementations, they are the right to modify the
|
|
92 |
* contents of some directory in the file system; on others they are the right
|
|
93 |
* to modify contents of some key in a registry.) Under any of these
|
|
94 |
* circumstances, it would generally be undesirable to let the program
|
|
95 |
* continue executing as if these operations would become permanent at a later
|
|
96 |
* time. While implementations are not required to throw an exception under
|
|
97 |
* these circumstances, they are encouraged to do so. A {@link
|
|
98 |
* SecurityException} would be appropriate.
|
|
99 |
*
|
|
100 |
* <p>Most of the SPI methods require the implementation to read or write
|
|
101 |
* information at a preferences node. The implementor should beware of the
|
|
102 |
* fact that another VM may have concurrently deleted this node from the
|
|
103 |
* backing store. It is the implementation's responsibility to recreate the
|
|
104 |
* node if it has been deleted.
|
|
105 |
*
|
|
106 |
* <p>Implementation note: In Sun's default <tt>Preferences</tt>
|
|
107 |
* implementations, the user's identity is inherited from the underlying
|
|
108 |
* operating system and does not change for the lifetime of the virtual
|
|
109 |
* machine. It is recognized that server-side <tt>Preferences</tt>
|
|
110 |
* implementations may have the user identity change from request to request,
|
|
111 |
* implicitly passed to <tt>Preferences</tt> methods via the use of a
|
|
112 |
* static {@link ThreadLocal} instance. Authors of such implementations are
|
|
113 |
* <i>strongly</i> encouraged to determine the user at the time preferences
|
|
114 |
* are accessed (for example by the {@link #get(String,String)} or {@link
|
|
115 |
* #put(String,String)} method) rather than permanently associating a user
|
|
116 |
* with each <tt>Preferences</tt> instance. The latter behavior conflicts
|
|
117 |
* with normal <tt>Preferences</tt> usage and would lead to great confusion.
|
|
118 |
*
|
|
119 |
* @author Josh Bloch
|
|
120 |
* @see Preferences
|
|
121 |
* @since 1.4
|
|
122 |
*/
|
|
123 |
public abstract class AbstractPreferences extends Preferences {
|
|
124 |
/**
|
|
125 |
* Our name relative to parent.
|
|
126 |
*/
|
|
127 |
private final String name;
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Our absolute path name.
|
|
131 |
*/
|
|
132 |
private final String absolutePath;
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Our parent node.
|
|
136 |
*/
|
|
137 |
final AbstractPreferences parent;
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Our root node.
|
|
141 |
*/
|
|
142 |
private final AbstractPreferences root; // Relative to this node
|
|
143 |
|
|
144 |
/**
|
|
145 |
* This field should be <tt>true</tt> if this node did not exist in the
|
|
146 |
* backing store prior to the creation of this object. The field
|
|
147 |
* is initialized to false, but may be set to true by a subclass
|
|
148 |
* constructor (and should not be modified thereafter). This field
|
|
149 |
* indicates whether a node change event should be fired when
|
|
150 |
* creation is complete.
|
|
151 |
*/
|
|
152 |
protected boolean newNode = false;
|
|
153 |
|
|
154 |
/**
|
|
155 |
* All known unremoved children of this node. (This "cache" is consulted
|
|
156 |
* prior to calling childSpi() or getChild().
|
|
157 |
*/
|
51
|
158 |
private Map<String, AbstractPreferences> kidCache
|
|
159 |
= new HashMap<String, AbstractPreferences>();
|
2
|
160 |
|
|
161 |
/**
|
|
162 |
* This field is used to keep track of whether or not this node has
|
|
163 |
* been removed. Once it's set to true, it will never be reset to false.
|
|
164 |
*/
|
|
165 |
private boolean removed = false;
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Registered preference change listeners.
|
|
169 |
*/
|
|
170 |
private PreferenceChangeListener[] prefListeners =
|
|
171 |
new PreferenceChangeListener[0];
|
|
172 |
|
|
173 |
/**
|
|
174 |
* Registered node change listeners.
|
|
175 |
*/
|
|
176 |
private NodeChangeListener[] nodeListeners = new NodeChangeListener[0];
|
|
177 |
|
|
178 |
/**
|
|
179 |
* An object whose monitor is used to lock this node. This object
|
|
180 |
* is used in preference to the node itself to reduce the likelihood of
|
|
181 |
* intentional or unintentional denial of service due to a locked node.
|
|
182 |
* To avoid deadlock, a node is <i>never</i> locked by a thread that
|
|
183 |
* holds a lock on a descendant of that node.
|
|
184 |
*/
|
|
185 |
protected final Object lock = new Object();
|
|
186 |
|
|
187 |
/**
|
|
188 |
* Creates a preference node with the specified parent and the specified
|
|
189 |
* name relative to its parent.
|
|
190 |
*
|
|
191 |
* @param parent the parent of this preference node, or null if this
|
|
192 |
* is the root.
|
|
193 |
* @param name the name of this preference node, relative to its parent,
|
|
194 |
* or <tt>""</tt> if this is the root.
|
|
195 |
* @throws IllegalArgumentException if <tt>name</tt> contains a slash
|
|
196 |
* (<tt>'/'</tt>), or <tt>parent</tt> is <tt>null</tt> and
|
|
197 |
* name isn't <tt>""</tt>.
|
|
198 |
*/
|
|
199 |
protected AbstractPreferences(AbstractPreferences parent, String name) {
|
|
200 |
if (parent==null) {
|
|
201 |
if (!name.equals(""))
|
|
202 |
throw new IllegalArgumentException("Root name '"+name+
|
|
203 |
"' must be \"\"");
|
|
204 |
this.absolutePath = "/";
|
|
205 |
root = this;
|
|
206 |
} else {
|
|
207 |
if (name.indexOf('/') != -1)
|
|
208 |
throw new IllegalArgumentException("Name '" + name +
|
|
209 |
"' contains '/'");
|
|
210 |
if (name.equals(""))
|
|
211 |
throw new IllegalArgumentException("Illegal name: empty string");
|
|
212 |
|
|
213 |
root = parent.root;
|
|
214 |
absolutePath = (parent==root ? "/" + name
|
|
215 |
: parent.absolutePath() + "/" + name);
|
|
216 |
}
|
|
217 |
this.name = name;
|
|
218 |
this.parent = parent;
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Implements the <tt>put</tt> method as per the specification in
|
|
223 |
* {@link Preferences#put(String,String)}.
|
|
224 |
*
|
|
225 |
* <p>This implementation checks that the key and value are legal,
|
|
226 |
* obtains this preference node's lock, checks that the node
|
|
227 |
* has not been removed, invokes {@link #putSpi(String,String)}, and if
|
|
228 |
* there are any preference change listeners, enqueues a notification
|
|
229 |
* event for processing by the event dispatch thread.
|
|
230 |
*
|
|
231 |
* @param key key with which the specified value is to be associated.
|
|
232 |
* @param value value to be associated with the specified key.
|
|
233 |
* @throws NullPointerException if key or value is <tt>null</tt>.
|
|
234 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
235 |
* <tt>MAX_KEY_LENGTH</tt> or if <tt>value.length</tt> exceeds
|
|
236 |
* <tt>MAX_VALUE_LENGTH</tt>.
|
|
237 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
238 |
* removed with the {@link #removeNode()} method.
|
|
239 |
*/
|
|
240 |
public void put(String key, String value) {
|
|
241 |
if (key==null || value==null)
|
|
242 |
throw new NullPointerException();
|
|
243 |
if (key.length() > MAX_KEY_LENGTH)
|
|
244 |
throw new IllegalArgumentException("Key too long: "+key);
|
|
245 |
if (value.length() > MAX_VALUE_LENGTH)
|
|
246 |
throw new IllegalArgumentException("Value too long: "+value);
|
|
247 |
|
|
248 |
synchronized(lock) {
|
|
249 |
if (removed)
|
|
250 |
throw new IllegalStateException("Node has been removed.");
|
|
251 |
|
|
252 |
putSpi(key, value);
|
|
253 |
enqueuePreferenceChangeEvent(key, value);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Implements the <tt>get</tt> method as per the specification in
|
|
259 |
* {@link Preferences#get(String,String)}.
|
|
260 |
*
|
|
261 |
* <p>This implementation first checks to see if <tt>key</tt> is
|
|
262 |
* <tt>null</tt> throwing a <tt>NullPointerException</tt> if this is
|
|
263 |
* the case. Then it obtains this preference node's lock,
|
|
264 |
* checks that the node has not been removed, invokes {@link
|
|
265 |
* #getSpi(String)}, and returns the result, unless the <tt>getSpi</tt>
|
|
266 |
* invocation returns <tt>null</tt> or throws an exception, in which case
|
|
267 |
* this invocation returns <tt>def</tt>.
|
|
268 |
*
|
|
269 |
* @param key key whose associated value is to be returned.
|
|
270 |
* @param def the value to be returned in the event that this
|
|
271 |
* preference node has no value associated with <tt>key</tt>.
|
|
272 |
* @return the value associated with <tt>key</tt>, or <tt>def</tt>
|
|
273 |
* if no value is associated with <tt>key</tt>.
|
|
274 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
275 |
* removed with the {@link #removeNode()} method.
|
|
276 |
* @throws NullPointerException if key is <tt>null</tt>. (A
|
|
277 |
* <tt>null</tt> default <i>is</i> permitted.)
|
|
278 |
*/
|
|
279 |
public String get(String key, String def) {
|
|
280 |
if (key==null)
|
|
281 |
throw new NullPointerException("Null key");
|
|
282 |
synchronized(lock) {
|
|
283 |
if (removed)
|
|
284 |
throw new IllegalStateException("Node has been removed.");
|
|
285 |
|
|
286 |
String result = null;
|
|
287 |
try {
|
|
288 |
result = getSpi(key);
|
|
289 |
} catch (Exception e) {
|
|
290 |
// Ignoring exception causes default to be returned
|
|
291 |
}
|
|
292 |
return (result==null ? def : result);
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Implements the <tt>remove(String)</tt> method as per the specification
|
|
298 |
* in {@link Preferences#remove(String)}.
|
|
299 |
*
|
|
300 |
* <p>This implementation obtains this preference node's lock,
|
|
301 |
* checks that the node has not been removed, invokes
|
|
302 |
* {@link #removeSpi(String)} and if there are any preference
|
|
303 |
* change listeners, enqueues a notification event for processing by the
|
|
304 |
* event dispatch thread.
|
|
305 |
*
|
|
306 |
* @param key key whose mapping is to be removed from the preference node.
|
|
307 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
308 |
* removed with the {@link #removeNode()} method.
|
|
309 |
*/
|
|
310 |
public void remove(String key) {
|
|
311 |
synchronized(lock) {
|
|
312 |
if (removed)
|
|
313 |
throw new IllegalStateException("Node has been removed.");
|
|
314 |
|
|
315 |
removeSpi(key);
|
|
316 |
enqueuePreferenceChangeEvent(key, null);
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
/**
|
|
321 |
* Implements the <tt>clear</tt> method as per the specification in
|
|
322 |
* {@link Preferences#clear()}.
|
|
323 |
*
|
|
324 |
* <p>This implementation obtains this preference node's lock,
|
|
325 |
* invokes {@link #keys()} to obtain an array of keys, and
|
|
326 |
* iterates over the array invoking {@link #remove(String)} on each key.
|
|
327 |
*
|
|
328 |
* @throws BackingStoreException if this operation cannot be completed
|
|
329 |
* due to a failure in the backing store, or inability to
|
|
330 |
* communicate with it.
|
|
331 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
332 |
* removed with the {@link #removeNode()} method.
|
|
333 |
*/
|
|
334 |
public void clear() throws BackingStoreException {
|
|
335 |
synchronized(lock) {
|
|
336 |
String[] keys = keys();
|
|
337 |
for (int i=0; i<keys.length; i++)
|
|
338 |
remove(keys[i]);
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Implements the <tt>putInt</tt> method as per the specification in
|
|
344 |
* {@link Preferences#putInt(String,int)}.
|
|
345 |
*
|
|
346 |
* <p>This implementation translates <tt>value</tt> to a string with
|
|
347 |
* {@link Integer#toString(int)} and invokes {@link #put(String,String)}
|
|
348 |
* on the result.
|
|
349 |
*
|
|
350 |
* @param key key with which the string form of value is to be associated.
|
|
351 |
* @param value value whose string form is to be associated with key.
|
|
352 |
* @throws NullPointerException if key is <tt>null</tt>.
|
|
353 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
354 |
* <tt>MAX_KEY_LENGTH</tt>.
|
|
355 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
356 |
* removed with the {@link #removeNode()} method.
|
|
357 |
*/
|
|
358 |
public void putInt(String key, int value) {
|
|
359 |
put(key, Integer.toString(value));
|
|
360 |
}
|
|
361 |
|
|
362 |
/**
|
|
363 |
* Implements the <tt>getInt</tt> method as per the specification in
|
|
364 |
* {@link Preferences#getInt(String,int)}.
|
|
365 |
*
|
|
366 |
* <p>This implementation invokes {@link #get(String,String) <tt>get(key,
|
|
367 |
* null)</tt>}. If the return value is non-null, the implementation
|
|
368 |
* attempts to translate it to an <tt>int</tt> with
|
|
369 |
* {@link Integer#parseInt(String)}. If the attempt succeeds, the return
|
|
370 |
* value is returned by this method. Otherwise, <tt>def</tt> is returned.
|
|
371 |
*
|
|
372 |
* @param key key whose associated value is to be returned as an int.
|
|
373 |
* @param def the value to be returned in the event that this
|
|
374 |
* preference node has no value associated with <tt>key</tt>
|
|
375 |
* or the associated value cannot be interpreted as an int.
|
|
376 |
* @return the int value represented by the string associated with
|
|
377 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
378 |
* associated value does not exist or cannot be interpreted as
|
|
379 |
* an int.
|
|
380 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
381 |
* removed with the {@link #removeNode()} method.
|
|
382 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
|
|
383 |
*/
|
|
384 |
public int getInt(String key, int def) {
|
|
385 |
int result = def;
|
|
386 |
try {
|
|
387 |
String value = get(key, null);
|
|
388 |
if (value != null)
|
|
389 |
result = Integer.parseInt(value);
|
|
390 |
} catch (NumberFormatException e) {
|
|
391 |
// Ignoring exception causes specified default to be returned
|
|
392 |
}
|
|
393 |
|
|
394 |
return result;
|
|
395 |
}
|
|
396 |
|
|
397 |
/**
|
|
398 |
* Implements the <tt>putLong</tt> method as per the specification in
|
|
399 |
* {@link Preferences#putLong(String,long)}.
|
|
400 |
*
|
|
401 |
* <p>This implementation translates <tt>value</tt> to a string with
|
|
402 |
* {@link Long#toString(long)} and invokes {@link #put(String,String)}
|
|
403 |
* on the result.
|
|
404 |
*
|
|
405 |
* @param key key with which the string form of value is to be associated.
|
|
406 |
* @param value value whose string form is to be associated with key.
|
|
407 |
* @throws NullPointerException if key is <tt>null</tt>.
|
|
408 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
409 |
* <tt>MAX_KEY_LENGTH</tt>.
|
|
410 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
411 |
* removed with the {@link #removeNode()} method.
|
|
412 |
*/
|
|
413 |
public void putLong(String key, long value) {
|
|
414 |
put(key, Long.toString(value));
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* Implements the <tt>getLong</tt> method as per the specification in
|
|
419 |
* {@link Preferences#getLong(String,long)}.
|
|
420 |
*
|
|
421 |
* <p>This implementation invokes {@link #get(String,String) <tt>get(key,
|
|
422 |
* null)</tt>}. If the return value is non-null, the implementation
|
|
423 |
* attempts to translate it to a <tt>long</tt> with
|
|
424 |
* {@link Long#parseLong(String)}. If the attempt succeeds, the return
|
|
425 |
* value is returned by this method. Otherwise, <tt>def</tt> is returned.
|
|
426 |
*
|
|
427 |
* @param key key whose associated value is to be returned as a long.
|
|
428 |
* @param def the value to be returned in the event that this
|
|
429 |
* preference node has no value associated with <tt>key</tt>
|
|
430 |
* or the associated value cannot be interpreted as a long.
|
|
431 |
* @return the long value represented by the string associated with
|
|
432 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
433 |
* associated value does not exist or cannot be interpreted as
|
|
434 |
* a long.
|
|
435 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
436 |
* removed with the {@link #removeNode()} method.
|
|
437 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
|
|
438 |
*/
|
|
439 |
public long getLong(String key, long def) {
|
|
440 |
long result = def;
|
|
441 |
try {
|
|
442 |
String value = get(key, null);
|
|
443 |
if (value != null)
|
|
444 |
result = Long.parseLong(value);
|
|
445 |
} catch (NumberFormatException e) {
|
|
446 |
// Ignoring exception causes specified default to be returned
|
|
447 |
}
|
|
448 |
|
|
449 |
return result;
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* Implements the <tt>putBoolean</tt> method as per the specification in
|
|
454 |
* {@link Preferences#putBoolean(String,boolean)}.
|
|
455 |
*
|
|
456 |
* <p>This implementation translates <tt>value</tt> to a string with
|
|
457 |
* {@link String#valueOf(boolean)} and invokes {@link #put(String,String)}
|
|
458 |
* on the result.
|
|
459 |
*
|
|
460 |
* @param key key with which the string form of value is to be associated.
|
|
461 |
* @param value value whose string form is to be associated with key.
|
|
462 |
* @throws NullPointerException if key is <tt>null</tt>.
|
|
463 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
464 |
* <tt>MAX_KEY_LENGTH</tt>.
|
|
465 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
466 |
* removed with the {@link #removeNode()} method.
|
|
467 |
*/
|
|
468 |
public void putBoolean(String key, boolean value) {
|
|
469 |
put(key, String.valueOf(value));
|
|
470 |
}
|
|
471 |
|
|
472 |
/**
|
|
473 |
* Implements the <tt>getBoolean</tt> method as per the specification in
|
|
474 |
* {@link Preferences#getBoolean(String,boolean)}.
|
|
475 |
*
|
|
476 |
* <p>This implementation invokes {@link #get(String,String) <tt>get(key,
|
|
477 |
* null)</tt>}. If the return value is non-null, it is compared with
|
|
478 |
* <tt>"true"</tt> using {@link String#equalsIgnoreCase(String)}. If the
|
|
479 |
* comparison returns <tt>true</tt>, this invocation returns
|
|
480 |
* <tt>true</tt>. Otherwise, the original return value is compared with
|
|
481 |
* <tt>"false"</tt>, again using {@link String#equalsIgnoreCase(String)}.
|
|
482 |
* If the comparison returns <tt>true</tt>, this invocation returns
|
|
483 |
* <tt>false</tt>. Otherwise, this invocation returns <tt>def</tt>.
|
|
484 |
*
|
|
485 |
* @param key key whose associated value is to be returned as a boolean.
|
|
486 |
* @param def the value to be returned in the event that this
|
|
487 |
* preference node has no value associated with <tt>key</tt>
|
|
488 |
* or the associated value cannot be interpreted as a boolean.
|
|
489 |
* @return the boolean value represented by the string associated with
|
|
490 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
491 |
* associated value does not exist or cannot be interpreted as
|
|
492 |
* a boolean.
|
|
493 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
494 |
* removed with the {@link #removeNode()} method.
|
|
495 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
|
|
496 |
*/
|
|
497 |
public boolean getBoolean(String key, boolean def) {
|
|
498 |
boolean result = def;
|
|
499 |
String value = get(key, null);
|
|
500 |
if (value != null) {
|
|
501 |
if (value.equalsIgnoreCase("true"))
|
|
502 |
result = true;
|
|
503 |
else if (value.equalsIgnoreCase("false"))
|
|
504 |
result = false;
|
|
505 |
}
|
|
506 |
|
|
507 |
return result;
|
|
508 |
}
|
|
509 |
|
|
510 |
/**
|
|
511 |
* Implements the <tt>putFloat</tt> method as per the specification in
|
|
512 |
* {@link Preferences#putFloat(String,float)}.
|
|
513 |
*
|
|
514 |
* <p>This implementation translates <tt>value</tt> to a string with
|
|
515 |
* {@link Float#toString(float)} and invokes {@link #put(String,String)}
|
|
516 |
* on the result.
|
|
517 |
*
|
|
518 |
* @param key key with which the string form of value is to be associated.
|
|
519 |
* @param value value whose string form is to be associated with key.
|
|
520 |
* @throws NullPointerException if key is <tt>null</tt>.
|
|
521 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
522 |
* <tt>MAX_KEY_LENGTH</tt>.
|
|
523 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
524 |
* removed with the {@link #removeNode()} method.
|
|
525 |
*/
|
|
526 |
public void putFloat(String key, float value) {
|
|
527 |
put(key, Float.toString(value));
|
|
528 |
}
|
|
529 |
|
|
530 |
/**
|
|
531 |
* Implements the <tt>getFloat</tt> method as per the specification in
|
|
532 |
* {@link Preferences#getFloat(String,float)}.
|
|
533 |
*
|
|
534 |
* <p>This implementation invokes {@link #get(String,String) <tt>get(key,
|
|
535 |
* null)</tt>}. If the return value is non-null, the implementation
|
|
536 |
* attempts to translate it to an <tt>float</tt> with
|
|
537 |
* {@link Float#parseFloat(String)}. If the attempt succeeds, the return
|
|
538 |
* value is returned by this method. Otherwise, <tt>def</tt> is returned.
|
|
539 |
*
|
|
540 |
* @param key key whose associated value is to be returned as a float.
|
|
541 |
* @param def the value to be returned in the event that this
|
|
542 |
* preference node has no value associated with <tt>key</tt>
|
|
543 |
* or the associated value cannot be interpreted as a float.
|
|
544 |
* @return the float value represented by the string associated with
|
|
545 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
546 |
* associated value does not exist or cannot be interpreted as
|
|
547 |
* a float.
|
|
548 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
549 |
* removed with the {@link #removeNode()} method.
|
|
550 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
|
|
551 |
*/
|
|
552 |
public float getFloat(String key, float def) {
|
|
553 |
float result = def;
|
|
554 |
try {
|
|
555 |
String value = get(key, null);
|
|
556 |
if (value != null)
|
|
557 |
result = Float.parseFloat(value);
|
|
558 |
} catch (NumberFormatException e) {
|
|
559 |
// Ignoring exception causes specified default to be returned
|
|
560 |
}
|
|
561 |
|
|
562 |
return result;
|
|
563 |
}
|
|
564 |
|
|
565 |
/**
|
|
566 |
* Implements the <tt>putDouble</tt> method as per the specification in
|
|
567 |
* {@link Preferences#putDouble(String,double)}.
|
|
568 |
*
|
|
569 |
* <p>This implementation translates <tt>value</tt> to a string with
|
|
570 |
* {@link Double#toString(double)} and invokes {@link #put(String,String)}
|
|
571 |
* on the result.
|
|
572 |
*
|
|
573 |
* @param key key with which the string form of value is to be associated.
|
|
574 |
* @param value value whose string form is to be associated with key.
|
|
575 |
* @throws NullPointerException if key is <tt>null</tt>.
|
|
576 |
* @throws IllegalArgumentException if <tt>key.length()</tt> exceeds
|
|
577 |
* <tt>MAX_KEY_LENGTH</tt>.
|
|
578 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
579 |
* removed with the {@link #removeNode()} method.
|
|
580 |
*/
|
|
581 |
public void putDouble(String key, double value) {
|
|
582 |
put(key, Double.toString(value));
|
|
583 |
}
|
|
584 |
|
|
585 |
/**
|
|
586 |
* Implements the <tt>getDouble</tt> method as per the specification in
|
|
587 |
* {@link Preferences#getDouble(String,double)}.
|
|
588 |
*
|
|
589 |
* <p>This implementation invokes {@link #get(String,String) <tt>get(key,
|
|
590 |
* null)</tt>}. If the return value is non-null, the implementation
|
|
591 |
* attempts to translate it to an <tt>double</tt> with
|
|
592 |
* {@link Double#parseDouble(String)}. If the attempt succeeds, the return
|
|
593 |
* value is returned by this method. Otherwise, <tt>def</tt> is returned.
|
|
594 |
*
|
|
595 |
* @param key key whose associated value is to be returned as a double.
|
|
596 |
* @param def the value to be returned in the event that this
|
|
597 |
* preference node has no value associated with <tt>key</tt>
|
|
598 |
* or the associated value cannot be interpreted as a double.
|
|
599 |
* @return the double value represented by the string associated with
|
|
600 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
601 |
* associated value does not exist or cannot be interpreted as
|
|
602 |
* a double.
|
|
603 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
604 |
* removed with the {@link #removeNode()} method.
|
|
605 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
|
|
606 |
*/
|
|
607 |
public double getDouble(String key, double def) {
|
|
608 |
double result = def;
|
|
609 |
try {
|
|
610 |
String value = get(key, null);
|
|
611 |
if (value != null)
|
|
612 |
result = Double.parseDouble(value);
|
|
613 |
} catch (NumberFormatException e) {
|
|
614 |
// Ignoring exception causes specified default to be returned
|
|
615 |
}
|
|
616 |
|
|
617 |
return result;
|
|
618 |
}
|
|
619 |
|
|
620 |
/**
|
|
621 |
* Implements the <tt>putByteArray</tt> method as per the specification in
|
|
622 |
* {@link Preferences#putByteArray(String,byte[])}.
|
|
623 |
*
|
|
624 |
* @param key key with which the string form of value is to be associated.
|
|
625 |
* @param value value whose string form is to be associated with key.
|
|
626 |
* @throws NullPointerException if key or value is <tt>null</tt>.
|
|
627 |
* @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH
|
|
628 |
* or if value.length exceeds MAX_VALUE_LENGTH*3/4.
|
|
629 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
630 |
* removed with the {@link #removeNode()} method.
|
|
631 |
*/
|
|
632 |
public void putByteArray(String key, byte[] value) {
|
|
633 |
put(key, Base64.byteArrayToBase64(value));
|
|
634 |
}
|
|
635 |
|
|
636 |
/**
|
|
637 |
* Implements the <tt>getByteArray</tt> method as per the specification in
|
|
638 |
* {@link Preferences#getByteArray(String,byte[])}.
|
|
639 |
*
|
|
640 |
* @param key key whose associated value is to be returned as a byte array.
|
|
641 |
* @param def the value to be returned in the event that this
|
|
642 |
* preference node has no value associated with <tt>key</tt>
|
|
643 |
* or the associated value cannot be interpreted as a byte array.
|
|
644 |
* @return the byte array value represented by the string associated with
|
|
645 |
* <tt>key</tt> in this preference node, or <tt>def</tt> if the
|
|
646 |
* associated value does not exist or cannot be interpreted as
|
|
647 |
* a byte array.
|
|
648 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
649 |
* removed with the {@link #removeNode()} method.
|
|
650 |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A
|
|
651 |
* <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.)
|
|
652 |
*/
|
|
653 |
public byte[] getByteArray(String key, byte[] def) {
|
|
654 |
byte[] result = def;
|
|
655 |
String value = get(key, null);
|
|
656 |
try {
|
|
657 |
if (value != null)
|
|
658 |
result = Base64.base64ToByteArray(value);
|
|
659 |
}
|
|
660 |
catch (RuntimeException e) {
|
|
661 |
// Ignoring exception causes specified default to be returned
|
|
662 |
}
|
|
663 |
|
|
664 |
return result;
|
|
665 |
}
|
|
666 |
|
|
667 |
/**
|
|
668 |
* Implements the <tt>keys</tt> method as per the specification in
|
|
669 |
* {@link Preferences#keys()}.
|
|
670 |
*
|
|
671 |
* <p>This implementation obtains this preference node's lock, checks that
|
|
672 |
* the node has not been removed and invokes {@link #keysSpi()}.
|
|
673 |
*
|
|
674 |
* @return an array of the keys that have an associated value in this
|
|
675 |
* preference node.
|
|
676 |
* @throws BackingStoreException if this operation cannot be completed
|
|
677 |
* due to a failure in the backing store, or inability to
|
|
678 |
* communicate with it.
|
|
679 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
680 |
* removed with the {@link #removeNode()} method.
|
|
681 |
*/
|
|
682 |
public String[] keys() throws BackingStoreException {
|
|
683 |
synchronized(lock) {
|
|
684 |
if (removed)
|
|
685 |
throw new IllegalStateException("Node has been removed.");
|
|
686 |
|
|
687 |
return keysSpi();
|
|
688 |
}
|
|
689 |
}
|
|
690 |
|
|
691 |
/**
|
|
692 |
* Implements the <tt>children</tt> method as per the specification in
|
|
693 |
* {@link Preferences#childrenNames()}.
|
|
694 |
*
|
|
695 |
* <p>This implementation obtains this preference node's lock, checks that
|
|
696 |
* the node has not been removed, constructs a <tt>TreeSet</tt> initialized
|
|
697 |
* to the names of children already cached (the children in this node's
|
|
698 |
* "child-cache"), invokes {@link #childrenNamesSpi()}, and adds all of the
|
|
699 |
* returned child-names into the set. The elements of the tree set are
|
|
700 |
* dumped into a <tt>String</tt> array using the <tt>toArray</tt> method,
|
|
701 |
* and this array is returned.
|
|
702 |
*
|
|
703 |
* @return the names of the children of this preference node.
|
|
704 |
* @throws BackingStoreException if this operation cannot be completed
|
|
705 |
* due to a failure in the backing store, or inability to
|
|
706 |
* communicate with it.
|
|
707 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
708 |
* removed with the {@link #removeNode()} method.
|
|
709 |
* @see #cachedChildren()
|
|
710 |
*/
|
|
711 |
public String[] childrenNames() throws BackingStoreException {
|
|
712 |
synchronized(lock) {
|
|
713 |
if (removed)
|
|
714 |
throw new IllegalStateException("Node has been removed.");
|
|
715 |
|
51
|
716 |
Set<String> s = new TreeSet<String>(kidCache.keySet());
|
|
717 |
for (String kid : childrenNamesSpi())
|
|
718 |
s.add(kid);
|
|
719 |
return s.toArray(EMPTY_STRING_ARRAY);
|
2
|
720 |
}
|
|
721 |
}
|
|
722 |
|
|
723 |
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
724 |
|
|
725 |
/**
|
|
726 |
* Returns all known unremoved children of this node.
|
|
727 |
*
|
|
728 |
* @return all known unremoved children of this node.
|
|
729 |
*/
|
|
730 |
protected final AbstractPreferences[] cachedChildren() {
|
51
|
731 |
return kidCache.values().toArray(EMPTY_ABSTRACT_PREFS_ARRAY);
|
2
|
732 |
}
|
|
733 |
|
|
734 |
private static final AbstractPreferences[] EMPTY_ABSTRACT_PREFS_ARRAY
|
|
735 |
= new AbstractPreferences[0];
|
|
736 |
|
|
737 |
/**
|
|
738 |
* Implements the <tt>parent</tt> method as per the specification in
|
|
739 |
* {@link Preferences#parent()}.
|
|
740 |
*
|
|
741 |
* <p>This implementation obtains this preference node's lock, checks that
|
|
742 |
* the node has not been removed and returns the parent value that was
|
|
743 |
* passed to this node's constructor.
|
|
744 |
*
|
|
745 |
* @return the parent of this preference node.
|
|
746 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
747 |
* removed with the {@link #removeNode()} method.
|
|
748 |
*/
|
|
749 |
public Preferences parent() {
|
|
750 |
synchronized(lock) {
|
|
751 |
if (removed)
|
|
752 |
throw new IllegalStateException("Node has been removed.");
|
|
753 |
|
|
754 |
return parent;
|
|
755 |
}
|
|
756 |
}
|
|
757 |
|
|
758 |
/**
|
|
759 |
* Implements the <tt>node</tt> method as per the specification in
|
|
760 |
* {@link Preferences#node(String)}.
|
|
761 |
*
|
|
762 |
* <p>This implementation obtains this preference node's lock and checks
|
|
763 |
* that the node has not been removed. If <tt>path</tt> is <tt>""</tt>,
|
|
764 |
* this node is returned; if <tt>path</tt> is <tt>"/"</tt>, this node's
|
|
765 |
* root is returned. If the first character in <tt>path</tt> is
|
|
766 |
* not <tt>'/'</tt>, the implementation breaks <tt>path</tt> into
|
|
767 |
* tokens and recursively traverses the path from this node to the
|
|
768 |
* named node, "consuming" a name and a slash from <tt>path</tt> at
|
|
769 |
* each step of the traversal. At each step, the current node is locked
|
|
770 |
* and the node's child-cache is checked for the named node. If it is
|
|
771 |
* not found, the name is checked to make sure its length does not
|
|
772 |
* exceed <tt>MAX_NAME_LENGTH</tt>. Then the {@link #childSpi(String)}
|
|
773 |
* method is invoked, and the result stored in this node's child-cache.
|
|
774 |
* If the newly created <tt>Preferences</tt> object's {@link #newNode}
|
|
775 |
* field is <tt>true</tt> and there are any node change listeners,
|
|
776 |
* a notification event is enqueued for processing by the event dispatch
|
|
777 |
* thread.
|
|
778 |
*
|
|
779 |
* <p>When there are no more tokens, the last value found in the
|
|
780 |
* child-cache or returned by <tt>childSpi</tt> is returned by this
|
|
781 |
* method. If during the traversal, two <tt>"/"</tt> tokens occur
|
|
782 |
* consecutively, or the final token is <tt>"/"</tt> (rather than a name),
|
|
783 |
* an appropriate <tt>IllegalArgumentException</tt> is thrown.
|
|
784 |
*
|
|
785 |
* <p> If the first character of <tt>path</tt> is <tt>'/'</tt>
|
|
786 |
* (indicating an absolute path name) this preference node's
|
|
787 |
* lock is dropped prior to breaking <tt>path</tt> into tokens, and
|
|
788 |
* this method recursively traverses the path starting from the root
|
|
789 |
* (rather than starting from this node). The traversal is otherwise
|
|
790 |
* identical to the one described for relative path names. Dropping
|
|
791 |
* the lock on this node prior to commencing the traversal at the root
|
|
792 |
* node is essential to avoid the possibility of deadlock, as per the
|
|
793 |
* {@link #lock locking invariant}.
|
|
794 |
*
|
|
795 |
* @param path the path name of the preference node to return.
|
|
796 |
* @return the specified preference node.
|
|
797 |
* @throws IllegalArgumentException if the path name is invalid (i.e.,
|
|
798 |
* it contains multiple consecutive slash characters, or ends
|
|
799 |
* with a slash character and is more than one character long).
|
|
800 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
801 |
* removed with the {@link #removeNode()} method.
|
|
802 |
*/
|
|
803 |
public Preferences node(String path) {
|
|
804 |
synchronized(lock) {
|
|
805 |
if (removed)
|
|
806 |
throw new IllegalStateException("Node has been removed.");
|
|
807 |
if (path.equals(""))
|
|
808 |
return this;
|
|
809 |
if (path.equals("/"))
|
|
810 |
return root;
|
|
811 |
if (path.charAt(0) != '/')
|
|
812 |
return node(new StringTokenizer(path, "/", true));
|
|
813 |
}
|
|
814 |
|
|
815 |
// Absolute path. Note that we've dropped our lock to avoid deadlock
|
|
816 |
return root.node(new StringTokenizer(path.substring(1), "/", true));
|
|
817 |
}
|
|
818 |
|
|
819 |
/**
|
|
820 |
* tokenizer contains <name> {'/' <name>}*
|
|
821 |
*/
|
|
822 |
private Preferences node(StringTokenizer path) {
|
|
823 |
String token = path.nextToken();
|
|
824 |
if (token.equals("/")) // Check for consecutive slashes
|
|
825 |
throw new IllegalArgumentException("Consecutive slashes in path");
|
|
826 |
synchronized(lock) {
|
51
|
827 |
AbstractPreferences child = kidCache.get(token);
|
2
|
828 |
if (child == null) {
|
|
829 |
if (token.length() > MAX_NAME_LENGTH)
|
|
830 |
throw new IllegalArgumentException(
|
|
831 |
"Node name " + token + " too long");
|
|
832 |
child = childSpi(token);
|
|
833 |
if (child.newNode)
|
|
834 |
enqueueNodeAddedEvent(child);
|
|
835 |
kidCache.put(token, child);
|
|
836 |
}
|
|
837 |
if (!path.hasMoreTokens())
|
|
838 |
return child;
|
|
839 |
path.nextToken(); // Consume slash
|
|
840 |
if (!path.hasMoreTokens())
|
|
841 |
throw new IllegalArgumentException("Path ends with slash");
|
|
842 |
return child.node(path);
|
|
843 |
}
|
|
844 |
}
|
|
845 |
|
|
846 |
/**
|
|
847 |
* Implements the <tt>nodeExists</tt> method as per the specification in
|
|
848 |
* {@link Preferences#nodeExists(String)}.
|
|
849 |
*
|
|
850 |
* <p>This implementation is very similar to {@link #node(String)},
|
|
851 |
* except that {@link #getChild(String)} is used instead of {@link
|
|
852 |
* #childSpi(String)}.
|
|
853 |
*
|
|
854 |
* @param path the path name of the node whose existence is to be checked.
|
|
855 |
* @return true if the specified node exists.
|
|
856 |
* @throws BackingStoreException if this operation cannot be completed
|
|
857 |
* due to a failure in the backing store, or inability to
|
|
858 |
* communicate with it.
|
|
859 |
* @throws IllegalArgumentException if the path name is invalid (i.e.,
|
|
860 |
* it contains multiple consecutive slash characters, or ends
|
|
861 |
* with a slash character and is more than one character long).
|
|
862 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
863 |
* removed with the {@link #removeNode()} method and
|
|
864 |
* <tt>pathname</tt> is not the empty string (<tt>""</tt>).
|
|
865 |
*/
|
|
866 |
public boolean nodeExists(String path)
|
|
867 |
throws BackingStoreException
|
|
868 |
{
|
|
869 |
synchronized(lock) {
|
|
870 |
if (path.equals(""))
|
|
871 |
return !removed;
|
|
872 |
if (removed)
|
|
873 |
throw new IllegalStateException("Node has been removed.");
|
|
874 |
if (path.equals("/"))
|
|
875 |
return true;
|
|
876 |
if (path.charAt(0) != '/')
|
|
877 |
return nodeExists(new StringTokenizer(path, "/", true));
|
|
878 |
}
|
|
879 |
|
|
880 |
// Absolute path. Note that we've dropped our lock to avoid deadlock
|
|
881 |
return root.nodeExists(new StringTokenizer(path.substring(1), "/",
|
|
882 |
true));
|
|
883 |
}
|
|
884 |
|
|
885 |
/**
|
|
886 |
* tokenizer contains <name> {'/' <name>}*
|
|
887 |
*/
|
|
888 |
private boolean nodeExists(StringTokenizer path)
|
|
889 |
throws BackingStoreException
|
|
890 |
{
|
|
891 |
String token = path.nextToken();
|
|
892 |
if (token.equals("/")) // Check for consecutive slashes
|
|
893 |
throw new IllegalArgumentException("Consecutive slashes in path");
|
|
894 |
synchronized(lock) {
|
51
|
895 |
AbstractPreferences child = kidCache.get(token);
|
2
|
896 |
if (child == null)
|
|
897 |
child = getChild(token);
|
|
898 |
if (child==null)
|
|
899 |
return false;
|
|
900 |
if (!path.hasMoreTokens())
|
|
901 |
return true;
|
|
902 |
path.nextToken(); // Consume slash
|
|
903 |
if (!path.hasMoreTokens())
|
|
904 |
throw new IllegalArgumentException("Path ends with slash");
|
|
905 |
return child.nodeExists(path);
|
|
906 |
}
|
|
907 |
}
|
|
908 |
|
|
909 |
/**
|
|
910 |
|
|
911 |
* Implements the <tt>removeNode()</tt> method as per the specification in
|
|
912 |
* {@link Preferences#removeNode()}.
|
|
913 |
*
|
|
914 |
* <p>This implementation checks to see that this node is the root; if so,
|
|
915 |
* it throws an appropriate exception. Then, it locks this node's parent,
|
|
916 |
* and calls a recursive helper method that traverses the subtree rooted at
|
|
917 |
* this node. The recursive method locks the node on which it was called,
|
|
918 |
* checks that it has not already been removed, and then ensures that all
|
|
919 |
* of its children are cached: The {@link #childrenNamesSpi()} method is
|
|
920 |
* invoked and each returned child name is checked for containment in the
|
|
921 |
* child-cache. If a child is not already cached, the {@link
|
|
922 |
* #childSpi(String)} method is invoked to create a <tt>Preferences</tt>
|
|
923 |
* instance for it, and this instance is put into the child-cache. Then
|
|
924 |
* the helper method calls itself recursively on each node contained in its
|
|
925 |
* child-cache. Next, it invokes {@link #removeNodeSpi()}, marks itself
|
|
926 |
* as removed, and removes itself from its parent's child-cache. Finally,
|
|
927 |
* if there are any node change listeners, it enqueues a notification
|
|
928 |
* event for processing by the event dispatch thread.
|
|
929 |
*
|
|
930 |
* <p>Note that the helper method is always invoked with all ancestors up
|
|
931 |
* to the "closest non-removed ancestor" locked.
|
|
932 |
*
|
|
933 |
* @throws IllegalStateException if this node (or an ancestor) has already
|
|
934 |
* been removed with the {@link #removeNode()} method.
|
|
935 |
* @throws UnsupportedOperationException if this method is invoked on
|
|
936 |
* the root node.
|
|
937 |
* @throws BackingStoreException if this operation cannot be completed
|
|
938 |
* due to a failure in the backing store, or inability to
|
|
939 |
* communicate with it.
|
|
940 |
*/
|
|
941 |
public void removeNode() throws BackingStoreException {
|
|
942 |
if (this==root)
|
|
943 |
throw new UnsupportedOperationException("Can't remove the root!");
|
|
944 |
synchronized(parent.lock) {
|
|
945 |
removeNode2();
|
|
946 |
parent.kidCache.remove(name);
|
|
947 |
}
|
|
948 |
}
|
|
949 |
|
|
950 |
/*
|
|
951 |
* Called with locks on all nodes on path from parent of "removal root"
|
|
952 |
* to this (including the former but excluding the latter).
|
|
953 |
*/
|
|
954 |
private void removeNode2() throws BackingStoreException {
|
|
955 |
synchronized(lock) {
|
|
956 |
if (removed)
|
|
957 |
throw new IllegalStateException("Node already removed.");
|
|
958 |
|
|
959 |
// Ensure that all children are cached
|
|
960 |
String[] kidNames = childrenNamesSpi();
|
|
961 |
for (int i=0; i<kidNames.length; i++)
|
|
962 |
if (!kidCache.containsKey(kidNames[i]))
|
|
963 |
kidCache.put(kidNames[i], childSpi(kidNames[i]));
|
|
964 |
|
|
965 |
// Recursively remove all cached children
|
51
|
966 |
for (Iterator<AbstractPreferences> i = kidCache.values().iterator();
|
|
967 |
i.hasNext();) {
|
2
|
968 |
try {
|
51
|
969 |
i.next().removeNode2();
|
2
|
970 |
i.remove();
|
|
971 |
} catch (BackingStoreException x) { }
|
|
972 |
}
|
|
973 |
|
|
974 |
// Now we have no descendants - it's time to die!
|
|
975 |
removeNodeSpi();
|
|
976 |
removed = true;
|
|
977 |
parent.enqueueNodeRemovedEvent(this);
|
|
978 |
}
|
|
979 |
}
|
|
980 |
|
|
981 |
/**
|
|
982 |
* Implements the <tt>name</tt> method as per the specification in
|
|
983 |
* {@link Preferences#name()}.
|
|
984 |
*
|
|
985 |
* <p>This implementation merely returns the name that was
|
|
986 |
* passed to this node's constructor.
|
|
987 |
*
|
|
988 |
* @return this preference node's name, relative to its parent.
|
|
989 |
*/
|
|
990 |
public String name() {
|
|
991 |
return name;
|
|
992 |
}
|
|
993 |
|
|
994 |
/**
|
|
995 |
* Implements the <tt>absolutePath</tt> method as per the specification in
|
|
996 |
* {@link Preferences#absolutePath()}.
|
|
997 |
*
|
|
998 |
* <p>This implementation merely returns the absolute path name that
|
|
999 |
* was computed at the time that this node was constructed (based on
|
|
1000 |
* the name that was passed to this node's constructor, and the names
|
|
1001 |
* that were passed to this node's ancestors' constructors).
|
|
1002 |
*
|
|
1003 |
* @return this preference node's absolute path name.
|
|
1004 |
*/
|
|
1005 |
public String absolutePath() {
|
|
1006 |
return absolutePath;
|
|
1007 |
}
|
|
1008 |
|
|
1009 |
/**
|
|
1010 |
* Implements the <tt>isUserNode</tt> method as per the specification in
|
|
1011 |
* {@link Preferences#isUserNode()}.
|
|
1012 |
*
|
|
1013 |
* <p>This implementation compares this node's root node (which is stored
|
|
1014 |
* in a private field) with the value returned by
|
|
1015 |
* {@link Preferences#userRoot()}. If the two object references are
|
|
1016 |
* identical, this method returns true.
|
|
1017 |
*
|
|
1018 |
* @return <tt>true</tt> if this preference node is in the user
|
|
1019 |
* preference tree, <tt>false</tt> if it's in the system
|
|
1020 |
* preference tree.
|
|
1021 |
*/
|
|
1022 |
public boolean isUserNode() {
|
51
|
1023 |
return AccessController.doPrivileged(
|
|
1024 |
new PrivilegedAction<Boolean>() {
|
|
1025 |
public Boolean run() {
|
|
1026 |
return root == Preferences.userRoot();
|
2
|
1027 |
}
|
51
|
1028 |
}).booleanValue();
|
2
|
1029 |
}
|
|
1030 |
|
|
1031 |
public void addPreferenceChangeListener(PreferenceChangeListener pcl) {
|
|
1032 |
if (pcl==null)
|
|
1033 |
throw new NullPointerException("Change listener is null.");
|
|
1034 |
synchronized(lock) {
|
|
1035 |
if (removed)
|
|
1036 |
throw new IllegalStateException("Node has been removed.");
|
|
1037 |
|
|
1038 |
// Copy-on-write
|
|
1039 |
PreferenceChangeListener[] old = prefListeners;
|
|
1040 |
prefListeners = new PreferenceChangeListener[old.length + 1];
|
|
1041 |
System.arraycopy(old, 0, prefListeners, 0, old.length);
|
|
1042 |
prefListeners[old.length] = pcl;
|
|
1043 |
}
|
|
1044 |
startEventDispatchThreadIfNecessary();
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
public void removePreferenceChangeListener(PreferenceChangeListener pcl) {
|
|
1048 |
synchronized(lock) {
|
|
1049 |
if (removed)
|
|
1050 |
throw new IllegalStateException("Node has been removed.");
|
|
1051 |
if ((prefListeners == null) || (prefListeners.length == 0))
|
|
1052 |
throw new IllegalArgumentException("Listener not registered.");
|
|
1053 |
|
|
1054 |
// Copy-on-write
|
|
1055 |
PreferenceChangeListener[] newPl =
|
|
1056 |
new PreferenceChangeListener[prefListeners.length - 1];
|
|
1057 |
int i = 0;
|
|
1058 |
while (i < newPl.length && prefListeners[i] != pcl)
|
|
1059 |
newPl[i] = prefListeners[i++];
|
|
1060 |
|
|
1061 |
if (i == newPl.length && prefListeners[i] != pcl)
|
|
1062 |
throw new IllegalArgumentException("Listener not registered.");
|
|
1063 |
while (i < newPl.length)
|
|
1064 |
newPl[i] = prefListeners[++i];
|
|
1065 |
prefListeners = newPl;
|
|
1066 |
}
|
|
1067 |
}
|
|
1068 |
|
|
1069 |
public void addNodeChangeListener(NodeChangeListener ncl) {
|
|
1070 |
if (ncl==null)
|
|
1071 |
throw new NullPointerException("Change listener is null.");
|
|
1072 |
synchronized(lock) {
|
|
1073 |
if (removed)
|
|
1074 |
throw new IllegalStateException("Node has been removed.");
|
|
1075 |
|
|
1076 |
// Copy-on-write
|
|
1077 |
if (nodeListeners == null) {
|
|
1078 |
nodeListeners = new NodeChangeListener[1];
|
|
1079 |
nodeListeners[0] = ncl;
|
|
1080 |
} else {
|
|
1081 |
NodeChangeListener[] old = nodeListeners;
|
|
1082 |
nodeListeners = new NodeChangeListener[old.length + 1];
|
|
1083 |
System.arraycopy(old, 0, nodeListeners, 0, old.length);
|
|
1084 |
nodeListeners[old.length] = ncl;
|
|
1085 |
}
|
|
1086 |
}
|
|
1087 |
startEventDispatchThreadIfNecessary();
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
public void removeNodeChangeListener(NodeChangeListener ncl) {
|
|
1091 |
synchronized(lock) {
|
|
1092 |
if (removed)
|
|
1093 |
throw new IllegalStateException("Node has been removed.");
|
|
1094 |
if ((nodeListeners == null) || (nodeListeners.length == 0))
|
|
1095 |
throw new IllegalArgumentException("Listener not registered.");
|
|
1096 |
|
|
1097 |
// Copy-on-write
|
|
1098 |
int i = 0;
|
|
1099 |
while (i < nodeListeners.length && nodeListeners[i] != ncl)
|
|
1100 |
i++;
|
|
1101 |
if (i == nodeListeners.length)
|
|
1102 |
throw new IllegalArgumentException("Listener not registered.");
|
|
1103 |
NodeChangeListener[] newNl =
|
|
1104 |
new NodeChangeListener[nodeListeners.length - 1];
|
|
1105 |
if (i != 0)
|
|
1106 |
System.arraycopy(nodeListeners, 0, newNl, 0, i);
|
|
1107 |
if (i != newNl.length)
|
|
1108 |
System.arraycopy(nodeListeners, i + 1,
|
|
1109 |
newNl, i, newNl.length - i);
|
|
1110 |
nodeListeners = newNl;
|
|
1111 |
}
|
|
1112 |
}
|
|
1113 |
|
|
1114 |
// "SPI" METHODS
|
|
1115 |
|
|
1116 |
/**
|
|
1117 |
* Put the given key-value association into this preference node. It is
|
|
1118 |
* guaranteed that <tt>key</tt> and <tt>value</tt> are non-null and of
|
|
1119 |
* legal length. Also, it is guaranteed that this node has not been
|
|
1120 |
* removed. (The implementor needn't check for any of these things.)
|
|
1121 |
*
|
|
1122 |
* <p>This method is invoked with the lock on this node held.
|
|
1123 |
*/
|
|
1124 |
protected abstract void putSpi(String key, String value);
|
|
1125 |
|
|
1126 |
/**
|
|
1127 |
* Return the value associated with the specified key at this preference
|
|
1128 |
* node, or <tt>null</tt> if there is no association for this key, or the
|
|
1129 |
* association cannot be determined at this time. It is guaranteed that
|
|
1130 |
* <tt>key</tt> is non-null. Also, it is guaranteed that this node has
|
|
1131 |
* not been removed. (The implementor needn't check for either of these
|
|
1132 |
* things.)
|
|
1133 |
*
|
|
1134 |
* <p> Generally speaking, this method should not throw an exception
|
|
1135 |
* under any circumstances. If, however, if it does throw an exception,
|
|
1136 |
* the exception will be intercepted and treated as a <tt>null</tt>
|
|
1137 |
* return value.
|
|
1138 |
*
|
|
1139 |
* <p>This method is invoked with the lock on this node held.
|
|
1140 |
*
|
|
1141 |
* @return the value associated with the specified key at this preference
|
|
1142 |
* node, or <tt>null</tt> if there is no association for this
|
|
1143 |
* key, or the association cannot be determined at this time.
|
|
1144 |
*/
|
|
1145 |
protected abstract String getSpi(String key);
|
|
1146 |
|
|
1147 |
/**
|
|
1148 |
* Remove the association (if any) for the specified key at this
|
|
1149 |
* preference node. It is guaranteed that <tt>key</tt> is non-null.
|
|
1150 |
* Also, it is guaranteed that this node has not been removed.
|
|
1151 |
* (The implementor needn't check for either of these things.)
|
|
1152 |
*
|
|
1153 |
* <p>This method is invoked with the lock on this node held.
|
|
1154 |
*/
|
|
1155 |
protected abstract void removeSpi(String key);
|
|
1156 |
|
|
1157 |
/**
|
|
1158 |
* Removes this preference node, invalidating it and any preferences that
|
|
1159 |
* it contains. The named child will have no descendants at the time this
|
|
1160 |
* invocation is made (i.e., the {@link Preferences#removeNode()} method
|
|
1161 |
* invokes this method repeatedly in a bottom-up fashion, removing each of
|
|
1162 |
* a node's descendants before removing the node itself).
|
|
1163 |
*
|
|
1164 |
* <p>This method is invoked with the lock held on this node and its
|
|
1165 |
* parent (and all ancestors that are being removed as a
|
|
1166 |
* result of a single invocation to {@link Preferences#removeNode()}).
|
|
1167 |
*
|
|
1168 |
* <p>The removal of a node needn't become persistent until the
|
|
1169 |
* <tt>flush</tt> method is invoked on this node (or an ancestor).
|
|
1170 |
*
|
|
1171 |
* <p>If this node throws a <tt>BackingStoreException</tt>, the exception
|
|
1172 |
* will propagate out beyond the enclosing {@link #removeNode()}
|
|
1173 |
* invocation.
|
|
1174 |
*
|
|
1175 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1176 |
* due to a failure in the backing store, or inability to
|
|
1177 |
* communicate with it.
|
|
1178 |
*/
|
|
1179 |
protected abstract void removeNodeSpi() throws BackingStoreException;
|
|
1180 |
|
|
1181 |
/**
|
|
1182 |
* Returns all of the keys that have an associated value in this
|
|
1183 |
* preference node. (The returned array will be of size zero if
|
|
1184 |
* this node has no preferences.) It is guaranteed that this node has not
|
|
1185 |
* been removed.
|
|
1186 |
*
|
|
1187 |
* <p>This method is invoked with the lock on this node held.
|
|
1188 |
*
|
|
1189 |
* <p>If this node throws a <tt>BackingStoreException</tt>, the exception
|
|
1190 |
* will propagate out beyond the enclosing {@link #keys()} invocation.
|
|
1191 |
*
|
|
1192 |
* @return an array of the keys that have an associated value in this
|
|
1193 |
* preference node.
|
|
1194 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1195 |
* due to a failure in the backing store, or inability to
|
|
1196 |
* communicate with it.
|
|
1197 |
*/
|
|
1198 |
protected abstract String[] keysSpi() throws BackingStoreException;
|
|
1199 |
|
|
1200 |
/**
|
|
1201 |
* Returns the names of the children of this preference node. (The
|
|
1202 |
* returned array will be of size zero if this node has no children.)
|
|
1203 |
* This method need not return the names of any nodes already cached,
|
|
1204 |
* but may do so without harm.
|
|
1205 |
*
|
|
1206 |
* <p>This method is invoked with the lock on this node held.
|
|
1207 |
*
|
|
1208 |
* <p>If this node throws a <tt>BackingStoreException</tt>, the exception
|
|
1209 |
* will propagate out beyond the enclosing {@link #childrenNames()}
|
|
1210 |
* invocation.
|
|
1211 |
*
|
|
1212 |
* @return an array containing the names of the children of this
|
|
1213 |
* preference node.
|
|
1214 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1215 |
* due to a failure in the backing store, or inability to
|
|
1216 |
* communicate with it.
|
|
1217 |
*/
|
|
1218 |
protected abstract String[] childrenNamesSpi()
|
|
1219 |
throws BackingStoreException;
|
|
1220 |
|
|
1221 |
/**
|
|
1222 |
* Returns the named child if it exists, or <tt>null</tt> if it does not.
|
|
1223 |
* It is guaranteed that <tt>nodeName</tt> is non-null, non-empty,
|
|
1224 |
* does not contain the slash character ('/'), and is no longer than
|
|
1225 |
* {@link #MAX_NAME_LENGTH} characters. Also, it is guaranteed
|
|
1226 |
* that this node has not been removed. (The implementor needn't check
|
|
1227 |
* for any of these things if he chooses to override this method.)
|
|
1228 |
*
|
|
1229 |
* <p>Finally, it is guaranteed that the named node has not been returned
|
|
1230 |
* by a previous invocation of this method or {@link #childSpi} after the
|
|
1231 |
* last time that it was removed. In other words, a cached value will
|
|
1232 |
* always be used in preference to invoking this method. (The implementor
|
|
1233 |
* needn't maintain his own cache of previously returned children if he
|
|
1234 |
* chooses to override this method.)
|
|
1235 |
*
|
|
1236 |
* <p>This implementation obtains this preference node's lock, invokes
|
|
1237 |
* {@link #childrenNames()} to get an array of the names of this node's
|
|
1238 |
* children, and iterates over the array comparing the name of each child
|
|
1239 |
* with the specified node name. If a child node has the correct name,
|
|
1240 |
* the {@link #childSpi(String)} method is invoked and the resulting
|
|
1241 |
* node is returned. If the iteration completes without finding the
|
|
1242 |
* specified name, <tt>null</tt> is returned.
|
|
1243 |
*
|
|
1244 |
* @param nodeName name of the child to be searched for.
|
|
1245 |
* @return the named child if it exists, or null if it does not.
|
|
1246 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1247 |
* due to a failure in the backing store, or inability to
|
|
1248 |
* communicate with it.
|
|
1249 |
*/
|
|
1250 |
protected AbstractPreferences getChild(String nodeName)
|
|
1251 |
throws BackingStoreException {
|
|
1252 |
synchronized(lock) {
|
|
1253 |
// assert kidCache.get(nodeName)==null;
|
|
1254 |
String[] kidNames = childrenNames();
|
|
1255 |
for (int i=0; i<kidNames.length; i++)
|
|
1256 |
if (kidNames[i].equals(nodeName))
|
|
1257 |
return childSpi(kidNames[i]);
|
|
1258 |
}
|
|
1259 |
return null;
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
/**
|
|
1263 |
* Returns the named child of this preference node, creating it if it does
|
|
1264 |
* not already exist. It is guaranteed that <tt>name</tt> is non-null,
|
|
1265 |
* non-empty, does not contain the slash character ('/'), and is no longer
|
|
1266 |
* than {@link #MAX_NAME_LENGTH} characters. Also, it is guaranteed that
|
|
1267 |
* this node has not been removed. (The implementor needn't check for any
|
|
1268 |
* of these things.)
|
|
1269 |
*
|
|
1270 |
* <p>Finally, it is guaranteed that the named node has not been returned
|
|
1271 |
* by a previous invocation of this method or {@link #getChild(String)}
|
|
1272 |
* after the last time that it was removed. In other words, a cached
|
|
1273 |
* value will always be used in preference to invoking this method.
|
|
1274 |
* Subclasses need not maintain their own cache of previously returned
|
|
1275 |
* children.
|
|
1276 |
*
|
|
1277 |
* <p>The implementer must ensure that the returned node has not been
|
|
1278 |
* removed. If a like-named child of this node was previously removed, the
|
|
1279 |
* implementer must return a newly constructed <tt>AbstractPreferences</tt>
|
|
1280 |
* node; once removed, an <tt>AbstractPreferences</tt> node
|
|
1281 |
* cannot be "resuscitated."
|
|
1282 |
*
|
|
1283 |
* <p>If this method causes a node to be created, this node is not
|
|
1284 |
* guaranteed to be persistent until the <tt>flush</tt> method is
|
|
1285 |
* invoked on this node or one of its ancestors (or descendants).
|
|
1286 |
*
|
|
1287 |
* <p>This method is invoked with the lock on this node held.
|
|
1288 |
*
|
|
1289 |
* @param name The name of the child node to return, relative to
|
|
1290 |
* this preference node.
|
|
1291 |
* @return The named child node.
|
|
1292 |
*/
|
|
1293 |
protected abstract AbstractPreferences childSpi(String name);
|
|
1294 |
|
|
1295 |
/**
|
|
1296 |
* Returns the absolute path name of this preferences node.
|
|
1297 |
*/
|
|
1298 |
public String toString() {
|
|
1299 |
return (this.isUserNode() ? "User" : "System") +
|
|
1300 |
" Preference Node: " + this.absolutePath();
|
|
1301 |
}
|
|
1302 |
|
|
1303 |
/**
|
|
1304 |
* Implements the <tt>sync</tt> method as per the specification in
|
|
1305 |
* {@link Preferences#sync()}.
|
|
1306 |
*
|
|
1307 |
* <p>This implementation calls a recursive helper method that locks this
|
|
1308 |
* node, invokes syncSpi() on it, unlocks this node, and recursively
|
|
1309 |
* invokes this method on each "cached child." A cached child is a child
|
|
1310 |
* of this node that has been created in this VM and not subsequently
|
|
1311 |
* removed. In effect, this method does a depth first traversal of the
|
|
1312 |
* "cached subtree" rooted at this node, calling syncSpi() on each node in
|
|
1313 |
* the subTree while only that node is locked. Note that syncSpi() is
|
|
1314 |
* invoked top-down.
|
|
1315 |
*
|
|
1316 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1317 |
* due to a failure in the backing store, or inability to
|
|
1318 |
* communicate with it.
|
|
1319 |
* @throws IllegalStateException if this node (or an ancestor) has been
|
|
1320 |
* removed with the {@link #removeNode()} method.
|
|
1321 |
* @see #flush()
|
|
1322 |
*/
|
|
1323 |
public void sync() throws BackingStoreException {
|
|
1324 |
sync2();
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
private void sync2() throws BackingStoreException {
|
|
1328 |
AbstractPreferences[] cachedKids;
|
|
1329 |
|
|
1330 |
synchronized(lock) {
|
|
1331 |
if (removed)
|
|
1332 |
throw new IllegalStateException("Node has been removed");
|
|
1333 |
syncSpi();
|
|
1334 |
cachedKids = cachedChildren();
|
|
1335 |
}
|
|
1336 |
|
|
1337 |
for (int i=0; i<cachedKids.length; i++)
|
|
1338 |
cachedKids[i].sync2();
|
|
1339 |
}
|
|
1340 |
|
|
1341 |
/**
|
|
1342 |
* This method is invoked with this node locked. The contract of this
|
|
1343 |
* method is to synchronize any cached preferences stored at this node
|
|
1344 |
* with any stored in the backing store. (It is perfectly possible that
|
|
1345 |
* this node does not exist on the backing store, either because it has
|
|
1346 |
* been deleted by another VM, or because it has not yet been created.)
|
|
1347 |
* Note that this method should <i>not</i> synchronize the preferences in
|
|
1348 |
* any subnodes of this node. If the backing store naturally syncs an
|
|
1349 |
* entire subtree at once, the implementer is encouraged to override
|
|
1350 |
* sync(), rather than merely overriding this method.
|
|
1351 |
*
|
|
1352 |
* <p>If this node throws a <tt>BackingStoreException</tt>, the exception
|
|
1353 |
* will propagate out beyond the enclosing {@link #sync()} invocation.
|
|
1354 |
*
|
|
1355 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1356 |
* due to a failure in the backing store, or inability to
|
|
1357 |
* communicate with it.
|
|
1358 |
*/
|
|
1359 |
protected abstract void syncSpi() throws BackingStoreException;
|
|
1360 |
|
|
1361 |
/**
|
|
1362 |
* Implements the <tt>flush</tt> method as per the specification in
|
|
1363 |
* {@link Preferences#flush()}.
|
|
1364 |
*
|
|
1365 |
* <p>This implementation calls a recursive helper method that locks this
|
|
1366 |
* node, invokes flushSpi() on it, unlocks this node, and recursively
|
|
1367 |
* invokes this method on each "cached child." A cached child is a child
|
|
1368 |
* of this node that has been created in this VM and not subsequently
|
|
1369 |
* removed. In effect, this method does a depth first traversal of the
|
|
1370 |
* "cached subtree" rooted at this node, calling flushSpi() on each node in
|
|
1371 |
* the subTree while only that node is locked. Note that flushSpi() is
|
|
1372 |
* invoked top-down.
|
|
1373 |
*
|
|
1374 |
* <p> If this method is invoked on a node that has been removed with
|
|
1375 |
* the {@link #removeNode()} method, flushSpi() is invoked on this node,
|
|
1376 |
* but not on others.
|
|
1377 |
*
|
|
1378 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1379 |
* due to a failure in the backing store, or inability to
|
|
1380 |
* communicate with it.
|
|
1381 |
* @see #flush()
|
|
1382 |
*/
|
|
1383 |
public void flush() throws BackingStoreException {
|
|
1384 |
flush2();
|
|
1385 |
}
|
|
1386 |
|
|
1387 |
private void flush2() throws BackingStoreException {
|
|
1388 |
AbstractPreferences[] cachedKids;
|
|
1389 |
|
|
1390 |
synchronized(lock) {
|
|
1391 |
flushSpi();
|
|
1392 |
if(removed)
|
|
1393 |
return;
|
|
1394 |
cachedKids = cachedChildren();
|
|
1395 |
}
|
|
1396 |
|
|
1397 |
for (int i = 0; i < cachedKids.length; i++)
|
|
1398 |
cachedKids[i].flush2();
|
|
1399 |
}
|
|
1400 |
|
|
1401 |
/**
|
|
1402 |
* This method is invoked with this node locked. The contract of this
|
|
1403 |
* method is to force any cached changes in the contents of this
|
|
1404 |
* preference node to the backing store, guaranteeing their persistence.
|
|
1405 |
* (It is perfectly possible that this node does not exist on the backing
|
|
1406 |
* store, either because it has been deleted by another VM, or because it
|
|
1407 |
* has not yet been created.) Note that this method should <i>not</i>
|
|
1408 |
* flush the preferences in any subnodes of this node. If the backing
|
|
1409 |
* store naturally flushes an entire subtree at once, the implementer is
|
|
1410 |
* encouraged to override flush(), rather than merely overriding this
|
|
1411 |
* method.
|
|
1412 |
*
|
|
1413 |
* <p>If this node throws a <tt>BackingStoreException</tt>, the exception
|
|
1414 |
* will propagate out beyond the enclosing {@link #flush()} invocation.
|
|
1415 |
*
|
|
1416 |
* @throws BackingStoreException if this operation cannot be completed
|
|
1417 |
* due to a failure in the backing store, or inability to
|
|
1418 |
* communicate with it.
|
|
1419 |
*/
|
|
1420 |
protected abstract void flushSpi() throws BackingStoreException;
|
|
1421 |
|
|
1422 |
/**
|
|
1423 |
* Returns <tt>true</tt> iff this node (or an ancestor) has been
|
|
1424 |
* removed with the {@link #removeNode()} method. This method
|
|
1425 |
* locks this node prior to returning the contents of the private
|
|
1426 |
* field used to track this state.
|
|
1427 |
*
|
|
1428 |
* @return <tt>true</tt> iff this node (or an ancestor) has been
|
|
1429 |
* removed with the {@link #removeNode()} method.
|
|
1430 |
*/
|
|
1431 |
protected boolean isRemoved() {
|
|
1432 |
synchronized(lock) {
|
|
1433 |
return removed;
|
|
1434 |
}
|
|
1435 |
}
|
|
1436 |
|
|
1437 |
/**
|
|
1438 |
* Queue of pending notification events. When a preference or node
|
|
1439 |
* change event for which there are one or more listeners occurs,
|
|
1440 |
* it is placed on this queue and the queue is notified. A background
|
|
1441 |
* thread waits on this queue and delivers the events. This decouples
|
|
1442 |
* event delivery from preference activity, greatly simplifying
|
|
1443 |
* locking and reducing opportunity for deadlock.
|
|
1444 |
*/
|
51
|
1445 |
private static final List<EventObject> eventQueue
|
|
1446 |
= new LinkedList<EventObject>();
|
2
|
1447 |
|
|
1448 |
/**
|
|
1449 |
* These two classes are used to distinguish NodeChangeEvents on
|
|
1450 |
* eventQueue so the event dispatch thread knows whether to call
|
|
1451 |
* childAdded or childRemoved.
|
|
1452 |
*/
|
|
1453 |
private class NodeAddedEvent extends NodeChangeEvent {
|
|
1454 |
private static final long serialVersionUID = -6743557530157328528L;
|
|
1455 |
NodeAddedEvent(Preferences parent, Preferences child) {
|
|
1456 |
super(parent, child);
|
|
1457 |
}
|
|
1458 |
}
|
|
1459 |
private class NodeRemovedEvent extends NodeChangeEvent {
|
|
1460 |
private static final long serialVersionUID = 8735497392918824837L;
|
|
1461 |
NodeRemovedEvent(Preferences parent, Preferences child) {
|
|
1462 |
super(parent, child);
|
|
1463 |
}
|
|
1464 |
}
|
|
1465 |
|
|
1466 |
/**
|
|
1467 |
* A single background thread ("the event notification thread") monitors
|
|
1468 |
* the event queue and delivers events that are placed on the queue.
|
|
1469 |
*/
|
|
1470 |
private static class EventDispatchThread extends Thread {
|
|
1471 |
public void run() {
|
|
1472 |
while(true) {
|
|
1473 |
// Wait on eventQueue till an event is present
|
|
1474 |
EventObject event = null;
|
|
1475 |
synchronized(eventQueue) {
|
|
1476 |
try {
|
|
1477 |
while (eventQueue.isEmpty())
|
|
1478 |
eventQueue.wait();
|
51
|
1479 |
event = eventQueue.remove(0);
|
2
|
1480 |
} catch (InterruptedException e) {
|
|
1481 |
// XXX Log "Event dispatch thread interrupted. Exiting"
|
|
1482 |
return;
|
|
1483 |
}
|
|
1484 |
}
|
|
1485 |
|
|
1486 |
// Now we have event & hold no locks; deliver evt to listeners
|
|
1487 |
AbstractPreferences src=(AbstractPreferences)event.getSource();
|
|
1488 |
if (event instanceof PreferenceChangeEvent) {
|
|
1489 |
PreferenceChangeEvent pce = (PreferenceChangeEvent)event;
|
|
1490 |
PreferenceChangeListener[] listeners = src.prefListeners();
|
|
1491 |
for (int i=0; i<listeners.length; i++)
|
|
1492 |
listeners[i].preferenceChange(pce);
|
|
1493 |
} else {
|
|
1494 |
NodeChangeEvent nce = (NodeChangeEvent)event;
|
|
1495 |
NodeChangeListener[] listeners = src.nodeListeners();
|
|
1496 |
if (nce instanceof NodeAddedEvent) {
|
|
1497 |
for (int i=0; i<listeners.length; i++)
|
|
1498 |
listeners[i].childAdded(nce);
|
|
1499 |
} else {
|
|
1500 |
// assert nce instanceof NodeRemovedEvent;
|
|
1501 |
for (int i=0; i<listeners.length; i++)
|
|
1502 |
listeners[i].childRemoved(nce);
|
|
1503 |
}
|
|
1504 |
}
|
|
1505 |
}
|
|
1506 |
}
|
|
1507 |
}
|
|
1508 |
|
|
1509 |
private static Thread eventDispatchThread = null;
|
|
1510 |
|
|
1511 |
/**
|
|
1512 |
* This method starts the event dispatch thread the first time it
|
|
1513 |
* is called. The event dispatch thread will be started only
|
|
1514 |
* if someone registers a listener.
|
|
1515 |
*/
|
|
1516 |
private static synchronized void startEventDispatchThreadIfNecessary() {
|
|
1517 |
if (eventDispatchThread == null) {
|
|
1518 |
// XXX Log "Starting event dispatch thread"
|
|
1519 |
eventDispatchThread = new EventDispatchThread();
|
|
1520 |
eventDispatchThread.setDaemon(true);
|
|
1521 |
eventDispatchThread.start();
|
|
1522 |
}
|
|
1523 |
}
|
|
1524 |
|
|
1525 |
/**
|
|
1526 |
* Return this node's preference/node change listeners. Even though
|
|
1527 |
* we're using a copy-on-write lists, we use synchronized accessors to
|
|
1528 |
* ensure information transmission from the writing thread to the
|
|
1529 |
* reading thread.
|
|
1530 |
*/
|
|
1531 |
PreferenceChangeListener[] prefListeners() {
|
|
1532 |
synchronized(lock) {
|
|
1533 |
return prefListeners;
|
|
1534 |
}
|
|
1535 |
}
|
|
1536 |
NodeChangeListener[] nodeListeners() {
|
|
1537 |
synchronized(lock) {
|
|
1538 |
return nodeListeners;
|
|
1539 |
}
|
|
1540 |
}
|
|
1541 |
|
|
1542 |
/**
|
|
1543 |
* Enqueue a preference change event for delivery to registered
|
|
1544 |
* preference change listeners unless there are no registered
|
|
1545 |
* listeners. Invoked with this.lock held.
|
|
1546 |
*/
|
|
1547 |
private void enqueuePreferenceChangeEvent(String key, String newValue) {
|
|
1548 |
if (prefListeners.length != 0) {
|
|
1549 |
synchronized(eventQueue) {
|
|
1550 |
eventQueue.add(new PreferenceChangeEvent(this, key, newValue));
|
|
1551 |
eventQueue.notify();
|
|
1552 |
}
|
|
1553 |
}
|
|
1554 |
}
|
|
1555 |
|
|
1556 |
/**
|
|
1557 |
* Enqueue a "node added" event for delivery to registered node change
|
|
1558 |
* listeners unless there are no registered listeners. Invoked with
|
|
1559 |
* this.lock held.
|
|
1560 |
*/
|
|
1561 |
private void enqueueNodeAddedEvent(Preferences child) {
|
|
1562 |
if (nodeListeners.length != 0) {
|
|
1563 |
synchronized(eventQueue) {
|
|
1564 |
eventQueue.add(new NodeAddedEvent(this, child));
|
|
1565 |
eventQueue.notify();
|
|
1566 |
}
|
|
1567 |
}
|
|
1568 |
}
|
|
1569 |
|
|
1570 |
/**
|
|
1571 |
* Enqueue a "node removed" event for delivery to registered node change
|
|
1572 |
* listeners unless there are no registered listeners. Invoked with
|
|
1573 |
* this.lock held.
|
|
1574 |
*/
|
|
1575 |
private void enqueueNodeRemovedEvent(Preferences child) {
|
|
1576 |
if (nodeListeners.length != 0) {
|
|
1577 |
synchronized(eventQueue) {
|
|
1578 |
eventQueue.add(new NodeRemovedEvent(this, child));
|
|
1579 |
eventQueue.notify();
|
|
1580 |
}
|
|
1581 |
}
|
|
1582 |
}
|
|
1583 |
|
|
1584 |
/**
|
|
1585 |
* Implements the <tt>exportNode</tt> method as per the specification in
|
|
1586 |
* {@link Preferences#exportNode(OutputStream)}.
|
|
1587 |
*
|
|
1588 |
* @param os the output stream on which to emit the XML document.
|
|
1589 |
* @throws IOException if writing to the specified output stream
|
|
1590 |
* results in an <tt>IOException</tt>.
|
|
1591 |
* @throws BackingStoreException if preference data cannot be read from
|
|
1592 |
* backing store.
|
|
1593 |
*/
|
|
1594 |
public void exportNode(OutputStream os)
|
|
1595 |
throws IOException, BackingStoreException
|
|
1596 |
{
|
|
1597 |
XmlSupport.export(os, this, false);
|
|
1598 |
}
|
|
1599 |
|
|
1600 |
/**
|
|
1601 |
* Implements the <tt>exportSubtree</tt> method as per the specification in
|
|
1602 |
* {@link Preferences#exportSubtree(OutputStream)}.
|
|
1603 |
*
|
|
1604 |
* @param os the output stream on which to emit the XML document.
|
|
1605 |
* @throws IOException if writing to the specified output stream
|
|
1606 |
* results in an <tt>IOException</tt>.
|
|
1607 |
* @throws BackingStoreException if preference data cannot be read from
|
|
1608 |
* backing store.
|
|
1609 |
*/
|
|
1610 |
public void exportSubtree(OutputStream os)
|
|
1611 |
throws IOException, BackingStoreException
|
|
1612 |
{
|
|
1613 |
XmlSupport.export(os, this, true);
|
|
1614 |
}
|
|
1615 |
}
|