Path: blob/master/src/java.base/share/classes/jdk/internal/util/StaticProperty.java
67760 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.util;2627import java.util.Properties;2829/**30* System Property access for internal use only.31* Read-only access to System property values initialized during Phase 132* are cached. Setting, clearing, or modifying the value using33* {@link System#setProperty) or {@link System#getProperties()} is ignored.34* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked35* in these access methods. The caller of these methods should take care to ensure36* that the returned property is not made accessible to untrusted code.</strong>37*/38public final class StaticProperty {3940// The class static initialization is triggered to initialize these final41// fields during init Phase 1 and before a security manager is set.42private static final String JAVA_HOME;43private static final String USER_HOME;44private static final String USER_DIR;45private static final String USER_NAME;46private static final String JAVA_LIBRARY_PATH;47private static final String SUN_BOOT_LIBRARY_PATH;48private static final String JDK_SERIAL_FILTER;49private static final String JDK_SERIAL_FILTER_FACTORY;50private static final String JAVA_IO_TMPDIR;51private static final String NATIVE_ENCODING;5253private StaticProperty() {}5455static {56Properties props = System.getProperties();57JAVA_HOME = getProperty(props, "java.home");58USER_HOME = getProperty(props, "user.home");59USER_DIR = getProperty(props, "user.dir");60USER_NAME = getProperty(props, "user.name");61JAVA_IO_TMPDIR = getProperty(props, "java.io.tmpdir");62JAVA_LIBRARY_PATH = getProperty(props, "java.library.path", "");63SUN_BOOT_LIBRARY_PATH = getProperty(props, "sun.boot.library.path", "");64JDK_SERIAL_FILTER = getProperty(props, "jdk.serialFilter", null);65JDK_SERIAL_FILTER_FACTORY = getProperty(props, "jdk.serialFilterFactory", null);66NATIVE_ENCODING = getProperty(props, "native.encoding");67}6869private static String getProperty(Properties props, String key) {70String v = props.getProperty(key);71if (v == null) {72throw new InternalError("null property: " + key);73}74return v;75}7677private static String getProperty(Properties props, String key,78String defaultVal) {79String v = props.getProperty(key);80return (v == null) ? defaultVal : v;81}8283/**84* Return the {@code java.home} system property.85*86* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked87* in this method. The caller of this method should take care to ensure88* that the returned property is not made accessible to untrusted code.</strong>89*90* @return the {@code java.home} system property91*/92public static String javaHome() {93return JAVA_HOME;94}9596/**97* Return the {@code user.home} system property.98*99* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked100* in this method. The caller of this method should take care to ensure101* that the returned property is not made accessible to untrusted code.</strong>102*103* @return the {@code user.home} system property104*/105public static String userHome() {106return USER_HOME;107}108109/**110* Return the {@code user.dir} system property.111*112* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked113* in this method. The caller of this method should take care to ensure114* that the returned property is not made accessible to untrusted code.</strong>115*116* @return the {@code user.dir} system property117*/118public static String userDir() {119return USER_DIR;120}121122/**123* Return the {@code user.name} system property.124*125* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked126* in this method. The caller of this method should take care to ensure127* that the returned property is not made accessible to untrusted code.</strong>128*129* @return the {@code user.name} system property130*/131public static String userName() {132return USER_NAME;133}134135/**136* Return the {@code java.library.path} system property.137*138* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked139* in this method. The caller of this method should take care to ensure140* that the returned property is not made accessible to untrusted code.</strong>141*142* @return the {@code java.library.path} system property143*/144public static String javaLibraryPath() {145return JAVA_LIBRARY_PATH;146}147148/**149* Return the {@code java.io.tmpdir} system property.150*151* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked152* in this method. The caller of this method should take care to ensure153* that the returned property is not made accessible to untrusted code.</strong>154*155* @return the {@code java.io.tmpdir} system property156*/157public static String javaIoTmpDir() {158return JAVA_IO_TMPDIR;159}160161/**162* Return the {@code sun.boot.library.path} system property.163*164* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked165* in this method. The caller of this method should take care to ensure166* that the returned property is not made accessible to untrusted code.</strong>167*168* @return the {@code sun.boot.library.path} system property169*/170public static String sunBootLibraryPath() {171return SUN_BOOT_LIBRARY_PATH;172}173174175/**176* Return the {@code jdk.serialFilter} system property.177*178* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked179* in this method. The caller of this method should take care to ensure180* that the returned property is not made accessible to untrusted code.</strong>181*182* @return the {@code jdk.serialFilter} system property183*/184public static String jdkSerialFilter() {185return JDK_SERIAL_FILTER;186}187188189/**190* Return the {@code jdk.serialFilterFactory} system property.191*192* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked193* in this method. The caller of this method should take care to ensure194* that the returned property is not made accessible to untrusted code.</strong>195*196* @return the {@code jdk.serialFilterFactory} system property197*/198public static String jdkSerialFilterFactory() {199return JDK_SERIAL_FILTER_FACTORY;200}201202/**203* Return the {@code native.encoding} system property.204*205* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked206* in this method. The caller of this method should take care to ensure207* that the returned property is not made accessible to untrusted code.</strong>208*209* @return the {@code native.encoding} system property210*/211public static String nativeEncoding() {212return NATIVE_ENCODING;213}214}215216217