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, "sun.arch.abi", raw.propDefault(Raw._sun_arch_abi_NDX));
  94         putIfAbsent(props, "sun.arch.data.model", raw.propDefault(Raw._sun_arch_data_model_NDX));
  95         putIfAbsent(props, "sun.os.patch.level", raw.propDefault(Raw._sun_os_patch_level_NDX));
  96         putIfAbsent(props, "sun.stdout.encoding", raw.propDefault(Raw._sun_stdout_encoding_NDX));
  97         putIfAbsent(props, "sun.stderr.encoding", raw.propDefault(Raw._sun_stderr_encoding_NDX));
  98         putIfAbsent(props, "sun.io.unicode.encoding", raw.propDefault(Raw._sun_io_unicode_encoding_NDX));
  99         putIfAbsent(props, "sun.cpu.isalist", raw.propDefault(Raw._sun_cpu_isalist_NDX));
 100         putIfAbsent(props, "sun.cpu.endian", raw.propDefault(Raw._sun_cpu_endian_NDX));
 101 
 102         /* Construct i18n related options */
 103         fillI18nProps(props,"user.language", raw.propDefault(Raw._display_language_NDX),
 104                 raw.propDefault(Raw._format_language_NDX));
 105         fillI18nProps(props,"user.script",   raw.propDefault(Raw._display_script_NDX),
 106                 raw.propDefault(Raw._format_script_NDX));
 107         fillI18nProps(props,"user.country",  raw.propDefault(Raw._display_country_NDX),
 108                 raw.propDefault(Raw._format_country_NDX));
 109         fillI18nProps(props,"user.variant",  raw.propDefault(Raw._display_variant_NDX),
 110                 raw.propDefault(Raw._format_variant_NDX));
 111 
 112         return props;
 113     }
 114 
 115     /**
 116      * Puts the property if it is non-null
 117      * @param props the Properties
 118      * @param key the key
 119      * @param value the value
 120      */
 121     private static void put(HashMap<String, String> props, String key, String value) {
 122         if (value != null) {
 123             props.put(key, value);
 124         }
 125     }
 126 
 127     /**
 128      * Puts the property if it is non-null and is not already in the Properties.
 129      * @param props the Properties
 130      * @param key the key
 131      * @param value the value
 132      */
 133     private static void putIfAbsent(HashMap<String, String> props, String key, String value) {
 134         if (value != null) {
 135             props.putIfAbsent(key, value);
 136         }
 137     }
 138 
 139     /**
 140      * For internationalization options, compute the values for
 141      * display and format properties
 142      * MUST NOT override command line defined values.
 143      *
 144      * @param base the base property name
 145      * @param display the display value for the base
 146      * @param format the format value for the base
 147      */
 148     private static void fillI18nProps(HashMap<String, String> cmdProps,
 149                                       String base,
 150                                       String display,
 151                                       String format) {
 152         // Do not override command line setting
 153         String baseValue = cmdProps.get(base);
 154         if (baseValue != null) {
 155             return;     // Do not override value from the command line
 156         }
 157 
 158         // Not overridden on the command line; define the properties if there are platform defined values
 159         if (display != null) {
 160             cmdProps.put(base, display);
 161             baseValue = display;
 162         }
 163 
 164         /* user.xxx.display property */
 165         String disp = base.concat(".display");
 166         String dispValue = cmdProps.get(disp);
 167         if (dispValue == null && display != null && !display.equals(baseValue)) {
 168             // Create the property only if different from the base property
 169             cmdProps.put(disp, display);
 170         }
 171 
 172         /* user.xxx.format property */
 173         String fmt = base.concat(".format");
 174         String fmtValue = cmdProps.get(fmt);
 175         if (fmtValue == null && format != null && !format.equals(baseValue)) {
 176             // Create the property only if different than the base property
 177             cmdProps.put(fmt, format);
 178         }
 179     }
 180 
 181     /**
 182      * Read the raw properties from native System.c.
 183      */
 184     public static class Raw {
 185         // Array indices written by native vmProperties()
 186         // The order is arbitrary (but alphabetic for convenience)
 187         @Native private static final int _display_country_NDX = 0;
 188         @Native private static final int _display_language_NDX = 1 + _display_country_NDX;
 189         @Native private static final int _display_script_NDX = 1 + _display_language_NDX;
 190         @Native private static final int _display_variant_NDX = 1 + _display_script_NDX;
 191         @Native private static final int _file_encoding_NDX = 1 + _display_variant_NDX;
 192         @Native private static final int _file_separator_NDX = 1 + _file_encoding_NDX;
 193         @Native private static final int _format_country_NDX = 1 + _file_separator_NDX;
 194         @Native private static final int _format_language_NDX = 1 + _format_country_NDX;
 195         @Native private static final int _format_script_NDX = 1 + _format_language_NDX;
 196         @Native private static final int _format_variant_NDX = 1 + _format_script_NDX;
 197         @Native private static final int _ftp_nonProxyHosts_NDX = 1 + _format_variant_NDX;
 198         @Native private static final int _ftp_proxyHost_NDX = 1 + _ftp_nonProxyHosts_NDX;
 199         @Native private static final int _ftp_proxyPort_NDX = 1 + _ftp_proxyHost_NDX;
 200         @Native private static final int _http_nonProxyHosts_NDX = 1 + _ftp_proxyPort_NDX;
 201         @Native private static final int _http_proxyHost_NDX = 1 + _http_nonProxyHosts_NDX;
 202         @Native private static final int _http_proxyPort_NDX = 1 + _http_proxyHost_NDX;
 203         @Native private static final int _https_proxyHost_NDX = 1 + _http_proxyPort_NDX;
 204         @Native private static final int _https_proxyPort_NDX = 1 + _https_proxyHost_NDX;
 205         @Native private static final int _java_io_tmpdir_NDX = 1 + _https_proxyPort_NDX;
 206         @Native private static final int _line_separator_NDX = 1 + _java_io_tmpdir_NDX;
 207         @Native private static final int _os_arch_NDX = 1 + _line_separator_NDX;
 208         @Native private static final int _os_name_NDX = 1 + _os_arch_NDX;
 209         @Native private static final int _os_version_NDX = 1 + _os_name_NDX;
 210         @Native private static final int _path_separator_NDX = 1 + _os_version_NDX;
 211         @Native private static final int _socksNonProxyHosts_NDX = 1 + _path_separator_NDX;
 212         @Native private static final int _socksProxyHost_NDX = 1 + _socksNonProxyHosts_NDX;
 213         @Native private static final int _socksProxyPort_NDX = 1 + _socksProxyHost_NDX;
 214         @Native private static final int _sun_arch_abi_NDX = 1 + _socksProxyPort_NDX;
 215         @Native private static final int _sun_arch_data_model_NDX = 1 + _sun_arch_abi_NDX;
 216         @Native private static final int _sun_cpu_endian_NDX = 1 + _sun_arch_data_model_NDX;
 217         @Native private static final int _sun_cpu_isalist_NDX = 1 + _sun_cpu_endian_NDX;
 218         @Native private static final int _sun_io_unicode_encoding_NDX = 1 + _sun_cpu_isalist_NDX;
 219         @Native private static final int _sun_jnu_encoding_NDX = 1 + _sun_io_unicode_encoding_NDX;
 220         @Native private static final int _sun_os_patch_level_NDX = 1 + _sun_jnu_encoding_NDX;
 221         @Native private static final int _sun_stderr_encoding_NDX = 1 + _sun_os_patch_level_NDX;
 222         @Native private static final int _sun_stdout_encoding_NDX = 1 + _sun_stderr_encoding_NDX;
 223         @Native private static final int _user_dir_NDX = 1 + _sun_stdout_encoding_NDX;
 224         @Native private static final int _user_home_NDX = 1 + _user_dir_NDX;
 225         @Native private static final int _user_name_NDX = 1 + _user_home_NDX;
 226         @Native private static final int FIXED_LENGTH = 1 + _user_name_NDX;
 227 
 228         // Array of Strings returned from the VM and Command line properties
 229         // The array is not used after initialization is complete.
 230         private final String[] platformProps;
 231 
 232         private Raw() {
 233             platformProps = platformProperties();
 234         }
 235 
 236         /**
 237          * Return the value for a well known default from native.
 238          * @param index the index of the known property
 239          * @return the value
 240          */
 241         String propDefault(int index) {
 242             return platformProps[index];
 243         }
 244 
 245         /**
 246          * Return a Properties instance of the command line and VM options
 247          * defined by name and value.
 248          * The Properties instance is sized to include the fixed properties.
 249          *
 250          * @return return a Properties instance of the command line and VM options
 251          */
 252         private HashMap<String, String> cmdProperties() {
 253             String[] vmProps = vmProperties();
 254             // While optimal initialCapacity here would be the exact number of properties
 255             // divided by LOAD_FACTOR, a large portion of the properties in Raw are
 256             // usually not set, so for typical cases the chosen capacity avoids resizing
 257             var cmdProps = new HashMap<String, String>((vmProps.length / 2) + Raw.FIXED_LENGTH);
 258             for (int i = 0; i < vmProps.length;) {
 259                 String k = vmProps[i++];
 260                 if (k != null) {
 261                     String v = vmProps[i++];
 262                     cmdProps.put(k, v != null ? v : "");
 263                 } else {
 264                     // no more key/value pairs
 265                     break;
 266                 }
 267             }
 268             return cmdProps;
 269         }
 270 
 271         /**
 272          * Returns the available VM and Command Line Properties.
 273          * The VM supplies some key/value pairs and processes the command line
 274          * to extract key/value pairs from the {@code "-Dkey=value"} arguments.
 275          *
 276          * @return an array of strings, with alternating key and value strings.
 277          *      Either keys or values may be null, the array may not be full.
 278          *      The first null key indicates there are no more key, value pairs.
 279          */
 280         private static native String[] vmProperties();
 281 
 282         /**
 283          * Returns the platform specific property values identified
 284          * by {@code "_xxx_NDX"} indexes.
 285          * The indexes are strictly private, to be shared only with the native code.
 286          *
 287          * @return a array of strings, the properties are indexed by the {@code _xxx_NDX}
 288          * indexes.  The values are Strings and may be null.
 289          */
 290         private static native String[] platformProperties();
 291     }
 292 }