1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.river.action;
20
21 import java.security.AccessController;
22 import java.security.PrivilegedAction;
23 import java.util.logging.Logger;
24 import java.util.logging.Level;
25
26 /**
27 * A convenience class for retrieving the string value of a system
28 * property as a privileged action.
29 *
30 * <p>An instance of this class can be used as the argument of {@link
31 * AccessController#doPrivileged(PrivilegedAction)
32 * AccessController.doPrivileged} or <code>
33 * net.jini.security.Security#doPrivileged(PrivilegedAction) Security.doPrivileged</code>.
34 *
35 * <p>The following code retrieves the value of the system property
36 * named <code>"prop"</code> as a privileged action:
37 *
38 * <pre>
39 * String s = (String) String.doPrivileged(
40 * new GetPropertyAction("prop"));
41 * </pre>
42 *
43 * <p>If the protection domain of the immediate caller of
44 * <code>doPrivileged</code> or the protection domain of this class
45 * does not imply the permissions necessary for the operation, the
46 * behavior is as if the system property is not defined.
47 *
48 * @author Sun Microsystems, Inc.
49 *
50 * @see PrivilegedAction
51 * @see AccessController
52 * <code>see net.jini.security.Security</code>
53 * @since 2.0
54 **/
55 public class GetPropertyAction implements PrivilegedAction<String> {
56
57 private static final Logger logger =
58 Logger.getLogger("org.apache.river.action.GetPropertyAction");
59
60 private final String theProp;
61 private final String defaultVal;
62
63 /**
64 * Constructor that takes the name of the system property whose
65 * string value needs to be determined.
66 *
67 * @param theProp the name of the system property
68 **/
69 public GetPropertyAction(String theProp) {
70 this.theProp = theProp;
71 defaultVal = null;
72 }
73
74 /**
75 * Constructor that takes the name of the system property and the
76 * default value of that property.
77 *
78 * @param theProp the name of the system property
79 * @param defaultVal the default value
80 **/
81 public GetPropertyAction(String theProp, String defaultVal) {
82 this.theProp = theProp;
83 this.defaultVal = defaultVal;
84 }
85
86 /**
87 * Determines the string value of the system property whose name
88 * was specified in the constructor.
89 *
90 * <p>If the system property is defined, then this method returns
91 * its value. Otherwise, if a default value was supplied to this
92 * object's constructor, then this method returns that default
93 * value, or else <code>null</code> is returned.
94 *
95 * @return the string value of the system property or the default
96 * value, or <code>null</code>
97 **/
98 @Override
99 public String run() {
100 try {
101 String value = System.getProperty(theProp);
102 if (value != null) {
103 return value;
104 }
105 } catch (SecurityException e) {
106 if (logger.isLoggable(Level.FINE)) {
107 logger.logp( Level.FINE,
108 GetPropertyAction.class.toString(), "run()",
109 "security exception reading \"{0}\", returning {1}"
110 , new Object[] {theProp, defaultVal});
111 throw e;
112 }
113 }
114 return defaultVal;
115 }
116 }