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 *          jdk.compiler
30 * @library /test/lib
31 * @build jdk.test.lib.compiler.CompilerUtils
32 * @run main/othervm -Xlog:class+load DelegateTest one-way
33 * @run main/othervm -Xlog:class+load DelegateTest cross
34 */
35
36import java.io.File;
37import java.net.MalformedURLException;
38import java.net.URL;
39import java.nio.file.Path;
40import java.nio.file.Paths;
41
42import jdk.test.lib.compiler.CompilerUtils;
43
44public class DelegateTest implements Runnable {
45
46    private static final Path TEST_DIR = Paths.get(System.getProperty("user.dir", "."));
47    private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), "src");
48
49    private String id;
50    private DelegatingLoader dl;
51    private String startClass;
52
53    private static DelegatingLoader saLoader, sbLoader;
54
55    public static void log(String line) {
56        System.out.println(line);
57    }
58
59    public static void main(String[] args) throws Exception {
60        if (!CompilerUtils.compile(SRC_DIR, TEST_DIR)) {
61            throw new RuntimeException("Failed to compile "
62                    + SRC_DIR.toAbsolutePath().toString());
63        }
64
65        URL[] url = new URL[1];
66        try {
67            url[0] = new URL("file://" + TEST_DIR + File.separator);
68        } catch (MalformedURLException e) {
69            e.printStackTrace();
70        }
71        // Set up Classloader delegation hierarchy
72        saLoader = new DelegatingLoader(url);
73        sbLoader = new DelegatingLoader(url);
74
75        String[] saClasses = { "comSA.SupBob", "comSA.Alice" };
76        String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };
77
78        saLoader.setDelegate(sbClasses, sbLoader);
79        sbLoader.setDelegate(saClasses, saLoader);
80
81        // test one-way delegate
82        String testType = args[0];
83        if (testType.equals("one-way")) {
84            test("comSA.Alice", "comSA.SupBob");
85        } else if (testType.equals("cross")) {
86            // test cross delegate
87            test("comSA.Alice", "comSB.Bob");
88        } else {
89            System.out.println("ERROR: unsupported - " + testType);
90        }
91    }
92
93    private static void test(String clsForSA, String clsForSB) throws InterruptedException {
94        DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA);
95        DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB);
96        Thread ta = new Thread(ia);
97        Thread tb = new Thread(ib);
98        ta.start();
99        tb.start();
100        ta.join();
101        tb.join();
102    }
103
104    public static void sleep() {
105        try {
106            Thread.sleep(500);
107        } catch (InterruptedException e) {
108            e.printStackTrace();
109            log("Thread interrupted");
110        }
111    }
112
113    private DelegateTest(String id, DelegatingLoader dl, String startClass) {
114        this.id = id;
115        this.dl = dl;
116        this.startClass = startClass;
117    }
118
119    public void run() {
120        log("Spawned thread " + id + " running");
121        try {
122            // To mirror the WAS deadlock, need to ensure class load
123            // is routed via the VM.
124            Class.forName(startClass, true, dl);
125        } catch (ClassNotFoundException e) {
126            e.printStackTrace();
127        }
128        log("Thread " + id + " terminating");
129    }
130}
131