jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
15 |
* accompanied this code).
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
23 |
* questions.
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
package java.security;
|
jtulach@1334
|
27 |
|
jtulach@1334
|
28 |
/**
|
jtulach@1334
|
29 |
* <p> The AccessController class is used for access control operations
|
jtulach@1334
|
30 |
* and decisions.
|
jtulach@1334
|
31 |
*
|
jtulach@1334
|
32 |
* <p> More specifically, the AccessController class is used for
|
jtulach@1334
|
33 |
* three purposes:
|
jtulach@1334
|
34 |
*
|
jtulach@1334
|
35 |
* <ul>
|
jtulach@1334
|
36 |
* <li> to decide whether an access to a critical system
|
jtulach@1334
|
37 |
* resource is to be allowed or denied, based on the security policy
|
jtulach@1334
|
38 |
* currently in effect,<p>
|
jtulach@1334
|
39 |
* <li>to mark code as being "privileged", thus affecting subsequent
|
jtulach@1334
|
40 |
* access determinations, and<p>
|
jtulach@1334
|
41 |
* <li>to obtain a "snapshot" of the current calling context so
|
jtulach@1334
|
42 |
* access-control decisions from a different context can be made with
|
jtulach@1334
|
43 |
* respect to the saved context. </ul>
|
jtulach@1334
|
44 |
*
|
jtulach@1334
|
45 |
* <p> The {@link #checkPermission(Permission) checkPermission} method
|
jtulach@1334
|
46 |
* determines whether the access request indicated by a specified
|
jtulach@1334
|
47 |
* permission should be granted or denied. A sample call appears
|
jtulach@1334
|
48 |
* below. In this example, <code>checkPermission</code> will determine
|
jtulach@1334
|
49 |
* whether or not to grant "read" access to the file named "testFile" in
|
jtulach@1334
|
50 |
* the "/temp" directory.
|
jtulach@1334
|
51 |
*
|
jtulach@1334
|
52 |
* <pre>
|
jtulach@1334
|
53 |
*
|
jtulach@1334
|
54 |
* FilePermission perm = new FilePermission("/temp/testFile", "read");
|
jtulach@1334
|
55 |
* AccessController.checkPermission(perm);
|
jtulach@1334
|
56 |
*
|
jtulach@1334
|
57 |
* </pre>
|
jtulach@1334
|
58 |
*
|
jtulach@1334
|
59 |
* <p> If a requested access is allowed,
|
jtulach@1334
|
60 |
* <code>checkPermission</code> returns quietly. If denied, an
|
jtulach@1334
|
61 |
* AccessControlException is
|
jtulach@1334
|
62 |
* thrown. AccessControlException can also be thrown if the requested
|
jtulach@1334
|
63 |
* permission is of an incorrect type or contains an invalid value.
|
jtulach@1334
|
64 |
* Such information is given whenever possible.
|
jtulach@1334
|
65 |
*
|
jtulach@1334
|
66 |
* Suppose the current thread traversed m callers, in the order of caller 1
|
jtulach@1334
|
67 |
* to caller 2 to caller m. Then caller m invoked the
|
jtulach@1334
|
68 |
* <code>checkPermission</code> method.
|
jtulach@1334
|
69 |
* The <code>checkPermission </code>method determines whether access
|
jtulach@1334
|
70 |
* is granted or denied based on the following algorithm:
|
jtulach@1334
|
71 |
*
|
jtulach@1334
|
72 |
* <pre> {@code
|
jtulach@1334
|
73 |
* for (int i = m; i > 0; i--) {
|
jtulach@1334
|
74 |
*
|
jtulach@1334
|
75 |
* if (caller i's domain does not have the permission)
|
jtulach@1334
|
76 |
* throw AccessControlException
|
jtulach@1334
|
77 |
*
|
jtulach@1334
|
78 |
* else if (caller i is marked as privileged) {
|
jtulach@1334
|
79 |
* if (a context was specified in the call to doPrivileged)
|
jtulach@1334
|
80 |
* context.checkPermission(permission)
|
jtulach@1334
|
81 |
* return;
|
jtulach@1334
|
82 |
* }
|
jtulach@1334
|
83 |
* };
|
jtulach@1334
|
84 |
*
|
jtulach@1334
|
85 |
* // Next, check the context inherited when the thread was created.
|
jtulach@1334
|
86 |
* // Whenever a new thread is created, the AccessControlContext at
|
jtulach@1334
|
87 |
* // that time is stored and associated with the new thread, as the
|
jtulach@1334
|
88 |
* // "inherited" context.
|
jtulach@1334
|
89 |
*
|
jtulach@1334
|
90 |
* inheritedContext.checkPermission(permission);
|
jtulach@1334
|
91 |
* }</pre>
|
jtulach@1334
|
92 |
*
|
jtulach@1334
|
93 |
* <p> A caller can be marked as being "privileged"
|
jtulach@1334
|
94 |
* (see {@link #doPrivileged(PrivilegedAction) doPrivileged} and below).
|
jtulach@1334
|
95 |
* When making access control decisions, the <code>checkPermission</code>
|
jtulach@1334
|
96 |
* method stops checking if it reaches a caller that
|
jtulach@1334
|
97 |
* was marked as "privileged" via a <code>doPrivileged</code>
|
jtulach@1334
|
98 |
* call without a context argument (see below for information about a
|
jtulach@1334
|
99 |
* context argument). If that caller's domain has the
|
jtulach@1334
|
100 |
* specified permission, no further checking is done and
|
jtulach@1334
|
101 |
* <code>checkPermission</code>
|
jtulach@1334
|
102 |
* returns quietly, indicating that the requested access is allowed.
|
jtulach@1334
|
103 |
* If that domain does not have the specified permission, an exception
|
jtulach@1334
|
104 |
* is thrown, as usual.
|
jtulach@1334
|
105 |
*
|
jtulach@1334
|
106 |
* <p> The normal use of the "privileged" feature is as follows. If you
|
jtulach@1334
|
107 |
* don't need to return a value from within the "privileged" block, do
|
jtulach@1334
|
108 |
* the following:
|
jtulach@1334
|
109 |
*
|
jtulach@1334
|
110 |
* <pre> {@code
|
jtulach@1334
|
111 |
* somemethod() {
|
jtulach@1334
|
112 |
* ...normal code here...
|
jtulach@1334
|
113 |
* AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
jtulach@1334
|
114 |
* public Void run() {
|
jtulach@1334
|
115 |
* // privileged code goes here, for example:
|
jtulach@1334
|
116 |
* System.loadLibrary("awt");
|
jtulach@1334
|
117 |
* return null; // nothing to return
|
jtulach@1334
|
118 |
* }
|
jtulach@1334
|
119 |
* });
|
jtulach@1334
|
120 |
* ...normal code here...
|
jtulach@1334
|
121 |
* }}</pre>
|
jtulach@1334
|
122 |
*
|
jtulach@1334
|
123 |
* <p>
|
jtulach@1334
|
124 |
* PrivilegedAction is an interface with a single method, named
|
jtulach@1334
|
125 |
* <code>run</code>.
|
jtulach@1334
|
126 |
* The above example shows creation of an implementation
|
jtulach@1334
|
127 |
* of that interface; a concrete implementation of the
|
jtulach@1334
|
128 |
* <code>run</code> method is supplied.
|
jtulach@1334
|
129 |
* When the call to <code>doPrivileged</code> is made, an
|
jtulach@1334
|
130 |
* instance of the PrivilegedAction implementation is passed
|
jtulach@1334
|
131 |
* to it. The <code>doPrivileged</code> method calls the
|
jtulach@1334
|
132 |
* <code>run</code> method from the PrivilegedAction
|
jtulach@1334
|
133 |
* implementation after enabling privileges, and returns the
|
jtulach@1334
|
134 |
* <code>run</code> method's return value as the
|
jtulach@1334
|
135 |
* <code>doPrivileged</code> return value (which is
|
jtulach@1334
|
136 |
* ignored in this example).
|
jtulach@1334
|
137 |
*
|
jtulach@1334
|
138 |
* <p> If you need to return a value, you can do something like the following:
|
jtulach@1334
|
139 |
*
|
jtulach@1334
|
140 |
* <pre> {@code
|
jtulach@1334
|
141 |
* somemethod() {
|
jtulach@1334
|
142 |
* ...normal code here...
|
jtulach@1334
|
143 |
* String user = AccessController.doPrivileged(
|
jtulach@1334
|
144 |
* new PrivilegedAction<String>() {
|
jtulach@1334
|
145 |
* public String run() {
|
jtulach@1334
|
146 |
* return System.getProperty("user.name");
|
jtulach@1334
|
147 |
* }
|
jtulach@1334
|
148 |
* });
|
jtulach@1334
|
149 |
* ...normal code here...
|
jtulach@1334
|
150 |
* }}</pre>
|
jtulach@1334
|
151 |
*
|
jtulach@1334
|
152 |
* <p>If the action performed in your <code>run</code> method could
|
jtulach@1334
|
153 |
* throw a "checked" exception (those listed in the <code>throws</code> clause
|
jtulach@1334
|
154 |
* of a method), then you need to use the
|
jtulach@1334
|
155 |
* <code>PrivilegedExceptionAction</code> interface instead of the
|
jtulach@1334
|
156 |
* <code>PrivilegedAction</code> interface:
|
jtulach@1334
|
157 |
*
|
jtulach@1334
|
158 |
* <pre> {@code
|
jtulach@1334
|
159 |
* somemethod() throws FileNotFoundException {
|
jtulach@1334
|
160 |
* ...normal code here...
|
jtulach@1334
|
161 |
* try {
|
jtulach@1334
|
162 |
* FileInputStream fis = AccessController.doPrivileged(
|
jtulach@1334
|
163 |
* new PrivilegedExceptionAction<FileInputStream>() {
|
jtulach@1334
|
164 |
* public FileInputStream run() throws FileNotFoundException {
|
jtulach@1334
|
165 |
* return new FileInputStream("someFile");
|
jtulach@1334
|
166 |
* }
|
jtulach@1334
|
167 |
* });
|
jtulach@1334
|
168 |
* } catch (PrivilegedActionException e) {
|
jtulach@1334
|
169 |
* // e.getException() should be an instance of FileNotFoundException,
|
jtulach@1334
|
170 |
* // as only "checked" exceptions will be "wrapped" in a
|
jtulach@1334
|
171 |
* // PrivilegedActionException.
|
jtulach@1334
|
172 |
* throw (FileNotFoundException) e.getException();
|
jtulach@1334
|
173 |
* }
|
jtulach@1334
|
174 |
* ...normal code here...
|
jtulach@1334
|
175 |
* }}</pre>
|
jtulach@1334
|
176 |
*
|
jtulach@1334
|
177 |
* <p> Be *very* careful in your use of the "privileged" construct, and
|
jtulach@1334
|
178 |
* always remember to make the privileged code section as small as possible.
|
jtulach@1334
|
179 |
*
|
jtulach@1334
|
180 |
* <p> Note that <code>checkPermission</code> always performs security checks
|
jtulach@1334
|
181 |
* within the context of the currently executing thread.
|
jtulach@1334
|
182 |
* Sometimes a security check that should be made within a given context
|
jtulach@1334
|
183 |
* will actually need to be done from within a
|
jtulach@1334
|
184 |
* <i>different</i> context (for example, from within a worker thread).
|
jtulach@1334
|
185 |
* The {@link #getContext() getContext} method and
|
jtulach@1334
|
186 |
* AccessControlContext class are provided
|
jtulach@1334
|
187 |
* for this situation. The <code>getContext</code> method takes a "snapshot"
|
jtulach@1334
|
188 |
* of the current calling context, and places
|
jtulach@1334
|
189 |
* it in an AccessControlContext object, which it returns. A sample call is
|
jtulach@1334
|
190 |
* the following:
|
jtulach@1334
|
191 |
*
|
jtulach@1334
|
192 |
* <pre>
|
jtulach@1334
|
193 |
*
|
jtulach@1334
|
194 |
* AccessControlContext acc = AccessController.getContext()
|
jtulach@1334
|
195 |
*
|
jtulach@1334
|
196 |
* </pre>
|
jtulach@1334
|
197 |
*
|
jtulach@1334
|
198 |
* <p>
|
jtulach@1334
|
199 |
* AccessControlContext itself has a <code>checkPermission</code> method
|
jtulach@1334
|
200 |
* that makes access decisions based on the context <i>it</i> encapsulates,
|
jtulach@1334
|
201 |
* rather than that of the current execution thread.
|
jtulach@1334
|
202 |
* Code within a different context can thus call that method on the
|
jtulach@1334
|
203 |
* previously-saved AccessControlContext object. A sample call is the
|
jtulach@1334
|
204 |
* following:
|
jtulach@1334
|
205 |
*
|
jtulach@1334
|
206 |
* <pre>
|
jtulach@1334
|
207 |
*
|
jtulach@1334
|
208 |
* acc.checkPermission(permission)
|
jtulach@1334
|
209 |
*
|
jtulach@1334
|
210 |
* </pre>
|
jtulach@1334
|
211 |
*
|
jtulach@1334
|
212 |
* <p> There are also times where you don't know a priori which permissions
|
jtulach@1334
|
213 |
* to check the context against. In these cases you can use the
|
jtulach@1334
|
214 |
* doPrivileged method that takes a context:
|
jtulach@1334
|
215 |
*
|
jtulach@1334
|
216 |
* <pre> {@code
|
jtulach@1334
|
217 |
* somemethod() {
|
jtulach@1334
|
218 |
* AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
jtulach@1334
|
219 |
* public Object run() {
|
jtulach@1334
|
220 |
* // Code goes here. Any permission checks within this
|
jtulach@1334
|
221 |
* // run method will require that the intersection of the
|
jtulach@1334
|
222 |
* // callers protection domain and the snapshot's
|
jtulach@1334
|
223 |
* // context have the desired permission.
|
jtulach@1334
|
224 |
* }
|
jtulach@1334
|
225 |
* }, acc);
|
jtulach@1334
|
226 |
* ...normal code here...
|
jtulach@1334
|
227 |
* }}</pre>
|
jtulach@1334
|
228 |
*
|
jtulach@1334
|
229 |
* @see AccessControlContext
|
jtulach@1334
|
230 |
*
|
jtulach@1334
|
231 |
* @author Li Gong
|
jtulach@1334
|
232 |
* @author Roland Schemers
|
jtulach@1334
|
233 |
*/
|
jtulach@1334
|
234 |
|
jtulach@1334
|
235 |
public final class AccessController {
|
jtulach@1334
|
236 |
|
jtulach@1334
|
237 |
/**
|
jtulach@1334
|
238 |
* Don't allow anyone to instantiate an AccessController
|
jtulach@1334
|
239 |
*/
|
jtulach@1334
|
240 |
private AccessController() { }
|
jtulach@1334
|
241 |
|
jtulach@1334
|
242 |
/**
|
jtulach@1334
|
243 |
* Performs the specified <code>PrivilegedAction</code> with privileges
|
jtulach@1334
|
244 |
* enabled. The action is performed with <i>all</i> of the permissions
|
jtulach@1334
|
245 |
* possessed by the caller's protection domain.
|
jtulach@1334
|
246 |
*
|
jtulach@1334
|
247 |
* <p> If the action's <code>run</code> method throws an (unchecked)
|
jtulach@1334
|
248 |
* exception, it will propagate through this method.
|
jtulach@1334
|
249 |
*
|
jtulach@1334
|
250 |
* <p> Note that any DomainCombiner associated with the current
|
jtulach@1334
|
251 |
* AccessControlContext will be ignored while the action is performed.
|
jtulach@1334
|
252 |
*
|
jtulach@1334
|
253 |
* @param action the action to be performed.
|
jtulach@1334
|
254 |
*
|
jtulach@1334
|
255 |
* @return the value returned by the action's <code>run</code> method.
|
jtulach@1334
|
256 |
*
|
jtulach@1334
|
257 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
258 |
*
|
jtulach@1334
|
259 |
* @see #doPrivileged(PrivilegedAction,AccessControlContext)
|
jtulach@1334
|
260 |
* @see #doPrivileged(PrivilegedExceptionAction)
|
jtulach@1334
|
261 |
* @see #doPrivilegedWithCombiner(PrivilegedAction)
|
jtulach@1334
|
262 |
* @see java.security.DomainCombiner
|
jtulach@1334
|
263 |
*/
|
jtulach@1334
|
264 |
|
jaroslav@1341
|
265 |
public static <T> T doPrivileged(PrivilegedAction<T> action) {
|
jaroslav@1341
|
266 |
return action.run();
|
jaroslav@1341
|
267 |
}
|
jtulach@1334
|
268 |
|
jtulach@1334
|
269 |
/**
|
jtulach@1334
|
270 |
* Performs the specified <code>PrivilegedAction</code> with privileges
|
jtulach@1334
|
271 |
* enabled. The action is performed with <i>all</i> of the permissions
|
jtulach@1334
|
272 |
* possessed by the caller's protection domain.
|
jtulach@1334
|
273 |
*
|
jtulach@1334
|
274 |
* <p> If the action's <code>run</code> method throws an (unchecked)
|
jtulach@1334
|
275 |
* exception, it will propagate through this method.
|
jtulach@1334
|
276 |
*
|
jtulach@1334
|
277 |
* <p> This method preserves the current AccessControlContext's
|
jtulach@1334
|
278 |
* DomainCombiner (which may be null) while the action is performed.
|
jtulach@1334
|
279 |
*
|
jtulach@1334
|
280 |
* @param action the action to be performed.
|
jtulach@1334
|
281 |
*
|
jtulach@1334
|
282 |
* @return the value returned by the action's <code>run</code> method.
|
jtulach@1334
|
283 |
*
|
jtulach@1334
|
284 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
285 |
*
|
jtulach@1334
|
286 |
* @see #doPrivileged(PrivilegedAction)
|
jtulach@1334
|
287 |
* @see java.security.DomainCombiner
|
jtulach@1334
|
288 |
*
|
jtulach@1334
|
289 |
* @since 1.6
|
jtulach@1334
|
290 |
*/
|
jtulach@1334
|
291 |
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
|
jaroslav@1341
|
292 |
return action.run();
|
jtulach@1334
|
293 |
}
|
jtulach@1334
|
294 |
|
jtulach@1334
|
295 |
|
jtulach@1334
|
296 |
/**
|
jtulach@1334
|
297 |
* Performs the specified <code>PrivilegedAction</code> with privileges
|
jtulach@1334
|
298 |
* enabled and restricted by the specified
|
jtulach@1334
|
299 |
* <code>AccessControlContext</code>.
|
jtulach@1334
|
300 |
* The action is performed with the intersection of the permissions
|
jtulach@1334
|
301 |
* possessed by the caller's protection domain, and those possessed
|
jtulach@1334
|
302 |
* by the domains represented by the specified
|
jtulach@1334
|
303 |
* <code>AccessControlContext</code>.
|
jtulach@1334
|
304 |
* <p>
|
jtulach@1334
|
305 |
* If the action's <code>run</code> method throws an (unchecked) exception,
|
jtulach@1334
|
306 |
* it will propagate through this method.
|
jtulach@1334
|
307 |
*
|
jtulach@1334
|
308 |
* @param action the action to be performed.
|
jtulach@1334
|
309 |
* @param context an <i>access control context</i>
|
jtulach@1334
|
310 |
* representing the restriction to be applied to the
|
jtulach@1334
|
311 |
* caller's domain's privileges before performing
|
jtulach@1334
|
312 |
* the specified action. If the context is
|
jtulach@1334
|
313 |
* <code>null</code>,
|
jtulach@1334
|
314 |
* then no additional restriction is applied.
|
jtulach@1334
|
315 |
*
|
jtulach@1334
|
316 |
* @return the value returned by the action's <code>run</code> method.
|
jtulach@1334
|
317 |
*
|
jtulach@1334
|
318 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
319 |
*
|
jtulach@1334
|
320 |
* @see #doPrivileged(PrivilegedAction)
|
jtulach@1334
|
321 |
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
|
jtulach@1334
|
322 |
*/
|
jaroslav@1341
|
323 |
// public static native <T> T doPrivileged(PrivilegedAction<T> action,
|
jaroslav@1341
|
324 |
// AccessControlContext context);
|
jtulach@1334
|
325 |
|
jtulach@1334
|
326 |
/**
|
jtulach@1334
|
327 |
* Performs the specified <code>PrivilegedExceptionAction</code> with
|
jtulach@1334
|
328 |
* privileges enabled. The action is performed with <i>all</i> of the
|
jtulach@1334
|
329 |
* permissions possessed by the caller's protection domain.
|
jtulach@1334
|
330 |
*
|
jtulach@1334
|
331 |
* <p> If the action's <code>run</code> method throws an <i>unchecked</i>
|
jtulach@1334
|
332 |
* exception, it will propagate through this method.
|
jtulach@1334
|
333 |
*
|
jtulach@1334
|
334 |
* <p> Note that any DomainCombiner associated with the current
|
jtulach@1334
|
335 |
* AccessControlContext will be ignored while the action is performed.
|
jtulach@1334
|
336 |
*
|
jtulach@1334
|
337 |
* @param action the action to be performed
|
jtulach@1334
|
338 |
*
|
jtulach@1334
|
339 |
* @return the value returned by the action's <code>run</code> method
|
jtulach@1334
|
340 |
*
|
jtulach@1334
|
341 |
* @exception PrivilegedActionException if the specified action's
|
jtulach@1334
|
342 |
* <code>run</code> method threw a <i>checked</i> exception
|
jtulach@1334
|
343 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
344 |
*
|
jtulach@1334
|
345 |
* @see #doPrivileged(PrivilegedAction)
|
jtulach@1334
|
346 |
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
|
jtulach@1334
|
347 |
* @see #doPrivilegedWithCombiner(PrivilegedExceptionAction)
|
jtulach@1334
|
348 |
* @see java.security.DomainCombiner
|
jtulach@1334
|
349 |
*/
|
jaroslav@1341
|
350 |
public static <T> T
|
jtulach@1334
|
351 |
doPrivileged(PrivilegedExceptionAction<T> action)
|
jaroslav@1341
|
352 |
throws PrivilegedActionException {
|
jaroslav@1341
|
353 |
try {
|
jaroslav@1341
|
354 |
return action.run();
|
jaroslav@1341
|
355 |
} catch (Exception ex) {
|
jaroslav@1341
|
356 |
throw new PrivilegedActionException(ex);
|
jaroslav@1341
|
357 |
}
|
jaroslav@1341
|
358 |
}
|
jtulach@1334
|
359 |
|
jtulach@1334
|
360 |
|
jtulach@1334
|
361 |
/**
|
jtulach@1334
|
362 |
* Performs the specified <code>PrivilegedExceptionAction</code> with
|
jtulach@1334
|
363 |
* privileges enabled. The action is performed with <i>all</i> of the
|
jtulach@1334
|
364 |
* permissions possessed by the caller's protection domain.
|
jtulach@1334
|
365 |
*
|
jtulach@1334
|
366 |
* <p> If the action's <code>run</code> method throws an <i>unchecked</i>
|
jtulach@1334
|
367 |
* exception, it will propagate through this method.
|
jtulach@1334
|
368 |
*
|
jtulach@1334
|
369 |
* <p> This method preserves the current AccessControlContext's
|
jtulach@1334
|
370 |
* DomainCombiner (which may be null) while the action is performed.
|
jtulach@1334
|
371 |
*
|
jtulach@1334
|
372 |
* @param action the action to be performed.
|
jtulach@1334
|
373 |
*
|
jtulach@1334
|
374 |
* @return the value returned by the action's <code>run</code> method
|
jtulach@1334
|
375 |
*
|
jtulach@1334
|
376 |
* @exception PrivilegedActionException if the specified action's
|
jtulach@1334
|
377 |
* <code>run</code> method threw a <i>checked</i> exception
|
jtulach@1334
|
378 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
379 |
*
|
jtulach@1334
|
380 |
* @see #doPrivileged(PrivilegedAction)
|
jtulach@1334
|
381 |
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
|
jtulach@1334
|
382 |
* @see java.security.DomainCombiner
|
jtulach@1334
|
383 |
*
|
jtulach@1334
|
384 |
* @since 1.6
|
jtulach@1334
|
385 |
*/
|
jtulach@1334
|
386 |
public static <T> T doPrivilegedWithCombiner
|
jtulach@1334
|
387 |
(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
|
jaroslav@1341
|
388 |
return doPrivileged(action);
|
jtulach@1334
|
389 |
}
|
jtulach@1334
|
390 |
|
jtulach@1334
|
391 |
/**
|
jtulach@1334
|
392 |
* Performs the specified <code>PrivilegedExceptionAction</code> with
|
jtulach@1334
|
393 |
* privileges enabled and restricted by the specified
|
jtulach@1334
|
394 |
* <code>AccessControlContext</code>. The action is performed with the
|
jtulach@1334
|
395 |
* intersection of the permissions possessed by the caller's
|
jtulach@1334
|
396 |
* protection domain, and those possessed by the domains represented by the
|
jtulach@1334
|
397 |
* specified <code>AccessControlContext</code>.
|
jtulach@1334
|
398 |
* <p>
|
jtulach@1334
|
399 |
* If the action's <code>run</code> method throws an <i>unchecked</i>
|
jtulach@1334
|
400 |
* exception, it will propagate through this method.
|
jtulach@1334
|
401 |
*
|
jtulach@1334
|
402 |
* @param action the action to be performed
|
jtulach@1334
|
403 |
* @param context an <i>access control context</i>
|
jtulach@1334
|
404 |
* representing the restriction to be applied to the
|
jtulach@1334
|
405 |
* caller's domain's privileges before performing
|
jtulach@1334
|
406 |
* the specified action. If the context is
|
jtulach@1334
|
407 |
* <code>null</code>,
|
jtulach@1334
|
408 |
* then no additional restriction is applied.
|
jtulach@1334
|
409 |
*
|
jtulach@1334
|
410 |
* @return the value returned by the action's <code>run</code> method
|
jtulach@1334
|
411 |
*
|
jtulach@1334
|
412 |
* @exception PrivilegedActionException if the specified action's
|
jtulach@1334
|
413 |
* <code>run</code> method
|
jtulach@1334
|
414 |
* threw a <i>checked</i> exception
|
jtulach@1334
|
415 |
* @exception NullPointerException if the action is <code>null</code>
|
jtulach@1334
|
416 |
*
|
jtulach@1334
|
417 |
* @see #doPrivileged(PrivilegedAction)
|
jtulach@1334
|
418 |
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
|
jtulach@1334
|
419 |
*/
|
jaroslav@1341
|
420 |
// public static native <T> T
|
jaroslav@1341
|
421 |
// doPrivileged(PrivilegedExceptionAction<T> action,
|
jaroslav@1341
|
422 |
// AccessControlContext context)
|
jaroslav@1341
|
423 |
// throws PrivilegedActionException;
|
jtulach@1334
|
424 |
|
jtulach@1334
|
425 |
/**
|
jtulach@1334
|
426 |
* This method takes a "snapshot" of the current calling context, which
|
jtulach@1334
|
427 |
* includes the current Thread's inherited AccessControlContext,
|
jtulach@1334
|
428 |
* and places it in an AccessControlContext object. This context may then
|
jtulach@1334
|
429 |
* be checked at a later point, possibly in another thread.
|
jtulach@1334
|
430 |
*
|
jtulach@1334
|
431 |
* @see AccessControlContext
|
jtulach@1334
|
432 |
*
|
jtulach@1334
|
433 |
* @return the AccessControlContext based on the current context.
|
jtulach@1334
|
434 |
*/
|
jtulach@1334
|
435 |
|
jaroslav@1341
|
436 |
// public static AccessControlContext getContext()
|
jaroslav@1341
|
437 |
// {
|
jaroslav@1341
|
438 |
// AccessControlContext acc = getStackAccessControlContext();
|
jaroslav@1341
|
439 |
// if (acc == null) {
|
jaroslav@1341
|
440 |
// // all we had was privileged system code. We don't want
|
jaroslav@1341
|
441 |
// // to return null though, so we construct a real ACC.
|
jaroslav@1341
|
442 |
// return new AccessControlContext(null, true);
|
jaroslav@1341
|
443 |
// } else {
|
jaroslav@1341
|
444 |
// return acc.optimize();
|
jaroslav@1341
|
445 |
// }
|
jaroslav@1341
|
446 |
// }
|
jtulach@1334
|
447 |
|
jtulach@1334
|
448 |
/**
|
jtulach@1334
|
449 |
* Determines whether the access request indicated by the
|
jtulach@1334
|
450 |
* specified permission should be allowed or denied, based on
|
jtulach@1334
|
451 |
* the current AccessControlContext and security policy.
|
jtulach@1334
|
452 |
* This method quietly returns if the access request
|
jtulach@1334
|
453 |
* is permitted, or throws an AccessControlException otherwise. The
|
jtulach@1334
|
454 |
* getPermission method of the AccessControlException returns the
|
jtulach@1334
|
455 |
* <code>perm</code> Permission object instance.
|
jtulach@1334
|
456 |
*
|
jtulach@1334
|
457 |
* @param perm the requested permission.
|
jtulach@1334
|
458 |
*
|
jtulach@1334
|
459 |
* @exception AccessControlException if the specified permission
|
jtulach@1334
|
460 |
* is not permitted, based on the current security policy.
|
jtulach@1334
|
461 |
* @exception NullPointerException if the specified permission
|
jtulach@1334
|
462 |
* is <code>null</code> and is checked based on the
|
jtulach@1334
|
463 |
* security policy currently in effect.
|
jtulach@1334
|
464 |
*/
|
jtulach@1334
|
465 |
|
jaroslav@1341
|
466 |
// public static void checkPermission(Permission perm)
|
jaroslav@1341
|
467 |
// throws AccessControlException
|
jaroslav@1341
|
468 |
// {
|
jaroslav@1341
|
469 |
// //System.err.println("checkPermission "+perm);
|
jaroslav@1341
|
470 |
// //Thread.currentThread().dumpStack();
|
jaroslav@1341
|
471 |
//
|
jaroslav@1341
|
472 |
// if (perm == null) {
|
jaroslav@1341
|
473 |
// throw new NullPointerException("permission can't be null");
|
jaroslav@1341
|
474 |
// }
|
jaroslav@1341
|
475 |
//
|
jaroslav@1341
|
476 |
// AccessControlContext stack = getStackAccessControlContext();
|
jaroslav@1341
|
477 |
// // if context is null, we had privileged system code on the stack.
|
jaroslav@1341
|
478 |
// if (stack == null) {
|
jaroslav@1341
|
479 |
// Debug debug = AccessControlContext.getDebug();
|
jaroslav@1341
|
480 |
// boolean dumpDebug = false;
|
jaroslav@1341
|
481 |
// if (debug != null) {
|
jaroslav@1341
|
482 |
// dumpDebug = !Debug.isOn("codebase=");
|
jaroslav@1341
|
483 |
// dumpDebug &= !Debug.isOn("permission=") ||
|
jaroslav@1341
|
484 |
// Debug.isOn("permission=" + perm.getClass().getCanonicalName());
|
jaroslav@1341
|
485 |
// }
|
jaroslav@1341
|
486 |
//
|
jaroslav@1341
|
487 |
// if (dumpDebug && Debug.isOn("stack")) {
|
jaroslav@1341
|
488 |
// Thread.currentThread().dumpStack();
|
jaroslav@1341
|
489 |
// }
|
jaroslav@1341
|
490 |
//
|
jaroslav@1341
|
491 |
// if (dumpDebug && Debug.isOn("domain")) {
|
jaroslav@1341
|
492 |
// debug.println("domain (context is null)");
|
jaroslav@1341
|
493 |
// }
|
jaroslav@1341
|
494 |
//
|
jaroslav@1341
|
495 |
// if (dumpDebug) {
|
jaroslav@1341
|
496 |
// debug.println("access allowed "+perm);
|
jaroslav@1341
|
497 |
// }
|
jaroslav@1341
|
498 |
// return;
|
jaroslav@1341
|
499 |
// }
|
jaroslav@1341
|
500 |
//
|
jaroslav@1341
|
501 |
// AccessControlContext acc = stack.optimize();
|
jaroslav@1341
|
502 |
// acc.checkPermission(perm);
|
jaroslav@1341
|
503 |
// }
|
jtulach@1334
|
504 |
}
|