< prev index next >

src/share/classes/sun/security/action/GetPropertyAction.java

Print this page
rev 12543 : 8181048: Refactor existing providers to refer to the same constants for default values for key length
Reviewed-by: mullan, ahgross
   1 /*
   2  * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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: <p>
  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 defaulVal 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 }
   1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 
  31 /**
  32  * A convenience class for retrieving the string value of a system
  33  * property as a privileged action.
  34  *
  35  * <p>An instance of this class can be used as the argument of
  36  * <code>AccessController.doPrivileged</code>.
  37  *
  38  * <p>The following code retrieves the value of the system
  39  * property named <code>"prop"</code> as a privileged action: <p>
  40  *
  41  * <pre>
  42  * String s = java.security.AccessController.doPrivileged
  43  *                      (new GetPropertyAction("prop"));
  44  * </pre>
  45  *
  46  * @author Roland Schemers
  47  * @see java.security.PrivilegedAction
  48  * @see java.security.AccessController
  49  * @since 1.2
  50  */
  51 
  52 public class GetPropertyAction implements PrivilegedAction<String> {

  53     private String theProp;
  54     private String defaultVal;
  55 
  56     /**
  57      * Constructor that takes the name of the system property whose
  58      * string value needs to be determined.
  59      *
  60      * @param theProp the name of the system property.
  61      */
  62     public GetPropertyAction(String theProp) {
  63         this.theProp = theProp;
  64     }
  65 
  66     /**
  67      * Constructor that takes the name of the system property and the default
  68      * value of that property.
  69      *
  70      * @param theProp the name of the system property.
  71      * @param defaulVal the default value.
  72      */
  73     public GetPropertyAction(String theProp, String defaultVal) {
  74         this.theProp = theProp;
  75         this.defaultVal = defaultVal;
  76     }
  77 
  78     /**
  79      * Determines the string value of the system property whose
  80      * name was specified in the constructor.
  81      *
  82      * @return the string value of the system property,
  83      *         or the default value if there is no property with that key.
  84      */
  85     public String run() {
  86         String value = System.getProperty(theProp);
  87         return (value == null) ? defaultVal : value;
  88     }
  89 
  90     /**
  91      * Convenience method to get a property without going through doPrivileged
  92      * if no security manager is present. This is unsafe for inclusion in a
  93      * public API but allowable here since this class is by default restricted
  94      * by the package.access security property.
  95      *
  96      * Note that this method performs a privileged action using caller-provided
  97      * inputs. The caller of this method should take care to ensure that the
  98      * inputs are not tainted and the returned property is not made accessible
  99      * to untrusted code if it contains sensitive information.
 100      *
 101      * @param theProp the name of the system property.
 102      */
 103     public static String privilegedGetProperty(String theProp) {
 104         if (System.getSecurityManager() == null) {
 105             return System.getProperty(theProp);
 106         } else {
 107             return AccessController.doPrivileged(
 108                     new GetPropertyAction(theProp));
 109         }
 110     }
 111 }
< prev index next >