1 23 24 29 30 package com.sun.enterprise.tools.verifier.apiscan.classfile; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.net.URLClassLoader ; 38 import java.net.URLConnection ; 39 import java.util.ArrayList ; 40 import java.util.StringTokenizer ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 52 class BCELClassFileLoader implements ClassFileLoader { 53 54 private ClassLoader cl; 55 private static String resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings"; 56 private static Logger logger = Logger.getLogger("apiscan.classfile", resourceBundleName); private final static String myClassName = "BCELClassFileLoader"; 59 65 public BCELClassFileLoader(String cp) { 66 ArrayList <URL > urls = new ArrayList <URL >(); 67 for (StringTokenizer st = new StringTokenizer (cp, File.pathSeparator); 68 st.hasMoreTokens();) { 69 String entry = st.nextToken(); 70 try { 71 urls.add(new File (entry).toURI().toURL()); 76 } catch (MalformedURLException e) { 77 logger.logp(Level.WARNING, myClassName, "init<>", getClass().getName() + ".exception1", new Object []{entry}); 78 logger.log(Level.WARNING, "", e); 79 } 80 } 81 cl = new URLClassLoader ((URL []) urls.toArray(new URL [0]), null); 84 } 85 86 92 public BCELClassFileLoader(ClassLoader cl) { 93 this.cl = cl; 94 } 95 96 public ClassFile load(String externalClassName) throws IOException { 98 logger.entering("BCELClassFileLoader", "load", externalClassName); String internalClassName = externalClassName.replace('.', '/'); 101 URL resource = cl.getResource(internalClassName + ".class"); 102 if(resource == null) 104 throw new IOException ("Not able to load " + internalClassName + ".class"); 105 URLConnection urlcon = resource.openConnection(); 106 urlcon.setUseCaches(false); InputStream is = urlcon.getInputStream(); 108 try { 109 ClassFile cf = new BCELClassFile(is, internalClassName + ".class"); matchClassSignature(cf, externalClassName); 111 return cf; 112 } finally { 113 is.close(); 114 } 115 } 116 117 private void matchClassSignature(ClassFile cf, String externalClassName) 122 throws IOException { 123 String nameOfLoadedClass = cf.getName(); 124 if (!nameOfLoadedClass.equals(externalClassName)) { 125 throw new IOException (externalClassName + ".class represents " + 126 cf.getName() + 127 ". Perhaps your package name is incorrect or you passed the " + 128 "name using internal form instead of using external form."); 129 } 130 } 131 } 132 | Popular Tags |