01: /*
02: * @(#)GetPropertyAction.java 1.13 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package sun.security.action;
29:
30: /**
31: * A convenience class for retrieving the string value of a system
32: * property as a privileged action.
33: *
34: * <p>An instance of this class can be used as the argument of
35: * <code>AccessController.doPrivileged</code>.
36: *
37: * <p>The following code retrieves the value of the system
38: * property named <code>"prop"</code> as a privileged action: <p>
39: *
40: * <pre>
41: * String s = (String) java.security.AccessController.doPrivileged(
42: * new GetPropertyAction("prop"));
43: * </pre>
44: *
45: * @author Roland Schemers
46: * @version 1.6, 02/02/00
47: * @see java.security.PrivilegedAction
48: * @see java.security.AccessController
49: * @since JDK1.2
50: */
51:
52: public class GetPropertyAction implements
53: java.security.PrivilegedAction {
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 defaulVal 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 Object run() {
87: String value = System.getProperty(theProp);
88: return (value == null) ? defaultVal : value;
89: }
90: }
|