net.wotonomy.foundation.internal
Class NetworkClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by net.wotonomy.foundation.internal.NetworkClassLoader

public class NetworkClassLoader
extends java.lang.ClassLoader

The correct name for this class should be URLClassLoader. But there is already a class by that name in JDK1.2. I have had quite a few problems with URLClassLoader in past, so I ended up writing this ClassLoader. I found that the Java 2's URLClassLoader, does not close the Jar file once opened. It is a pretty good optimization step, but if you modify the class in the jar file, it does not pick it up. Some operating systems may not let you modify the jar file while it is still open. IMHO, it does make sense to close the jar file after you are done reading the class data. But this approach may not get you the performance of the URLClassLoader, but it works in all cases and also runs on JDK1.1. I have enhanced this class loader to read all the zip/jar entries once & cache the data, so that there is no overhead of opening/closing jar file to pick up each entry.

Author:
Harish Prabandham

Constructor Summary
NetworkClassLoader(java.lang.ClassLoader parent)
          Creates a new instance of the class loader.
 
Method Summary
 void addURL(java.net.URL url)
          Adds the given URL to this class loader.
 void clear()
          This method resets this ClassLoader's state.
protected  java.lang.Class defineClass(java.lang.String classname, byte[] classdata)
          java.lang.ClassLoader's defineClass method is final, so the its subclasses cannot override this method.
protected  void finalize()
          This method resets this ClassLoader's state and resets the references for garbage collection.
 java.lang.Class findClass(java.lang.String name)
          Call this to bypass the implementation of loadClass.
protected  java.net.URL findResource(java.lang.String name)
          Overridden to search for a resource and return a "jar"-style URL or normal "file" URL as necessary.
 java.io.InputStream getResourceAsStream(java.lang.String name)
           
 java.util.Enumeration getURLs()
           
 java.lang.Class loadClass(java.lang.String name, boolean resolve)
           
protected  byte[] loadClassData(java.lang.String classname)
           
protected  byte[] loadResource(java.lang.String resource)
           
protected  byte[] loadResource(java.net.URL url, java.lang.String resourceName)
           
protected  void setParent(java.lang.ClassLoader parent)
          Sets the parent/delegate class loader.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResources, findSystemClass, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkClassLoader

public NetworkClassLoader(java.lang.ClassLoader parent)
Creates a new instance of the class loader.

Parameters:
delegate/parent - class loader.
Method Detail

setParent

protected final void setParent(java.lang.ClassLoader parent)
Sets the parent/delegate class loader.

Parameters:
delegate/parent - class loader.

addURL

public void addURL(java.net.URL url)
Adds the given URL to this class loader. If the URL ends with "/", then it is assumed to be a directory otherwise, it is assumed to be a zip/jar file. If the same URL is added again, the URL is re-opened and this zip/jar file is used for serving any future class requests.

Parameters:
URL - where to look for the classes.

getURLs

public java.util.Enumeration getURLs()
Returns:
An enumeration of URLs where this class loader looks for classes.

findClass

public java.lang.Class findClass(java.lang.String name)
Call this to bypass the implementation of loadClass.

Overrides:
findClass in class java.lang.ClassLoader

loadResource

protected byte[] loadResource(java.net.URL url,
                              java.lang.String resourceName)
                       throws java.io.IOException
Throws:
java.io.IOException

loadResource

protected byte[] loadResource(java.lang.String resource)

loadClassData

protected byte[] loadClassData(java.lang.String classname)

findResource

protected java.net.URL findResource(java.lang.String name)
Overridden to search for a resource and return a "jar"-style URL or normal "file" URL as necessary.

Overrides:
findResource in class java.lang.ClassLoader

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Overrides:
getResourceAsStream in class java.lang.ClassLoader
Returns:
The resource as the input stream if such a resource exists, otherwise returns null.

defineClass

protected java.lang.Class defineClass(java.lang.String classname,
                                      byte[] classdata)
java.lang.ClassLoader's defineClass method is final, so the its subclasses cannot override this method. But, this class calls this method in the loadClass() instead.

Parameters:
The - name of the class without ".class" extension.
The - class data bytes.
Returns:
The class object.

loadClass

public java.lang.Class loadClass(java.lang.String name,
                                 boolean resolve)
                          throws java.lang.ClassNotFoundException
Overrides:
loadClass in class java.lang.ClassLoader
Throws:
java.lang.ClassNotFoundException

clear

public final void clear()
This method resets this ClassLoader's state. It completely removes all the URLs and classes in this class loader cache.


finalize

protected void finalize()
                 throws java.lang.Throwable
This method resets this ClassLoader's state and resets the references for garbage collection.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable


Copyright © 2006 null. All Rights Reserved.