AbstractPreferences.java 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /* AbstractPreferences -- Partial implementation of a Preference node
  2. Copyright (C) 2001, 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.prefs;
  32. import gnu.classpath.toolkit.DefaultDaemonThreadFactory;
  33. import gnu.java.lang.CPStringBuilder;
  34. import gnu.java.util.prefs.NodeWriter;
  35. import java.io.ByteArrayOutputStream;
  36. import java.io.IOException;
  37. import java.io.OutputStream;
  38. import java.util.ArrayList;
  39. import java.util.Collection;
  40. import java.util.HashMap;
  41. import java.util.Iterator;
  42. import java.util.TreeSet;
  43. import java.util.concurrent.Executor;
  44. import java.util.concurrent.Executors;
  45. /**
  46. * Partial implementation of a Preference node.
  47. *
  48. * @since 1.4
  49. * @author Mark Wielaard (mark@klomp.org)
  50. */
  51. public abstract class AbstractPreferences extends Preferences {
  52. // protected fields
  53. /**
  54. * Object used to lock this preference node. Any thread only locks nodes
  55. * downwards when it has the lock on the current node. No method should
  56. * synchronize on the lock of any of its parent nodes while holding the
  57. * lock on the current node.
  58. */
  59. protected final Object lock = new Object();
  60. /**
  61. * Set to true in the contructor if the node did not exist in the backing
  62. * store when this preference node object was created. Should be set in
  63. * the constructor of a subclass. Defaults to false. Used to fire node
  64. * changed events.
  65. */
  66. protected boolean newNode = false;
  67. // private fields
  68. /**
  69. * The parent preferences node or null when this is the root node.
  70. */
  71. private final AbstractPreferences parent;
  72. /**
  73. * The name of this node.
  74. * Only when this is a root node (parent == null) the name is empty.
  75. * It has a maximum of 80 characters and cannot contain any '/' characters.
  76. */
  77. private final String name;
  78. /** True when this node has been remove, false otherwise. */
  79. private boolean removed = false;
  80. /**
  81. * Holds all the child names and nodes of this node that have been
  82. * accessed by earlier <code>getChild()</code> or <code>childSpi()</code>
  83. * invocations and that have not been removed.
  84. */
  85. private HashMap<String, AbstractPreferences> childCache
  86. = new HashMap<String, AbstractPreferences>();
  87. /**
  88. * A list of all the registered NodeChangeListener objects.
  89. */
  90. private ArrayList<NodeChangeListener> nodeListeners;
  91. /**
  92. * A list of all the registered PreferenceChangeListener objects.
  93. */
  94. private ArrayList<PreferenceChangeListener> preferenceListeners;
  95. // constructor
  96. /**
  97. * Creates a new AbstractPreferences node with the given parent and name.
  98. *
  99. * @param parent the parent of this node or null when this is the root node
  100. * @param name the name of this node, can not be null, only 80 characters
  101. * maximum, must be empty when parent is null and cannot
  102. * contain any '/' characters
  103. * @exception IllegalArgumentException when name is null, greater then 80
  104. * characters, not the empty string but parent is null or
  105. * contains a '/' character
  106. */
  107. protected AbstractPreferences(AbstractPreferences parent, String name) {
  108. if ( (name == null) // name should be given
  109. || (name.length() > MAX_NAME_LENGTH) // 80 characters max
  110. || (parent == null && name.length() != 0) // root has no name
  111. || (parent != null && name.length() == 0) // all other nodes do
  112. || (name.indexOf('/') != -1)) // must not contain '/'
  113. throw new IllegalArgumentException("Illegal name argument '"
  114. + name
  115. + "' (parent is "
  116. + (parent == null ? "" : "not ")
  117. + "null)");
  118. this.parent = parent;
  119. this.name = name;
  120. }
  121. // identification methods
  122. /**
  123. * Returns the absolute path name of this preference node.
  124. * The absolute path name of a node is the path name of its parent node
  125. * plus a '/' plus its own name. If the node is the root node and has no
  126. * parent then its path name is "" and its absolute path name is "/".
  127. */
  128. public String absolutePath() {
  129. if (parent == null)
  130. return "/";
  131. else
  132. return parent.path() + '/' + name;
  133. }
  134. /**
  135. * Private helper method for absolutePath. Returns the empty string for a
  136. * root node and otherwise the parentPath of its parent plus a '/'.
  137. */
  138. private String path() {
  139. if (parent == null)
  140. return "";
  141. else
  142. return parent.path() + '/' + name;
  143. }
  144. /**
  145. * Returns true if this node comes from the user preferences tree, false
  146. * if it comes from the system preferences tree.
  147. */
  148. public boolean isUserNode() {
  149. AbstractPreferences root = this;
  150. while (root.parent != null)
  151. root = root.parent;
  152. return root == Preferences.userRoot();
  153. }
  154. /**
  155. * Returns the name of this preferences node. The name of the node cannot
  156. * be null, can be mostly 80 characters and cannot contain any '/'
  157. * characters. The root node has as name "".
  158. */
  159. public String name() {
  160. return name;
  161. }
  162. /**
  163. * Returns the String given by
  164. * <code>
  165. * (isUserNode() ? "User":"System") + " Preference Node: " + absolutePath()
  166. * </code>
  167. */
  168. public String toString() {
  169. return (isUserNode() ? "User":"System")
  170. + " Preference Node: "
  171. + absolutePath();
  172. }
  173. /**
  174. * Returns all known unremoved children of this node.
  175. *
  176. * @return All known unremoved children of this node
  177. */
  178. protected final AbstractPreferences[] cachedChildren()
  179. {
  180. Collection<AbstractPreferences> vals = childCache.values();
  181. return vals.toArray(new AbstractPreferences[vals.size()]);
  182. }
  183. /**
  184. * Returns all the direct sub nodes of this preferences node.
  185. * Needs access to the backing store to give a meaningfull answer.
  186. * <p>
  187. * This implementation locks this node, checks if the node has not yet
  188. * been removed and throws an <code>IllegalStateException</code> when it
  189. * has been. Then it creates a new <code>TreeSet</code> and adds any
  190. * already cached child nodes names. To get any uncached names it calls
  191. * <code>childrenNamesSpi()</code> and adds the result to the set. Finally
  192. * it calls <code>toArray()</code> on the created set. When the call to
  193. * <code>childrenNamesSpi</code> thows an <code>BackingStoreException</code>
  194. * this method will not catch that exception but propagate the exception
  195. * to the caller.
  196. *
  197. * @exception BackingStoreException when the backing store cannot be
  198. * reached
  199. * @exception IllegalStateException when this node has been removed
  200. */
  201. public String[] childrenNames() throws BackingStoreException {
  202. synchronized(lock) {
  203. if (isRemoved())
  204. throw new IllegalStateException("Node removed");
  205. TreeSet<String> childrenNames = new TreeSet<String>();
  206. // First get all cached node names
  207. childrenNames.addAll(childCache.keySet());
  208. // Then add any others
  209. String names[] = childrenNamesSpi();
  210. for (int i = 0; i < names.length; i++) {
  211. childrenNames.add(names[i]);
  212. }
  213. // And return the array of names
  214. String[] children = new String[childrenNames.size()];
  215. childrenNames.toArray(children);
  216. return children;
  217. }
  218. }
  219. /**
  220. * Returns a sub node of this preferences node if the given path is
  221. * relative (does not start with a '/') or a sub node of the root
  222. * if the path is absolute (does start with a '/').
  223. * <p>
  224. * This method first locks this node and checks if the node has not been
  225. * removed, if it has been removed it throws an exception. Then if the
  226. * path is relative (does not start with a '/') it checks if the path is
  227. * legal (does not end with a '/' and has no consecutive '/' characters).
  228. * Then it recursively gets a name from the path, gets the child node
  229. * from the child-cache of this node or calls the <code>childSpi()</code>
  230. * method to create a new child sub node. This is done recursively on the
  231. * newly created sub node with the rest of the path till the path is empty.
  232. * If the path is absolute (starts with a '/') the lock on this node is
  233. * droped and this method is called on the root of the preferences tree
  234. * with as argument the complete path minus the first '/'.
  235. *
  236. * @exception IllegalStateException if this node has been removed
  237. * @exception IllegalArgumentException if the path contains two or more
  238. * consecutive '/' characters, ends with a '/' charactor and is not the
  239. * string "/" (indicating the root node) or any name on the path is more
  240. * than 80 characters long
  241. */
  242. public Preferences node(String path) {
  243. synchronized(lock) {
  244. if (isRemoved())
  245. throw new IllegalStateException("Node removed");
  246. // Is it a relative path?
  247. if (!path.startsWith("/")) {
  248. // Check if it is a valid path
  249. if (path.indexOf("//") != -1 || path.endsWith("/"))
  250. throw new IllegalArgumentException(path);
  251. return getNode(path);
  252. }
  253. }
  254. // path started with a '/' so it is absolute
  255. // we drop the lock and start from the root (omitting the first '/')
  256. Preferences root = isUserNode() ? userRoot() : systemRoot();
  257. return root.node(path.substring(1));
  258. }
  259. /**
  260. * Private helper method for <code>node()</code>. Called with this node
  261. * locked. Returns this node when path is the empty string, if it is not
  262. * empty the next node name is taken from the path (all chars till the
  263. * next '/' or end of path string) and the node is either taken from the
  264. * child-cache of this node or the <code>childSpi()</code> method is called
  265. * on this node with the name as argument. Then this method is called
  266. * recursively on the just constructed child node with the rest of the
  267. * path.
  268. *
  269. * @param path should not end with a '/' character and should not contain
  270. * consecutive '/' characters
  271. * @exception IllegalArgumentException if path begins with a name that is
  272. * larger then 80 characters.
  273. */
  274. private Preferences getNode(String path) {
  275. // if mark is dom then goto end
  276. // Empty String "" indicates this node
  277. if (path.length() == 0)
  278. return this;
  279. // Calculate child name and rest of path
  280. String childName;
  281. String childPath;
  282. int nextSlash = path.indexOf('/');
  283. if (nextSlash == -1) {
  284. childName = path;
  285. childPath = "";
  286. } else {
  287. childName = path.substring(0, nextSlash);
  288. childPath = path.substring(nextSlash+1);
  289. }
  290. // Get the child node
  291. AbstractPreferences child;
  292. child = (AbstractPreferences)childCache.get(childName);
  293. if (child == null) {
  294. if (childName.length() > MAX_NAME_LENGTH)
  295. throw new IllegalArgumentException(childName);
  296. // Not in childCache yet so create a new sub node
  297. child = childSpi(childName);
  298. childCache.put(childName, child);
  299. if (child.newNode && nodeListeners != null)
  300. fire(new NodeChangeEvent(this, child), true);
  301. }
  302. // Lock the child and go down
  303. synchronized(child.lock) {
  304. return child.getNode(childPath);
  305. }
  306. }
  307. /**
  308. * Returns true if the node that the path points to exists in memory or
  309. * in the backing store. Otherwise it returns false or an exception is
  310. * thrown. When this node is removed the only valid parameter is the
  311. * empty string (indicating this node), the return value in that case
  312. * will be false.
  313. *
  314. * @exception BackingStoreException when the backing store cannot be
  315. * reached
  316. * @exception IllegalStateException if this node has been removed
  317. * and the path is not the empty string (indicating this node)
  318. * @exception IllegalArgumentException if the path contains two or more
  319. * consecutive '/' characters, ends with a '/' charactor and is not the
  320. * string "/" (indicating the root node) or any name on the path is more
  321. * then 80 characters long
  322. */
  323. public boolean nodeExists(String path) throws BackingStoreException {
  324. synchronized(lock) {
  325. if (isRemoved() && path.length() != 0)
  326. throw new IllegalStateException("Node removed");
  327. // Is it a relative path?
  328. if (!path.startsWith("/")) {
  329. // Check if it is a valid path
  330. if (path.indexOf("//") != -1 || path.endsWith("/"))
  331. throw new IllegalArgumentException(path);
  332. return existsNode(path);
  333. }
  334. }
  335. // path started with a '/' so it is absolute
  336. // we drop the lock and start from the root (omitting the first '/')
  337. Preferences root = isUserNode() ? userRoot() : systemRoot();
  338. return root.nodeExists(path.substring(1));
  339. }
  340. private boolean existsNode(String path) throws BackingStoreException {
  341. // Empty String "" indicates this node
  342. if (path.length() == 0)
  343. return(!isRemoved());
  344. // Calculate child name and rest of path
  345. String childName;
  346. String childPath;
  347. int nextSlash = path.indexOf('/');
  348. if (nextSlash == -1) {
  349. childName = path;
  350. childPath = "";
  351. } else {
  352. childName = path.substring(0, nextSlash);
  353. childPath = path.substring(nextSlash+1);
  354. }
  355. // Get the child node
  356. AbstractPreferences child;
  357. child = (AbstractPreferences)childCache.get(childName);
  358. if (child == null) {
  359. if (childName.length() > MAX_NAME_LENGTH)
  360. throw new IllegalArgumentException(childName);
  361. // Not in childCache yet so create a new sub node
  362. child = getChild(childName);
  363. if (child == null)
  364. return false;
  365. childCache.put(childName, child);
  366. }
  367. // Lock the child and go down
  368. synchronized(child.lock) {
  369. return child.existsNode(childPath);
  370. }
  371. }
  372. /**
  373. * Returns the child sub node if it exists in the backing store or null
  374. * if it does not exist. Called (indirectly) by <code>nodeExists()</code>
  375. * when a child node name can not be found in the cache.
  376. * <p>
  377. * Gets the lock on this node, calls <code>childrenNamesSpi()</code> to
  378. * get an array of all (possibly uncached) children and compares the
  379. * given name with the names in the array. If the name is found in the
  380. * array <code>childSpi()</code> is called to get an instance, otherwise
  381. * null is returned.
  382. *
  383. * @exception BackingStoreException when the backing store cannot be
  384. * reached
  385. */
  386. protected AbstractPreferences getChild(String name)
  387. throws BackingStoreException
  388. {
  389. synchronized(lock) {
  390. // Get all the names (not yet in the cache)
  391. String[] names = childrenNamesSpi();
  392. for (int i=0; i < names.length; i++)
  393. if (name.equals(names[i]))
  394. return childSpi(name);
  395. // No child with that name found
  396. return null;
  397. }
  398. }
  399. /**
  400. * Returns true if this node has been removed with the
  401. * <code>removeNode()</code> method, false otherwise.
  402. * <p>
  403. * Gets the lock on this node and then returns a boolean field set by
  404. * <code>removeNode</code> methods.
  405. */
  406. protected boolean isRemoved() {
  407. synchronized(lock) {
  408. return removed;
  409. }
  410. }
  411. /**
  412. * Returns the parent preferences node of this node or null if this is
  413. * the root of the preferences tree.
  414. * <p>
  415. * Gets the lock on this node, checks that the node has not been removed
  416. * and returns the parent given to the constructor.
  417. *
  418. * @exception IllegalStateException if this node has been removed
  419. */
  420. public Preferences parent() {
  421. synchronized(lock) {
  422. if (isRemoved())
  423. throw new IllegalStateException("Node removed");
  424. return parent;
  425. }
  426. }
  427. // export methods
  428. // Inherit javadoc.
  429. public void exportNode(OutputStream os)
  430. throws BackingStoreException,
  431. IOException
  432. {
  433. NodeWriter nodeWriter = new NodeWriter(this, os);
  434. nodeWriter.writePrefs();
  435. }
  436. // Inherit javadoc.
  437. public void exportSubtree(OutputStream os)
  438. throws BackingStoreException,
  439. IOException
  440. {
  441. NodeWriter nodeWriter = new NodeWriter(this, os);
  442. nodeWriter.writePrefsTree();
  443. }
  444. // preference entry manipulation methods
  445. /**
  446. * Returns an (possibly empty) array with all the keys of the preference
  447. * entries of this node.
  448. * <p>
  449. * This method locks this node and checks if the node has not been
  450. * removed, if it has been removed it throws an exception, then it returns
  451. * the result of calling <code>keysSpi()</code>.
  452. *
  453. * @exception BackingStoreException when the backing store cannot be
  454. * reached
  455. * @exception IllegalStateException if this node has been removed
  456. */
  457. public String[] keys() throws BackingStoreException {
  458. synchronized(lock) {
  459. if (isRemoved())
  460. throw new IllegalStateException("Node removed");
  461. return keysSpi();
  462. }
  463. }
  464. /**
  465. * Returns the value associated with the key in this preferences node. If
  466. * the default value of the key cannot be found in the preferences node
  467. * entries or something goes wrong with the backing store the supplied
  468. * default value is returned.
  469. * <p>
  470. * Checks that key is not null and not larger then 80 characters,
  471. * locks this node, and checks that the node has not been removed.
  472. * Then it calls <code>keySpi()</code> and returns
  473. * the result of that method or the given default value if it returned
  474. * null or throwed an exception.
  475. *
  476. * @exception IllegalArgumentException if key is larger then 80 characters
  477. * @exception IllegalStateException if this node has been removed
  478. * @exception NullPointerException if key is null
  479. */
  480. public String get(String key, String defaultVal) {
  481. if (key.length() > MAX_KEY_LENGTH)
  482. throw new IllegalArgumentException(key);
  483. synchronized(lock) {
  484. if (isRemoved())
  485. throw new IllegalStateException("Node removed");
  486. String value;
  487. try {
  488. value = getSpi(key);
  489. } catch (ThreadDeath death) {
  490. throw death;
  491. } catch (Throwable t) {
  492. value = null;
  493. }
  494. if (value != null) {
  495. return value;
  496. } else {
  497. return defaultVal;
  498. }
  499. }
  500. }
  501. /**
  502. * Convenience method for getting the given entry as a boolean.
  503. * When the string representation of the requested entry is either
  504. * "true" or "false" (ignoring case) then that value is returned,
  505. * otherwise the given default boolean value is returned.
  506. *
  507. * @exception IllegalArgumentException if key is larger then 80 characters
  508. * @exception IllegalStateException if this node has been removed
  509. * @exception NullPointerException if key is null
  510. */
  511. public boolean getBoolean(String key, boolean defaultVal) {
  512. String value = get(key, null);
  513. if ("true".equalsIgnoreCase(value))
  514. return true;
  515. if ("false".equalsIgnoreCase(value))
  516. return false;
  517. return defaultVal;
  518. }
  519. /**
  520. * Convenience method for getting the given entry as a byte array.
  521. * When the string representation of the requested entry is a valid
  522. * Base64 encoded string (without any other characters, such as newlines)
  523. * then the decoded Base64 string is returned as byte array,
  524. * otherwise the given default byte array value is returned.
  525. *
  526. * @exception IllegalArgumentException if key is larger then 80 characters
  527. * @exception IllegalStateException if this node has been removed
  528. * @exception NullPointerException if key is null
  529. */
  530. public byte[] getByteArray(String key, byte[] defaultVal) {
  531. String value = get(key, null);
  532. byte[] b = null;
  533. if (value != null) {
  534. b = decode64(value);
  535. }
  536. if (b != null)
  537. return b;
  538. else
  539. return defaultVal;
  540. }
  541. /**
  542. * Helper method for decoding a Base64 string as an byte array.
  543. * Returns null on encoding error. This method does not allow any other
  544. * characters present in the string then the 65 special base64 chars.
  545. */
  546. private static byte[] decode64(String s) {
  547. ByteArrayOutputStream bs = new ByteArrayOutputStream((s.length()/4)*3);
  548. char[] c = new char[s.length()];
  549. s.getChars(0, s.length(), c, 0);
  550. // Convert from base64 chars
  551. int endchar = -1;
  552. for(int j = 0; j < c.length && endchar == -1; j++) {
  553. if (c[j] >= 'A' && c[j] <= 'Z') {
  554. c[j] -= 'A';
  555. } else if (c[j] >= 'a' && c[j] <= 'z') {
  556. c[j] = (char) (c[j] + 26 - 'a');
  557. } else if (c[j] >= '0' && c[j] <= '9') {
  558. c[j] = (char) (c[j] + 52 - '0');
  559. } else if (c[j] == '+') {
  560. c[j] = 62;
  561. } else if (c[j] == '/') {
  562. c[j] = 63;
  563. } else if (c[j] == '=') {
  564. endchar = j;
  565. } else {
  566. return null; // encoding exception
  567. }
  568. }
  569. int remaining = endchar == -1 ? c.length : endchar;
  570. int i = 0;
  571. while (remaining > 0) {
  572. // Four input chars (6 bits) are decoded as three bytes as
  573. // 000000 001111 111122 222222
  574. byte b0 = (byte) (c[i] << 2);
  575. if (remaining >= 2) {
  576. b0 += (c[i+1] & 0x30) >> 4;
  577. }
  578. bs.write(b0);
  579. if (remaining >= 3) {
  580. byte b1 = (byte) ((c[i+1] & 0x0F) << 4);
  581. b1 += (byte) ((c[i+2] & 0x3C) >> 2);
  582. bs.write(b1);
  583. }
  584. if (remaining >= 4) {
  585. byte b2 = (byte) ((c[i+2] & 0x03) << 6);
  586. b2 += c[i+3];
  587. bs.write(b2);
  588. }
  589. i += 4;
  590. remaining -= 4;
  591. }
  592. return bs.toByteArray();
  593. }
  594. /**
  595. * Convenience method for getting the given entry as a double.
  596. * When the string representation of the requested entry can be decoded
  597. * with <code>Double.parseDouble()</code> then that double is returned,
  598. * otherwise the given default double value is returned.
  599. *
  600. * @exception IllegalArgumentException if key is larger then 80 characters
  601. * @exception IllegalStateException if this node has been removed
  602. * @exception NullPointerException if key is null
  603. */
  604. public double getDouble(String key, double defaultVal) {
  605. String value = get(key, null);
  606. if (value != null) {
  607. try {
  608. return Double.parseDouble(value);
  609. } catch (NumberFormatException nfe) { /* ignore */ }
  610. }
  611. return defaultVal;
  612. }
  613. /**
  614. * Convenience method for getting the given entry as a float.
  615. * When the string representation of the requested entry can be decoded
  616. * with <code>Float.parseFloat()</code> then that float is returned,
  617. * otherwise the given default float value is returned.
  618. *
  619. * @exception IllegalArgumentException if key is larger then 80 characters
  620. * @exception IllegalStateException if this node has been removed
  621. * @exception NullPointerException if key is null
  622. */
  623. public float getFloat(String key, float defaultVal) {
  624. String value = get(key, null);
  625. if (value != null) {
  626. try {
  627. return Float.parseFloat(value);
  628. } catch (NumberFormatException nfe) { /* ignore */ }
  629. }
  630. return defaultVal;
  631. }
  632. /**
  633. * Convenience method for getting the given entry as an integer.
  634. * When the string representation of the requested entry can be decoded
  635. * with <code>Integer.parseInt()</code> then that integer is returned,
  636. * otherwise the given default integer value is returned.
  637. *
  638. * @exception IllegalArgumentException if key is larger then 80 characters
  639. * @exception IllegalStateException if this node has been removed
  640. * @exception NullPointerException if key is null
  641. */
  642. public int getInt(String key, int defaultVal) {
  643. String value = get(key, null);
  644. if (value != null) {
  645. try {
  646. return Integer.parseInt(value);
  647. } catch (NumberFormatException nfe) { /* ignore */ }
  648. }
  649. return defaultVal;
  650. }
  651. /**
  652. * Convenience method for getting the given entry as a long.
  653. * When the string representation of the requested entry can be decoded
  654. * with <code>Long.parseLong()</code> then that long is returned,
  655. * otherwise the given default long value is returned.
  656. *
  657. * @exception IllegalArgumentException if key is larger then 80 characters
  658. * @exception IllegalStateException if this node has been removed
  659. * @exception NullPointerException if key is null
  660. */
  661. public long getLong(String key, long defaultVal) {
  662. String value = get(key, null);
  663. if (value != null) {
  664. try {
  665. return Long.parseLong(value);
  666. } catch (NumberFormatException nfe) { /* ignore */ }
  667. }
  668. return defaultVal;
  669. }
  670. /**
  671. * Sets the value of the given preferences entry for this node.
  672. * Key and value cannot be null, the key cannot exceed 80 characters
  673. * and the value cannot exceed 8192 characters.
  674. * <p>
  675. * The result will be immediately visible in this VM, but may not be
  676. * immediately written to the backing store.
  677. * <p>
  678. * Checks that key and value are valid, locks this node, and checks that
  679. * the node has not been removed. Then it calls <code>putSpi()</code>.
  680. *
  681. * @exception NullPointerException if either key or value are null
  682. * @exception IllegalArgumentException if either key or value are to large
  683. * @exception IllegalStateException when this node has been removed
  684. */
  685. public void put(String key, String value) {
  686. if (key.length() > MAX_KEY_LENGTH
  687. || value.length() > MAX_VALUE_LENGTH)
  688. throw new IllegalArgumentException("key ("
  689. + key.length() + ")"
  690. + " or value ("
  691. + value.length() + ")"
  692. + " to large");
  693. synchronized(lock) {
  694. if (isRemoved())
  695. throw new IllegalStateException("Node removed");
  696. putSpi(key, value);
  697. if (preferenceListeners != null)
  698. fire(new PreferenceChangeEvent(this, key, value));
  699. }
  700. }
  701. /**
  702. * Convenience method for setting the given entry as a boolean.
  703. * The boolean is converted with <code>Boolean.toString(value)</code>
  704. * and then stored in the preference entry as that string.
  705. *
  706. * @exception NullPointerException if key is null
  707. * @exception IllegalArgumentException if the key length is to large
  708. * @exception IllegalStateException when this node has been removed
  709. */
  710. public void putBoolean(String key, boolean value) {
  711. put(key, Boolean.toString(value));
  712. }
  713. /**
  714. * Convenience method for setting the given entry as an array of bytes.
  715. * The byte array is converted to a Base64 encoded string
  716. * and then stored in the preference entry as that string.
  717. * <p>
  718. * Note that a byte array encoded as a Base64 string will be about 1.3
  719. * times larger then the original length of the byte array, which means
  720. * that the byte array may not be larger about 6 KB.
  721. *
  722. * @exception NullPointerException if either key or value are null
  723. * @exception IllegalArgumentException if either key or value are to large
  724. * @exception IllegalStateException when this node has been removed
  725. */
  726. public void putByteArray(String key, byte[] value) {
  727. put(key, encode64(value));
  728. }
  729. /**
  730. * Helper method for encoding an array of bytes as a Base64 String.
  731. */
  732. private static String encode64(byte[] b) {
  733. CPStringBuilder sb = new CPStringBuilder((b.length/3)*4);
  734. int i = 0;
  735. int remaining = b.length;
  736. char c[] = new char[4];
  737. while (remaining > 0) {
  738. // Three input bytes are encoded as four chars (6 bits) as
  739. // 00000011 11112222 22333333
  740. c[0] = (char) ((b[i] & 0xFC) >> 2);
  741. c[1] = (char) ((b[i] & 0x03) << 4);
  742. if (remaining >= 2) {
  743. c[1] += (char) ((b[i+1] & 0xF0) >> 4);
  744. c[2] = (char) ((b[i+1] & 0x0F) << 2);
  745. if (remaining >= 3) {
  746. c[2] += (char) ((b[i+2] & 0xC0) >> 6);
  747. c[3] = (char) (b[i+2] & 0x3F);
  748. } else {
  749. c[3] = 64;
  750. }
  751. } else {
  752. c[2] = 64;
  753. c[3] = 64;
  754. }
  755. // Convert to base64 chars
  756. for(int j = 0; j < 4; j++) {
  757. if (c[j] < 26) {
  758. c[j] += 'A';
  759. } else if (c[j] < 52) {
  760. c[j] = (char) (c[j] - 26 + 'a');
  761. } else if (c[j] < 62) {
  762. c[j] = (char) (c[j] - 52 + '0');
  763. } else if (c[j] == 62) {
  764. c[j] = '+';
  765. } else if (c[j] == 63) {
  766. c[j] = '/';
  767. } else {
  768. c[j] = '=';
  769. }
  770. }
  771. sb.append(c);
  772. i += 3;
  773. remaining -= 3;
  774. }
  775. return sb.toString();
  776. }
  777. /**
  778. * Convenience method for setting the given entry as a double.
  779. * The double is converted with <code>Double.toString(double)</code>
  780. * and then stored in the preference entry as that string.
  781. *
  782. * @exception NullPointerException if the key is null
  783. * @exception IllegalArgumentException if the key length is to large
  784. * @exception IllegalStateException when this node has been removed
  785. */
  786. public void putDouble(String key, double value) {
  787. put(key, Double.toString(value));
  788. }
  789. /**
  790. * Convenience method for setting the given entry as a float.
  791. * The float is converted with <code>Float.toString(float)</code>
  792. * and then stored in the preference entry as that string.
  793. *
  794. * @exception NullPointerException if the key is null
  795. * @exception IllegalArgumentException if the key length is to large
  796. * @exception IllegalStateException when this node has been removed
  797. */
  798. public void putFloat(String key, float value) {
  799. put(key, Float.toString(value));
  800. }
  801. /**
  802. * Convenience method for setting the given entry as an integer.
  803. * The integer is converted with <code>Integer.toString(int)</code>
  804. * and then stored in the preference entry as that string.
  805. *
  806. * @exception NullPointerException if the key is null
  807. * @exception IllegalArgumentException if the key length is to large
  808. * @exception IllegalStateException when this node has been removed
  809. */
  810. public void putInt(String key, int value) {
  811. put(key, Integer.toString(value));
  812. }
  813. /**
  814. * Convenience method for setting the given entry as a long.
  815. * The long is converted with <code>Long.toString(long)</code>
  816. * and then stored in the preference entry as that string.
  817. *
  818. * @exception NullPointerException if the key is null
  819. * @exception IllegalArgumentException if the key length is to large
  820. * @exception IllegalStateException when this node has been removed
  821. */
  822. public void putLong(String key, long value) {
  823. put(key, Long.toString(value));
  824. }
  825. /**
  826. * Removes the preferences entry from this preferences node.
  827. * <p>
  828. * The result will be immediately visible in this VM, but may not be
  829. * immediately written to the backing store.
  830. * <p>
  831. * This implementation checks that the key is not larger then 80
  832. * characters, gets the lock of this node, checks that the node has
  833. * not been removed and calls <code>removeSpi</code> with the given key.
  834. *
  835. * @exception NullPointerException if the key is null
  836. * @exception IllegalArgumentException if the key length is to large
  837. * @exception IllegalStateException when this node has been removed
  838. */
  839. public void remove(String key) {
  840. if (key.length() > MAX_KEY_LENGTH)
  841. throw new IllegalArgumentException(key);
  842. synchronized(lock) {
  843. if (isRemoved())
  844. throw new IllegalStateException("Node removed");
  845. removeSpi(key);
  846. if (preferenceListeners != null)
  847. fire(new PreferenceChangeEvent(this, key, null));
  848. }
  849. }
  850. /**
  851. * Removes all entries from this preferences node. May need access to the
  852. * backing store to get and clear all entries.
  853. * <p>
  854. * The result will be immediately visible in this VM, but may not be
  855. * immediatly written to the backing store.
  856. * <p>
  857. * This implementation locks this node, checks that the node has not been
  858. * removed and calls <code>keys()</code> to get a complete array of keys
  859. * for this node. For every key found <code>removeSpi()</code> is called.
  860. *
  861. * @exception BackingStoreException when the backing store cannot be
  862. * reached
  863. * @exception IllegalStateException if this node has been removed
  864. */
  865. public void clear() throws BackingStoreException {
  866. synchronized(lock) {
  867. if (isRemoved())
  868. throw new IllegalStateException("Node Removed");
  869. String[] keys = keys();
  870. for (int i = 0; i < keys.length; i++) {
  871. removeSpi(keys[i]);
  872. }
  873. }
  874. }
  875. /**
  876. * Writes all preference changes on this and any subnode that have not
  877. * yet been written to the backing store. This has no effect on the
  878. * preference entries in this VM, but it makes sure that all changes
  879. * are visible to other programs (other VMs might need to call the
  880. * <code>sync()</code> method to actually see the changes to the backing
  881. * store.
  882. * <p>
  883. * Locks this node, calls the <code>flushSpi()</code> method, gets all
  884. * the (cached - already existing in this VM) subnodes and then calls
  885. * <code>flushSpi()</code> on every subnode with this node unlocked and
  886. * only that particular subnode locked.
  887. *
  888. * @exception BackingStoreException when the backing store cannot be
  889. * reached
  890. */
  891. public void flush() throws BackingStoreException {
  892. flushNode(false);
  893. }
  894. /**
  895. * Writes and reads all preference changes to and from this and any
  896. * subnodes. This makes sure that all local changes are written to the
  897. * backing store and that all changes to the backing store are visible
  898. * in this preference node (and all subnodes).
  899. * <p>
  900. * Checks that this node is not removed, locks this node, calls the
  901. * <code>syncSpi()</code> method, gets all the subnodes and then calls
  902. * <code>syncSpi()</code> on every subnode with this node unlocked and
  903. * only that particular subnode locked.
  904. *
  905. * @exception BackingStoreException when the backing store cannot be
  906. * reached
  907. * @exception IllegalStateException if this node has been removed
  908. */
  909. public void sync() throws BackingStoreException {
  910. flushNode(true);
  911. }
  912. /**
  913. * Private helper method that locks this node and calls either
  914. * <code>flushSpi()</code> if <code>sync</code> is false, or
  915. * <code>flushSpi()</code> if <code>sync</code> is true. Then it gets all
  916. * the currently cached subnodes. For every subnode it calls this method
  917. * recursively with this node no longer locked.
  918. * <p>
  919. * Called by either <code>flush()</code> or <code>sync()</code>
  920. */
  921. private void flushNode(boolean sync) throws BackingStoreException {
  922. String[] keys = null;
  923. synchronized(lock) {
  924. if (sync) {
  925. syncSpi();
  926. } else {
  927. flushSpi();
  928. }
  929. keys = (String[]) childCache.keySet().toArray(new String[]{});
  930. }
  931. if (keys != null) {
  932. for (int i = 0; i < keys.length; i++) {
  933. // Have to lock this node again to access the childCache
  934. AbstractPreferences subNode;
  935. synchronized(lock) {
  936. subNode = (AbstractPreferences) childCache.get(keys[i]);
  937. }
  938. // The child could already have been removed from the cache
  939. if (subNode != null) {
  940. subNode.flushNode(sync);
  941. }
  942. }
  943. }
  944. }
  945. /**
  946. * Removes this and all subnodes from the backing store and clears all
  947. * entries. After removal this instance will not be useable (except for
  948. * a few methods that don't throw a <code>InvalidStateException</code>),
  949. * even when a new node with the same path name is created this instance
  950. * will not be usable again.
  951. * <p>
  952. * Checks that this is not a root node. If not it locks the parent node,
  953. * then locks this node and checks that the node has not yet been removed.
  954. * Then it makes sure that all subnodes of this node are in the child cache,
  955. * by calling <code>childSpi()</code> on any children not yet in the cache.
  956. * Then for all children it locks the subnode and removes it. After all
  957. * subnodes have been purged the child cache is cleared, this nodes removed
  958. * flag is set and any listeners are called. Finally this node is removed
  959. * from the child cache of the parent node.
  960. *
  961. * @exception BackingStoreException when the backing store cannot be
  962. * reached
  963. * @exception IllegalStateException if this node has already been removed
  964. * @exception UnsupportedOperationException if this is a root node
  965. */
  966. public void removeNode() throws BackingStoreException {
  967. // Check if it is a root node
  968. if (parent == null)
  969. throw new UnsupportedOperationException("Cannot remove root node");
  970. synchronized (parent.lock) {
  971. synchronized(this.lock) {
  972. if (isRemoved())
  973. throw new IllegalStateException("Node Removed");
  974. purge();
  975. }
  976. parent.childCache.remove(name);
  977. }
  978. }
  979. /**
  980. * Private helper method used to completely remove this node.
  981. * Called by <code>removeNode</code> with the parent node and this node
  982. * locked.
  983. * <p>
  984. * Makes sure that all subnodes of this node are in the child cache,
  985. * by calling <code>childSpi()</code> on any children not yet in the
  986. * cache. Then for all children it locks the subnode and calls this method
  987. * on that node. After all subnodes have been purged the child cache is
  988. * cleared, this nodes removed flag is set and any listeners are called.
  989. */
  990. private void purge() throws BackingStoreException
  991. {
  992. // Make sure all children have an AbstractPreferences node in cache
  993. String children[] = childrenNamesSpi();
  994. for (int i = 0; i < children.length; i++) {
  995. if (childCache.get(children[i]) == null)
  996. childCache.put(children[i], childSpi(children[i]));
  997. }
  998. // purge all children
  999. Iterator i = childCache.values().iterator();
  1000. while (i.hasNext()) {
  1001. AbstractPreferences node = (AbstractPreferences) i.next();
  1002. synchronized(node.lock) {
  1003. node.purge();
  1004. }
  1005. }
  1006. // Cache is empty now
  1007. childCache.clear();
  1008. // remove this node
  1009. removeNodeSpi();
  1010. removed = true;
  1011. if (nodeListeners != null)
  1012. fire(new NodeChangeEvent(parent, this), false);
  1013. }
  1014. // listener methods
  1015. /**
  1016. * Add a listener which is notified when a sub-node of this node
  1017. * is added or removed.
  1018. * @param listener the listener to add
  1019. */
  1020. public void addNodeChangeListener(NodeChangeListener listener)
  1021. {
  1022. synchronized (lock)
  1023. {
  1024. if (isRemoved())
  1025. throw new IllegalStateException("node has been removed");
  1026. if (listener == null)
  1027. throw new NullPointerException("listener is null");
  1028. if (nodeListeners == null)
  1029. nodeListeners = new ArrayList<NodeChangeListener>();
  1030. nodeListeners.add(listener);
  1031. }
  1032. }
  1033. /**
  1034. * Add a listener which is notified when a value in this node
  1035. * is added, changed, or removed.
  1036. * @param listener the listener to add
  1037. */
  1038. public void addPreferenceChangeListener(PreferenceChangeListener listener)
  1039. {
  1040. synchronized (lock)
  1041. {
  1042. if (isRemoved())
  1043. throw new IllegalStateException("node has been removed");
  1044. if (listener == null)
  1045. throw new NullPointerException("listener is null");
  1046. if (preferenceListeners == null)
  1047. preferenceListeners = new ArrayList<PreferenceChangeListener>();
  1048. preferenceListeners.add(listener);
  1049. }
  1050. }
  1051. /**
  1052. * Remove the indicated node change listener from the list of
  1053. * listeners to notify.
  1054. * @param listener the listener to remove
  1055. */
  1056. public void removeNodeChangeListener(NodeChangeListener listener)
  1057. {
  1058. synchronized (lock)
  1059. {
  1060. if (isRemoved())
  1061. throw new IllegalStateException("node has been removed");
  1062. if (listener == null)
  1063. throw new NullPointerException("listener is null");
  1064. if (nodeListeners != null)
  1065. nodeListeners.remove(listener);
  1066. }
  1067. }
  1068. /**
  1069. * Remove the indicated preference change listener from the list of
  1070. * listeners to notify.
  1071. * @param listener the listener to remove
  1072. */
  1073. public void removePreferenceChangeListener (PreferenceChangeListener listener)
  1074. {
  1075. synchronized (lock)
  1076. {
  1077. if (isRemoved())
  1078. throw new IllegalStateException("node has been removed");
  1079. if (listener == null)
  1080. throw new NullPointerException("listener is null");
  1081. if (preferenceListeners != null)
  1082. preferenceListeners.remove(listener);
  1083. }
  1084. }
  1085. /**
  1086. * Send a preference change event to all listeners. Note that
  1087. * the caller is responsible for holding the node's lock, and
  1088. * for checking that the list of listeners is not null.
  1089. * @param event the event to send
  1090. */
  1091. private void fire(final PreferenceChangeEvent event)
  1092. {
  1093. for (final PreferenceChangeListener listener : preferenceListeners)
  1094. {
  1095. Runnable dispatcher = new Runnable() {
  1096. public void run()
  1097. {
  1098. listener.preferenceChange(event);
  1099. }
  1100. };
  1101. Executor executor =
  1102. Executors.newSingleThreadExecutor(new DefaultDaemonThreadFactory());
  1103. executor.execute(dispatcher);
  1104. }
  1105. }
  1106. /**
  1107. * Send a node change event to all listeners. Note that
  1108. * the caller is responsible for holding the node's lock, and
  1109. * for checking that the list of listeners is not null.
  1110. * @param event the event to send
  1111. */
  1112. private void fire(final NodeChangeEvent event, final boolean added)
  1113. {
  1114. for (final NodeChangeListener listener : nodeListeners)
  1115. {
  1116. Runnable dispatcher = new Runnable() {
  1117. public void run()
  1118. {
  1119. if (added)
  1120. listener.childAdded(event);
  1121. else
  1122. listener.childRemoved(event);
  1123. }
  1124. };
  1125. Executor executor =
  1126. Executors.newSingleThreadExecutor(new DefaultDaemonThreadFactory());
  1127. executor.execute(dispatcher);
  1128. }
  1129. }
  1130. // abstract spi methods
  1131. /**
  1132. * Returns the names of the sub nodes of this preference node.
  1133. * This method only has to return any not yet cached child names,
  1134. * but may return all names if that is easier. It must not return
  1135. * null when there are no children, it has to return an empty array
  1136. * in that case. Since this method must consult the backing store to
  1137. * get all the sub node names it may throw a BackingStoreException.
  1138. * <p>
  1139. * Called by <code>childrenNames()</code> with this node locked.
  1140. */
  1141. protected abstract String[] childrenNamesSpi() throws BackingStoreException;
  1142. /**
  1143. * Returns a child note with the given name.
  1144. * This method is called by the <code>node()</code> method (indirectly
  1145. * through the <code>getNode()</code> helper method) with this node locked
  1146. * if a sub node with this name does not already exist in the child cache.
  1147. * If the child node did not aleady exist in the backing store the boolean
  1148. * field <code>newNode</code> of the returned node should be set.
  1149. * <p>
  1150. * Note that this method should even return a non-null child node if the
  1151. * backing store is not available since it may not throw a
  1152. * <code>BackingStoreException</code>.
  1153. */
  1154. protected abstract AbstractPreferences childSpi(String name);
  1155. /**
  1156. * Returns an (possibly empty) array with all the keys of the preference
  1157. * entries of this node.
  1158. * <p>
  1159. * Called by <code>keys()</code> with this node locked if this node has
  1160. * not been removed. May throw an exception when the backing store cannot
  1161. * be accessed.
  1162. *
  1163. * @exception BackingStoreException when the backing store cannot be
  1164. * reached
  1165. */
  1166. protected abstract String[] keysSpi() throws BackingStoreException;
  1167. /**
  1168. * Returns the value associated with the key in this preferences node or
  1169. * null when the key does not exist in this preferences node.
  1170. * <p>
  1171. * Called by <code>key()</code> with this node locked after checking that
  1172. * key is valid, not null and that the node has not been removed.
  1173. * <code>key()</code> will catch any exceptions that this method throws.
  1174. */
  1175. protected abstract String getSpi(String key);
  1176. /**
  1177. * Sets the value of the given preferences entry for this node.
  1178. * The implementation is not required to propagate the change to the
  1179. * backing store immediately. It may not throw an exception when it tries
  1180. * to write to the backing store and that operation fails, the failure
  1181. * should be registered so a later invocation of <code>flush()</code>
  1182. * or <code>sync()</code> can signal the failure.
  1183. * <p>
  1184. * Called by <code>put()</code> with this node locked after checking that
  1185. * key and value are valid and non-null.
  1186. */
  1187. protected abstract void putSpi(String key, String value);
  1188. /**
  1189. * Removes the given key entry from this preferences node.
  1190. * The implementation is not required to propagate the change to the
  1191. * backing store immediately. It may not throw an exception when it tries
  1192. * to write to the backing store and that operation fails, the failure
  1193. * should be registered so a later invocation of <code>flush()</code>
  1194. * or <code>sync()</code> can signal the failure.
  1195. * <p>
  1196. * Called by <code>remove()</code> with this node locked after checking
  1197. * that the key is valid and non-null.
  1198. */
  1199. protected abstract void removeSpi(String key);
  1200. /**
  1201. * Writes all entries of this preferences node that have not yet been
  1202. * written to the backing store and possibly creates this node in the
  1203. * backing store, if it does not yet exist. Should only write changes to
  1204. * this node and not write changes to any subnodes.
  1205. * Note that the node can be already removed in this VM. To check if
  1206. * that is the case the implementation can call <code>isRemoved()</code>.
  1207. * <p>
  1208. * Called (indirectly) by <code>flush()</code> with this node locked.
  1209. */
  1210. protected abstract void flushSpi() throws BackingStoreException;
  1211. /**
  1212. * Writes all entries of this preferences node that have not yet been
  1213. * written to the backing store and reads any entries that have changed
  1214. * in the backing store but that are not yet visible in this VM.
  1215. * Should only sync this node and not change any of the subnodes.
  1216. * Note that the node can be already removed in this VM. To check if
  1217. * that is the case the implementation can call <code>isRemoved()</code>.
  1218. * <p>
  1219. * Called (indirectly) by <code>sync()</code> with this node locked.
  1220. */
  1221. protected abstract void syncSpi() throws BackingStoreException;
  1222. /**
  1223. * Clears this node from this VM and removes it from the backing store.
  1224. * After this method has been called the node is marked as removed.
  1225. * <p>
  1226. * Called (indirectly) by <code>removeNode()</code> with this node locked
  1227. * after all the sub nodes of this node have already been removed.
  1228. */
  1229. protected abstract void removeNodeSpi() throws BackingStoreException;
  1230. }