src/share/classes/sun/misc/VM.java

Print this page
rev 9156 : 8022854: System.setProperties(null) causes implementation private properties to reappear
Reviewed-by: duke


 172     public static void awaitBooted() throws InterruptedException {
 173         synchronized (lock) {
 174             while (!booted) {
 175                 lock.wait();
 176             }
 177         }
 178     }
 179 
 180     // A user-settable upper limit on the maximum amount of allocatable direct
 181     // buffer memory.  This value may be changed during VM initialization if
 182     // "java" is launched with "-XX:MaxDirectMemorySize=<size>".
 183     //
 184     // The initial value of this field is arbitrary; during JRE initialization
 185     // it will be reset to the value specified on the command line, if any,
 186     // otherwise to Runtime.getRuntime().maxMemory().
 187     //
 188     private static long directMemory = 64 * 1024 * 1024;
 189 
 190     // Returns the maximum amount of allocatable direct buffer memory.
 191     // The directMemory variable is initialized during system initialization
 192     // in the saveAndRemoveProperties method.
 193     //
 194     public static long maxDirectMemory() {
 195         return directMemory;
 196     }
 197 
 198     // User-controllable flag that determines if direct buffers should be page
 199     // aligned. The "-XX:+PageAlignDirectMemory" option can be used to force
 200     // buffers, allocated by ByteBuffer.allocateDirect, to be page aligned.
 201     private static boolean pageAlignDirectMemory;
 202 
 203     // Returns {@code true} if the direct buffers should be page aligned. This
 204     // variable is initialized by saveAndRemoveProperties.
 205     public static boolean isDirectMemoryPageAligned() {
 206         return pageAlignDirectMemory;
 207     }
 208 
 209     // A user-settable boolean to determine whether ClassLoader.loadClass should
 210     // accept array syntax.  This value may be changed during VM initialization
 211     // via the system property "sun.lang.ClassLoader.allowArraySyntax".
 212     //
 213     // The default for 1.5 is "true", array syntax is allowed.  In 1.6, the
 214     // default will be "false".  The presence of this system property to
 215     // control array syntax allows applications the ability to preview this new
 216     // behaviour.
 217     //
 218     private static boolean defaultAllowArraySyntax = false;
 219     private static boolean allowArraySyntax = defaultAllowArraySyntax;
 220 
 221     // The allowArraySyntax boolean is initialized during system initialization
 222     // in the saveAndRemoveProperties method.
 223     //
 224     // It is initialized based on the value of the system property
 225     // "sun.lang.ClassLoader.allowArraySyntax".  If the system property is not
 226     // provided, the default for 1.5 is "true".  In 1.6, the default will be
 227     // "false".  If the system property is provided, then the value of
 228     // allowArraySyntax will be equal to "true" if Boolean.parseBoolean()
 229     // returns "true".   Otherwise, the field will be set to "false".
 230     //
 231     public static boolean allowArraySyntax() {
 232         return allowArraySyntax;
 233     }
 234 
 235     /**
 236      * Returns true if the given class loader is in the system domain
 237      * in which all permissions are granted.
 238      */
 239     public static boolean isSystemDomainLoader(ClassLoader loader) {
 240         return loader == null;
 241     }
 242 
 243     /**
 244      * Returns the system property of the specified key saved at
 245      * system initialization time.  This method should only be used
 246      * for the system properties that are not changed during runtime.
 247      * It accesses a private copy of the system properties so
 248      * that user's locking of the system properties object will not
 249      * cause the library to deadlock.
 250      *
 251      * Note that the saved system properties do not include
 252      * the ones set by sun.misc.Version.init().
 253      *
 254      */
 255     public static String getSavedProperty(String key) {
 256         if (savedProps.isEmpty())
 257             throw new IllegalStateException("Should be non-empty if initialized");
 258 
 259         return savedProps.getProperty(key);
 260     }
 261 
 262     // TODO: the Property Management needs to be refactored and
 263     // the appropriate prop keys need to be accessible to the
 264     // calling classes to avoid duplication of keys.
 265     private static final Properties savedProps = new Properties();
 266 
 267     // Save a private copy of the system properties and remove
 268     // the system properties that are not intended for public access.
 269     //
 270     // This method can only be invoked during system initialization.
 271     public static void saveAndRemoveProperties(Properties props) {

 272         if (booted)
 273             throw new IllegalStateException("System initialization has completed");
 274 
 275         savedProps.putAll(props);
 276 
 277         // Set the maximum amount of direct memory.  This value is controlled
 278         // by the vm option -XX:MaxDirectMemorySize=<size>.
 279         // The maximum amount of allocatable direct buffer memory (in bytes)
 280         // from the system property sun.nio.MaxDirectMemorySize set by the VM.
 281         // The system property will be removed.
 282         String s = (String)props.remove("sun.nio.MaxDirectMemorySize");
 283         if (s != null) {
 284             if (s.equals("-1")) {
 285                 // -XX:MaxDirectMemorySize not given, take default
 286                 directMemory = Runtime.getRuntime().maxMemory();
 287             } else {
 288                 long l = Long.parseLong(s);
 289                 if (l > -1)
 290                     directMemory = l;
 291             }
 292         }
 293 
 294         // Check if direct buffers should be page aligned
 295         s = (String)props.remove("sun.nio.PageAlignDirectMemory");
 296         if ("true".equals(s))
 297             pageAlignDirectMemory = true;
 298 
 299         // Set a boolean to determine whether ClassLoader.loadClass accepts
 300         // array syntax.  This value is controlled by the system property
 301         // "sun.lang.ClassLoader.allowArraySyntax".
 302         s = props.getProperty("sun.lang.ClassLoader.allowArraySyntax");
 303         allowArraySyntax = (s == null
 304                                ? defaultAllowArraySyntax
 305                                : Boolean.parseBoolean(s));


















 306 
 307         // Remove other private system properties
 308         // used by java.lang.Integer.IntegerCache
 309         props.remove("java.lang.Integer.IntegerCache.high");
 310 
 311         // used by java.util.zip.ZipFile
 312         props.remove("sun.zip.disableMemoryMapping");
 313 
 314         // used by sun.launcher.LauncherHelper
 315         props.remove("sun.java.launcher.diag");


 316     }
 317 
 318     // Initialize any miscellenous operating system settings that need to be
 319     // set for the class libraries.
 320     //
 321     public static void initializeOSEnvironment() {
 322         if (!booted) {
 323             OSEnvironment.initialize();
 324         }
 325     }
 326 
 327     /* Current count of objects pending for finalization */
 328     private static volatile int finalRefCount = 0;
 329 
 330     /* Peak count of objects pending for finalization */
 331     private static volatile int peakFinalRefCount = 0;
 332 
 333     /*
 334      * Gets the number of objects pending for finalization.
 335      *




 172     public static void awaitBooted() throws InterruptedException {
 173         synchronized (lock) {
 174             while (!booted) {
 175                 lock.wait();
 176             }
 177         }
 178     }
 179 
 180     // A user-settable upper limit on the maximum amount of allocatable direct
 181     // buffer memory.  This value may be changed during VM initialization if
 182     // "java" is launched with "-XX:MaxDirectMemorySize=<size>".
 183     //
 184     // The initial value of this field is arbitrary; during JRE initialization
 185     // it will be reset to the value specified on the command line, if any,
 186     // otherwise to Runtime.getRuntime().maxMemory().
 187     //
 188     private static long directMemory = 64 * 1024 * 1024;
 189 
 190     // Returns the maximum amount of allocatable direct buffer memory.
 191     // The directMemory variable is initialized during system initialization
 192     // in the saveVMSystemProperties method.
 193     //
 194     public static long maxDirectMemory() {
 195         return directMemory;
 196     }
 197 
 198     // User-controllable flag that determines if direct buffers should be page
 199     // aligned. The "-XX:+PageAlignDirectMemory" option can be used to force
 200     // buffers, allocated by ByteBuffer.allocateDirect, to be page aligned.
 201     private static boolean pageAlignDirectMemory;
 202 
 203     // Returns {@code true} if the direct buffers should be page aligned. This
 204     // variable is initialized by saveVMSystemProperties.
 205     public static boolean isDirectMemoryPageAligned() {
 206         return pageAlignDirectMemory;
 207     }
 208 
 209     // A user-settable boolean to determine whether ClassLoader.loadClass should
 210     // accept array syntax.  This value may be changed during VM initialization
 211     // via the system property "sun.lang.ClassLoader.allowArraySyntax".
 212     //
 213     // The default for 1.5 is "true", array syntax is allowed.  In 1.6+, the
 214     // default is "false".  The presence of this system property to
 215     // control array syntax allows applications the ability to preview this new
 216     // behaviour.
 217     //
 218     private static boolean defaultAllowArraySyntax = false;
 219     private static boolean allowArraySyntax = defaultAllowArraySyntax;
 220 
 221     // The allowArraySyntax boolean is initialized during system initialization
 222     // in the saveVMSystemProperties method.
 223     //
 224     // It is initialized based on the value of the system property
 225     // "sun.lang.ClassLoader.allowArraySyntax".  If the system property is not
 226     // provided, the default for 1.5 is "true".  In 1.6+, the default is
 227     // "false".  If the system property is provided, then the value of
 228     // allowArraySyntax will be equal to "true" if Boolean.parseBoolean()
 229     // returns "true".   Otherwise, the field will be set to "false".
 230     //
 231     public static boolean allowArraySyntax() {
 232         return allowArraySyntax;
 233     }
 234 
 235     /**
 236      * Returns true if the given class loader is in the system domain
 237      * in which all permissions are granted.
 238      */
 239     public static boolean isSystemDomainLoader(ClassLoader loader) {
 240         return loader == null;
 241     }
 242 
 243     /**
 244      * Returns the system property of the specified key saved at
 245      * system initialization time.  This method should only be used
 246      * for the system properties that are not changed during runtime.
 247      * It accesses a private copy of the system properties so
 248      * that user's locking of the system properties object will not
 249      * cause the library to deadlock.
 250      *
 251      * Note that the saved system properties do not include
 252      * the ones set by sun.misc.Version.init().
 253      *
 254      */
 255     public static String getSavedProperty(String key) {
 256         if (savedProps.isEmpty())
 257             throw new IllegalStateException("Should be non-empty if initialized");
 258 
 259         return savedProps.getProperty(key);
 260     }
 261 
 262     // TODO: the Property Management needs to be refactored and
 263     // the appropriate prop keys need to be accessible to the
 264     // calling classes to avoid duplication of keys.
 265     private static final Properties savedProps = new Properties();
 266 
 267     /**
 268      * Save a private copy of the system properties.
 269      *
 270      * @implNote This method can only be invoked during system initialization.
 271      */
 272     public static void saveVMSystemProperties(Properties props) {
 273         if (booted)
 274             throw new IllegalStateException("System initialization has completed");
 275 
 276         savedProps.putAll(props);
 277 
 278         // Set the maximum amount of direct memory.  This value is controlled
 279         // by the vm option -XX:MaxDirectMemorySize=<size>.
 280         // The maximum amount of allocatable direct buffer memory (in bytes)
 281         // from the system property sun.nio.MaxDirectMemorySize set by the VM.
 282         // The system property will be removed.
 283         String s = savedProps.getProperty("sun.nio.MaxDirectMemorySize");
 284         if (s != null) {
 285             if (s.equals("-1")) {
 286                 // -XX:MaxDirectMemorySize not given, take default
 287                 directMemory = Runtime.getRuntime().maxMemory();
 288             } else {
 289                 long l = Long.parseLong(s);
 290                 if (l > -1)
 291                     directMemory = l;
 292             }
 293         }
 294 
 295         // Check if direct buffers should be page aligned
 296         s = savedProps.getProperty("sun.nio.PageAlignDirectMemory");
 297         pageAlignDirectMemory = Boolean.parseBoolean(s);

 298 
 299         // Set a boolean to determine whether ClassLoader.loadClass accepts
 300         // array syntax.  This value is controlled by the system property
 301         // "sun.lang.ClassLoader.allowArraySyntax".
 302         s = savedProps.getProperty("sun.lang.ClassLoader.allowArraySyntax");
 303         allowArraySyntax = Boolean.parseBoolean(s);
 304    }
 305 
 306     /**
 307      * Returns a copy of the saved system properties with private implementation
 308      * properties removed.
 309      *
 310      * @return a newly created Properties instance containing only sanitized
 311      */
 312     public static Properties getSanitizedSavedProperties() {
 313         Properties sanitized = new Properties();
 314 
 315         sanitized.putAll(savedProps);
 316 
 317         // Remove implementation private system properties
 318 
 319         sanitized.remove("sun.nio.MaxDirectMemorySize");
 320 
 321         sanitized.remove("sun.nio.PageAlignDirectMemory");
 322 
 323         sanitized.remove("sun.lang.ClassLoader.allowArraySyntax");
 324 

 325         // used by java.lang.Integer.IntegerCache
 326         sanitized.remove("java.lang.Integer.IntegerCache.high");
 327 
 328         // used by java.util.zip.ZipFile
 329         sanitized.remove("sun.zip.disableMemoryMapping");
 330 
 331         // used by sun.launcher.LauncherHelper
 332         sanitized.remove("sun.java.launcher.diag");
 333 
 334         return sanitized;
 335      }
 336 
 337     // Initialize any miscellenous operating system settings that need to be
 338     // set for the class libraries.
 339     //
 340     public static void initializeOSEnvironment() {
 341         if (!booted) {
 342             OSEnvironment.initialize();
 343         }
 344     }
 345 
 346     /* Current count of objects pending for finalization */
 347     private static volatile int finalRefCount = 0;
 348 
 349     /* Peak count of objects pending for finalization */
 350     private static volatile int peakFinalRefCount = 0;
 351 
 352     /*
 353      * Gets the number of objects pending for finalization.
 354      *