Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/action/GetPropertyAction.java
38830 views
/*1* Copyright (c) 1998, 2017, 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;2930/**31* A convenience class for retrieving the string value of a system32* property as a privileged action.33*34* <p>An instance of this class can be used as the argument of35* <code>AccessController.doPrivileged</code>.36*37* <p>The following code retrieves the value of the system38* property named <code>"prop"</code> as a privileged action: <p>39*40* <pre>41* String s = java.security.AccessController.doPrivileged42* (new GetPropertyAction("prop"));43* </pre>44*45* @author Roland Schemers46* @see java.security.PrivilegedAction47* @see java.security.AccessController48* @since 1.249*/5051public class GetPropertyAction implements PrivilegedAction<String> {52private String theProp;53private String defaultVal;5455/**56* Constructor that takes the name of the system property whose57* string value needs to be determined.58*59* @param theProp the name of the system property.60*/61public GetPropertyAction(String theProp) {62this.theProp = theProp;63}6465/**66* Constructor that takes the name of the system property and the default67* value of that property.68*69* @param theProp the name of the system property.70* @param defaulVal the default value.71*/72public GetPropertyAction(String theProp, String defaultVal) {73this.theProp = theProp;74this.defaultVal = defaultVal;75}7677/**78* Determines the string value of the system property whose79* name was specified in the constructor.80*81* @return the string value of the system property,82* or the default value if there is no property with that key.83*/84public String run() {85String value = System.getProperty(theProp);86return (value == null) ? defaultVal : value;87}8889/**90* Convenience method to get a property without going through doPrivileged91* if no security manager is present. This is unsafe for inclusion in a92* public API but allowable here since this class is by default restricted93* by the package.access security property.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*/102public static String privilegedGetProperty(String theProp) {103if (System.getSecurityManager() == null) {104return System.getProperty(theProp);105} else {106return AccessController.doPrivileged(107new GetPropertyAction(theProp));108}109}110111/**112* Convenience method to get a property without going through doPrivileged113* if no security manager is present. This is unsafe for inclusion in a114* public API but allowable here since this class is now encapsulated.115*116* Note that this method performs a privileged action using caller-provided117* inputs. The caller of this method should take care to ensure that the118* inputs are not tainted and the returned property is not made accessible119* to untrusted code if it contains sensitive information.120*121* @param theProp the name of the system property.122* @param defaultVal the default value.123*/124public static String privilegedGetProperty(String theProp,125String defaultVal) {126if (System.getSecurityManager() == null) {127return System.getProperty(theProp, defaultVal);128} else {129return AccessController.doPrivileged(130new GetPropertyAction(theProp, defaultVal));131}132}133}134135136