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));
104
105 /* Construct i18n related options */
106 fillI18nProps(props,"user.language", raw.propDefault(Raw._display_language_NDX),
107 raw.propDefault(Raw._format_language_NDX));
108 fillI18nProps(props,"user.script", raw.propDefault(Raw._display_script_NDX),
109 raw.propDefault(Raw._format_script_NDX));
110 fillI18nProps(props,"user.country", raw.propDefault(Raw._display_country_NDX),
111 raw.propDefault(Raw._format_country_NDX));
112 fillI18nProps(props,"user.variant", raw.propDefault(Raw._display_variant_NDX),
113 raw.propDefault(Raw._format_variant_NDX));
114
115 return props;
116 }
117
118 /**
119 * Puts the property if it is non-null
120 * @param props the Properties
121 * @param key the key
122 * @param value the value
123 */
124 private static void put(Properties props, String key, String value) {
125 if (value != null) {
126 props.put(key, value);
127 }
128 }
129
130 /**
131 * Puts the property if it is non-null and is not already in the Properties.
132 * @param props the Properties
133 * @param key the key
134 * @param value the value
135 */
136 private static void putIfAbsent(Properties props, String key, String value) {
137 if (value != null) {
138 props.putIfAbsent(key, value);
139 }
140 }
141
142 /**
143 * For internationalization options, compute the values for
144 * display and format properties
145 * MUST NOT override command line defined values.
146 *
147 * @param base the base property name
148 * @param display the display value for the base
149 * @param format the format value for the base
150 */
151 private static void fillI18nProps(Properties cmdProps, String base, String display,
152 String format) {
153 // Do not override command line setting
154 String baseValue = cmdProps.getProperty(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.getProperty(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.getProperty(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;
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 /**
|
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.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 /**
34 * System Property initialization for internal use only
35 * Retrieves the platform, JVM, and command line properties,
36 * applies initial defaults and returns the Properties instance
37 * that becomes the System.getProperties instance.
38 */
39 public final class SystemProps {
40
41 // no instances
42 private SystemProps() {}
43
44 /**
45 * Create and initialize the system properties from the native properties
46 * and command line properties.
47 * Note: Build-defined properties such as versions and vendor information
48 * are initialized by VersionProps.java-template.
49 *
50 * @return a Properties instance initialized with all of the properties
51 */
52 public static Map<String, String> initProperties() {
53
54 // Initially, cmdProperties only includes -D and props from the VM
55 Raw raw = new Raw();
56 HashMap<String, String> props = raw.cmdProperties();
57
58 String javaHome = props.get("java.home");
59 assert javaHome != null : "java.home not set";
60
61 putIfAbsent(props, "user.home", raw.propDefault(Raw._user_home_NDX));
62 putIfAbsent(props, "user.dir", raw.propDefault(Raw._user_dir_NDX));
63 putIfAbsent(props, "user.name", raw.propDefault(Raw._user_name_NDX));
64
65 // Platform defined encoding cannot be overridden on the command line
66 put(props, "sun.jnu.encoding", raw.propDefault(Raw._sun_jnu_encoding_NDX));
67
68 // Add properties that have not been overridden on the cmdline
69 putIfAbsent(props, "file.encoding",
70 ((raw.propDefault(Raw._file_encoding_NDX) == null)
71 ? raw.propDefault(Raw._sun_jnu_encoding_NDX)
72 : raw.propDefault(Raw._file_encoding_NDX)));
73
74 // Use platform values if not overridden by a commandline -Dkey=value
75 // In no particular order
76 putIfAbsent(props, "os.name", raw.propDefault(Raw._os_name_NDX));
77 putIfAbsent(props, "os.arch", raw.propDefault(Raw._os_arch_NDX));
78 putIfAbsent(props, "os.version", raw.propDefault(Raw._os_version_NDX));
107
108 /* Construct i18n related options */
109 fillI18nProps(props,"user.language", raw.propDefault(Raw._display_language_NDX),
110 raw.propDefault(Raw._format_language_NDX));
111 fillI18nProps(props,"user.script", raw.propDefault(Raw._display_script_NDX),
112 raw.propDefault(Raw._format_script_NDX));
113 fillI18nProps(props,"user.country", raw.propDefault(Raw._display_country_NDX),
114 raw.propDefault(Raw._format_country_NDX));
115 fillI18nProps(props,"user.variant", raw.propDefault(Raw._display_variant_NDX),
116 raw.propDefault(Raw._format_variant_NDX));
117
118 return props;
119 }
120
121 /**
122 * Puts the property if it is non-null
123 * @param props the Properties
124 * @param key the key
125 * @param value the value
126 */
127 private static void put(HashMap<String, String> props, String key, String value) {
128 if (value != null) {
129 props.put(key, value);
130 }
131 }
132
133 /**
134 * Puts the property if it is non-null and is not already in the Properties.
135 * @param props the Properties
136 * @param key the key
137 * @param value the value
138 */
139 private static void putIfAbsent(HashMap<String, String> props, String key, String value) {
140 if (value != null) {
141 props.putIfAbsent(key, value);
142 }
143 }
144
145 /**
146 * For internationalization options, compute the values for
147 * display and format properties
148 * MUST NOT override command line defined values.
149 *
150 * @param base the base property name
151 * @param display the display value for the base
152 * @param format the format value for the base
153 */
154 private static void fillI18nProps(HashMap<String, String> cmdProps,
155 String base,
156 String display,
157 String format) {
158 // Do not override command line setting
159 String baseValue = cmdProps.get(base);
160 if (baseValue != null) {
161 return; // Do not override value from the command line
162 }
163
164 // Not overridden on the command line; define the properties if there are platform defined values
165 if (display != null) {
166 cmdProps.put(base, display);
167 baseValue = display;
168 }
169
170 /* user.xxx.display property */
171 String disp = base.concat(".display");
172 String dispValue = cmdProps.get(disp);
173 if (dispValue == null && display != null && !display.equals(baseValue)) {
174 // Create the property only if different from the base property
175 cmdProps.put(disp, display);
176 }
177
178 /* user.xxx.format property */
179 String fmt = base.concat(".format");
180 String fmtValue = cmdProps.get(fmt);
181 if (fmtValue == null && format != null && !format.equals(baseValue)) {
182 // Create the property only if different than the base property
183 cmdProps.put(fmt, format);
184 }
185 }
186
187 /**
188 * Read the raw properties from native System.c.
189 */
190 public static class Raw {
191 // Array indices written by native vmProperties()
192 // The order is arbitrary (but alphabetic for convenience)
193 @Native private static final int _awt_toolkit_NDX = 0;
194 @Native private static final int _display_country_NDX = 1 + _awt_toolkit_NDX;
195 @Native private static final int _display_language_NDX = 1 + _display_country_NDX;
196 @Native private static final int _display_script_NDX = 1 + _display_language_NDX;
197 @Native private static final int _display_variant_NDX = 1 + _display_script_NDX;
198 @Native private static final int _file_encoding_NDX = 1 + _display_variant_NDX;
199 @Native private static final int _file_separator_NDX = 1 + _file_encoding_NDX;
200 @Native private static final int _format_country_NDX = 1 + _file_separator_NDX;
243 private Raw() {
244 platformProps = platformProperties();
245 }
246
247 /**
248 * Return the value for a well known default from native.
249 * @param index the index of the known property
250 * @return the value
251 */
252 String propDefault(int index) {
253 return platformProps[index];
254 }
255
256 /**
257 * Return a Properties instance of the command line and VM options
258 * defined by name and value.
259 * The Properties instance is sized to include the fixed properties.
260 *
261 * @return return a Properties instance of the command line and VM options
262 */
263 private HashMap<String, String> cmdProperties() {
264 String[] vmProps = vmProperties();
265 var cmdProps = new HashMap<String, String>((vmProps.length / 2) + Raw.FIXED_LENGTH);
266 for (int i = 0; i < vmProps.length;) {
267 String k = vmProps[i++];
268 if (k != null) {
269 String v = vmProps[i++];
270 cmdProps.put(k, v != null ? v : "");
271 } else {
272 // no more key/value pairs
273 break;
274 }
275 }
276 return cmdProps;
277 }
278
279 /**
280 * Returns the available VM and Command Line Properties.
281 * The VM supplies some key/value pairs and processes the command line
282 * to extract key/value pairs from the {@code "-Dkey=value"} arguments.
283 *
284 * @return an array of strings, with alternating key and value strings.
285 * Either keys or values may be null, the array may not be full.
286 * The first null key indicates there are no more key, value pairs.
287 */
288 private static native String[] vmProperties();
289
290 /**
|