6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
|
import java.util.*; |
9 |
+ |
import java.security.AccessControlContext; |
10 |
+ |
import java.security.AccessController; |
11 |
+ |
import java.security.PrivilegedAction; |
12 |
+ |
import java.security.PrivilegedExceptionAction; |
13 |
|
|
14 |
|
/** |
15 |
|
* Factory and utility methods for {@link Executor}, {@link |
237 |
|
return ftask.get(); |
238 |
|
} |
239 |
|
|
240 |
+ |
|
241 |
+ |
/** |
242 |
+ |
* Executes a privileged action under the current access control |
243 |
+ |
* context and returns a Future representing the pending result |
244 |
+ |
* object of that action. |
245 |
+ |
* |
246 |
+ |
* @param executor the Executor to which the task will be submitted |
247 |
+ |
* @param action the action to submit |
248 |
+ |
* @return a Future representing pending completion of the action |
249 |
+ |
* @throws RejectedExecutionException if action cannot be scheduled |
250 |
+ |
* for execution |
251 |
+ |
*/ |
252 |
+ |
public static Future<Object> execute(Executor executor, PrivilegedAction action) { |
253 |
+ |
return execute(executor, action, AccessController.getContext()); |
254 |
+ |
} |
255 |
+ |
|
256 |
+ |
/** |
257 |
+ |
* Executes a privileged action under the given access control |
258 |
+ |
* context and returns a Future representing the pending result |
259 |
+ |
* object of that action. |
260 |
+ |
* |
261 |
+ |
* @param executor the Executor to which the task will be submitted |
262 |
+ |
* @param action the action to submit |
263 |
+ |
* @param acc the access control context under which action should run |
264 |
+ |
* @return a Future representing pending completion of the action |
265 |
+ |
* @throws RejectedExecutionException if action cannot be scheduled |
266 |
+ |
* for execution |
267 |
+ |
*/ |
268 |
+ |
public static Future<Object> execute(Executor executor, PrivilegedAction action, |
269 |
+ |
AccessControlContext acc) { |
270 |
+ |
Callable<Object> task = new PrivilegedActionAdapter(action); |
271 |
+ |
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task, acc); |
272 |
+ |
executor.execute(future); |
273 |
+ |
return future; |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
/** |
277 |
+ |
* Executes a privileged exception action under the current access control |
278 |
+ |
* context and returns a Future representing the pending result |
279 |
+ |
* object of that action. |
280 |
+ |
* |
281 |
+ |
* @param executor the Executor to which the task will be submitted |
282 |
+ |
* @param action the action to submit |
283 |
+ |
* @return a Future representing pending completion of the action |
284 |
+ |
* @throws RejectedExecutionException if action cannot be scheduled |
285 |
+ |
* for execution |
286 |
+ |
*/ |
287 |
+ |
public static Future<Object> execute(Executor executor, PrivilegedExceptionAction action) { |
288 |
+ |
return execute(executor, action, AccessController.getContext()); |
289 |
+ |
} |
290 |
+ |
|
291 |
+ |
/** |
292 |
+ |
* Executes a privileged exception action under the given access control |
293 |
+ |
* context and returns a Future representing the pending result |
294 |
+ |
* object of that action. |
295 |
+ |
* |
296 |
+ |
* @param executor the Executor to which the task will be submitted |
297 |
+ |
* @param action the action to submit |
298 |
+ |
* @param acc the access control context under which action should run |
299 |
+ |
* @return a Future representing pending completion of the action |
300 |
+ |
* @throws RejectedExecutionException if action cannot be scheduled |
301 |
+ |
* for execution |
302 |
+ |
*/ |
303 |
+ |
public static Future<Object> execute(Executor executor, PrivilegedExceptionAction action, |
304 |
+ |
AccessControlContext acc) { |
305 |
+ |
Callable<Object> task = new PrivilegedExceptionActionAdapter(action); |
306 |
+ |
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task, acc); |
307 |
+ |
executor.execute(future); |
308 |
+ |
return future; |
309 |
+ |
} |
310 |
|
|
311 |
+ |
|
312 |
+ |
private static class PrivilegedActionAdapter implements Callable<Object> { |
313 |
+ |
PrivilegedActionAdapter(PrivilegedAction action) { |
314 |
+ |
this.action = action; |
315 |
+ |
} |
316 |
+ |
public Object call () { |
317 |
+ |
return action.run(); |
318 |
+ |
} |
319 |
+ |
private final PrivilegedAction action; |
320 |
+ |
} |
321 |
+ |
|
322 |
+ |
private static class PrivilegedExceptionActionAdapter implements Callable<Object> { |
323 |
+ |
PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { |
324 |
+ |
this.action = action; |
325 |
+ |
} |
326 |
+ |
public Object call () throws Exception { |
327 |
+ |
return action.run(); |
328 |
+ |
} |
329 |
+ |
private final PrivilegedExceptionAction action; |
330 |
+ |
} |
331 |
+ |
|
332 |
+ |
|
333 |
|
/** Cannot instantiate. */ |
334 |
|
private Executors() {} |
335 |
|
} |