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