< prev index next >

src/java.base/share/classes/sun/security/action/GetPropertyAction.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 string value of a system
  30  * property 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 value of the system
  36  * property named <code>"prop"</code> as a privileged action:
  37  *
  38  * <pre>
  39  * String s = java.security.AccessController.doPrivileged
  40  *                      (new GetPropertyAction("prop"));
  41  * </pre>
  42  *
  43  * @author Roland Schemers
  44  * @see java.security.PrivilegedAction
  45  * @see java.security.AccessController
  46  * @since 1.2
  47  */
  48 
  49 public class GetPropertyAction
  50         implements java.security.PrivilegedAction<String> {
  51     private String theProp;
  52     private String defaultVal;
  53 
  54     /**
  55      * Constructor that takes the name of the system property whose
  56      * string value needs to be determined.
  57      *
  58      * @param theProp the name of the system property.
  59      */
  60     public GetPropertyAction(String theProp) {
  61         this.theProp = theProp;
  62     }
  63 
  64     /**
  65      * Constructor that takes the name of the system property and the default
  66      * value of that property.
  67      *
  68      * @param theProp the name of the system property.
  69      * @param defaultVal the default value.
  70      */
  71     public GetPropertyAction(String theProp, String defaultVal) {
  72         this.theProp = theProp;
  73         this.defaultVal = defaultVal;
  74     }
  75 
  76     /**
  77      * Determines the string value of the system property whose
  78      * name was specified in the constructor.
  79      *
  80      * @return the string value of the system property,
  81      *         or the default value if there is no property with that key.
  82      */
  83     public String run() {
  84         String value = System.getProperty(theProp);
  85         return (value == null) ? defaultVal : value;





















































  86     }
  87 }


   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 import java.security.PrivilegedAction;
  30 import java.util.Properties;
  31 
  32 /**
  33  * A convenience class for retrieving the string value of a system
  34  * property as a privileged action.
  35  *
  36  * <p>An instance of this class can be used as the argument of
  37  * <code>AccessController.doPrivileged</code>.
  38  *
  39  * <p>The following code retrieves the value of the system
  40  * property named <code>"prop"</code> as a privileged action:
  41  *
  42  * <pre>
  43  * String s = java.security.AccessController.doPrivileged
  44  *                      (new GetPropertyAction("prop"));
  45  * </pre>
  46  *
  47  * @author Roland Schemers
  48  * @see java.security.PrivilegedAction
  49  * @see java.security.AccessController
  50  * @since 1.2
  51  */
  52 
  53 public class GetPropertyAction implements PrivilegedAction<String> {

  54     private String theProp;
  55     private String defaultVal;
  56 
  57     /**
  58      * Constructor that takes the name of the system property whose
  59      * string value needs to be determined.
  60      *
  61      * @param theProp the name of the system property.
  62      */
  63     public GetPropertyAction(String theProp) {
  64         this.theProp = theProp;
  65     }
  66 
  67     /**
  68      * Constructor that takes the name of the system property and the default
  69      * value of that property.
  70      *
  71      * @param theProp the name of the system property.
  72      * @param defaultVal the default value.
  73      */
  74     public GetPropertyAction(String theProp, String defaultVal) {
  75         this.theProp = theProp;
  76         this.defaultVal = defaultVal;
  77     }
  78 
  79     /**
  80      * Determines the string value of the system property whose
  81      * name was specified in the constructor.
  82      *
  83      * @return the string value of the system property,
  84      *         or the default value if there is no property with that key.
  85      */
  86     public String run() {
  87         String value = System.getProperty(theProp);
  88         return (value == null) ? defaultVal : value;
  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     }
 143 }
< prev index next >