Path: blob/master/src/java.base/share/classes/sun/security/action/GetIntegerAction.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 integer 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 integer value of the system37* property named <code>"prop"</code> as a privileged action. Since it does38* not pass a default value to be used in case the property39* <code>"prop"</code> is not defined, it has to check the result for40* <code>null</code>:41*42* <pre>43* Integer tmp = java.security.AccessController.doPrivileged44* (new sun.security.action.GetIntegerAction("prop"));45* int i;46* if (tmp != null) {47* i = tmp.intValue();48* }49* </pre>50*51* <p>The following code retrieves the integer value of the system52* property named <code>"prop"</code> as a privileged action, and also passes53* a default value to be used in case the property <code>"prop"</code> is not54* defined:55*56* <pre>57* int i = ((Integer)java.security.AccessController.doPrivileged(58* new GetIntegerAction("prop", 3))).intValue();59* </pre>60*61* @author Roland Schemers62* @see java.security.PrivilegedAction63* @see java.security.AccessController64* @since 1.265*/6667public class GetIntegerAction68implements java.security.PrivilegedAction<Integer> {69private String theProp;70private int defaultVal;71private boolean defaultSet;7273/**74* Constructor that takes the name of the system property whose integer75* value needs to be determined.76*77* @param theProp the name of the system property.78*/79public GetIntegerAction(String theProp) {80this.theProp = theProp;81}8283/**84* Constructor that takes the name of the system property and the default85* value of that property.86*87* @param theProp the name of the system property.88* @param defaultVal the default value.89*/90public GetIntegerAction(String theProp, int defaultVal) {91this.theProp = theProp;92this.defaultVal = defaultVal;93this.defaultSet = true;94}9596/**97* Determines the integer value of the system property whose name was98* specified in the constructor.99*100* <p>If there is no property of the specified name, or if the property101* does not have the correct numeric format, then an <code>Integer</code>102* object representing the default value that was specified in the103* constructor is returned, or <code>null</code> if no default value was104* specified.105*106* @return the <code>Integer</code> value of the property.107*/108public Integer run() {109Integer value = Integer.getInteger(theProp);110if ((value == null) && defaultSet)111return defaultVal;112return value;113}114115/**116* Convenience method to get a property without going through doPrivileged117* if no security manager is present. This is unsafe for inclusion in a118* public API but allowable here since this class is now encapsulated.119*120* Note that this method performs a privileged action using caller-provided121* inputs. The caller of this method should take care to ensure that the122* inputs are not tainted and the returned property is not made accessible123* to untrusted code if it contains sensitive information.124*125* @param theProp the name of the system property.126*/127@SuppressWarnings("removal")128public static Integer privilegedGetProperty(String theProp) {129if (System.getSecurityManager() == null) {130return Integer.getInteger(theProp);131} else {132return AccessController.doPrivileged(133new GetIntegerAction(theProp));134}135}136137/**138* Convenience method to get a property without going through doPrivileged139* if no security manager is present. This is unsafe for inclusion in a140* public API but allowable here since this class is now encapsulated.141*142* Note that this method performs a privileged action using caller-provided143* inputs. The caller of this method should take care to ensure that the144* inputs are not tainted and the returned property is not made accessible145* to untrusted code if it contains sensitive information.146*147* @param theProp the name of the system property.148* @param defaultVal the default value.149*/150@SuppressWarnings("removal")151public static Integer privilegedGetProperty(String theProp,152int defaultVal) {153Integer value;154if (System.getSecurityManager() == null) {155value = Integer.getInteger(theProp);156} else {157value = AccessController.doPrivileged(158new GetIntegerAction(theProp));159}160return (value != null) ? value : defaultVal;161}162}163164165