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 26 package java.net.http; 27 28 import java.security.AccessControlContext; 29 import java.security.AccessController; 30 import java.security.PrivilegedAction; 31 import java.util.concurrent.Executor; 32 import java.util.concurrent.ExecutorService; 33 import java.util.function.Supplier; 34 35 /** 36 * Wraps the supplied user ExecutorService. 37 * 38 * 1) when a Security manager set, the correct access control context 39 * is used to execute task 40 * 41 * 2) memory fence implemented 42 */ 43 class ExecutorWrapper { 44 45 final ExecutorService userExecutor; // the actual executor service used 46 final Executor executor; 47 48 public static ExecutorWrapper wrap(ExecutorService userExecutor) { 49 return new ExecutorWrapper(userExecutor); 50 } 51 52 /** 53 * Returns a dummy ExecutorWrapper which uses the calling thread 54 */ 55 public static ExecutorWrapper callingThread() { 56 return new ExecutorWrapper(); 57 } 58 59 private ExecutorWrapper(ExecutorService userExecutor) { 60 // used for executing in calling thread 61 this.userExecutor = userExecutor; 62 this.executor = userExecutor; 63 } 64 65 private ExecutorWrapper() { 66 this.userExecutor = null; 67 this.executor = (Runnable command) -> { 68 command.run(); 69 }; 70 } 71 72 public ExecutorService userExecutor() { 73 return userExecutor; 74 } 75 76 public synchronized void synchronize() {} 77 78 public void execute(Runnable r, Supplier<AccessControlContext> ctxSupplier) { 79 synchronize(); 80 Runnable r1 = () -> { 81 try { 82 r.run(); 83 } catch (Throwable t) { 84 Log.logError(t); 85 } 86 }; 87 88 if (ctxSupplier != null && System.getSecurityManager() != null) { 89 AccessControlContext acc = ctxSupplier.get(); 90 if (acc == null) { 91 throw new InternalError(); 92 } 93 AccessController.doPrivilegedWithCombiner( 94 (PrivilegedAction<Void>)() -> { 95 executor.execute(r1); // all throwables must be caught 96 return null; 97 }, acc); 98 } else { 99 executor.execute(r1); // all throwables must be caught 100 } 101 } 102 }