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