1 /*
2  * Copyright (c) 2018, 2021, 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 
26 package jdk.internal.util;
27 
28 import java.util.Properties;
29 
30 /**
31  * System Property access for internal use only.
32  * Read-only access to System property values initialized during Phase 1
33  * are cached.  Setting, clearing, or modifying the value using
34  * {@link System#setProperty) or {@link System#getProperties()} is ignored.
35  * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
36  * in these access methods. The caller of these methods should take care to ensure
37  * that the returned property is not made accessible to untrusted code.</strong>
38  */
39 public final class StaticProperty {
40 
41     // The class static initialization is triggered to initialize these final
42     // fields during init Phase 1 and before a security manager is set.
43     private static final String JAVA_HOME;
44     private static final String USER_HOME;
45     private static final String USER_DIR;
46     private static final String USER_NAME;
47     private static final String JAVA_LIBRARY_PATH;
48     private static final String JAVA_IO_TMPDIR;
49     // Android-removed: Serial filters are unsupported and some paths are unused.
50     // private static final String SUN_BOOT_LIBRARY_PATH;
51     // private static final String JDK_SERIAL_FILTER;
52     // private static final String JDK_SERIAL_FILTER_FACTORY;
53     // private static final String NATIVE_ENCODING;
54 
StaticProperty()55     private StaticProperty() {}
56 
57     static {
58         Properties props = System.getProperties();
59         JAVA_HOME = getProperty(props, "java.home");
60         USER_HOME = getProperty(props, "user.home");
61         USER_DIR  = getProperty(props, "user.dir");
62         USER_NAME = getProperty(props, "user.name");
63         JAVA_IO_TMPDIR = getProperty(props, "java.io.tmpdir");
64         JAVA_LIBRARY_PATH = getProperty(props, "java.library.path", "");
65         // Android-removed: Serial filters are unsupported and some paths are unused.
66         // SUN_BOOT_LIBRARY_PATH = getProperty(props, "sun.boot.library.path", "");
67         // JDK_SERIAL_FILTER = getProperty(props, "jdk.serialFilter", null);
68         // JDK_SERIAL_FILTER_FACTORY = getProperty(props, "jdk.serialFilterFactory", null);
69         // NATIVE_ENCODING = getProperty(props, "native.encoding");
70     }
71 
getProperty(Properties props, String key)72     private static String getProperty(Properties props, String key) {
73         String v = props.getProperty(key);
74         if (v == null) {
75             throw new InternalError("null property: " + key);
76         }
77         return v;
78     }
79 
getProperty(Properties props, String key, String defaultVal)80     private static String getProperty(Properties props, String key,
81                                       String defaultVal) {
82         String v = props.getProperty(key);
83         return (v == null) ? defaultVal : v;
84     }
85 
86     /**
87      * Return the {@code java.home} system property.
88      *
89      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
90      * in this method. The caller of this method should take care to ensure
91      * that the returned property is not made accessible to untrusted code.</strong>
92      *
93      * @return the {@code java.home} system property
94      */
javaHome()95     public static String javaHome() {
96         return JAVA_HOME;
97     }
98 
99     /**
100      * Return the {@code user.home} system property.
101      *
102      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
103      * in this method. The caller of this method should take care to ensure
104      * that the returned property is not made accessible to untrusted code.</strong>
105      *
106      * @return the {@code user.home} system property
107      */
userHome()108     public static String userHome() {
109         return USER_HOME;
110     }
111 
112     /**
113      * Return the {@code user.dir} system property.
114      *
115      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
116      * in this method. The caller of this method should take care to ensure
117      * that the returned property is not made accessible to untrusted code.</strong>
118      *
119      * @return the {@code user.dir} system property
120      */
userDir()121     public static String userDir() {
122         return USER_DIR;
123     }
124 
125     /**
126      * Return the {@code user.name} system property.
127      *
128      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
129      * in this method. The caller of this method should take care to ensure
130      * that the returned property is not made accessible to untrusted code.</strong>
131      *
132      * @return the {@code user.name} system property
133      */
userName()134     public static String userName() {
135         return USER_NAME;
136     }
137 
138     /**
139      * Return the {@code java.library.path} system property.
140      *
141      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
142      * in this method. The caller of this method should take care to ensure
143      * that the returned property is not made accessible to untrusted code.</strong>
144      *
145      * @return the {@code java.library.path} system property
146      */
javaLibraryPath()147     public static String javaLibraryPath() {
148         return JAVA_LIBRARY_PATH;
149     }
150 
151     /**
152      * Return the {@code java.io.tmpdir} system property.
153      *
154      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
155      * in this method. The caller of this method should take care to ensure
156      * that the returned property is not made accessible to untrusted code.</strong>
157      *
158      * @return the {@code java.io.tmpdir} system property
159      */
javaIoTmpDir()160     public static String javaIoTmpDir() {
161         return JAVA_IO_TMPDIR;
162     }
163 
164     /**
165      * Return the {@code sun.boot.library.path} system property.
166      *
167      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
168      * in this method. The caller of this method should take care to ensure
169      * that the returned property is not made accessible to untrusted code.</strong>
170      *
171      * @return the {@code sun.boot.library.path} system property
172      */
173     // Android-removed: Unsupported.
174     /*
175     public static String sunBootLibraryPath() {
176         return SUN_BOOT_LIBRARY_PATH;
177     }
178     */
179 
180 
181     /**
182      * Return the {@code jdk.serialFilter} system property.
183      *
184      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
185      * in this method. The caller of this method should take care to ensure
186      * that the returned property is not made accessible to untrusted code.</strong>
187      *
188      * @return the {@code jdk.serialFilter} system property
189      */
190     // Android-removed: Unsupported.
191     /*
192     public static String jdkSerialFilter() {
193         return JDK_SERIAL_FILTER;
194     }
195     */
196 
197 
198     /**
199      * Return the {@code jdk.serialFilterFactory} system property.
200      *
201      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
202      * in this method. The caller of this method should take care to ensure
203      * that the returned property is not made accessible to untrusted code.</strong>
204      *
205      * @return the {@code jdk.serialFilterFactory} system property
206      */
207     // Android-removed: Unsupported.
208     /*
209     public static String jdkSerialFilterFactory() {
210         return JDK_SERIAL_FILTER_FACTORY;
211     }
212     */
213 
214     /**
215      * Return the {@code native.encoding} system property.
216      *
217      * <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
218      * in this method. The caller of this method should take care to ensure
219      * that the returned property is not made accessible to untrusted code.</strong>
220      *
221      * @return the {@code native.encoding} system property
222      */
223     // Android-removed: Unsupported.
224     /*
225     public static String nativeEncoding() {
226         return NATIVE_ENCODING;
227     }
228     */
229 }
230