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.HashMap;
  30 import java.util.Map;
  31 
  32 /**
  33  * System Property initialization for internal use only
  34  * Retrieves the platform, JVM, and command line properties,
  35  * applies initial defaults and returns the Properties instance
  36  * that becomes the System.getProperties instance.
  37  */
  38 public final class SystemProps {
  39 
  40     // no instances
  41     private SystemProps() {}
  42 
  43     /**
  44      * Create and initialize the system properties from the native properties
  45      * and command line properties.
  46      * Note:  Build-defined properties such as versions and vendor information
  47      * are initialized by VersionProps.java-template.
  48      *
  49      * @return a Properties instance initialized with all of the properties
  50      */
  51     public static Map<String, String> initProperties() {
  52 
  53         // Initially, cmdProperties only includes -D and props from the VM
  54         Raw raw = new Raw();
  55         HashMap<String, String> props = raw.cmdProperties();
  56 
  57         String javaHome = props.get("java.home");
  58         assert javaHome != null : "java.home not set";
  59 
  60         putIfAbsent(props, "user.home", raw.propDefault(Raw._user_home_NDX));
  61         putIfAbsent(props, "user.dir", raw.propDefault(Raw._user_dir_NDX));
  62         putIfAbsent(props, "user.name", raw.propDefault(Raw._user_name_NDX));
  63 
  64         // Platform defined encoding cannot be overridden on the command line
  65         put(props, "sun.jnu.encoding", raw.propDefault(Raw._sun_jnu_encoding_NDX));
  66 
  67         // Add properties that have not been overridden on the cmdline
  68         putIfAbsent(props, "file.encoding",
  69                 ((raw.propDefault(Raw._file_encoding_NDX) == null)
  70                         ? raw.propDefault(Raw._sun_jnu_encoding_NDX)
  71                         : raw.propDefault(Raw._file_encoding_NDX)));
  72 
  73         // Use platform values if not overridden by a commandline -Dkey=value
  74         // In no particular order
  75         putIfAbsent(props, "os.name", raw.propDefault(Raw._os_name_NDX));
  76         putIfAbsent(props, "os.arch", raw.propDefault(Raw._os_arch_NDX));
  77         putIfAbsent(props, "os.version", raw.propDefault(Raw._os_version_NDX));
  78         putIfAbsent(props, "line.separator", raw.propDefault(Raw._line_separator_NDX));
  79         putIfAbsent(props, "file.separator", raw.propDefault(Raw._file_separator_NDX));
  80         putIfAbsent(props, "path.separator", raw.propDefault(Raw._path_separator_NDX));
  81         putIfAbsent(props, "java.io.tmpdir", raw.propDefault(Raw._java_io_tmpdir_NDX));
  82         putIfAbsent(props, "http.proxyHost", raw.propDefault(Raw._http_proxyHost_NDX));
  83         putIfAbsent(props, "http.proxyPort", raw.propDefault(Raw._http_proxyPort_NDX));
  84         putIfAbsent(props, "https.proxyHost", raw.propDefault(Raw._https_proxyHost_NDX));
  85         putIfAbsent(props, "https.proxyPort", raw.propDefault(Raw._https_proxyPort_NDX));
  86         putIfAbsent(props, "ftp.proxyHost", raw.propDefault(Raw._ftp_proxyHost_NDX));
  87         putIfAbsent(props, "ftp.proxyPort", raw.propDefault(Raw._ftp_proxyPort_NDX));
  88         putIfAbsent(props, "socksProxyHost", raw.propDefault(Raw._socksProxyHost_NDX));
  89         putIfAbsent(props, "socksProxyPort", raw.propDefault(Raw._socksProxyPort_NDX));
  90         putIfAbsent(props, "http.nonProxyHosts", raw.propDefault(Raw._http_nonProxyHosts_NDX));
  91         putIfAbsent(props, "ftp.nonProxyHosts", raw.propDefault(Raw._ftp_nonProxyHosts_NDX));
  92         putIfAbsent(props, "socksNonProxyHosts", raw.propDefault(Raw._socksNonProxyHosts_NDX));
  93         putIfAbsent(props, "awt.toolkit", raw.propDefault(Raw._awt_toolkit_NDX));
  94         putIfAbsent(props, "sun.arch.abi", raw.propDefault(Raw._sun_arch_abi_NDX));
  95         putIfAbsent(props, "sun.arch.data.model", raw.propDefault(Raw._sun_arch_data_model_NDX));
  96         putIfAbsent(props, "sun.os.patch.level", raw.propDefault(Raw._sun_os_patch_level_NDX));
  97         putIfAbsent(props, "sun.stdout.encoding", raw.propDefault(Raw._sun_stdout_encoding_NDX));
  98         putIfAbsent(props, "sun.stderr.encoding", raw.propDefault(Raw._sun_stderr_encoding_NDX));
  99         putIfAbsent(props, "sun.io.unicode.encoding", raw.propDefault(Raw._sun_io_unicode_encoding_NDX));
 100         putIfAbsent(props, "sun.cpu.isalist", raw.propDefault(Raw._sun_cpu_isalist_NDX));
 101         putIfAbsent(props, "sun.cpu.endian", raw.propDefault(Raw._sun_cpu_endian_NDX));
 102 
 103         /* Construct i18n related options */
 104         fillI18nProps(props,"user.language", raw.propDefault(Raw._display_language_NDX),
 105                 raw.propDefault(Raw._format_language_NDX));
 106         fillI18nProps(props,"user.script",   raw.propDefault(Raw._display_script_NDX),
 107                 raw.propDefault(Raw._format_script_NDX));
 108         fillI18nProps(props,"user.country",  raw.propDefault(Raw._display_country_NDX),
 109                 raw.propDefault(Raw._format_country_NDX));
 110         fillI18nProps(props,"user.variant",  raw.propDefault(Raw._display_variant_NDX),
 111                 raw.propDefault(Raw._format_variant_NDX));
 112 
 113         return props;
 114     }
 115 
 116     /**
 117      * Puts the property if it is non-null
 118      * @param props the Properties
 119      * @param key the key
 120      * @param value the value
 121      */
 122     private static void put(HashMap<String, String> props, String key, String value) {
 123         if (value != null) {
 124             props.put(key, value);
 125         }
 126     }
 127 
 128     /**
 129      * Puts the property if it is non-null and is not already in the Properties.
 130      * @param props the Properties
 131      * @param key the key
 132      * @param value the value
 133      */
 134     private static void putIfAbsent(HashMap<String, String> props, String key, String value) {
 135         if (value != null) {
 136             props.putIfAbsent(key, value);
 137         }
 138     }
 139 
 140     /**
 141      * For internationalization options, compute the values for
 142      * display and format properties
 143      * MUST NOT override command line defined values.
 144      *
 145      * @param base the base property name
 146      * @param display the display value for the base
 147      * @param format the format value for the base
 148      */
 149     private static void fillI18nProps(HashMap<String, String> cmdProps,
 150                                       String base,
 151                                       String display,
 152                                       String format) {
 153         // Do not override command line setting
 154         String baseValue = cmdProps.get(base);
 155         if (baseValue != null) {
 156             return;     // Do not override value from the command line
 157         }
 158 
 159         // Not overridden on the command line; define the properties if there are platform defined values
 160         if (display != null) {
 161             cmdProps.put(base, display);
 162             baseValue = display;
 163         }
 164 
 165         /* user.xxx.display property */
 166         String disp = base.concat(".display");
 167         String dispValue = cmdProps.get(disp);
 168         if (dispValue == null && display != null && !display.equals(baseValue)) {
 169             // Create the property only if different from the base property
 170             cmdProps.put(disp, display);
 171         }
 172 
 173         /* user.xxx.format property */
 174         String fmt = base.concat(".format");
 175         String fmtValue = cmdProps.get(fmt);
 176         if (fmtValue == null && format != null && !format.equals(baseValue)) {
 177             // Create the property only if different than the base property
 178             cmdProps.put(fmt, format);
 179         }
 180     }
 181 
 182     /**
 183      * Read the raw properties from native System.c.
 184      */
 185     public static class Raw {
 186         // Array indices written by native vmProperties()
 187         // The order is arbitrary (but alphabetic for convenience)
 188         @Native private static final int _awt_toolkit_NDX = 0;
 189         @Native private static final int _display_country_NDX = 1 + _awt_toolkit_NDX;
 190         @Native private static final int _display_language_NDX = 1 + _display_country_NDX;
 191         @Native private static final int _display_script_NDX = 1 + _display_language_NDX;
 192         @Native private static final int _display_variant_NDX = 1 + _display_script_NDX;
 193         @Native private static final int _file_encoding_NDX = 1 + _display_variant_NDX;
 194         @Native private static final int _file_separator_NDX = 1 + _file_encoding_NDX;
 195         @Native private static final int _format_country_NDX = 1 + _file_separator_NDX;
 196         @Native private static final int _format_language_NDX = 1 + _format_country_NDX;
 197         @Native private static final int _format_script_NDX = 1 + _format_language_NDX;
 198         @Native private static final int _format_variant_NDX = 1 + _format_script_NDX;
 199         @Native private static final int _ftp_nonProxyHosts_NDX = 1 + _format_variant_NDX;
 200         @Native private static final int _ftp_proxyHost_NDX = 1 + _ftp_nonProxyHosts_NDX;
 201         @Native private static final int _ftp_proxyPort_NDX = 1 + _ftp_proxyHost_NDX;
 202         @Native private static final int _http_nonProxyHosts_NDX = 1 + _ftp_proxyPort_NDX;
 203         @Native private static final int _http_proxyHost_NDX = 1 + _http_nonProxyHosts_NDX;
 204         @Native private static final int _http_proxyPort_NDX = 1 + _http_proxyHost_NDX;
 205         @Native private static final int _https_proxyHost_NDX = 1 + _http_proxyPort_NDX;
 206         @Native private static final int _https_proxyPort_NDX = 1 + _https_proxyHost_NDX;
 207         @Native private static final int _java_io_tmpdir_NDX = 1 + _https_proxyPort_NDX;
 208         @Native private static final int _line_separator_NDX = 1 + _java_io_tmpdir_NDX;
 209         @Native private static final int _os_arch_NDX = 1 + _line_separator_NDX;
 210         @Native private static final int _os_name_NDX = 1 + _os_arch_NDX;
 211         @Native private static final int _os_version_NDX = 1 + _os_name_NDX;
 212         @Native private static final int _path_separator_NDX = 1 + _os_version_NDX;
 213         @Native private static final int _socksNonProxyHosts_NDX = 1 + _path_separator_NDX;
 214         @Native private static final int _socksProxyHost_NDX = 1 + _socksNonProxyHosts_NDX;
 215         @Native private static final int _socksProxyPort_NDX = 1 + _socksProxyHost_NDX;
 216         @Native private static final int _sun_arch_abi_NDX = 1 + _socksProxyPort_NDX;
 217         @Native private static final int _sun_arch_data_model_NDX = 1 + _sun_arch_abi_NDX;
 218         @Native private static final int _sun_cpu_endian_NDX = 1 + _sun_arch_data_model_NDX;
 219         @Native private static final int _sun_cpu_isalist_NDX = 1 + _sun_cpu_endian_NDX;
 220         @Native private static final int _sun_io_unicode_encoding_NDX = 1 + _sun_cpu_isalist_NDX;
 221         @Native private static final int _sun_jnu_encoding_NDX = 1 + _sun_io_unicode_encoding_NDX;
 222         @Native private static final int _sun_os_patch_level_NDX = 1 + _sun_jnu_encoding_NDX;
 223         @Native private static final int _sun_stderr_encoding_NDX = 1 + _sun_os_patch_level_NDX;
 224         @Native private static final int _sun_stdout_encoding_NDX = 1 + _sun_stderr_encoding_NDX;
 225         @Native private static final int _user_dir_NDX = 1 + _sun_stdout_encoding_NDX;
 226         @Native private static final int _user_home_NDX = 1 + _user_dir_NDX;
 227         @Native private static final int _user_name_NDX = 1 + _user_home_NDX;
 228         @Native private static final int FIXED_LENGTH = 1 + _user_name_NDX;
 229 
 230         // Array of Strings returned from the VM and Command line properties
 231         // The array is not used after initialization is complete.
 232         private final String[] platformProps;
 233 
 234         private Raw() {
 235             platformProps = platformProperties();
 236         }
 237 
 238         /**
 239          * Return the value for a well known default from native.
 240          * @param index the index of the known property
 241          * @return the value
 242          */
 243         String propDefault(int index) {
 244             return platformProps[index];
 245         }
 246 
 247         /**
 248          * Return a Properties instance of the command line and VM options
 249          * defined by name and value.
 250          * The Properties instance is sized to include the fixed properties.
 251          *
 252          * @return return a Properties instance of the command line and VM options
 253          */
 254         private HashMap<String, String> cmdProperties() {
 255             String[] vmProps = vmProperties();
 256             // While optimal initialCapacity here would be the exact number of properties
 257             // divided by LOAD_FACTOR, a large portion of the properties in Raw are
 258             // usually not set, so for typical cases the chosen capacity avoids resizing
 259             var cmdProps = new HashMap<String, String>((vmProps.length / 2) + Raw.FIXED_LENGTH);
 260             for (int i = 0; i < vmProps.length;) {
 261                 String k = vmProps[i++];
 262                 if (k != null) {
 263                     String v = vmProps[i++];
 264                     cmdProps.put(k, v != null ? v : "");
 265                 } else {
 266                     // no more key/value pairs
 267                     break;
 268                 }
 269             }
 270             return cmdProps;
 271         }
 272 
 273         /**
 274          * Returns the available VM and Command Line Properties.
 275          * The VM supplies some key/value pairs and processes the command line
 276          * to extract key/value pairs from the {@code "-Dkey=value"} arguments.
 277          *
 278          * @return an array of strings, with alternating key and value strings.
 279          *      Either keys or values may be null, the array may not be full.
 280          *      The first null key indicates there are no more key, value pairs.
 281          */
 282         private static native String[] vmProperties();
 283 
 284         /**
 285          * Returns the platform specific property values identified
 286          * by {@code "_xxx_NDX"} indexes.
 287          * The indexes are strictly private, to be shared only with the native code.
 288          *
 289          * @return a array of strings, the properties are indexed by the {@code _xxx_NDX}
 290          * indexes.  The values are Strings and may be null.
 291          */
 292         private static native String[] platformProperties();
 293     }
 294 }