The URL Class Loader is a simple network class loader taken from JAVA
Network Programming by Eliotte Rusty Harold. The only additions
are allowNetworkExtensions, which is checked before anything is
loaded from the network, and name = name.replace('.', '/'), which
changes from class paths to URL paths.
*/
package w3c.model.www.pep.altlib;
import java.util.Hashtable;
import java.net.*;
import java.io.*;
public class URLClassLoader extends ClassLoader
Hashtable cache;
URL u;
/* enable dynamic extensions */
final static boolean allowNetworkExtensions=false;
/* Set to false force extensions to be loaded from the network */
final static boolean allowLocalExtensions=true;
String pakage; /* expected package of loaded classes */
public URLClassLoader ( URLu, Stringpakage) {
this.u = u;
this.pakage = pakage;
cache = Hashtable();
}
public synchronized Class loadClass ( Stringname, booleanresolve) throws ClassNotFoundException{
Classc = (Class) cache.get(name);
if (c == null)) {
if (allowLocalExtensions || name.indexOf(pakage) == -1))
{
c = findSystemClass(name);
} catch (ClassNotFoundExceptione)) {
}
}
if (c == null)) {
byteb[] = loadClassData(name);
c = defineClass(b, 0, b.length);
cache.put(name, c);
}
if (resolve))
resolveClass(c);
return c;
}
private byte loadClassData ( Stringname) throws ClassNotFoundException{
byte[] b;
InputStreamtheClass = null;
intbfr = 128;
if (!allowNetworkExtensions))
throw ClassNotFoundException(name + " not authorized");
try {
name = name.replace('.', '/');
URLclassURL = URL(u, name + ".class");
System.out.println("<!- loading class ""+classURL.toString()+"" ->");
URLConnectionuc = classURL.openConnection();
// on the advice of my attorney, uc.getInputStream before uc.getContentLength
try {
theClass = uc.getInputStream();
} catch (NullPointerExceptione)) {
System.err.println(e);
throw ClassNotFoundException(name + " input stream problem");
}
intcl = uc.getContentLength();
b = byte[cl == -1 ? bfr * 16 : cl];
intred = 0;
intoffset = 0;
while (red >= 0)) {
red = theClass.read(b, offset, bfr);
if (red == -1))
break;
offset += red;
if (cl == -1 && offset == b.length)) { // grow array
bytetemp[] = byte[offset * 2];
System.arraycopy(b, 0, temp, 0, offset);
b = temp;
} elseif (offset > b.length)) // class longer than advertised
throw ClassNotFoundException(name + " error reading data into array");
}
if (offset < b.length)) { // shrink
bytetemp[] = byte[offset];
System.arraycopy(b, 0, temp, 0, offset);
b = temp;
}
} catch (Exceptione)) {
throw ClassNotFoundException(name + "" + e);
}
finally {
try {
if (theClass != null))
theClass.close();
}catch (IOExceptione)) {
}
}
return b;
}[]