1 23 24 25 package com.sun.enterprise.tools.verifier.apiscan.classfile; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.lang.ref.WeakReference ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import java.util.StringTokenizer ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 49 class ASMClassFileLoader implements ClassFileLoader { 50 51 private ClassLoader cl; 52 private static String resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings"; 53 private static Logger logger = Logger.getLogger("apiscan.classfile", resourceBundleName); private final static String myClassName = "ASMClassFileLoader"; private Map <String , WeakReference <ClassFile>> loadedClassesCache = 57 new HashMap <String , WeakReference <ClassFile>>(); 58 64 public ASMClassFileLoader(String cp) { 65 ArrayList <URL > urls = new ArrayList <URL >(); 66 for (StringTokenizer st = new StringTokenizer (cp, File.pathSeparator); 67 st.hasMoreTokens();) { 68 String entry = st.nextToken(); 69 try { 70 urls.add(new File (entry).toURI().toURL()); 71 } catch (MalformedURLException e) { 72 logger.logp(Level.WARNING, myClassName, "init<>", getClass().getName() + ".exception1", new Object []{entry}); 73 logger.log(Level.WARNING, "", e); 74 } 75 } 76 cl = new URLClassLoader ((URL []) urls.toArray(new URL [0]), null); 79 } 80 81 87 public ASMClassFileLoader(ClassLoader cl) { 88 this.cl = cl; 89 } 90 91 public ClassFile load(String externalClassName) throws IOException { 93 logger.entering("ASMClassFileLoader", "load", externalClassName); WeakReference <ClassFile> cachedCF = loadedClassesCache.get(externalClassName); 95 if(cachedCF!=null){ 96 ClassFile cf = cachedCF.get(); 97 if(cf!=null){ 98 return cf; 99 } else { 100 logger.logp(Level.FINE, "ASMClassFileLoader", "load", "{0} has been garbage collected from cache!", externalClassName); } 103 } 104 return load0(externalClassName); 105 } 106 107 private ClassFile load0(String externalClassName) throws IOException { 108 String internalClassName = externalClassName.replace('.', '/'); 110 InputStream is = cl.getResourceAsStream(internalClassName + ".class"); 111 if (is == null) throw new IOException ( 113 "Not able to load " + internalClassName + ".class"); 114 try { 115 ClassFile cf = new ASMClassFile(is); 116 matchClassSignature(cf, externalClassName); 117 loadedClassesCache.put(externalClassName, 118 new WeakReference <ClassFile>(cf)); 119 return cf; 120 } finally { 121 is.close(); 122 } 123 124 } 125 126 private void matchClassSignature(ClassFile cf, String externalClassName) 131 throws IOException { 132 String nameOfLoadedClass = cf.getName(); 133 if (!nameOfLoadedClass.equals(externalClassName)) { 134 throw new IOException (externalClassName + ".class represents " + 135 cf.getName() + 136 ". Perhaps your package name is incorrect or you passed the " + 137 "name using internal form instead of using external form."); 138 } 139 } 140 } 141 | Popular Tags |