Path: blob/master/src/java.base/share/classes/sun/security/action/GetPropertyAction.java
41159 views
/*1* Copyright (c) 1998, 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 sun.security.action;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.Properties;3031/**32* A convenience class for retrieving the string value of a system33* property as a privileged action.34*35* <p>An instance of this class can be used as the argument of36* <code>AccessController.doPrivileged</code>.37*38* <p>The following code retrieves the value of the system39* property named <code>"prop"</code> as a privileged action:40*41* <pre>42* String s = java.security.AccessController.doPrivileged43* (new GetPropertyAction("prop"));44* </pre>45*46* @author Roland Schemers47* @see java.security.PrivilegedAction48* @see java.security.AccessController49* @since 1.250*/5152public class GetPropertyAction implements PrivilegedAction<String> {53private String theProp;54private String defaultVal;5556/**57* Constructor that takes the name of the system property whose58* string value needs to be determined.59*60* @param theProp the name of the system property.61*/62public GetPropertyAction(String theProp) {63this.theProp = theProp;64}6566/**67* Constructor that takes the name of the system property and the default68* value of that property.69*70* @param theProp the name of the system property.71* @param defaultVal the default value.72*/73public GetPropertyAction(String theProp, String defaultVal) {74this.theProp = theProp;75this.defaultVal = defaultVal;76}7778/**79* Determines the string value of the system property whose80* name was specified in the constructor.81*82* @return the string value of the system property,83* or the default value if there is no property with that key.84*/85public String run() {86String value = System.getProperty(theProp);87return (value == null) ? defaultVal : value;88}8990/**91* Convenience method to get a property without going through doPrivileged92* if no security manager is present. This is unsafe for inclusion in a93* public API but allowable here since this class is now encapsulated.94*95* Note that this method performs a privileged action using caller-provided96* inputs. The caller of this method should take care to ensure that the97* inputs are not tainted and the returned property is not made accessible98* to untrusted code if it contains sensitive information.99*100* @param theProp the name of the system property.101*/102@SuppressWarnings("removal")103public static String privilegedGetProperty(String theProp) {104if (System.getSecurityManager() == null) {105return System.getProperty(theProp);106} else {107return AccessController.doPrivileged(108new GetPropertyAction(theProp));109}110}111112/**113* Convenience method to get a property without going through doPrivileged114* if no security manager is present. This is unsafe for inclusion in a115* public API but allowable here since this class is now encapsulated.116*117* Note that this method performs a privileged action using caller-provided118* inputs. The caller of this method should take care to ensure that the119* inputs are not tainted and the returned property is not made accessible120* to untrusted code if it contains sensitive information.121*122* @param theProp the name of the system property.123* @param defaultVal the default value.124*/125@SuppressWarnings("removal")126public static String privilegedGetProperty(String theProp,127String defaultVal) {128if (System.getSecurityManager() == null) {129return System.getProperty(theProp, defaultVal);130} else {131return AccessController.doPrivileged(132new GetPropertyAction(theProp, defaultVal));133}134}135136/**137* Convenience method to call <code>System.getProperties</code> without138* having to go through doPrivileged if no security manager is present.139* This is unsafe for inclusion in a public API but allowable here since140* this class is now encapsulated.141*142* Note that this method performs a privileged action, and callers of143* this method should take care to ensure that the returned properties144* are not made accessible to untrusted code since it may contain145* sensitive information.146*/147@SuppressWarnings("removal")148public static Properties privilegedGetProperties() {149if (System.getSecurityManager() == null) {150return System.getProperties();151} else {152return AccessController.doPrivileged(153new PrivilegedAction<Properties>() {154public Properties run() {155return System.getProperties();156}157}158);159}160}161}162163164