| 1 7 8 package java.util.prefs; 9 10 import java.util.*; 11 import java.io.*; 12 import java.security.AccessController ; 13 import java.security.PrivilegedAction ; 14 import java.lang.Integer ; 16 import java.lang.Long ; 17 import java.lang.Float ; 18 import java.lang.Double ; 19 20 106 public abstract class AbstractPreferences extends Preferences { 107 110 private final String name; 111 112 115 private final String absolutePath; 116 117 120 final AbstractPreferences parent; 121 122 125 private final AbstractPreferences root; 127 135 protected boolean newNode = false; 136 137 141 private Map kidCache = new HashMap(); 142 143 147 private boolean removed = false; 148 149 152 private PreferenceChangeListener [] prefListeners = 153 new PreferenceChangeListener [0]; 154 155 158 private NodeChangeListener [] nodeListeners = new NodeChangeListener [0]; 159 160 167 protected final Object lock = new Object (); 168 169 181 protected AbstractPreferences(AbstractPreferences parent, String name) { 182 if (parent==null) { 183 if (!name.equals("")) 184 throw new IllegalArgumentException ("Root name '"+name+ 185 "' must be \"\""); 186 this.absolutePath = "/"; 187 root = this; 188 } else { 189 if (name.indexOf('/') != -1) 190 throw new IllegalArgumentException ("Name '" + name + 191 "' contains '/'"); 192 if (name.equals("")) 193 throw new IllegalArgumentException ("Illegal name: empty string"); 194 195 root = parent.root; 196 absolutePath = (parent==root ? "/" + name 197 : parent.absolutePath() + "/" + name); 198 } 199 this.name = name; 200 this.parent = parent; 201 } 202 203 222 public void put(String key, String value) { 223 if (key==null || value==null) 224 throw new NullPointerException (); 225 if (key.length() > MAX_KEY_LENGTH) 226 throw new IllegalArgumentException ("Key too long: "+key); 227 if (value.length() > MAX_VALUE_LENGTH) 228 throw new IllegalArgumentException ("Value too long: "+value); 229 230 synchronized(lock) { 231 if (removed) 232 throw new IllegalStateException ("Node has been removed."); 233 234 putSpi(key, value); 235 enqueuePreferenceChangeEvent(key, value); 236 } 237 } 238 239 261 public String get(String key, String def) { 262 if (key==null) 263 throw new NullPointerException ("Null key"); 264 synchronized(lock) { 265 if (removed) 266 throw new IllegalStateException ("Node has been removed."); 267 268 String result = null; 269 try { 270 result = getSpi(key); 271 } catch (Exception e) { 272 } 274 return (result==null ? def : result); 275 } 276 } 277 278 292 public void remove(String key) { 293 synchronized(lock) { 294 if (removed) 295 throw new IllegalStateException ("Node has been removed."); 296 297 removeSpi(key); 298 enqueuePreferenceChangeEvent(key, null); 299 } 300 } 301 302 316 public void clear() throws BackingStoreException { 317 synchronized(lock) { 318 String [] keys = keys(); 319 for (int i=0; i<keys.length; i++) 320 remove(keys[i]); 321 } 322 } 323 324 340 public void putInt(String key, int value) { 341 put(key, Integer.toString(value)); 342 } 343 344 366 public int getInt(String key, int def) { 367 int result = def; 368 try { 369 String value = get(key, null); 370 if (value != null) 371 result = Integer.parseInt(value); 372 } catch (NumberFormatException e) { 373 } 375 376 return result; 377 } 378 379 395 public void putLong(String key, long value) { 396 put(key, Long.toString(value)); 397 } 398 399 421 public long getLong(String key, long def) { 422 long result = def; 423 try { 424 String value = get(key, null); 425 if (value != null) 426 result = Long.parseLong(value); 427 } catch (NumberFormatException e) { 428 } 430 431 return result; 432 } 433 434 450 public void putBoolean(String key, boolean value) { 451 put(key, String.valueOf(value)); 452 } 453 454 479 public boolean getBoolean(String key, boolean def) { 480 boolean result = def; 481 String value = get(key, null); 482 if (value != null) { 483 if (value.equalsIgnoreCase("true")) 484 result = true; 485 else if (value.equalsIgnoreCase("false")) 486 result = false; 487 } 488 489 return result; 490 } 491 492 508 public void putFloat(String key, float value) { 509 put(key, Float.toString(value)); 510 } 511 512 534 public float getFloat(String key, float def) { 535 float result = def; 536 try { 537 String value = get(key, null); 538 if (value != null) 539 result = Float.parseFloat(value); 540 } catch (NumberFormatException e) { 541 } 543 544 return result; 545 } 546 547 563 public void putDouble(String key, double value) { 564 put(key, Double.toString(value)); 565 } 566 567 589 public double getDouble(String key, double def) { 590 double result = def; 591 try { 592 String value = get(key, null); 593 if (value != null) 594 result = Double.parseDouble(value); 595 } catch (NumberFormatException e) { 596 } 598 599 return result; 600 } 601 602 614 public void putByteArray(String key, byte[] value) { 615 put(key, Base64.byteArrayToBase64(value)); 616 } 617 618 635 public byte[] getByteArray(String key, byte[] def) { 636 byte[] result = def; 637 String value = get(key, null); 638 try { 639 if (value != null) 640 result = Base64.base64ToByteArray(value); 641 } 642 catch (RuntimeException e) { 643 } 645 646 return result; 647 } 648 649 664 public String [] keys() throws BackingStoreException { 665 synchronized(lock) { 666 if (removed) 667 throw new IllegalStateException ("Node has been removed."); 668 669 return keysSpi(); 670 } 671 } 672 673 693 public String [] childrenNames() throws BackingStoreException { 694 synchronized(lock) { 695 if (removed) 696 throw new IllegalStateException ("Node has been removed."); 697 698 Set s = new TreeSet(kidCache.keySet()); 699 String [] kids = childrenNamesSpi(); 700 for(int i=0; i<kids.length; i++) 701 s.add(kids[i]); 702 return (String []) s.toArray(EMPTY_STRING_ARRAY); 703 } 704 } 705 706 private static final String [] EMPTY_STRING_ARRAY = new String [0]; 707 708 713 protected final AbstractPreferences [] cachedChildren() { 714 return (AbstractPreferences []) kidCache.values(). 715 toArray(EMPTY_ABSTRACT_PREFS_ARRAY); 716 } 717 718 private static final AbstractPreferences [] EMPTY_ABSTRACT_PREFS_ARRAY 719 = new AbstractPreferences [0]; 720 721 733 public Preferences parent() { 734 synchronized(lock) { 735 if (removed) 736 throw new IllegalStateException ("Node has been removed."); 737 738 return parent; 739 } 740 } 741 742 787 public Preferences node(String path) { 788 synchronized(lock) { 789 if (removed) 790 throw new IllegalStateException ("Node has been removed."); 791 if (path.equals("")) 792 return this; 793 if (path.equals("/")) 794 return root; 795 if (path.charAt(0) != '/') 796 return node(new StringTokenizer(path, "/", true)); 797 } 798 799 return root.node(new StringTokenizer(path.substring(1), "/", true)); 801 } 802 803 806 private Preferences node(StringTokenizer path) { 807 String token = path.nextToken(); 808 if (token.equals("/")) throw new IllegalArgumentException ("Consecutive slashes in path"); 810 synchronized(lock) { 811 AbstractPreferences child=(AbstractPreferences )kidCache.get(token); 812 if (child == null) { 813 if (token.length() > MAX_NAME_LENGTH) 814 throw new IllegalArgumentException ( 815 "Node name " + token + " too long"); 816 child = childSpi(token); 817 if (child.newNode) 818 enqueueNodeAddedEvent(child); 819 kidCache.put(token, child); 820 } 821 if (!path.hasMoreTokens()) 822 return child; 823 path.nextToken(); if (!path.hasMoreTokens()) 825 throw new IllegalArgumentException ("Path ends with slash"); 826 return child.node(path); 827 } 828 } 829 830 850 public boolean nodeExists(String path) 851 throws BackingStoreException  852 { 853 synchronized(lock) { 854 if (path.equals("")) 855 return !removed; 856 if (removed) 857 throw new IllegalStateException ("Node has been removed."); 858 if (path.equals("/")) 859 return true; 860 if (path.charAt(0) != '/') 861 &n
|