< prev index next >

src/java.prefs/share/classes/java/util/prefs/Preferences.java

Print this page




 221  * @author  Josh Bloch
 222  * @since   1.4
 223  */
 224 public abstract class Preferences {
 225 
 226     private static final PreferencesFactory factory = factory();
 227 
 228     private static PreferencesFactory factory() {
 229         // 1. Try user-specified system property
 230         String factoryName = AccessController.doPrivileged(
 231             new PrivilegedAction<String>() {
 232                 public String run() {
 233                     return System.getProperty(
 234                         "java.util.prefs.PreferencesFactory");}});
 235         if (factoryName != null) {
 236             // FIXME: This code should be run in a doPrivileged and
 237             // not use the context classloader, to avoid being
 238             // dependent on the invoking thread.
 239             // Checking AllPermission also seems wrong.
 240             try {
 241                 return (PreferencesFactory)
 242                     Class.forName(factoryName, false,
 243                                   ClassLoader.getSystemClassLoader())
 244                     .newInstance();

 245             } catch (Exception ex) {
 246                 try {
 247                     // workaround for javaws, plugin,
 248                     // load factory class using non-system classloader
 249                     SecurityManager sm = System.getSecurityManager();
 250                     if (sm != null) {
 251                         sm.checkPermission(new java.security.AllPermission());
 252                     }
 253                     return (PreferencesFactory)
 254                         Class.forName(factoryName, false,
 255                                       Thread.currentThread()
 256                                       .getContextClassLoader())
 257                         .newInstance();

 258                 } catch (Exception e) {
 259                     throw new InternalError(
 260                         "Can't instantiate Preferences factory "
 261                         + factoryName, e);
 262                 }
 263             }
 264         }
 265 
 266         return AccessController.doPrivileged(
 267             new PrivilegedAction<PreferencesFactory>() {
 268                 public PreferencesFactory run() {
 269                     return factory1();}});
 270     }
 271 
 272     private static PreferencesFactory factory1() {
 273         // 2. Try service provider interface
 274         Iterator<PreferencesFactory> itr = ServiceLoader
 275             .load(PreferencesFactory.class, ClassLoader.getSystemClassLoader())
 276             .iterator();
 277 


 282             } catch (ServiceConfigurationError sce) {
 283                 if (sce.getCause() instanceof SecurityException) {
 284                     // Ignore the security exception, try the next provider
 285                     continue;
 286                 }
 287                 throw sce;
 288             }
 289         }
 290 
 291         // 3. Use platform-specific system-wide default
 292         String osName = System.getProperty("os.name");
 293         String platformFactory;
 294         if (osName.startsWith("Windows")) {
 295             platformFactory = "java.util.prefs.WindowsPreferencesFactory";
 296         } else if (osName.contains("OS X")) {
 297             platformFactory = "java.util.prefs.MacOSXPreferencesFactory";
 298         } else {
 299             platformFactory = "java.util.prefs.FileSystemPreferencesFactory";
 300         }
 301         try {
 302             return (PreferencesFactory)
 303                 Class.forName(platformFactory, false,
 304                               Preferences.class.getClassLoader()).newInstance();

 305         } catch (Exception e) {
 306             throw new InternalError(
 307                 "Can't instantiate platform default Preferences factory "
 308                 + platformFactory, e);
 309         }
 310     }
 311 
 312     /**
 313      * Maximum length of string allowed as a key (80 characters).
 314      */
 315     public static final int MAX_KEY_LENGTH = 80;
 316 
 317     /**
 318      * Maximum length of string allowed as a value (8192 characters).
 319      */
 320     public static final int MAX_VALUE_LENGTH = 8*1024;
 321 
 322     /**
 323      * Maximum length of a node name (80 characters).
 324      */




 221  * @author  Josh Bloch
 222  * @since   1.4
 223  */
 224 public abstract class Preferences {
 225 
 226     private static final PreferencesFactory factory = factory();
 227 
 228     private static PreferencesFactory factory() {
 229         // 1. Try user-specified system property
 230         String factoryName = AccessController.doPrivileged(
 231             new PrivilegedAction<String>() {
 232                 public String run() {
 233                     return System.getProperty(
 234                         "java.util.prefs.PreferencesFactory");}});
 235         if (factoryName != null) {
 236             // FIXME: This code should be run in a doPrivileged and
 237             // not use the context classloader, to avoid being
 238             // dependent on the invoking thread.
 239             // Checking AllPermission also seems wrong.
 240             try {
 241                 @SuppressWarnings("deprecation")
 242                 Object result =Class.forName(factoryName, false,
 243                                              ClassLoader.getSystemClassLoader())
 244                     .newInstance();
 245                 return (PreferencesFactory)result;
 246             } catch (Exception ex) {
 247                 try {
 248                     // workaround for javaws, plugin,
 249                     // load factory class using non-system classloader
 250                     SecurityManager sm = System.getSecurityManager();
 251                     if (sm != null) {
 252                         sm.checkPermission(new java.security.AllPermission());
 253                     }
 254                     @SuppressWarnings("deprecation")
 255                     Object result = Class.forName(factoryName, false,
 256                                                   Thread.currentThread()
 257                                                   .getContextClassLoader())
 258                         .newInstance();
 259                     return (PreferencesFactory) result;
 260                 } catch (Exception e) {
 261                     throw new InternalError(
 262                         "Can't instantiate Preferences factory "
 263                         + factoryName, e);
 264                 }
 265             }
 266         }
 267 
 268         return AccessController.doPrivileged(
 269             new PrivilegedAction<PreferencesFactory>() {
 270                 public PreferencesFactory run() {
 271                     return factory1();}});
 272     }
 273 
 274     private static PreferencesFactory factory1() {
 275         // 2. Try service provider interface
 276         Iterator<PreferencesFactory> itr = ServiceLoader
 277             .load(PreferencesFactory.class, ClassLoader.getSystemClassLoader())
 278             .iterator();
 279 


 284             } catch (ServiceConfigurationError sce) {
 285                 if (sce.getCause() instanceof SecurityException) {
 286                     // Ignore the security exception, try the next provider
 287                     continue;
 288                 }
 289                 throw sce;
 290             }
 291         }
 292 
 293         // 3. Use platform-specific system-wide default
 294         String osName = System.getProperty("os.name");
 295         String platformFactory;
 296         if (osName.startsWith("Windows")) {
 297             platformFactory = "java.util.prefs.WindowsPreferencesFactory";
 298         } else if (osName.contains("OS X")) {
 299             platformFactory = "java.util.prefs.MacOSXPreferencesFactory";
 300         } else {
 301             platformFactory = "java.util.prefs.FileSystemPreferencesFactory";
 302         }
 303         try {
 304             @SuppressWarnings("deprecation")
 305             Object result = Class.forName(platformFactory, false,
 306                                           Preferences.class.getClassLoader()).newInstance();
 307             return (PreferencesFactory) result;
 308         } catch (Exception e) {
 309             throw new InternalError(
 310                 "Can't instantiate platform default Preferences factory "
 311                 + platformFactory, e);
 312         }
 313     }
 314 
 315     /**
 316      * Maximum length of string allowed as a key (80 characters).
 317      */
 318     public static final int MAX_KEY_LENGTH = 80;
 319 
 320     /**
 321      * Maximum length of string allowed as a value (8192 characters).
 322      */
 323     public static final int MAX_VALUE_LENGTH = 8*1024;
 324 
 325     /**
 326      * Maximum length of a node name (80 characters).
 327      */


< prev index next >