Path: blob/master/test/jdk/java/lang/ClassLoader/deadlock/DelegatingLoader.java
41153 views
/*1* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.MalformedURLException;24import java.net.URL;25import java.net.URLClassLoader;26import java.util.concurrent.ConcurrentHashMap;27import java.util.concurrent.locks.*;28import java.lang.reflect.*;2930public class DelegatingLoader extends URLClassLoader {3132private DelegatingLoader delLoader;33private String[] delClasses;3435static {36boolean supportParallel = false;37try {38Class c = Class.forName("java.lang.ClassLoader");39Method m = c.getDeclaredMethod("registerAsParallelCapable",40new Class[0]);41m.setAccessible(true);42Object result = (Boolean) m.invoke(null);43if (result instanceof Boolean) {44supportParallel = ((Boolean) result).booleanValue();45} else {46// Should never happen47System.out.println("Error: ClassLoader.registerAsParallelCapable() did not return a boolean!");48System.exit(1);49}50} catch (NoSuchMethodException nsme) {51System.out.println("No ClassLoader.registerAsParallelCapable() API");52} catch (NoSuchMethodError nsme2) {53System.out.println("No ClassLoader.registerAsParallelCapable() API");54} catch (Exception ex) {55ex.printStackTrace();56// Exit immediately to indicate an error57System.exit(1);58}59System.out.println("Parallel ClassLoader registration: " +60supportParallel);61}6263public DelegatingLoader(URL urls[]) {64super(urls);65System.out.println("DelegatingLoader using URL " + urls[0]);66}6768public void setDelegate(String[] delClasses, DelegatingLoader delLoader) {69this.delClasses = delClasses;70this.delLoader = delLoader;71}7273public Class loadClass(String className, boolean resolve)74throws ClassNotFoundException {75for (int i = 0; i < delClasses.length; i++) {76if (delClasses[i].equals(className)) {77DelegateTest.log("Delegating class loading for " + className);78try {79Thread.sleep(500);80} catch (InterruptedException ie) {81return null;82}83return delLoader.loadClass(className, resolve);84}85}8687DelegateTest.log("Loading local class " + className);88// synchronized (getClassLoadingLock(className)) {89return super.loadClass(className, resolve);90// }91}92}939495