You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassLoaderRepository.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package org.aspectj.apache.bcel.util;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import java.io.IOException;
  56. import java.io.InputStream;
  57. import java.lang.ref.Reference;
  58. import java.lang.ref.SoftReference;
  59. import java.net.URL;
  60. import java.net.URLClassLoader;
  61. import java.util.HashMap;
  62. import java.util.Map;
  63. import java.util.WeakHashMap;
  64. import org.aspectj.apache.bcel.classfile.ClassParser;
  65. import org.aspectj.apache.bcel.classfile.JavaClass;
  66. /**
  67. * The repository maintains information about which classes have
  68. * been loaded.
  69. *
  70. * It loads its data from the ClassLoader implementation
  71. * passed into its constructor.
  72. *
  73. * @see org.aspectj.apache.bcel.Repository
  74. *
  75. * @version $Id: ClassLoaderRepository.java,v 1.7 2006/08/18 14:51:00 acolyer Exp $
  76. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  77. * @author David Dixon-Peugh
  78. */
  79. public class ClassLoaderRepository implements Repository {
  80. private static java.lang.ClassLoader bootClassLoader = null;
  81. private java.lang.ClassLoader loader;
  82. private WeakHashMap /*<String classname,JavaClass>*/loadedClassesLocalCache = new WeakHashMap();
  83. private static Map /*<URL,JavaClass>*/loadedUrlsSharedCache = new HashMap();
  84. public static boolean useSharedCache = true;
  85. private static long timeManipulatingURLs = 0L;
  86. private static long timeSpentLoading = 0L;
  87. private static int classesLoadedCount = 0;
  88. private static int cacheHitsShared = 0;
  89. private static int missSharedEvicted = 0; // Misses in shared cache access due to reference GC
  90. private static int misses = 0;
  91. private int cacheHitsLocal = 0;
  92. private int missLocalEvicted = 0; // Misses in local cache access due to reference GC
  93. static {
  94. useSharedCache = System.getProperty("org.aspectj.apache.bcel.useSharedCache","true").equalsIgnoreCase("true");
  95. }
  96. public ClassLoaderRepository( java.lang.ClassLoader loader ) {
  97. this.loader = (loader != null) ? loader : getBootClassLoader();
  98. }
  99. private static synchronized java.lang.ClassLoader getBootClassLoader() {
  100. if (bootClassLoader == null) {
  101. bootClassLoader = new URLClassLoader(new URL[0]);
  102. }
  103. return bootClassLoader;
  104. }
  105. /**
  106. * Store a new JavaClass into this repository as a soft reference and return the reference
  107. */
  108. private Reference storeClassAsReference( JavaClass clazz ) {
  109. Reference ref = new SoftReference(clazz);
  110. loadedClassesLocalCache.put( clazz.getClassName(), ref);
  111. clazz.setRepository( this );
  112. return ref;
  113. }
  114. /**
  115. * Store a reference in the shared cache
  116. */
  117. private void storeReferenceShared(URL url, Reference ref) {
  118. if (useSharedCache) loadedUrlsSharedCache.put(url, ref);
  119. }
  120. /**
  121. * Store a new JavaClass into this Repository.
  122. */
  123. public void storeClass( JavaClass clazz ) {
  124. storeClassAsReference(clazz);
  125. }
  126. /**
  127. * Remove class from repository
  128. */
  129. public void removeClass(JavaClass clazz) {
  130. loadedClassesLocalCache.remove(clazz.getClassName());
  131. }
  132. /**
  133. * Find an already defined JavaClass in the local cache.
  134. */
  135. public JavaClass findClass( String className ) {
  136. Object o = loadedClassesLocalCache.get( className );
  137. if (o != null) {
  138. o = ((Reference)o).get();
  139. if (o != null) {
  140. return (JavaClass)o;
  141. } else {
  142. missLocalEvicted++;
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * Find an already defined JavaClass in the shared cache.
  149. */
  150. private JavaClass findClassShared(URL url) {
  151. if (!useSharedCache) return null;
  152. Object o = (Reference)loadedUrlsSharedCache.get(url);
  153. if (o != null) {
  154. o = ((Reference)o).get();
  155. if (o != null) {
  156. return (JavaClass)o;
  157. } else {
  158. missSharedEvicted++;
  159. }
  160. }
  161. return null;
  162. }
  163. /**
  164. * Lookup a JavaClass object from the Class Name provided.
  165. */
  166. public JavaClass loadClass( String className ) throws ClassNotFoundException {
  167. String classFile = className.replace('.', '/');
  168. // Check the local cache
  169. JavaClass clazz = findClass(className);
  170. if (clazz != null) { cacheHitsLocal++; return clazz; }
  171. try {
  172. // Work out the URL
  173. long time = System.currentTimeMillis();
  174. java.net.URL url = (useSharedCache?loader.getResource( classFile + ".class" ):null);
  175. if (useSharedCache && url==null) throw new ClassNotFoundException(className + " not found.");
  176. InputStream is = (useSharedCache?url.openStream():loader.getResourceAsStream( classFile + ".class" ));
  177. timeManipulatingURLs += (System.currentTimeMillis() - time);
  178. // Check the shared cache
  179. clazz = findClassShared(url);
  180. if (clazz != null) { cacheHitsShared++; timeSpentLoading+=(System.currentTimeMillis() - time); return clazz; }
  181. // Didn't find it in either cache
  182. misses++;
  183. if (is == null) { // TODO move this up?
  184. throw new ClassNotFoundException(className + " not found.");
  185. }
  186. ClassParser parser = new ClassParser( is, className );
  187. clazz = parser.parse();
  188. // Store it in both caches
  189. Reference ref = storeClassAsReference( clazz );
  190. storeReferenceShared(url,ref);
  191. timeSpentLoading += (System.currentTimeMillis() - time);
  192. classesLoadedCount++;
  193. return clazz;
  194. } catch (IOException e) {
  195. throw new ClassNotFoundException( e.toString() );
  196. }
  197. }
  198. /**
  199. * Produce a report on cache usage.
  200. */
  201. public String reportAllStatistics() {
  202. StringBuffer sb = new StringBuffer();
  203. sb.append("BCEL repository report.");
  204. if (!useSharedCache) sb.append(" (Shared cache deactivated)");
  205. sb.append(" Total time spent loading: "+timeSpentLoading+"ms.");
  206. sb.append(" Time manipulating URLs: "+timeManipulatingURLs+"ms.");
  207. sb.append(" Classes loaded: "+classesLoadedCount+".");
  208. if (useSharedCache) sb.append(" URL cache (hits/missDueToEviction): ("+cacheHitsShared+"/"+missSharedEvicted+").");
  209. sb.append(" Local cache (hits/missDueToEviction): ("+cacheHitsLocal+"/"+missLocalEvicted+").");
  210. return sb.toString();
  211. }
  212. public int reportLocalCacheHits() {
  213. return cacheHitsLocal;
  214. }
  215. public static int reportSharedCacheHits() {
  216. return cacheHitsShared;
  217. }
  218. /**
  219. * Reset statistics and clear all caches
  220. */
  221. public void reset() {
  222. timeManipulatingURLs = 0L;
  223. timeSpentLoading = 0L;
  224. classesLoadedCount = 0;
  225. cacheHitsLocal = 0;
  226. cacheHitsShared = 0;
  227. missSharedEvicted = 0;
  228. missLocalEvicted = 0;
  229. misses = 0;
  230. clear();
  231. clearShared();
  232. }
  233. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  234. return loadClass(clazz.getName());
  235. }
  236. /** Clear all entries from the local cache */
  237. public void clear() {
  238. loadedClassesLocalCache.clear();
  239. }
  240. /** Clear all entries from the shared cache */
  241. public static void clearShared() {
  242. loadedUrlsSharedCache.clear();
  243. }
  244. }