Path: blob/master/src/java.base/share/classes/sun/security/action/GetBooleanAction.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;2829/**30* A convenience class for retrieving the boolean value of a system property31* as a privileged action.32*33* <p>An instance of this class can be used as the argument of34* <code>AccessController.doPrivileged</code>.35*36* <p>The following code retrieves the boolean value of the system37* property named <code>"prop"</code> as a privileged action:38*39* <pre>40* boolean b = java.security.AccessController.doPrivileged41* (new GetBooleanAction("prop")).booleanValue();42* </pre>43*44* @author Roland Schemers45* @see java.security.PrivilegedAction46* @see java.security.AccessController47* @since 1.248*/4950public class GetBooleanAction51implements java.security.PrivilegedAction<Boolean> {52private String theProp;5354/**55* Constructor that takes the name of the system property whose boolean56* value needs to be determined.57*58* @param theProp the name of the system property.59*/60public GetBooleanAction(String theProp) {61this.theProp = theProp;62}6364/**65* Determines the boolean value of the system property whose name was66* specified in the constructor.67*68* @return the <code>Boolean</code> value of the system property.69*/70public Boolean run() {71return Boolean.getBoolean(theProp);72}7374/**75* Convenience method to get a property without going through doPrivileged76* if no security manager is present. This is unsafe for inclusion in a77* public API but allowable here since this class is now encapsulated.78*79* Note that this method performs a privileged action using caller-provided80* inputs. The caller of this method should take care to ensure that the81* inputs are not tainted and the returned property is not made accessible82* to untrusted code if it contains sensitive information.83*84* @param theProp the name of the system property.85*/86@SuppressWarnings("removal")87public static boolean privilegedGetProperty(String theProp) {88if (System.getSecurityManager() == null) {89return Boolean.getBoolean(theProp);90} else {91return AccessController.doPrivileged(92new GetBooleanAction(theProp));93}94}95}969798