1 22 package org.jboss.xb.binding.introspection; 23 24 import java.util.Map ; 25 import java.util.WeakHashMap ; 26 import java.lang.ref.WeakReference ; 27 import org.jboss.xb.binding.JBossXBRuntimeException; 28 import org.jboss.xb.util.NoopMap; 29 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 30 31 35 public class ClassInfos 36 { 37 private static Map classloaderCache = new WeakHashMap (); 38 39 43 public static void disableCache() 44 { 45 synchronized(classloaderCache) 46 { 47 classloaderCache = NoopMap.INSTANCE; 48 } 49 } 50 51 54 public static void enableCache() 55 { 56 synchronized(classloaderCache) 57 { 58 if(!isCacheEnabled()) 59 { 60 classloaderCache = new WeakHashMap (); 61 } 62 } 63 } 64 65 68 public static boolean isCacheEnabled() 69 { 70 synchronized(classloaderCache) 71 { 72 return classloaderCache != NoopMap.INSTANCE; 73 } 74 } 75 76 79 public static void flushCache() 80 { 81 synchronized(classloaderCache) 82 { 83 classloaderCache.clear(); 84 } 85 } 86 87 91 public static void flushCache(String cls) 92 { 93 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 94 Map classLoaderCache = getClassLoaderCache(cl); 95 classLoaderCache.remove(cls); 96 } 97 98 102 public static void flushCache(Class cls) 103 { 104 Map classLoaderCache = getClassLoaderCache(cls.getClassLoader()); 105 classLoaderCache.remove(cls.getName()); 106 } 107 108 public static ClassInfo getClassInfo(Class cls) 109 { 110 Map classLoaderCache = getClassLoaderCache(cls.getClassLoader()); 111 112 WeakReference weak = (WeakReference )classLoaderCache.get(cls.getName()); 113 if(weak != null) 114 { 115 Object result = weak.get(); 116 if(result != null) 117 { 118 return (ClassInfo)result; 119 } 120 } 121 122 ClassInfo clsInfo = new ClassInfo(cls); 123 weak = new WeakReference (clsInfo); 124 classLoaderCache.put(cls.getName(), weak); 125 return clsInfo; 126 } 127 128 public static ClassInfo getClassInfo(String name, boolean required) 129 { 130 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 131 Map classLoaderCache = getClassLoaderCache(cl); 132 133 WeakReference weak = (WeakReference )classLoaderCache.get(name); 134 if(weak != null) 135 { 136 Object result = weak.get(); 137 if(result != null) 138 { 139 return (ClassInfo)result; 140 } 141 } 142 143 try 144 { 145 ClassInfo clsInfo = new ClassInfo(cl.loadClass(name)); 146 weak = new WeakReference (clsInfo); 147 classLoaderCache.put(name, weak); 148 return clsInfo; 149 } 150 catch(ClassNotFoundException e) 151 { 152 if(required) 153 { 154 throw new JBossXBRuntimeException("Failed to load class " + name); 155 } 156 } 157 158 return null; 159 } 160 161 private static Map getClassLoaderCache(ClassLoader cl) 162 { 163 synchronized(classloaderCache) 164 { 165 Map result = (Map ) classloaderCache.get(cl); 166 if (result == null) 167 { 168 result = new ConcurrentHashMap(); 169 classloaderCache.put(cl, result); 170 } 171 return result; 172 } 173 } 174 } 175 | Popular Tags |