author | redestad |
Thu, 21 Apr 2016 13:39:53 +0200 | |
changeset 37593 | 824750ada3d6 |
parent 30374 | 2abaf49910ea |
child 37781 | 71ed5645f17c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.action; |
|
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 |
|
2 | 30 |
/** |
31 |
* A convenience class for retrieving the integer value of a system property |
|
32 |
* 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 integer value of the system |
|
38 |
* property named <code>"prop"</code> as a privileged action. Since it does |
|
39 |
* not pass a default value to be used in case the property |
|
40 |
* <code>"prop"</code> is not defined, it has to check the result for |
|
30374 | 41 |
* <code>null</code>: |
2 | 42 |
* |
43 |
* <pre> |
|
44 |
* Integer tmp = java.security.AccessController.doPrivileged |
|
45 |
* (new sun.security.action.GetIntegerAction("prop")); |
|
46 |
* int i; |
|
47 |
* if (tmp != null) { |
|
48 |
* i = tmp.intValue(); |
|
49 |
* } |
|
50 |
* </pre> |
|
51 |
* |
|
52 |
* <p>The following code retrieves the integer value of the system |
|
53 |
* property named <code>"prop"</code> as a privileged action, and also passes |
|
54 |
* a default value to be used in case the property <code>"prop"</code> is not |
|
30374 | 55 |
* defined: |
2 | 56 |
* |
57 |
* <pre> |
|
58 |
* int i = ((Integer)java.security.AccessController.doPrivileged( |
|
59 |
* new GetIntegerAction("prop", 3))).intValue(); |
|
60 |
* </pre> |
|
61 |
* |
|
62 |
* @author Roland Schemers |
|
63 |
* @see java.security.PrivilegedAction |
|
64 |
* @see java.security.AccessController |
|
65 |
* @since 1.2 |
|
66 |
*/ |
|
67 |
||
68 |
public class GetIntegerAction |
|
69 |
implements java.security.PrivilegedAction<Integer> { |
|
70 |
private String theProp; |
|
71 |
private int defaultVal; |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
72 |
private boolean defaultSet; |
2 | 73 |
|
74 |
/** |
|
75 |
* Constructor that takes the name of the system property whose integer |
|
76 |
* value needs to be determined. |
|
77 |
* |
|
78 |
* @param theProp the name of the system property. |
|
79 |
*/ |
|
80 |
public GetIntegerAction(String theProp) { |
|
81 |
this.theProp = theProp; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Constructor that takes the name of the system property and the default |
|
86 |
* value of that property. |
|
87 |
* |
|
88 |
* @param theProp the name of the system property. |
|
30374 | 89 |
* @param defaultVal the default value. |
2 | 90 |
*/ |
91 |
public GetIntegerAction(String theProp, int defaultVal) { |
|
92 |
this.theProp = theProp; |
|
93 |
this.defaultVal = defaultVal; |
|
94 |
this.defaultSet = true; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Determines the integer value of the system property whose name was |
|
99 |
* specified in the constructor. |
|
100 |
* |
|
101 |
* <p>If there is no property of the specified name, or if the property |
|
102 |
* does not have the correct numeric format, then an <code>Integer</code> |
|
103 |
* object representing the default value that was specified in the |
|
104 |
* constructor is returned, or <code>null</code> if no default value was |
|
105 |
* specified. |
|
106 |
* |
|
107 |
* @return the <code>Integer</code> value of the property. |
|
108 |
*/ |
|
109 |
public Integer run() { |
|
110 |
Integer value = Integer.getInteger(theProp); |
|
111 |
if ((value == null) && defaultSet) |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
112 |
return defaultVal; |
2 | 113 |
return value; |
114 |
} |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
115 |
|
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
116 |
/** |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
117 |
* 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
|
118 |
* 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
|
119 |
* 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
|
120 |
* |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
121 |
* @param theProp the name of the system property. |
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 |
public static Integer getProperty(String theProp) { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
124 |
if (System.getSecurityManager() == null) { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
125 |
return Integer.getInteger(theProp); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
126 |
} else { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
127 |
return AccessController.doPrivileged( |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
128 |
new GetIntegerAction(theProp)); |
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 |
} |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
131 |
|
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
132 |
/** |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
133 |
* 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
|
134 |
* 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
|
135 |
* 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
|
136 |
* |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
137 |
* @param theProp the name of the system property. |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
138 |
* @param defaultVal the default value. |
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 |
public static Integer getProperty(String theProp, int defaultVal) { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
141 |
Integer value; |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
142 |
if (System.getSecurityManager() == null) { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
143 |
value = Integer.getInteger(theProp); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
144 |
} else { |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
145 |
value = AccessController.doPrivileged( |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
146 |
new GetIntegerAction(theProp)); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
147 |
} |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
148 |
return (value != null) ? value : defaultVal; |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
30374
diff
changeset
|
149 |
} |
2 | 150 |
} |