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