jdk/src/java.base/share/classes/sun/security/action/GetPropertyAction.java
changeset 37593 824750ada3d6
parent 30374 2abaf49910ea
child 37781 71ed5645f17c
equal deleted inserted replaced
37592:c80f098887f4 37593:824750ada3d6
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.security.action;
    26 package sun.security.action;
    27 
    27 
       
    28 import java.security.AccessController;
       
    29 import java.security.PrivilegedAction;
       
    30 import java.util.Properties;
       
    31 
    28 /**
    32 /**
    29  * A convenience class for retrieving the string value of a system
    33  * A convenience class for retrieving the string value of a system
    30  * property as a privileged action.
    34  * property as a privileged action.
    31  *
    35  *
    32  * <p>An instance of this class can be used as the argument of
    36  * <p>An instance of this class can be used as the argument of
    44  * @see java.security.PrivilegedAction
    48  * @see java.security.PrivilegedAction
    45  * @see java.security.AccessController
    49  * @see java.security.AccessController
    46  * @since 1.2
    50  * @since 1.2
    47  */
    51  */
    48 
    52 
    49 public class GetPropertyAction
    53 public class GetPropertyAction implements PrivilegedAction<String> {
    50         implements java.security.PrivilegedAction<String> {
       
    51     private String theProp;
    54     private String theProp;
    52     private String defaultVal;
    55     private String defaultVal;
    53 
    56 
    54     /**
    57     /**
    55      * Constructor that takes the name of the system property whose
    58      * Constructor that takes the name of the system property whose
    82      */
    85      */
    83     public String run() {
    86     public String run() {
    84         String value = System.getProperty(theProp);
    87         String value = System.getProperty(theProp);
    85         return (value == null) ? defaultVal : value;
    88         return (value == null) ? defaultVal : value;
    86     }
    89     }
       
    90 
       
    91     /**
       
    92      * Convenience method to get a property without going through doPrivileged
       
    93      * if no security manager is present. This is unsafe for inclusion in a
       
    94      * public API but allowable here since this class is now encapsulated.
       
    95      *
       
    96      * @param theProp the name of the system property.
       
    97      */
       
    98     public static String getProperty(String theProp) {
       
    99         if (System.getSecurityManager() == null) {
       
   100             return System.getProperty(theProp);
       
   101         } else {
       
   102             return AccessController.doPrivileged(
       
   103                     new GetPropertyAction(theProp));
       
   104         }
       
   105     }
       
   106 
       
   107     /**
       
   108      * Convenience method to get a property without going through doPrivileged
       
   109      * if no security manager is present. This is unsafe for inclusion in a
       
   110      * public API but allowable here since this class is now encapsulated.
       
   111      *
       
   112      * @param theProp the name of the system property.
       
   113      * @param defaultVal the default value.
       
   114      */
       
   115     public static String getProperty(String theProp, String defaultVal) {
       
   116         if (System.getSecurityManager() == null) {
       
   117             return System.getProperty(theProp, defaultVal);
       
   118         } else {
       
   119             return AccessController.doPrivileged(
       
   120                     new GetPropertyAction(theProp, defaultVal));
       
   121         }
       
   122     }
       
   123 
       
   124     /**
       
   125      * Convenience method to call <code>System.getProperties</code> without
       
   126      * having to go through doPrivileged if no security manager is present.
       
   127      * This is unsafe for inclusion in a public API but allowable here since
       
   128      * this class is now encapsulated.
       
   129      */
       
   130     public static Properties getProperties() {
       
   131         if (System.getSecurityManager() == null) {
       
   132             return System.getProperties();
       
   133         } else {
       
   134             return AccessController.doPrivileged(
       
   135                     new PrivilegedAction<Properties>() {
       
   136                         public Properties run() {
       
   137                             return System.getProperties();
       
   138                         }
       
   139                     }
       
   140             );
       
   141         }
       
   142     }
    87 }
   143 }