< prev index next >

src/java.base/share/classes/sun/security/action/GetIntegerAction.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.action;
  27 


  28 /**
  29  * A convenience class for retrieving the integer value of a system property
  30  * as a privileged action.
  31  *
  32  * <p>An instance of this class can be used as the argument of
  33  * <code>AccessController.doPrivileged</code>.
  34  *
  35  * <p>The following code retrieves the integer value of the system
  36  * property named <code>"prop"</code> as a privileged action. Since it does
  37  * not pass a default value to be used in case the property
  38  * <code>"prop"</code> is not defined, it has to check the result for
  39  * <code>null</code>:
  40  *
  41  * <pre>
  42  * Integer tmp = java.security.AccessController.doPrivileged
  43  *     (new sun.security.action.GetIntegerAction("prop"));
  44  * int i;
  45  * if (tmp != null) {
  46  *     i = tmp.intValue();
  47  * }


  50  * <p>The following code retrieves the integer value of the system
  51  * property named <code>"prop"</code> as a privileged action, and also passes
  52  * a default value to be used in case the property <code>"prop"</code> is not
  53  * defined:
  54  *
  55  * <pre>
  56  * int i = ((Integer)java.security.AccessController.doPrivileged(
  57  *                         new GetIntegerAction("prop", 3))).intValue();
  58  * </pre>
  59  *
  60  * @author Roland Schemers
  61  * @see java.security.PrivilegedAction
  62  * @see java.security.AccessController
  63  * @since 1.2
  64  */
  65 
  66 public class GetIntegerAction
  67         implements java.security.PrivilegedAction<Integer> {
  68     private String theProp;
  69     private int defaultVal;
  70     private boolean defaultSet = false;
  71 
  72     /**
  73      * Constructor that takes the name of the system property whose integer
  74      * value needs to be determined.
  75      *
  76      * @param theProp the name of the system property.
  77      */
  78     public GetIntegerAction(String theProp) {
  79         this.theProp = theProp;
  80     }
  81 
  82     /**
  83      * Constructor that takes the name of the system property and the default
  84      * value of that property.
  85      *
  86      * @param theProp the name of the system property.
  87      * @param defaultVal the default value.
  88      */
  89     public GetIntegerAction(String theProp, int defaultVal) {
  90         this.theProp = theProp;


  92         this.defaultSet = true;
  93     }
  94 
  95     /**
  96      * Determines the integer value of the system property whose name was
  97      * specified in the constructor.
  98      *
  99      * <p>If there is no property of the specified name, or if the property
 100      * does not have the correct numeric format, then an <code>Integer</code>
 101      * object representing the default value that was specified in the
 102      * constructor is returned, or <code>null</code> if no default value was
 103      * specified.
 104      *
 105      * @return the <code>Integer</code> value of the property.
 106      */
 107     public Integer run() {
 108         Integer value = Integer.getInteger(theProp);
 109         if ((value == null) && defaultSet)
 110             return defaultVal;
 111         return value;



































 112     }
 113 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.action;
  27 
  28 import java.security.AccessController;
  29 
  30 /**
  31  * A convenience class for retrieving the integer value of a system property
  32  * as a privileged action.
  33  *
  34  * <p>An instance of this class can be used as the argument of
  35  * <code>AccessController.doPrivileged</code>.
  36  *
  37  * <p>The following code retrieves the integer value of the system
  38  * property named <code>"prop"</code> as a privileged action. Since it does
  39  * not pass a default value to be used in case the property
  40  * <code>"prop"</code> is not defined, it has to check the result for
  41  * <code>null</code>:
  42  *
  43  * <pre>
  44  * Integer tmp = java.security.AccessController.doPrivileged
  45  *     (new sun.security.action.GetIntegerAction("prop"));
  46  * int i;
  47  * if (tmp != null) {
  48  *     i = tmp.intValue();
  49  * }


  52  * <p>The following code retrieves the integer value of the system
  53  * property named <code>"prop"</code> as a privileged action, and also passes
  54  * a default value to be used in case the property <code>"prop"</code> is not
  55  * defined:
  56  *
  57  * <pre>
  58  * int i = ((Integer)java.security.AccessController.doPrivileged(
  59  *                         new GetIntegerAction("prop", 3))).intValue();
  60  * </pre>
  61  *
  62  * @author Roland Schemers
  63  * @see java.security.PrivilegedAction
  64  * @see java.security.AccessController
  65  * @since 1.2
  66  */
  67 
  68 public class GetIntegerAction
  69         implements java.security.PrivilegedAction<Integer> {
  70     private String theProp;
  71     private int defaultVal;
  72     private boolean defaultSet;
  73 
  74     /**
  75      * Constructor that takes the name of the system property whose integer
  76      * value needs to be determined.
  77      *
  78      * @param theProp the name of the system property.
  79      */
  80     public GetIntegerAction(String theProp) {
  81         this.theProp = theProp;
  82     }
  83 
  84     /**
  85      * Constructor that takes the name of the system property and the default
  86      * value of that property.
  87      *
  88      * @param theProp the name of the system property.
  89      * @param defaultVal the default value.
  90      */
  91     public GetIntegerAction(String theProp, int defaultVal) {
  92         this.theProp = theProp;


  94         this.defaultSet = true;
  95     }
  96 
  97     /**
  98      * Determines the integer value of the system property whose name was
  99      * 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 Integer run() {
 110         Integer value = Integer.getInteger(theProp);
 111         if ((value == null) && defaultSet)
 112             return defaultVal;
 113         return value;
 114     }
 115 
 116     /**
 117      * Convenience method to get a property without going through doPrivileged
 118      * if no security manager is present. This is unsafe for inclusion in a
 119      * public API but allowable here since this class is now encapsulated.
 120      *
 121      * @param theProp the name of the system property.
 122      */
 123     public static Integer getProperty(String theProp) {
 124         if (System.getSecurityManager() == null) {
 125             return Integer.getInteger(theProp);
 126         } else {
 127             return AccessController.doPrivileged(
 128                     new GetIntegerAction(theProp));
 129         }
 130     }
 131 
 132     /**
 133      * Convenience method to get a property without going through doPrivileged
 134      * if no security manager is present. This is unsafe for inclusion in a
 135      * public API but allowable here since this class is now encapsulated.
 136      *
 137      * @param theProp the name of the system property.
 138      * @param defaultVal the default value.
 139      */
 140     public static Integer getProperty(String theProp, int defaultVal) {
 141         Integer value;
 142         if (System.getSecurityManager() == null) {
 143             value = Integer.getInteger(theProp);
 144         } else {
 145             value = AccessController.doPrivileged(
 146                     new GetIntegerAction(theProp));
 147         }
 148         return (value != null) ? value : defaultVal;
 149     }
 150 }
< prev index next >