1 /* 2 * Copyright (c) 2008, 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4607272 27 * @summary tests tasks can be submitted to a channel group's thread pool. 28 * @library /lib/testlibrary bootlib 29 * @build JarUtils PrivilegedThreadFactory Attack 30 * @run driver SetupJar 31 * @run main/othervm -Xbootclasspath/a:privileged.jar AsExecutor 32 */ 33 34 import java.nio.channels.AsynchronousChannelGroup; 35 import java.util.concurrent.CountDownLatch; 36 import java.util.concurrent.Executor; 37 import java.util.concurrent.Executors; 38 import java.util.concurrent.ThreadFactory; 39 40 public class AsExecutor { 41 42 public static void main(String[] args) throws Exception { 43 // create channel groups 44 ThreadFactory factory = new PrivilegedThreadFactory(); 45 AsynchronousChannelGroup group1 = AsynchronousChannelGroup 46 .withFixedThreadPool(5, factory); 47 AsynchronousChannelGroup group2 = AsynchronousChannelGroup 48 .withCachedThreadPool(Executors.newCachedThreadPool(factory), 0); 49 AsynchronousChannelGroup group3 = AsynchronousChannelGroup 50 .withThreadPool(Executors.newFixedThreadPool(10, factory)); 51 52 try { 53 // execute simple tasks 54 testSimpleTask(group1); 55 testSimpleTask(group2); 56 testSimpleTask(group3); 57 58 // install security manager and test again 59 System.setSecurityManager( new SecurityManager() ); 60 testSimpleTask(group1); 61 testSimpleTask(group2); 62 testSimpleTask(group3); 63 64 // attempt to execute tasks that run with only frames from boot 65 // class loader on the stack. 66 testAttackingTask(group1); 67 testAttackingTask(group2); 68 testAttackingTask(group3); 69 } finally { 70 group1.shutdown(); 71 group2.shutdown(); 72 group3.shutdown(); 73 } 74 } 75 76 static void testSimpleTask(AsynchronousChannelGroup group) throws Exception { 77 Executor executor = (Executor)group; 78 final CountDownLatch latch = new CountDownLatch(1); 79 executor.execute(new Runnable() { 80 public void run() { 81 latch.countDown(); 82 } 83 }); 84 latch.await(); 85 } 86 87 static void testAttackingTask(AsynchronousChannelGroup group) throws Exception { 88 Executor executor = (Executor)group; 89 Attack task = new Attack(); 90 executor.execute(task); 91 task.waitUntilDone(); 92 if (!task.failedDueToSecurityException()) 93 throw new RuntimeException("SecurityException expected"); 94 } 95 96 }