1 /* 2 * Copyright (c) 2009, 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 import java.net.MalformedURLException; 25 import java.net.URL; 26 import java.net.URLClassLoader; 27 import java.util.concurrent.ConcurrentHashMap; 28 import java.util.concurrent.locks.*; 29 import java.lang.reflect.*; 30 31 public class DelegatingLoader extends URLClassLoader { 32 33 private DelegatingLoader delLoader; 34 private String[] delClasses; 35 36 static { 37 boolean supportParallel = false; 38 try { 39 Class c = Class.forName("java.lang.ClassLoader"); 40 Method m = c.getDeclaredMethod("registerAsParallelCapable", 41 new Class[0]); 42 m.setAccessible(true); 43 Object result = (Boolean) m.invoke(null); 44 if (result instanceof Boolean) { 45 supportParallel = ((Boolean) result).booleanValue(); 46 } else { 47 // Should never happen 48 System.out.println("Error: ClassLoader.registerAsParallelCapable() did not return a boolean!"); 49 System.exit(1); 50 } 51 } catch (NoSuchMethodException nsme) { 52 System.out.println("No ClassLoader.registerAsParallelCapable() API"); 53 } catch (NoSuchMethodError nsme2) { 54 System.out.println("No ClassLoader.registerAsParallelCapable() API"); 55 } catch (Exception ex) { 56 ex.printStackTrace(); 57 // Exit immediately to indicate an error 58 System.exit(1); 59 } 60 System.out.println("Parallel ClassLoader registration: " + 61 supportParallel); 62 } 63 64 public DelegatingLoader(URL urls[]) { 65 super(urls); 66 System.out.println("DelegatingLoader using URL " + urls[0]); 67 } 68 69 public void setDelegate(String[] delClasses, DelegatingLoader delLoader) { 70 this.delClasses = delClasses; 71 this.delLoader = delLoader; 72 } 73 74 public Class loadClass(String className, boolean resolve) 75 throws ClassNotFoundException { 76 for (int i = 0; i < delClasses.length; i++) { 77 if (delClasses[i].equals(className)) { 78 Starter.log("Delegating class loading for " + className); 79 try { 80 Thread.sleep(500); 81 } catch (InterruptedException ie) { 82 return null; 83 } 84 return delLoader.loadClass(className, resolve); 85 } 86 } 87 88 Starter.log("Loading local class " + className); 89 // synchronized (getClassLoadingLock(className)) { 90 return super.loadClass(className, resolve); 91 // } 92 } 93 }