1 5 6 package com.tc.aspectwerkz.reflect.impl.asm; 7 8 import com.tc.aspectwerkz.exception.DefinitionException; 9 import com.tc.aspectwerkz.reflect.ClassInfo; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.lang.ref.Reference ; 14 import java.lang.ref.SoftReference ; 15 import java.lang.ref.WeakReference ; 16 import java.util.HashMap ; 17 import java.util.Properties ; 18 19 24 public class AsmClassInfoRepository { 25 28 private static final HashMap s_repositories = new HashMap (); 29 30 33 private final HashMap m_repository = new HashMap (); 34 35 38 private transient final WeakReference m_loaderRef; 39 40 43 private final Properties m_annotationProperties; 44 45 50 private AsmClassInfoRepository(final ClassLoader loader) { 51 m_loaderRef = new WeakReference (loader); 52 m_annotationProperties = new Properties (); 53 if (loader != null) { 54 try { 55 InputStream stream = loader.getResourceAsStream("annotation.properties"); 56 if (stream != null) { 57 try { 58 m_annotationProperties.load(stream); 59 } finally { 60 try { 61 stream.close(); 62 } catch (Exception e) { 63 } 65 } 66 } 67 } catch (IOException e) { 68 throw new DefinitionException("could not find resource [annotation.properties] on classpath"); 69 } 70 } 71 } 72 73 79 public static AsmClassInfoRepository getRepository(final ClassLoader loader) { 80 Integer hash = new Integer (loader == null ? 0 : loader.hashCode()); 82 synchronized (s_repositories) { 83 AsmClassInfoRepository repository = lookup(hash); 84 85 if (repository != null) { return repository; } 87 } 88 89 AsmClassInfoRepository repo = new AsmClassInfoRepository(loader); 91 92 synchronized (s_repositories) { 94 AsmClassInfoRepository repository = lookup(hash); 95 96 if (repository != null) { return repository; } 98 99 s_repositories.put(hash, new SoftReference (repo)); 100 } 101 102 return repo; 103 104 } 105 106 private static AsmClassInfoRepository lookup(Integer hash) { 107 Reference repositoryRef = (Reference ) s_repositories.get(hash); 108 return ((repositoryRef == null) ? null : (AsmClassInfoRepository) repositoryRef.get()); 109 } 110 111 116 public static void removeClassInfoFromAllClassLoaders(final String className) { 117 throw new UnsupportedOperationException ("fix algorithm"); 119 } 120 121 127 public ClassInfo getClassInfo(final String className) { 128 Reference classInfoRef = ((Reference ) m_repository.get(new Integer (className.hashCode()))); 129 ClassInfo info = classInfoRef == null ? null : (ClassInfo) classInfoRef.get(); 130 if (info == null) { return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); } 131 return info; 132 } 133 134 139 public void addClassInfo(final ClassInfo classInfo) { 140 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 142 m_repository.put(new Integer (classInfo.getName().hashCode()), new SoftReference (classInfo)); 143 } else { 144 } 147 } 148 149 155 public boolean hasClassInfo(final String name) { 156 Reference classInfoRef = (Reference ) m_repository.get(new Integer (name.hashCode())); 157 return (classInfoRef == null) ? false : (classInfoRef.get() != null); 158 } 159 160 165 public void removeClassInfo(final String className) { 166 m_repository.remove(new Integer (className.hashCode())); 167 } 168 169 174 public Properties getAnnotationProperties() { 175 return m_annotationProperties; 176 } 177 178 187 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 188 if (loader == null) { return null; } 189 ClassLoader parent = loader.getParent(); 190 if (parent == null) { 191 return null; 192 } else { 193 AsmClassInfoRepository parentRep = AsmClassInfoRepository.getRepository(parent); 194 195 Reference classInfoRef = ((Reference ) parentRep.m_repository.get(new Integer (className.hashCode()))); 196 ClassInfo info = classInfoRef == null ? null : (ClassInfo) classInfoRef.get(); 197 if (info != null) { 198 return info; 199 } else { 200 return checkParentClassRepository(className, parent); 201 } 202 } 203 } 204 205 211 212 } | Popular Tags |