ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/PrivilegedFutureTask.java
Revision: 1.4
Committed: Sat Dec 20 14:00:05 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -0 lines
State: FILE REMOVED
Log Message:
Replace PrivilegedFutureTask with Executors.privilegedCallable

File Contents

# User Rev Content
1 tim 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain. Use, modify, and
4     * redistribute this code in any way without acknowledgement.
5     */
6    
7     package java.util.concurrent;
8    
9     import java.security.AccessControlContext;
10     import java.security.AccessControlException;
11     import java.security.AccessController;
12     import java.security.PrivilegedAction;
13    
14     /**
15 dl 1.3 * A {@link FutureTask} that executes, if allowed, with the current
16     * access control context and context class loader of the thread
17     * creating the task. A new <tt>PrivilegedFutureTask</tt>
18     * can be created within an {@link AccessController#doPrivileged}
19     * action to run tasks under the selected permission settings
20     * holding within that action.
21     * @see Executors
22 tim 1.1 *
23     * @since 1.5
24     * @author Doug Lea
25     */
26     public class PrivilegedFutureTask<T> extends FutureTask<T> {
27 dl 1.3 private final ClassLoader ccl;
28     private final AccessControlContext acc;
29 tim 1.1
30     /**
31     * Constructs a <tt>PrivilegedFutureTask</tt> that will, upon running,
32     * execute the given <tt>Callable</tt> under the current access control
33     * context, with the current context class loader as the context class
34     * loader.
35     *
36     * @throws AccessControlException if the current access control context
37     * does not have permission to both set and get context class loader.
38     */
39     public PrivilegedFutureTask(Callable<T> task) {
40     super(task);
41 tim 1.2 this.ccl = Thread.currentThread().getContextClassLoader();
42     this.acc = AccessController.getContext();
43     acc.checkPermission(new RuntimePermission("getContextClassLoader"));
44     acc.checkPermission(new RuntimePermission("setContextClassLoader"));
45 tim 1.1 }
46    
47 dl 1.3 /**
48     * Executes within the established access control and context
49     * class loader if possible, else causes invocations of {@link
50     * Future#get} to receive the associated AccessControlException.
51     */
52 tim 1.1 public void run() {
53 dl 1.3 try {
54     AccessController.doPrivileged(new PrivilegedFutureAction(), acc);
55     } catch(Throwable ex) {
56     setException(ex);
57     }
58 tim 1.1 }
59    
60 dl 1.3 private class PrivilegedFutureAction implements PrivilegedAction {
61     public Object run() {
62     ClassLoader saved = null;
63     try {
64     ClassLoader current = Thread.currentThread().getContextClassLoader();
65     if (ccl != current) {
66     Thread.currentThread().setContextClassLoader(ccl);
67     saved = current;
68     }
69     PrivilegedFutureTask.super.run();
70     return null;
71     } finally {
72     if (saved != null)
73     Thread.currentThread().setContextClassLoader(saved);
74     }
75     }
76 tim 1.1 }
77     }