1 6 package org.jfox.ioc.classloader; 7 8 import java.net.URL ; 9 import java.net.URLClassLoader ; 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 import java.util.List ; 13 14 17 18 public class LocalClassLoader extends URLClassLoader { 19 22 private ClassLoaderRepository repo; 23 24 private PrivateURLClassLoader privateCL = null; 26 27 32 public LocalClassLoader(URL [] urls, ClassLoaderRepository repo) { 33 super(urls, repo); 34 privateCL = new PrivateURLClassLoader(new URL [0], ClassLoader 35 .getSystemClassLoader()); 36 this.repo = repo; 37 repo.addLocalCL(this); 38 } 39 40 public Class <?> loadClass(String name) throws ClassNotFoundException { 41 try { 42 Class c = super.loadClass(name); 43 if(c.getClassLoader() == this) { 45 repo.cacheClass(c); 46 } 47 return c; 48 49 } 50 catch(ClassNotFoundException e2) { 51 try { 52 return privateCL.loadClass(name); 53 } 54 catch(ClassNotFoundException e4) { 55 56 } 57 } 58 throw new ClassNotFoundException (name); 59 60 } 61 62 70 73 81 92 93 98 public void addURL(URL url) { 99 privateCL.addURL(url); 100 } 101 102 public URL getResource(String name) { 103 URL url = super.getResource(name); 104 if(url == null) { 105 url = repo.getResource(name); 106 } 107 return url; 108 109 } 110 111 116 public URL [] getURLs() { 117 List urls = new ArrayList (); 118 urls.addAll(Arrays.asList(repo.getURLs())); 119 urls.addAll(Arrays.asList(super.getURLs())); 120 return (URL []) urls.toArray(new URL [0]); 121 } 122 123 124 public String toString() { 125 return "LocalClassLoader" + Arrays.asList(getURLs()); 126 } 127 } 128 129 | Popular Tags |