src/java.base/share/classes/jdk/internal/util/SystemProps.java
changeset 52724 0bdbf854472f
child 52725 c470f977ade8
equal deleted inserted replaced
52723:f24ae8376e71 52724:0bdbf854472f
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.internal.util;
       
    26 
       
    27 
       
    28 import java.lang.annotation.Native;
       
    29 import java.util.Properties;
       
    30 
       
    31 /**
       
    32  * System Property initialization for internal use only
       
    33  * Retrieves the platform, JVM, and command line properties,
       
    34  * applies initial defaults and returns the Properties instance
       
    35  * that becomes the System.getProperties instance.
       
    36  */
       
    37 public final class SystemProps {
       
    38 
       
    39     // no instances
       
    40     private SystemProps() {}
       
    41 
       
    42     /**
       
    43      * Create and initialize the system properties from the native properties
       
    44      * and command line properties.
       
    45      * Note:  Build-defined properties such as versions and vendor information
       
    46      * are initialized by VersionProps.java-template.
       
    47      *
       
    48      * @return a Properties instance initialized with all of the properties
       
    49      */
       
    50     public static Properties initProperties() {
       
    51         // Initially, cmdProperties only includes -D and props from the VM
       
    52         Raw raw = new Raw();
       
    53         Properties props = raw.cmdProperties();
       
    54 
       
    55         String javaHome = props.getProperty("java.home");
       
    56         assert javaHome != null : "java.home not set";
       
    57 
       
    58         putIfAbsent(props, "user.home", raw.propDefault(Raw._user_home_NDX));
       
    59         putIfAbsent(props, "user.dir", raw.propDefault(Raw._user_dir_NDX));
       
    60         putIfAbsent(props, "user.name", raw.propDefault(Raw._user_name_NDX));
       
    61 
       
    62         // Platform defined encoding cannot be overridden on the command line
       
    63         put(props, "sun.jnu.encoding", raw.propDefault(Raw._sun_jnu_encoding_NDX));
       
    64 
       
    65         // Add properties that have not been overridden on the cmdline
       
    66         putIfAbsent(props, "file.encoding",
       
    67                 ((raw.propDefault(Raw._file_encoding_NDX) == null)
       
    68                         ? raw.propDefault(Raw._sun_jnu_encoding_NDX)
       
    69                         : raw.propDefault(Raw._file_encoding_NDX)));
       
    70 
       
    71         // Use platform values if not overridden by a commandline -Dkey=value
       
    72         // In no particular order
       
    73         putIfAbsent(props, "os.name", raw.propDefault(Raw._os_name_NDX));
       
    74         putIfAbsent(props, "os.arch", raw.propDefault(Raw._os_arch_NDX));
       
    75         putIfAbsent(props, "os.version", raw.propDefault(Raw._os_version_NDX));
       
    76         putIfAbsent(props, "line.separator", raw.propDefault(Raw._line_separator_NDX));
       
    77         putIfAbsent(props, "file.separator", raw.propDefault(Raw._file_separator_NDX));
       
    78         putIfAbsent(props, "path.separator", raw.propDefault(Raw._path_separator_NDX));
       
    79         putIfAbsent(props, "java.io.tmpdir", raw.propDefault(Raw._java_io_tmpdir_NDX));
       
    80         putIfAbsent(props, "http.proxyHost", raw.propDefault(Raw._http_proxyHost_NDX));
       
    81         putIfAbsent(props, "http.proxyPort", raw.propDefault(Raw._http_proxyPort_NDX));
       
    82         putIfAbsent(props, "https.proxyHost", raw.propDefault(Raw._https_proxyHost_NDX));
       
    83         putIfAbsent(props, "https.proxyPort", raw.propDefault(Raw._https_proxyPort_NDX));
       
    84         putIfAbsent(props, "ftp.proxyHost", raw.propDefault(Raw._ftp_proxyHost_NDX));
       
    85         putIfAbsent(props, "ftp.proxyPort", raw.propDefault(Raw._ftp_proxyPort_NDX));
       
    86         putIfAbsent(props, "socksProxyHost", raw.propDefault(Raw._socksProxyHost_NDX));
       
    87         putIfAbsent(props, "socksProxyPort", raw.propDefault(Raw._socksProxyPort_NDX));
       
    88         putIfAbsent(props, "gopherProxySet", raw.propDefault(Raw._gopherProxySet_NDX));
       
    89         putIfAbsent(props, "gopherProxyHost", raw.propDefault(Raw._gopherProxyHost_NDX));
       
    90         putIfAbsent(props, "gopherProxyPort", raw.propDefault(Raw._gopherProxyPort_NDX));
       
    91         putIfAbsent(props, "http.nonProxyHosts", raw.propDefault(Raw._http_nonProxyHosts_NDX));
       
    92         putIfAbsent(props, "ftp.nonProxyHosts", raw.propDefault(Raw._ftp_nonProxyHosts_NDX));
       
    93         putIfAbsent(props, "socksNonProxyHosts", raw.propDefault(Raw._socksNonProxyHosts_NDX));
       
    94         putIfAbsent(props, "awt.toolkit", raw.propDefault(Raw._awt_toolkit_NDX));
       
    95         putIfAbsent(props, "java.awt.headless", raw.propDefault(Raw._java_awt_headless_NDX));
       
    96         putIfAbsent(props, "java.awt.printerjob", raw.propDefault(Raw._java_awt_printerjob_NDX));
       
    97         putIfAbsent(props, "java.awt.graphicsenv", raw.propDefault(Raw._java_awt_graphicsenv_NDX));
       
    98         putIfAbsent(props, "sun.desktop", raw.propDefault(Raw._sun_desktop_NDX));
       
    99         putIfAbsent(props, "sun.java2d.fontpath", raw.propDefault(Raw._sun_java2d_fontpath_NDX));
       
   100         putIfAbsent(props, "sun.arch.abi", raw.propDefault(Raw._sun_arch_abi_NDX));
       
   101         putIfAbsent(props, "sun.arch.data.model", raw.propDefault(Raw._sun_arch_data_model_NDX));
       
   102         putIfAbsent(props, "sun.os.patch.level", raw.propDefault(Raw._sun_os_patch_level_NDX));
       
   103         putIfAbsent(props, "sun.stdout.encoding", raw.propDefault(Raw._sun_stdout_encoding_NDX));
       
   104         putIfAbsent(props, "sun.stderr.encoding", raw.propDefault(Raw._sun_stderr_encoding_NDX));
       
   105         putIfAbsent(props, "sun.io.unicode.encoding", raw.propDefault(Raw._sun_io_unicode_encoding_NDX));
       
   106         putIfAbsent(props, "sun.cpu.isalist", raw.propDefault(Raw._sun_cpu_isalist_NDX));
       
   107         putIfAbsent(props, "sun.cpu.endian", raw.propDefault(Raw._sun_cpu_endian_NDX));
       
   108 
       
   109         /* Construct i18n related options */
       
   110         fillI18nProps(props,"user.language", raw.propDefault(Raw._display_language_NDX),
       
   111                 raw.propDefault(Raw._format_language_NDX));
       
   112         fillI18nProps(props,"user.script",   raw.propDefault(Raw._display_script_NDX),
       
   113                 raw.propDefault(Raw._format_script_NDX));
       
   114         fillI18nProps(props,"user.country",  raw.propDefault(Raw._display_country_NDX),
       
   115                 raw.propDefault(Raw._format_country_NDX));
       
   116         fillI18nProps(props,"user.variant",  raw.propDefault(Raw._display_variant_NDX),
       
   117                 raw.propDefault(Raw._format_variant_NDX));
       
   118 
       
   119         return props;
       
   120     }
       
   121 
       
   122     /**
       
   123      * Puts the property if it is non-null
       
   124      * @param props the Properties
       
   125      * @param key the key
       
   126      * @param value the value
       
   127      */
       
   128     private static void put(Properties props, String key, String value) {
       
   129         if (value != null) {
       
   130             props.put(key, value);
       
   131         }
       
   132     }
       
   133 
       
   134     /**
       
   135      * Puts the property if it is non-null and is not already in the Properties.
       
   136      * @param props the Properties
       
   137      * @param key the key
       
   138      * @param value the value
       
   139      */
       
   140     private static void putIfAbsent(Properties props, String key, String value) {
       
   141         if (value != null) {
       
   142             props.putIfAbsent(key, value);
       
   143         }
       
   144     }
       
   145 
       
   146     /**
       
   147      * For internationalization options, compute the values for
       
   148      * display and format properties
       
   149      * MUST NOT override command line defined values.
       
   150      *
       
   151      * @param base the base property name
       
   152      * @param display the display value for the base
       
   153      * @param format the format value for the base
       
   154      */
       
   155     private static void fillI18nProps(Properties cmdProps, String base, String display,
       
   156                                       String format) {
       
   157         // Do not override command line setting
       
   158         String baseValue = cmdProps.getProperty(base);
       
   159         if (baseValue == null) {
       
   160             // Not overridden on the command line; define the properties if there are platform defined values
       
   161             baseValue = display;
       
   162         }
       
   163         if (baseValue != null) {
       
   164             cmdProps.put(base, baseValue);
       
   165         }
       
   166 
       
   167         /* user.xxx.display property */
       
   168         String disp = base + ".display";
       
   169         String dispValue = cmdProps.getProperty(disp);
       
   170         if (dispValue == null && display != null && !display.equals(baseValue)) {
       
   171             // Create the property only if different from the base property
       
   172             cmdProps.put(disp, display);
       
   173         }
       
   174 
       
   175         /* user.xxx.format property */
       
   176         String fmt = base + ".format";
       
   177         String fmtValue = cmdProps.getProperty(fmt);
       
   178         if (fmtValue == null && format != null && !format.equals(baseValue)) {
       
   179             // Create the property only if different than the base property
       
   180             cmdProps.put(fmt, format);
       
   181         }
       
   182     }
       
   183 
       
   184     /**
       
   185      * Read the raw properties from native System.c.
       
   186      */
       
   187     public static class Raw {
       
   188         // Array indices written by native vmProperties()
       
   189         // The order is arbitrary (but alphabetic for convenience)
       
   190         @Native private static final int _awt_toolkit_NDX = 0;
       
   191         @Native private static final int _display_country_NDX = 1 + _awt_toolkit_NDX;
       
   192         @Native private static final int _display_language_NDX = 1 + _display_country_NDX;
       
   193         @Native private static final int _display_script_NDX = 1 + _display_language_NDX;
       
   194         @Native private static final int _display_variant_NDX = 1 + _display_script_NDX;
       
   195         @Native private static final int _file_encoding_NDX = 1 + _display_variant_NDX;
       
   196         @Native private static final int _file_separator_NDX = 1 + _file_encoding_NDX;
       
   197         @Native private static final int _format_country_NDX = 1 + _file_separator_NDX;
       
   198         @Native private static final int _format_language_NDX = 1 + _format_country_NDX;
       
   199         @Native private static final int _format_script_NDX = 1 + _format_language_NDX;
       
   200         @Native private static final int _format_variant_NDX = 1 + _format_script_NDX;
       
   201         @Native private static final int _ftp_nonProxyHosts_NDX = 1 + _format_variant_NDX;
       
   202         @Native private static final int _ftp_proxyHost_NDX = 1 + _ftp_nonProxyHosts_NDX;
       
   203         @Native private static final int _ftp_proxyPort_NDX = 1 + _ftp_proxyHost_NDX;
       
   204         @Native private static final int _gopherProxyHost_NDX = 1 + _ftp_proxyPort_NDX;
       
   205         @Native private static final int _gopherProxyPort_NDX = 1 + _gopherProxyHost_NDX;
       
   206         @Native private static final int _gopherProxySet_NDX = 1 + _gopherProxyPort_NDX;
       
   207         @Native private static final int _http_nonProxyHosts_NDX = 1 + _gopherProxySet_NDX;
       
   208         @Native private static final int _http_proxyHost_NDX = 1 + _http_nonProxyHosts_NDX;
       
   209         @Native private static final int _http_proxyPort_NDX = 1 + _http_proxyHost_NDX;
       
   210         @Native private static final int _https_proxyHost_NDX = 1 + _http_proxyPort_NDX;
       
   211         @Native private static final int _https_proxyPort_NDX = 1 + _https_proxyHost_NDX;
       
   212         @Native private static final int _java_awt_graphicsenv_NDX = 1 + _https_proxyPort_NDX;
       
   213         @Native private static final int _java_awt_printerjob_NDX = 1 + _java_awt_graphicsenv_NDX;
       
   214         @Native private static final int _java_awt_headless_NDX = 1 + _java_awt_printerjob_NDX;
       
   215         @Native private static final int _java_io_tmpdir_NDX = 1 + _java_awt_headless_NDX;
       
   216         @Native private static final int _line_separator_NDX = 1 + _java_io_tmpdir_NDX;
       
   217         @Native private static final int _os_arch_NDX = 1 + _line_separator_NDX;
       
   218         @Native private static final int _os_name_NDX = 1 + _os_arch_NDX;
       
   219         @Native private static final int _os_version_NDX = 1 + _os_name_NDX;
       
   220         @Native private static final int _path_separator_NDX = 1 + _os_version_NDX;
       
   221         @Native private static final int _socksNonProxyHosts_NDX = 1 + _path_separator_NDX;
       
   222         @Native private static final int _socksProxyHost_NDX = 1 + _socksNonProxyHosts_NDX;
       
   223         @Native private static final int _socksProxyPort_NDX = 1 + _socksProxyHost_NDX;
       
   224         @Native private static final int _sun_arch_abi_NDX = 1 + _socksProxyPort_NDX;
       
   225         @Native private static final int _sun_arch_data_model_NDX = 1 + _sun_arch_abi_NDX;
       
   226         @Native private static final int _sun_cpu_endian_NDX = 1 + _sun_arch_data_model_NDX;
       
   227         @Native private static final int _sun_cpu_isalist_NDX = 1 + _sun_cpu_endian_NDX;
       
   228         @Native private static final int _sun_desktop_NDX = 1 + _sun_cpu_isalist_NDX;
       
   229         @Native private static final int _sun_io_unicode_encoding_NDX = 1 + _sun_desktop_NDX;
       
   230         @Native private static final int _sun_java2d_fontpath_NDX = 1 + _sun_io_unicode_encoding_NDX;
       
   231         @Native private static final int _sun_jnu_encoding_NDX = 1 + _sun_java2d_fontpath_NDX;
       
   232         @Native private static final int _sun_os_patch_level_NDX = 1 + _sun_jnu_encoding_NDX;
       
   233         @Native private static final int _sun_stderr_encoding_NDX = 1 + _sun_os_patch_level_NDX;
       
   234         @Native private static final int _sun_stdout_encoding_NDX = 1 + _sun_stderr_encoding_NDX;
       
   235         @Native private static final int _user_dir_NDX = 1 + _sun_stdout_encoding_NDX;
       
   236         @Native private static final int _user_home_NDX = 1 + _user_dir_NDX;
       
   237         @Native private static final int _user_name_NDX = 1 + _user_home_NDX;
       
   238         @Native private static final int FIXED_LENGTH = 1 + _user_name_NDX;
       
   239 
       
   240         // Array of Strings returned from the VM and Command line properties
       
   241         // The array is not used after initialization is complete.
       
   242         private final String[] platformProps;
       
   243 
       
   244         private Raw() {
       
   245             platformProps = platformProperties();
       
   246         }
       
   247 
       
   248         /**
       
   249          * Return the value for a well known default from native.
       
   250          * @param index the index of the known property
       
   251          * @return the value
       
   252          */
       
   253         String propDefault(int index) {
       
   254             return platformProps[index];
       
   255         }
       
   256 
       
   257         /**
       
   258          * Return a Properties instance of the command line and VM options
       
   259          * defined by name and value.
       
   260          * The Properties instance is sized to include the fixed properties.
       
   261          *
       
   262          * @return return a Properties instance of the command line and VM options
       
   263          */
       
   264         private Properties cmdProperties() {
       
   265             String[] vmProps = vmProperties();
       
   266             int nProps = vmProps.length / 2;
       
   267             var cmdProps = new Properties(nProps + Raw.FIXED_LENGTH);
       
   268             for (int i = 0; i < nProps; i++) {
       
   269                 String k = vmProps[i * 2];
       
   270                 if (k != null) {
       
   271                     String v = vmProps[i * 2 + 1];
       
   272                     cmdProps.setProperty(k, v != null ? v : "");
       
   273                 } else {
       
   274                     // no more key/value pairs
       
   275                     break;
       
   276                 }
       
   277             }
       
   278             return cmdProps;
       
   279         }
       
   280 
       
   281         /**
       
   282          * Returns the available VM and Command Line Properties.
       
   283          * The VM supplies some key/value pairs and processes the command line
       
   284          * to extract key/value pairs from the {@code "-Dkey=value"} arguments.
       
   285          *
       
   286          * @return an array of strings, with alternating key and value strings.
       
   287          *      Either keys or values may be null, the array may not be full.
       
   288          *      The first null key indicates there are no more key, value pairs.
       
   289          */
       
   290         private static native String[] vmProperties();
       
   291 
       
   292         /**
       
   293          * Returns the platform specific property values identified
       
   294          * by {@code "_xxx_NDX"} indexes.
       
   295          * The indexes are strictly private, to be shared only with the native code.
       
   296          *
       
   297          * @return a array of strings, the properties are indexed by the {@code _xxx_NDX}
       
   298          * indexes.  The values are Strings and may be null.
       
   299          */
       
   300         private static native String[] platformProperties();
       
   301     }
       
   302 }