ExecutorWrapper.java revision 16234:3b25414eb6af
1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26package jdk.incubator.http;
27
28import java.net.SocketPermission;
29import java.security.AccessControlContext;
30import java.security.AccessController;
31import java.security.PrivilegedAction;
32import java.util.concurrent.Executor;
33import jdk.internal.misc.InnocuousThread;
34
35/**
36 * Wraps the supplied user Executor
37 *
38 * when a Security manager set, the correct access control context is used to execute task
39 *
40 * The access control context is captured at creation time of this object
41 */
42class ExecutorWrapper {
43
44    final Executor userExecutor; // the undeerlying executor provided by user
45    final Executor executor; // the executur which wraps the user's one
46    final AccessControlContext acc;
47    final ClassLoader ccl;
48
49    public ExecutorWrapper(Executor userExecutor, AccessControlContext acc) {
50        this.userExecutor = userExecutor;
51        this.acc = acc;
52        this.ccl = getCCL();
53        if (System.getSecurityManager() == null) {
54            this.executor = userExecutor;
55        } else {
56            this.executor = this::run;
57        }
58    }
59
60    private ClassLoader getCCL() {
61        return AccessController.doPrivileged(
62            (PrivilegedAction<ClassLoader>) () -> {
63                return Thread.currentThread().getContextClassLoader();
64            }
65        );
66    }
67
68    /**
69     * This is only used for the default HttpClient to deal with
70     * different application contexts that might be using it.
71     * The default client uses InnocuousThreads in its Executor.
72     */
73    private void prepareThread() {
74        final Thread me = Thread.currentThread();
75        if (!(me instanceof InnocuousThread))
76            return;
77        InnocuousThread innocuousMe = (InnocuousThread)me;
78
79        AccessController.doPrivileged(
80            (PrivilegedAction<Void>) () -> {
81                innocuousMe.setContextClassLoader(ccl);
82                innocuousMe.eraseThreadLocals();
83                return null;
84            }
85        );
86    }
87
88
89    void run(Runnable r) {
90        prepareThread();
91        try {
92            userExecutor.execute(r); // all throwables must be caught
93        } catch (Throwable t) {
94            t.printStackTrace();
95        }
96    }
97
98    public Executor userExecutor() {
99        return userExecutor;
100    }
101
102    public Executor executor() {
103        return executor;
104    }
105}
106