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
26package sun.security.action;
27
28import java.security.AccessController;
29import java.security.PrivilegedAction;
30import 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
53public 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     * 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
112    /**
113     * Convenience method to get a property without going through doPrivileged
114     * if no security manager is present. This is unsafe for inclusion in a
115     * public API but allowable here since this class is now encapsulated.
116     *
117     * Note that this method performs a privileged action using caller-provided
118     * inputs. The caller of this method should take care to ensure that the
119     * inputs are not tainted and the returned property is not made accessible
120     * to untrusted code if it contains sensitive information.
121     *
122     * @param theProp the name of the system property.
123     * @param defaultVal the default value.
124     */
125    public static String privilegedGetProperty(String theProp,
126            String defaultVal) {
127        if (System.getSecurityManager() == null) {
128            return System.getProperty(theProp, defaultVal);
129        } else {
130            return AccessController.doPrivileged(
131                    new GetPropertyAction(theProp, defaultVal));
132        }
133    }
134
135    /**
136     * Convenience method to call <code>System.getProperties</code> without
137     * having to go through doPrivileged if no security manager is present.
138     * This is unsafe for inclusion in a public API but allowable here since
139     * this class is now encapsulated.
140     *
141     * Note that this method performs a privileged action, and callers of
142     * this method should take care to ensure that the returned properties
143     * are not made accessible to untrusted code since it may contain
144     * sensitive information.
145     */
146    public static Properties privilegedGetProperties() {
147        if (System.getSecurityManager() == null) {
148            return System.getProperties();
149        } else {
150            return AccessController.doPrivileged(
151                    new PrivilegedAction<Properties>() {
152                        public Properties run() {
153                            return System.getProperties();
154                        }
155                    }
156            );
157        }
158    }
159}
160