1 /* 2 * Copyright (c) 2009, 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 4735126 27 * @summary (cl) ClassLoader.loadClass locks all instances in chain when delegating 28 * @modules java.base/java.lang:open 29 * @build Alice Bob SupAlice SupBob 30 * @run driver SetupLoader 31 * @run main/othervm -Xlog:class+load DelegateTest one-way 32 * @run main/othervm -Xlog:class+load DelegateTest cross 33 */ 34 35 import java.net.MalformedURLException; 36 import java.net.URL; 37 38 public class DelegateTest implements Runnable { 39 40 private String id; 41 private DelegatingLoader dl; 42 private String startClass; 43 44 private static DelegatingLoader saLoader, sbLoader; 45 46 public static void log(String line) { 47 System.out.println(line); 48 } 49 50 public static void main(String[] args) throws Exception { 51 URL[] urlsa = new URL[1]; 52 URL[] urlsb = new URL[1]; 53 try { 54 String testDir = System.getProperty("user.dir", "."); 55 String sep = System.getProperty("file.separator"); 56 urlsa[0] = new URL("file://" + testDir + sep + "SA" + sep); 57 urlsb[0] = new URL("file://" + testDir + sep + "SB" + sep); 58 } catch (MalformedURLException e) { 59 e.printStackTrace(); 60 } 61 // Set up Classloader delegation hierarchy 62 saLoader = new DelegatingLoader(urlsa); 63 sbLoader = new DelegatingLoader(urlsb); 64 65 String[] saClasses = { "comSA.SupBob", "comSA.Alice" }; 66 String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" }; 67 68 saLoader.setDelegate(sbClasses, sbLoader); 69 sbLoader.setDelegate(saClasses, saLoader); 70 71 // test one-way delegate 72 String testType = args[0]; 73 if (testType.equals("one-way")) { 74 test("comSA.Alice", "comSA.SupBob"); 75 } else if (testType.equals("cross")) { 76 // test cross delegate 77 test("comSA.Alice", "comSB.Bob"); 78 } else { 79 System.out.println("ERROR: unsupported - " + testType); 80 } 81 } 82 83 private static void test(String clsForSA, String clsForSB) throws InterruptedException { 84 DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA); 85 DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB); 86 Thread ta = new Thread(ia); 87 Thread tb = new Thread(ib); 88 ta.start(); 89 tb.start(); 90 ta.join(); 91 tb.join(); 92 } 93 94 public static void sleep() { 95 try { 96 Thread.sleep(500); 97 } catch (InterruptedException e) { 98 e.printStackTrace(); 99 log("Thread interrupted"); 100 } 101 } 102 103 private DelegateTest(String id, DelegatingLoader dl, String startClass) { 104 this.id = id; 105 this.dl = dl; 106 this.startClass = startClass; 107 } 108 109 public void run() { 110 log("Spawned thread " + id + " running"); 111 try { 112 // To mirror the WAS deadlock, need to ensure class load 113 // is routed via the VM. 114 Class.forName(startClass, true, dl); 115 } catch (ClassNotFoundException e) { 116 e.printStackTrace(); 117 } 118 log("Thread " + id + " terminating"); 119 } 120 }