src/share/classes/java/lang/System.java

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


 640      *
 641      * @return the system-dependent line separator string
 642      * @since 1.7
 643      */
 644     public static String lineSeparator() {
 645         return lineSeparator;
 646     }
 647 
 648     private static String lineSeparator;
 649 
 650     /**
 651      * Sets the system properties to the <code>Properties</code>
 652      * argument.
 653      * <p>
 654      * First, if there is a security manager, its
 655      * <code>checkPropertiesAccess</code> method is called with no
 656      * arguments. This may result in a security exception.
 657      * <p>
 658      * The argument becomes the current set of system properties for use
 659      * by the {@link #getProperty(String)} method. If the argument is
 660      * <code>null</code>, then the current set of system properties is
 661      * forgotten.
 662      *
 663      * @param      props   the new system properties.
 664      * @exception  SecurityException  if a security manager exists and its
 665      *             <code>checkPropertiesAccess</code> method doesn't allow access
 666      *              to the system properties.
 667      * @see        #getProperties
 668      * @see        java.util.Properties
 669      * @see        java.lang.SecurityException
 670      * @see        java.lang.SecurityManager#checkPropertiesAccess()
 671      */
 672     public static void setProperties(Properties props) {
 673         SecurityManager sm = getSecurityManager();
 674         if (sm != null) {
 675             sm.checkPropertiesAccess();
 676         }
 677         if (props == null) {
 678             props = new Properties();
 679             initProperties(props);
 680         }
 681         System.props = props;
 682     }
 683 
 684     /**
 685      * Gets the system property indicated by the specified key.
 686      * <p>
 687      * First, if there is a security manager, its
 688      * <code>checkPropertyAccess</code> method is called with the key as
 689      * its argument. This may result in a SecurityException.
 690      * <p>
 691      * If there is no current set of system properties, a set of system
 692      * properties is first created and initialized in the same manner as
 693      * for the <code>getProperties</code> method.
 694      *
 695      * @param      key   the name of the system property.
 696      * @return     the string value of the system property,
 697      *             or <code>null</code> if there is no property with that key.
 698      *
 699      * @exception  SecurityException  if a security manager exists and its
 700      *             <code>checkPropertyAccess</code> method doesn't allow
 701      *              access to the specified system property.


1145         return new PrintStream(new BufferedOutputStream(fos, 128), true);
1146     }
1147 
1148 
1149     /**
1150      * Initialize the system class.  Called after thread initialization.
1151      */
1152     private static void initializeSystemClass() {
1153 
1154         // VM might invoke JNU_NewStringPlatform() to set those encoding
1155         // sensitive properties (user.home, user.name, boot.class.path, etc.)
1156         // during "props" initialization, in which it may need access, via
1157         // System.getProperty(), to the related system encoding property that
1158         // have been initialized (put into "props") at early stage of the
1159         // initialization. So make sure the "props" is available at the
1160         // very beginning of the initialization and all system properties to
1161         // be put into it directly.
1162         props = new Properties();
1163         initProperties(props);  // initialized by the VM
1164 



1165         // There are certain system configurations that may be controlled by
1166         // VM options such as the maximum amount of direct memory and
1167         // Integer cache size used to support the object identity semantics
1168         // of autoboxing.  Typically, the library will obtain these values
1169         // from the properties set by the VM.  If the properties are for
1170         // internal implementation use only, these properties should be
1171         // removed from the system properties.
1172         //
1173         // See java.lang.Integer.IntegerCache and the
1174         // sun.misc.VM.saveAndRemoveProperties method for example.
1175         //
1176         // Save a private copy of the system properties object that
1177         // can only be accessed by the internal implementation.  Remove
1178         // certain system properties that are not intended for public access.
1179         sun.misc.VM.saveAndRemoveProperties(props);
1180 



1181 
1182         lineSeparator = props.getProperty("line.separator");
1183         sun.misc.Version.init();
1184 
1185         FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
1186         FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
1187         FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
1188         setIn0(new BufferedInputStream(fdIn));
1189         setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
1190         setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
1191 
1192         // Load the zip library now in order to keep java.util.zip.ZipFile
1193         // from trying to use itself to load this library later.
1194         loadLibrary("zip");
1195 
1196         // Setup Java signal handlers for HUP, TERM, and INT (where available).
1197         Terminator.setup();
1198 
1199         // Initialize any miscellenous operating system settings that need to be
1200         // set for the class libraries. Currently this is no-op everywhere except
1201         // for Windows where the process-wide error mode is set before the java.io
1202         // classes are used.
1203         sun.misc.VM.initializeOSEnvironment();




 640      *
 641      * @return the system-dependent line separator string
 642      * @since 1.7
 643      */
 644     public static String lineSeparator() {
 645         return lineSeparator;
 646     }
 647 
 648     private static String lineSeparator;
 649 
 650     /**
 651      * Sets the system properties to the <code>Properties</code>
 652      * argument.
 653      * <p>
 654      * First, if there is a security manager, its
 655      * <code>checkPropertiesAccess</code> method is called with no
 656      * arguments. This may result in a security exception.
 657      * <p>
 658      * The argument becomes the current set of system properties for use
 659      * by the {@link #getProperty(String)} method. If the argument is
 660      * <code>null</code>, then the default system properties are reloaded.

 661      *
 662      * @param      props   the new system properties.
 663      * @exception  SecurityException  if a security manager exists and its
 664      *             <code>checkPropertiesAccess</code> method doesn't allow access
 665      *              to the system properties.
 666      * @see        #getProperties
 667      * @see        java.util.Properties
 668      * @see        java.lang.SecurityException
 669      * @see        java.lang.SecurityManager#checkPropertiesAccess()
 670      */
 671     public static void setProperties(Properties props) {
 672         SecurityManager sm = getSecurityManager();
 673         if (sm != null) {
 674             sm.checkPropertiesAccess();
 675         }
 676 
 677         System.props = (props != null)
 678             ? props
 679             : sun.misc.VM.getSanitizedSavedProperties();

 680     }
 681 
 682     /**
 683      * Gets the system property indicated by the specified key.
 684      * <p>
 685      * First, if there is a security manager, its
 686      * <code>checkPropertyAccess</code> method is called with the key as
 687      * its argument. This may result in a SecurityException.
 688      * <p>
 689      * If there is no current set of system properties, a set of system
 690      * properties is first created and initialized in the same manner as
 691      * for the <code>getProperties</code> method.
 692      *
 693      * @param      key   the name of the system property.
 694      * @return     the string value of the system property,
 695      *             or <code>null</code> if there is no property with that key.
 696      *
 697      * @exception  SecurityException  if a security manager exists and its
 698      *             <code>checkPropertyAccess</code> method doesn't allow
 699      *              access to the specified system property.


1143         return new PrintStream(new BufferedOutputStream(fos, 128), true);
1144     }
1145 
1146 
1147     /**
1148      * Initialize the system class.  Called after thread initialization.
1149      */
1150     private static void initializeSystemClass() {
1151 
1152         // VM might invoke JNU_NewStringPlatform() to set those encoding
1153         // sensitive properties (user.home, user.name, boot.class.path, etc.)
1154         // during "props" initialization, in which it may need access, via
1155         // System.getProperty(), to the related system encoding property that
1156         // have been initialized (put into "props") at early stage of the
1157         // initialization. So make sure the "props" is available at the
1158         // very beginning of the initialization and all system properties to
1159         // be put into it directly.
1160         props = new Properties();
1161         initProperties(props);  // initialized by the VM
1162 
1163         // requires System.setProperty()
1164         sun.misc.Version.init();
1165 
1166         // There are certain system configurations that may be controlled by
1167         // VM options such as the maximum amount of direct memory and
1168         // Integer cache size used to support the object identity semantics
1169         // of autoboxing.  Typically, the library will obtain these values
1170         // from the properties set by the VM.


1171         //
1172         // See java.lang.Integer.IntegerCache and the
1173         // sun.misc.VM.getSanitizedSavedProperties method for example.
1174         //
1175         // Save a private copy of the system properties object that
1176         // can only be accessed by the internal implementation.
1177         sun.misc.VM.saveVMSystemProperties(props);

1178 
1179         // Initialize the public system properties without certain properties
1180         // that are intended for internal implementation use only.
1181         props = sun.misc.VM.getSanitizedSavedProperties();
1182 
1183         lineSeparator = props.getProperty("line.separator");

1184 
1185         FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
1186         FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
1187         FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
1188         setIn0(new BufferedInputStream(fdIn));
1189         setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
1190         setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
1191 
1192         // Load the zip library now in order to keep java.util.zip.ZipFile
1193         // from trying to use itself to load this library later.
1194         loadLibrary("zip");
1195 
1196         // Setup Java signal handlers for HUP, TERM, and INT (where available).
1197         Terminator.setup();
1198 
1199         // Initialize any miscellenous operating system settings that need to be
1200         // set for the class libraries. Currently this is no-op everywhere except
1201         // for Windows where the process-wide error mode is set before the java.io
1202         // classes are used.
1203         sun.misc.VM.initializeOSEnvironment();