001: /*
002: * @(#)GetIntegerAction.java 1.14 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.action;
029:
030: /**
031: * A convenience class for retrieving the integer value of a system property
032: * as a privileged action.
033: *
034: * <p>An instance of this class can be used as the argument of
035: * <code>AccessController.doPrivileged</code>.
036: *
037: * <p>The following code retrieves the integer value of the system
038: * property named <code>"prop"</code> as a privileged action. Since it does
039: * not pass a default value to be used in case the property
040: * <code>"prop"</code> is not defined, it has to check the result for
041: * <code>null</code>: <p>
042: *
043: * <pre>
044: * Integer tmp = (Integer)java.security.AccessController.doPrivileged
045: * (new sun.security.action.GetIntegerAction("prop"));
046: * int i;
047: * if (tmp != null) {
048: * i = tmp.intValue();
049: * }
050: * </pre>
051: *
052: * <p>The following code retrieves the integer value of the system
053: * property named <code>"prop"</code> as a privileged action, and also passes
054: * a default value to be used in case the property <code>"prop"</code> is not
055: * defined: <p>
056: *
057: * <pre>
058: * int i = ((Integer)java.security.AccessController.doPrivileged(
059: * new GetIntegerAction("prop", 3))).intValue();
060: * </pre>
061: *
062: * @author Roland Schemers
063: * @version 1.7, 02/02/00
064: * @see java.security.PrivilegedAction
065: * @see java.security.AccessController
066: * @since JDK1.2
067: */
068:
069: public class GetIntegerAction implements java.security.PrivilegedAction {
070: private String theProp;
071: private int defaultVal;
072: private boolean defaultSet = false;
073:
074: /**
075: * Constructor that takes the name of the system property whose integer
076: * value needs to be determined.
077: *
078: * @param theProp the name of the system property.
079: */
080: public GetIntegerAction(String theProp) {
081: this .theProp = theProp;
082: }
083:
084: /**
085: * Constructor that takes the name of the system property and the default
086: * value of that property.
087: *
088: * @param theProp the name of the system property.
089: * @param defaulVal the default value.
090: */
091: public GetIntegerAction(String theProp, int defaultVal) {
092: this .theProp = theProp;
093: this .defaultVal = defaultVal;
094: this .defaultSet = true;
095: }
096:
097: /**
098: * Determines the integer value of the system property whose name was
099: * specified in the constructor.
100: *
101: * <p>If there is no property of the specified name, or if the property
102: * does not have the correct numeric format, then an <code>Integer</code>
103: * object representing the default value that was specified in the
104: * constructor is returned, or <code>null</code> if no default value was
105: * specified.
106: *
107: * @return the <code>Integer</code> value of the property.
108: */
109: public Object run() {
110: Integer value = Integer.getInteger(theProp);
111: if ((value == null) && defaultSet)
112: return new Integer(defaultVal);
113: return value;
114: }
115: }
|