Path: blob/master/src/java.base/share/classes/sun/security/action/GetLongAction.java
41159 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 <code>Long</code> value of a system29* property 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 <code>Long</code> 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>:39*40* <pre>41* Long tmp = java.security.AccessController.doPrivileged42* (new sun.security.action.GetLongAction("prop"));43* long l;44* if (tmp != null) {45* l = tmp.longValue();46* }47* </pre>48*49* <p>The following code retrieves the <code>Long</code> 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:53*54* <pre>55* long l = java.security.AccessController.doPrivileged56* (new GetLongAction("prop")).longValue();57* </pre>58*59* @author Roland Schemers60* @see java.security.PrivilegedAction61* @see java.security.AccessController62* @since 1.263*/6465public class GetLongAction implements java.security.PrivilegedAction<Long> {66private String theProp;67private long defaultVal;68private boolean defaultSet = false;6970/**71* Constructor that takes the name of the system property whose72* <code>Long</code> value needs to be determined.73*74* @param theProp the name of the system property.75*/76public GetLongAction(String theProp) {77this.theProp = theProp;78}7980/**81* Constructor that takes the name of the system property and the default82* value of that property.83*84* @param theProp the name of the system property.85* @param defaultVal the default value.86*/87public GetLongAction(String theProp, long defaultVal) {88this.theProp = theProp;89this.defaultVal = defaultVal;90this.defaultSet = true;91}9293/**94* Determines the <code>Long</code> value of the system property whose95* name was specified in the constructor.96*97* <p>If there is no property of the specified name, or if the property98* does not have the correct numeric format, then a <code>Long</code>99* object representing the default value that was specified in the100* constructor is returned, or <code>null</code> if no default value was101* specified.102*103* @return the <code>Long</code> value of the property.104*/105public Long run() {106Long value = Long.getLong(theProp);107if ((value == null) && defaultSet)108return defaultVal;109return value;110}111}112113114