jdk/src/java.base/share/classes/sun/security/action/GetPropertyAction.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 30374 2abaf49910ea
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    28
import java.security.AccessController;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    29
import java.security.PrivilegedAction;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    30
import java.util.Properties;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A convenience class for retrieving the string value of a system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * property as a privileged action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>An instance of this class can be used as the argument of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>AccessController.doPrivileged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>The following code retrieves the value of the system
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    40
 * property named <code>"prop"</code> as a privileged action:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * String s = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                      (new GetPropertyAction("prop"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see java.security.PrivilegedAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    53
public class GetPropertyAction implements PrivilegedAction<String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String theProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String defaultVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructor that takes the name of the system property whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * string value needs to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param theProp the name of the system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public GetPropertyAction(String theProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.theProp = theProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Constructor that takes the name of the system property and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * value of that property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param theProp the name of the system property.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    72
     * @param defaultVal the default value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public GetPropertyAction(String theProp, String defaultVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.theProp = theProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.defaultVal = defaultVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Determines the string value of the system property whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * name was specified in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return the string value of the system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *         or the default value if there is no property with that key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String value = System.getProperty(theProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return (value == null) ? defaultVal : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    90
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    91
    /**
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    92
     * Convenience method to get a property without going through doPrivileged
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    93
     * if no security manager is present. This is unsafe for inclusion in a
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    94
     * public API but allowable here since this class is now encapsulated.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    95
     *
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    96
     * @param theProp the name of the system property.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    97
     */
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    98
    public static String getProperty(String theProp) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
    99
        if (System.getSecurityManager() == null) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   100
            return System.getProperty(theProp);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   101
        } else {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   102
            return AccessController.doPrivileged(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   103
                    new GetPropertyAction(theProp));
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   104
        }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   105
    }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   106
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   107
    /**
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   108
     * Convenience method to get a property without going through doPrivileged
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   109
     * if no security manager is present. This is unsafe for inclusion in a
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   110
     * public API but allowable here since this class is now encapsulated.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   111
     *
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   112
     * @param theProp the name of the system property.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   113
     * @param defaultVal the default value.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   114
     */
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   115
    public static String getProperty(String theProp, String defaultVal) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   116
        if (System.getSecurityManager() == null) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   117
            return System.getProperty(theProp, defaultVal);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   118
        } else {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   119
            return AccessController.doPrivileged(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   120
                    new GetPropertyAction(theProp, defaultVal));
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   121
        }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   122
    }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   123
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   124
    /**
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   125
     * Convenience method to call <code>System.getProperties</code> without
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   126
     * having to go through doPrivileged if no security manager is present.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   127
     * This is unsafe for inclusion in a public API but allowable here since
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   128
     * this class is now encapsulated.
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   129
     */
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   130
    public static Properties getProperties() {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   131
        if (System.getSecurityManager() == null) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   132
            return System.getProperties();
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   133
        } else {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   134
            return AccessController.doPrivileged(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   135
                    new PrivilegedAction<Properties>() {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   136
                        public Properties run() {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   137
                            return System.getProperties();
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   138
                        }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   139
                    }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   140
            );
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   141
        }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 30374
diff changeset
   142
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}