ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/PrivilegedFutureTask.java
Revision: 1.1
Committed: Sat Oct 25 04:23:25 2003 UTC (20 years, 7 months ago) by tim
Branch: MAIN
Log Message:
Add PrivilegedFutureTask along with related methods in Executors.

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     * A {@link FutureTask} that executes with the given (or current, if
16     * not specified) access control context and/or the given (or current)
17     * context class loader.
18     *
19     * @since 1.5
20     * @author Doug Lea
21     */
22     public class PrivilegedFutureTask<T> extends FutureTask<T> {
23    
24     /**
25     * Constructs a <tt>PrivilegedFutureTask</tt> that will, upon running,
26     * execute the given <tt>Callable</tt> under the current access control
27     * context, with the current context class loader as the context class
28     * loader.
29     *
30     * @throws AccessControlException if the current access control context
31     * does not have permission to both set and get context class loader.
32     */
33     public PrivilegedFutureTask(Callable<T> task) {
34     this(task,
35     Thread.currentThread().getContextClassLoader(),
36     AccessController.getContext());
37     }
38    
39     /**
40     * Constructs a <tt>PrivilegedFutureTask</tt> that will, upon running,
41     * execute the given <tt>Callable</tt> under the current access control
42     * context, with the given class loader as the context class loader,
43     *
44     * @throws AccessControlException if <tt>ccl</tt> is non-null and
45     * the current access control context does not have permission
46     * to both set and get context class loader.
47     */
48     public PrivilegedFutureTask(Callable<T> task, ClassLoader ccl) {
49     this(task, ccl, AccessController.getContext());
50     }
51    
52     /**
53     * Constructs a <tt>PrivilegedFutureTask</tt> that will, upon running,
54     * execute the given <tt>Callable</tt> with the current context class
55     * loader as the context class loader and the given access control
56     * context as the current access control context.
57     *
58     * @throws AccessControlException if <tt>acc</tt>is non-null and
59     * <tt>acc</tt> does not have permission to both set and get
60     * context class loader.
61     */
62     public PrivilegedFutureTask(Callable<T> task, AccessControlContext acc) {
63     this(task, Thread.currentThread().getContextClassLoader(), acc);
64     }
65    
66     /**
67     * Constructs a <tt>PrivilegedFutureTask</tt> that will, upon running,
68     * execute the given <tt>Callable</tt> with the given class loader as
69     * the context class loader and the given access control context as the
70     * current access control context.
71     *
72     * @throws AccessControlException if both <tt>ccl</tt> and <tt>acc</tt>
73     * arguments are non-null and <tt>acc</tt> does not have permission
74     * to both set and get context class loader.
75     */
76     public PrivilegedFutureTask(Callable<T> task, ClassLoader ccl, AccessControlContext acc) {
77     super(task);
78     if (ccl != null && acc != null) {
79     acc.checkPermission(new RuntimePermission("getContextClassLoader"));
80     acc.checkPermission(new RuntimePermission("setContextClassLoader"));
81     }
82     this.ccl = ccl;
83     this.acc = acc;
84     }
85    
86    
87     public void run() {
88     if (acc != null)
89     AccessController.doPrivileged(new PrivilegedAction() {
90     public Object run() {
91     runPrivileged();
92     return null;
93     }
94     }, acc);
95     else
96     runUnprivileged();
97     }
98    
99    
100     private void runPrivileged() {
101     ClassLoader saved = null;
102     if (ccl != null) {
103     ClassLoader current = Thread.currentThread().getContextClassLoader();
104     if (ccl != current) {
105     Thread.currentThread().setContextClassLoader(ccl);
106     saved = current;
107     }
108     }
109    
110     try {
111     super.run();
112     }
113     finally {
114     if (saved != null)
115     Thread.currentThread().setContextClassLoader(saved);
116     }
117     }
118    
119     private void runUnprivileged() {
120     ClassLoader saved = null;
121     if (ccl != null) {
122     ClassLoader current = null;
123     try {
124     current = Thread.currentThread().getContextClassLoader();
125     }
126     catch (AccessControlException e) {}
127    
128     if (current != null && ccl != current) {
129     try {
130     Thread.currentThread().setContextClassLoader(ccl);
131     // we only get here if we successfully set a CCL
132     // different from the current CCL
133     saved = current;
134     }
135     catch (AccessControlException e) {}
136     }
137     }
138     try {
139     super.run();
140     }
141     finally {
142     if (saved != null)
143     Thread.currentThread().setContextClassLoader(saved);
144     }
145     }
146    
147     private final ClassLoader ccl;
148     private final AccessControlContext acc;
149     }
150