Path: blob/master/src/java.base/share/classes/jdk/internal/util/StaticProperty.java
41159 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 JAVA_IO_TMPDIR;50private static final String NATIVE_ENCODING;5152private StaticProperty() {}5354static {55Properties props = System.getProperties();56JAVA_HOME = getProperty(props, "java.home");57USER_HOME = getProperty(props, "user.home");58USER_DIR = getProperty(props, "user.dir");59USER_NAME = getProperty(props, "user.name");60JAVA_IO_TMPDIR = getProperty(props, "java.io.tmpdir");61JAVA_LIBRARY_PATH = getProperty(props, "java.library.path", "");62SUN_BOOT_LIBRARY_PATH = getProperty(props, "sun.boot.library.path", "");63JDK_SERIAL_FILTER = getProperty(props, "jdk.serialFilter", null);64NATIVE_ENCODING = getProperty(props, "native.encoding");65}6667private static String getProperty(Properties props, String key) {68String v = props.getProperty(key);69if (v == null) {70throw new InternalError("null property: " + key);71}72return v;73}7475private static String getProperty(Properties props, String key,76String defaultVal) {77String v = props.getProperty(key);78return (v == null) ? defaultVal : v;79}8081/**82* Return the {@code java.home} system property.83*84* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked85* in this method. The caller of this method should take care to ensure86* that the returned property is not made accessible to untrusted code.</strong>87*88* @return the {@code java.home} system property89*/90public static String javaHome() {91return JAVA_HOME;92}9394/**95* Return the {@code user.home} system property.96*97* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked98* in this method. The caller of this method should take care to ensure99* that the returned property is not made accessible to untrusted code.</strong>100*101* @return the {@code user.home} system property102*/103public static String userHome() {104return USER_HOME;105}106107/**108* Return the {@code user.dir} system property.109*110* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked111* in this method. The caller of this method should take care to ensure112* that the returned property is not made accessible to untrusted code.</strong>113*114* @return the {@code user.dir} system property115*/116public static String userDir() {117return USER_DIR;118}119120/**121* Return the {@code user.name} system property.122*123* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked124* in this method. The caller of this method should take care to ensure125* that the returned property is not made accessible to untrusted code.</strong>126*127* @return the {@code user.name} system property128*/129public static String userName() {130return USER_NAME;131}132133/**134* Return the {@code java.library.path} system property.135*136* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked137* in this method. The caller of this method should take care to ensure138* that the returned property is not made accessible to untrusted code.</strong>139*140* @return the {@code java.library.path} system property141*/142public static String javaLibraryPath() {143return JAVA_LIBRARY_PATH;144}145146/**147* Return the {@code java.io.tmpdir} system property.148*149* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked150* in this method. The caller of this method should take care to ensure151* that the returned property is not made accessible to untrusted code.</strong>152*153* @return the {@code java.io.tmpdir} system property154*/155public static String javaIoTmpDir() {156return JAVA_IO_TMPDIR;157}158159/**160* Return the {@code sun.boot.library.path} system property.161*162* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked163* in this method. The caller of this method should take care to ensure164* that the returned property is not made accessible to untrusted code.</strong>165*166* @return the {@code sun.boot.library.path} system property167*/168public static String sunBootLibraryPath() {169return SUN_BOOT_LIBRARY_PATH;170}171172173/**174* Return the {@code jdk.serialFilter} system property.175*176* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked177* in this method. The caller of this method should take care to ensure178* that the returned property is not made accessible to untrusted code.</strong>179*180* @return the {@code jdk.serialFilter} system property181*/182public static String jdkSerialFilter() {183return JDK_SERIAL_FILTER;184}185186/**187* Return the {@code native.encoding} system property.188*189* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked190* in this method. The caller of this method should take care to ensure191* that the returned property is not made accessible to untrusted code.</strong>192*193* @return the {@code native.encoding} system property194*/195public static String nativeEncoding() {196return NATIVE_ENCODING;197}198}199200201