Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/action/GetIntegerAction.java
38830 views
/*1* Copyright (c) 1998, 2006, 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;2627/**28* A convenience class for retrieving the integer value of a system property29* as a privileged action.30*31* <p>An instance of this class can be used as the argument of32* <code>AccessController.doPrivileged</code>.33*34* <p>The following code retrieves the integer value of the system35* property named <code>"prop"</code> as a privileged action. Since it does36* not pass a default value to be used in case the property37* <code>"prop"</code> is not defined, it has to check the result for38* <code>null</code>: <p>39*40* <pre>41* Integer tmp = java.security.AccessController.doPrivileged42* (new sun.security.action.GetIntegerAction("prop"));43* int i;44* if (tmp != null) {45* i = tmp.intValue();46* }47* </pre>48*49* <p>The following code retrieves the integer value of the system50* property named <code>"prop"</code> as a privileged action, and also passes51* a default value to be used in case the property <code>"prop"</code> is not52* defined: <p>53*54* <pre>55* int i = ((Integer)java.security.AccessController.doPrivileged(56* new GetIntegerAction("prop", 3))).intValue();57* </pre>58*59* @author Roland Schemers60* @see java.security.PrivilegedAction61* @see java.security.AccessController62* @since 1.263*/6465public class GetIntegerAction66implements java.security.PrivilegedAction<Integer> {67private String theProp;68private int defaultVal;69private boolean defaultSet = false;7071/**72* Constructor that takes the name of the system property whose integer73* value needs to be determined.74*75* @param theProp the name of the system property.76*/77public GetIntegerAction(String theProp) {78this.theProp = theProp;79}8081/**82* Constructor that takes the name of the system property and the default83* value of that property.84*85* @param theProp the name of the system property.86* @param defaulVal the default value.87*/88public GetIntegerAction(String theProp, int defaultVal) {89this.theProp = theProp;90this.defaultVal = defaultVal;91this.defaultSet = true;92}9394/**95* Determines the integer value of the system property whose name was96* specified in the constructor.97*98* <p>If there is no property of the specified name, or if the property99* does not have the correct numeric format, then an <code>Integer</code>100* object representing the default value that was specified in the101* constructor is returned, or <code>null</code> if no default value was102* specified.103*104* @return the <code>Integer</code> value of the property.105*/106public Integer run() {107Integer value = Integer.getInteger(theProp);108if ((value == null) && defaultSet)109return new Integer(defaultVal);110return value;111}112}113114115