1 7 8 package java.util.prefs; 9 10 import java.io.InputStream ; 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 import java.security.AccessController ; 14 import java.security.Permission ; 15 import java.security.PrivilegedAction ; 16 import java.util.Iterator ; 17 import sun.misc.Service; 18 import sun.misc.ServiceConfigurationError; 19 20 21 import java.lang.RuntimePermission ; 23 import java.lang.Integer ; 24 import java.lang.Long ; 25 import java.lang.Float ; 26 import java.lang.Double ; 27 28 208 public abstract class Preferences { 209 210 private static final PreferencesFactory factory = factory(); 211 212 private static PreferencesFactory factory() { 213 String factoryName = AccessController.doPrivileged( 215 new PrivilegedAction <String >() { 216 public String run() { 217 return System.getProperty( 218 "java.util.prefs.PreferencesFactory");}}); 219 if (factoryName != null) { 220 try { 225 return (PreferencesFactory ) 226 Class.forName(factoryName, false, 227 ClassLoader.getSystemClassLoader()) 228 .newInstance(); 229 } catch (Exception ex) { 230 try { 231 SecurityManager sm = System.getSecurityManager(); 234 if (sm != null) { 235 sm.checkPermission(new java.security.AllPermission ()); 236 } 237 return (PreferencesFactory ) 238 Class.forName(factoryName, false, 239 Thread.currentThread() 240 .getContextClassLoader()) 241 .newInstance(); 242 } catch (Exception e) { 243 InternalError error = new InternalError ( 244 "Can't instantiate Preferences factory " 245 + factoryName); 246 error.initCause(e); 247 throw error; 248 } 249 } 250 } 251 252 return AccessController.doPrivileged( 253 new PrivilegedAction <PreferencesFactory >() { 254 public PreferencesFactory run() { 255 return factory1();}}); 256 } 257 258 private static PreferencesFactory factory1() { 259 Iterator i = Service.providers(PreferencesFactory .class, 261 ClassLoader.getSystemClassLoader()); 262 while (i.hasNext()) { 264 try { 265 return (PreferencesFactory ) i.next(); 266 } catch (ServiceConfigurationError sce) { 267 if (sce.getCause() instanceof SecurityException ) { 268 continue; 270 } 271 throw sce; 272 } 273 } 274 275 String platformFactory = 277 System.getProperty("os.name").startsWith("Windows") 278 ? "java.util.prefs.WindowsPreferencesFactory" 279 : "java.util.prefs.FileSystemPreferencesFactory"; 280 try { 281 return (PreferencesFactory ) 282 Class.forName(platformFactory, false, null).newInstance(); 283 } catch (Exception e) { 284 InternalError error = new InternalError ( 285 "Can't instantiate platform default Preferences factory " 286 + platformFactory); 287 error.initCause(e); 288 throw error; 289 } 290 } 291 292 295 public static final int MAX_KEY_LENGTH = 80; 296 297 300 public static final int MAX_VALUE_LENGTH = 8*1024; 301 302 305 public static final int MAX_NAME_LENGTH = 80; 306 307 347 public static Preferences userNodeForPackage(Class <?> c) { 348 return userRoot().node(nodeName(c)); 349 } 350 351 391 public static Preferences systemNodeForPackage(Class <?> c) { 392 return systemRoot().node(nodeName(c)); 393 } 394 395 402 private static String nodeName(Class c) { 403 if (c.isArray()) 404 throw new IllegalArgumentException ( 405 "Arrays have no associated preferences node."); 406 String className = c.getName(); 407 int pkgEndIndex = className.lastIndexOf('.'); 408 if (pkgEndIndex < 0) 409 return "/<unnamed>"; 410 String packageName = className.substring(0, pkgEndIndex); 411 return "/" + packageName.replace('.', '/'); 412 } 413 414 419 private static Permission prefsPerm = new RuntimePermission ("preferences"); 420 421 429 public static Preferences userRoot() { 430 SecurityManager security = System.getSecurityManager(); 431 if (security != null) 432 security.checkPermission(prefsPerm); 433 434 return factory.userRoot(); 435 } 436 437 445 public static Preferences systemRoot() { 446 SecurityManager security = System.getSecurityManager(); 447 if (security != null) 448 security.checkPermission(prefsPerm); 449 450 return factory.systemRoot(); 451 } 452 453 457 protected Preferences() { 458 } 459 460 473 public abstract void put(String key, String value); 474 475 496 public abstract String get(String key, String def); 497 498 512 public abstract void remove(String key); 513 514 531 public abstract void clear() throws BackingStoreException ; 532 533 549 public abstract void putInt(String key, int value); 550 551 581 public abstract int getInt(String key, int def); 582 583 599 public abstract void putLong(String key, long value); 600 601 631 public abstract long getLong(String key, long def); 632 633 650 public abstract void putBoolean(String key, boolean value); 651 652 686 public abstract boolean getBoolean(String key, boolean def); 687 688 704 public abstract void putFloat(String key, float value); 705 706 735 public abstract float getFloat(String key, float def); 736 737 753 public abstract void putDouble(String key, double value); 754 755 784 public abstract double getDouble(String key, double def); 785 786 809 public abstract void putByteArray(String key, byte[] value); 810 811 848 public abstract byte[] getByteArray(String key, byte[] def); 849 850 868 public abstract String [] keys() throws BackingStoreException ; 869 870 882 public abstract String [] childrenNames() throws BackingStoreException ; 883 884 892 public abstract Preferences parent(); 893 894 916 public abstract Preferences node(String pathName); 917 918 944 public abstract boolean nodeExists(String pathName) 945 throws BackingStoreException ; 946 947 977 public abstract void removeNode() throws BackingStoreException ; 978 979 984 public abstract String name(); 985 986 991 public abstract String absolutePath(); 992 993 1001 public abstract boolean isUserNode(); 1002 1003 1008 public abstract String toString(); 1009 1010 1034 public abstract void flush() throws BackingStoreException ; 1035 1036 1051 public abstract void sync() throws BackingStoreException ; 1052 1053 1077 public abstract void addPreferenceChangeListener( 1078 PreferenceChangeListener pcl); 1079 1080 1091 public abstract void removePreferenceChangeListener( 1092 PreferenceChangeListener pcl); 1093 1094 1123 public abstract void addNodeChangeListener(NodeChangeListener ncl); 1124 1125 1136 public abstract void removeNodeChangeListener(NodeChangeListener ncl); 1137 1138 1166 public abstract void exportNode(OutputStream os) 1167 throws IOException , BackingStoreException ; 1168 1169 1198 public abstract void exportSubtree(OutputStream os) 1199 throws IOException , BackingStoreException ; 1200 1201 1233 public static void importPreferences(InputStream is) 1234 throws IOException , InvalidPreferencesFormatException 1235 { 1236 XmlSupport.importPreferences(is); 1237 } 1238} 1239 | Popular Tags |