1 26 27 package net.sourceforge.groboutils.util.classes.v1.jdk2; 28 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 35 import java.util.Hashtable ; 36 37 import net.sourceforge.groboutils.util.classes.v1.IUrlClassLoader; 38 39 40 50 public class UrlClassLoader implements IUrlClassLoader 51 { 52 55 58 private Hashtable m_urlLoaders = new Hashtable (); 59 60 63 66 public UrlClassLoader() 67 { 68 } 70 71 72 75 76 87 public Class loadClass( String className, String url ) 88 { 89 URL murl = convertUrl( url ); 90 ClassLoader cl = getClassLoader( murl ); 91 Class c = null; 92 93 for (int i = 0; i < 5 && c == null; ++i) 95 { 96 try 97 { 98 c = cl.loadClass( className ); 99 } 100 catch (ClassNotFoundException cnfe) 101 { 102 cnfe.printStackTrace(); 104 } 105 } 106 return c; 107 } 108 109 110 115 public void flush() 116 { 117 this.m_urlLoaders = new Hashtable (); 118 } 119 120 121 122 125 126 130 protected URL convertUrl( String url ) 131 { 132 if (url == null) 133 { 134 return null; 135 } 136 137 try 138 { 139 return new URL ( url ); 140 } 141 catch (IOException ioe) 142 { 143 try 145 { 146 return new URL ( "file:" + url ); 147 } 148 catch (IOException e) 149 { 150 return null; 151 } 152 } 153 } 154 155 156 159 protected ClassLoader getClassLoader( URL url ) 160 { 161 ClassLoader cl; 162 if (url == null) 163 { 164 cl = Thread.currentThread().getContextClassLoader(); 165 } 166 else 167 { 168 cl = (URLClassLoader )this.m_urlLoaders.get( url ); 169 if (cl == null) 170 { 171 cl = new URLClassLoader ( new URL [] { url } ); 172 this.m_urlLoaders.put( url, cl ); 173 } 174 } 175 return cl; 176 } 177 178 179 } 182 183 | Popular Tags |