1 package org.sapia.clazzy; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.URL ; 6 7 import org.sapia.clazzy.loader.FileSystemLoader; 8 9 27 public class FileSystemClassLoader extends BaseClassLoader implements Consts{ 28 29 private FileSystemLoader _loader; 30 31 public FileSystemClassLoader(File baseDir) { 32 _loader = new FileSystemLoader(baseDir); 33 } 34 35 public FileSystemClassLoader(ClassLoader parent, File baseDir) { 36 super(parent); 37 _loader = new FileSystemLoader(baseDir); 38 } 39 40 44 public File getFile() { 45 return _loader.getBaseDir(); 46 } 47 48 51 protected Class findClass(String name) throws ClassNotFoundException { 52 if(!_loader.getBaseDir().exists() || !_loader.getBaseDir().isDirectory()) { 53 throw new ClassNotFoundException (name); 54 } 55 String className = name.replace('.', File.separatorChar)+".class"; 56 57 58 byte[] classBytes = _loader.loadBytes(className); 59 if(classBytes == null){ 60 throw new ClassNotFoundException (name); 61 } 62 String pckg = Utils.getPackageNameFor(name); 63 if(pckg != null && getPackage(pckg) == null) 64 definePackage(pckg, 65 PACKAGE_SPEC_TITLE, 66 PACKAGE_SPEC_VERSION, 67 PACKAGE_SPEC_VENDOR, 68 PACKAGE_IMPL_TITLE, 69 PACKAGE_IMPL_VERSION, 70 PACKAGE_IMPL_VENDOR, 71 null); 72 return defineClass(name, classBytes, 0, classBytes.length); 73 } 74 75 78 protected URL findResource(String name) { 79 if(!_loader.getBaseDir().exists() || !_loader.getBaseDir().isDirectory()) { 80 return null; 81 } 82 try{ 83 File f = new File (_loader.getBaseDir(), name); 84 if(f.exists() && f.isFile()){ 85 return f.toURL(); 86 } 87 return null; 88 }catch(IOException e){ 89 return null; 90 } 91 } 92 } 93 | Popular Tags |