1 package org.sapia.clazzy; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.File ; 5 import java.io.InputStream ; 6 import java.net.MalformedURLException ; 7 import java.net.URL ; 8 import java.util.ResourceBundle ; 9 10 import org.sapia.clazzy.loader.JarLoader; 11 12 47 public class JarClassLoader extends BaseClassLoader implements Consts{ 48 49 private JarLoader _jar; 50 51 ResourceBundle bundle; 52 53 public JarClassLoader(File jar) { 54 _jar = new JarLoader(jar); 55 } 56 57 public JarClassLoader(ClassLoader parent, File jar) { 58 super(parent); 59 _jar = new JarLoader(jar); 60 } 61 62 66 public File getFile() { 67 return _jar.getJarFile(); 68 } 69 70 73 protected Class findClass(String name) throws ClassNotFoundException { 74 if(!_jar.getJarFile().exists()) { 75 throw new ClassNotFoundException (name); 76 } 77 78 String resourceName = name.replace('.', '/') + ".class"; 79 byte[] classBytes = _jar.loadBytes(resourceName); 80 if(classBytes == null){ 81 throw new ClassNotFoundException (name); 82 } 83 String pckg = Utils.getPackageNameFor(name); 84 if(pckg != null && getPackage(pckg) == null){ 85 definePackage(pckg, 86 PACKAGE_IMPL_TITLE, 87 PACKAGE_IMPL_VERSION, 88 PACKAGE_IMPL_VENDOR, 89 PACKAGE_IMPL_TITLE, 90 PACKAGE_IMPL_VERSION, 91 PACKAGE_IMPL_VENDOR, 92 null); 93 } 94 return defineClass(name, classBytes, 0, classBytes.length); 95 } 96 97 101 public InputStream findResourceAsStream(String name){ 102 if(!_jar.getJarFile().exists()) { 103 return null; 104 } 105 byte[] toReturn = _jar.loadBytes(name); 106 if(toReturn == null){ 107 return null; 108 } 109 return new ByteArrayInputStream (toReturn); 110 } 111 112 115 protected URL findResource(String name) { 116 try{ 117 return new URL (ClazzyURLStreamHandlerFactory.PROTOCOL, "", _jar.getJarFile().getAbsolutePath()+"?"+name); 118 }catch(MalformedURLException e){ 119 return null; 120 } 121 } 122 123 126 public void close(){ 127 _jar.close(); 128 } 129 130 } 131 | Popular Tags |