KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > impl > java > JavaClassInfoRepository


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.reflect.impl.java;
5
6 import java.lang.ref.WeakReference JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.WeakHashMap JavaDoc;
10
11 import com.tc.aspectwerkz.reflect.ClassInfo;
12
13 /**
14  * A repository for the class info hierarchy. Is class loader aware. <p/>TODO refactor some with
15  * ASMClassInfoRepository but keep em separate for system runtime sake in AOPC (WLS)
16  *
17  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
18  */

19 public class JavaClassInfoRepository {
20   /**
21    * Map with all the class info repositories mapped to their class loader.
22    */

23   private static final HashMap JavaDoc s_repositories = new HashMap JavaDoc();
24
25   /**
26    * Map with all the class info mapped to their class names.
27    */

28   private final Map JavaDoc m_repository = new WeakHashMap JavaDoc();
29
30   /**
31    * Class loader for the class repository.
32    */

33   private transient final WeakReference JavaDoc m_loaderRef;
34
35   /**
36    * Creates a new repository.
37    *
38    * @param loader
39    */

40   private JavaClassInfoRepository(final ClassLoader JavaDoc loader) {
41     m_loaderRef = new WeakReference JavaDoc(loader);
42   }
43
44   /**
45    * Returns the class info repository for the specific class loader
46    *
47    * @param loader
48    * @return
49    */

50   public static synchronized JavaClassInfoRepository getRepository(final ClassLoader JavaDoc loader) {
51     Integer JavaDoc hash = new Integer JavaDoc(loader == null ? 0 : loader.hashCode());
52     WeakReference JavaDoc repositoryRef = (WeakReference JavaDoc) s_repositories.get(hash);
53     JavaClassInfoRepository repository = repositoryRef == null ? null : (JavaClassInfoRepository) repositoryRef.get();
54     if (repository != null) {
55       return repository;
56     } else {
57       JavaClassInfoRepository repo = new JavaClassInfoRepository(loader);
58       s_repositories.put(hash, new WeakReference JavaDoc(repo));
59       return repo;
60     }
61   }
62
63   /**
64    * Remove a class from the repository.
65    *
66    * @param className the name of the class
67    */

68   public static void removeClassInfoFromAllClassLoaders(final String JavaDoc className) {
69     //TODO - fix algorithm
70
throw new UnsupportedOperationException JavaDoc("fix algorithm");
71   }
72
73   /**
74    * Returns the class info.
75    *
76    * @param className
77    * @return
78    */

79   public ClassInfo getClassInfo(final String JavaDoc className) {
80     ClassInfo info = (ClassInfo) m_repository.get(className);
81     if (info == null) {
82       return checkParentClassRepository(className, (ClassLoader JavaDoc) m_loaderRef.get());
83     }
84     return (ClassInfo) m_repository.get(className);
85   }
86
87   /**
88    * Adds a new class info.
89    *
90    * @param classInfo
91    */

92   public void addClassInfo(final ClassInfo classInfo) {
93     // is the class loaded by a class loader higher up in the hierarchy?
94
if (checkParentClassRepository(classInfo.getName(), (ClassLoader JavaDoc) m_loaderRef.get()) == null) {
95       m_repository.put(new String JavaDoc(classInfo.getName()), classInfo);
96     } else {
97       // TODO: remove class in child class repository and add it for the current (parent) CL
98
}
99   }
100
101   /**
102    * Checks if the class info for a specific class exists.
103    *
104    * @param name
105    * @return
106    */

107   public boolean hasClassInfo(final String JavaDoc name) {
108     return m_repository.containsKey(name);
109   }
110
111   /**
112    * Searches for a class info up in the class loader hierarchy.
113    *
114    * @param className
115    * @param loader
116    * @return the class info
117    * @TODO might clash for specific class loader lookup algorithms, user need to override this class and implement
118    * this method
119    */

120   public ClassInfo checkParentClassRepository(final String JavaDoc className, final ClassLoader JavaDoc loader) {
121     if (loader == null) {
122       return null;
123     }
124     ClassInfo info;
125     ClassLoader JavaDoc parent = loader.getParent();
126     if (parent == null) {
127       return null;
128     } else {
129       info = JavaClassInfoRepository.getRepository(parent).getClassInfo(className);
130       if (info != null) {
131         return info;
132       } else {
133         return checkParentClassRepository(className, parent);
134       }
135     }
136   }
137 }
Popular Tags