1 7 15 16 package com.sun.corba.se.impl.util; 17 18 import sun.corba.Bridge ; 19 20 import java.util.Map ; 21 import java.util.WeakHashMap ; 22 import java.util.Collections ; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 27 30 class JDKClassLoader { 31 32 private static final JDKClassLoaderCache classCache 33 = new JDKClassLoaderCache(); 34 35 private static final Bridge bridge = 36 (Bridge)AccessController.doPrivileged( 37 new PrivilegedAction () { 38 public Object run() { 39 return Bridge.get() ; 40 } 41 } 42 ) ; 43 44 static Class loadClass(Class aClass, String className) 45 throws ClassNotFoundException { 46 47 if (className == null) { 49 throw new NullPointerException (); 50 } 51 if (className.length() == 0) { 52 throw new ClassNotFoundException (); 53 } 54 55 63 ClassLoader loader; 66 if (aClass != null) { 67 loader = aClass.getClassLoader(); 68 } else { 69 loader = bridge.getLatestUserDefinedLoader(); 70 } 71 Object key = classCache.createKey(className, loader); 73 74 if (classCache.knownToFail(key)) { 75 throw new ClassNotFoundException (className); 76 } else { 77 try { 78 return Class.forName(className, false, loader); 82 } catch(ClassNotFoundException cnfe) { 83 classCache.recordFailure(key); 87 throw cnfe; 88 } 89 } 90 } 91 92 95 private static class JDKClassLoaderCache 96 { 97 public final void recordFailure(Object key) { 101 cache.put(key, JDKClassLoaderCache.KNOWN_TO_FAIL); 102 } 103 104 public final Object createKey(String className, ClassLoader latestLoader) { 111 return new CacheKey(className, latestLoader); 112 } 113 114 public final boolean knownToFail(Object key) { 117 return cache.get(key) == JDKClassLoaderCache.KNOWN_TO_FAIL; 118 } 119 120 private final Map cache 122 = Collections.synchronizedMap(new WeakHashMap ()); 123 124 private static final Object KNOWN_TO_FAIL = new Object (); 128 129 private static class CacheKey 132 { 133 String className; 134 ClassLoader loader; 135 136 public CacheKey(String className, ClassLoader loader) { 137 this.className = className; 138 this.loader = loader; 139 } 140 141 public int hashCode() { 144 if (loader == null) 145 return className.hashCode(); 146 else 147 return className.hashCode() ^ loader.hashCode(); 148 } 149 150 public boolean equals(Object obj) { 151 try { 152 153 if (obj == null) 155 return false; 156 157 CacheKey other = (CacheKey)obj; 158 159 return (className.equals(other.className) && 168 loader == other.loader); 169 170 } catch (ClassCastException cce) { 171 return false; 172 } 173 } 174 } 175 } 176 } 177 | Popular Tags |