loader
Class NetworkClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--loader.NetworkClassLoader

public class NetworkClassLoader
extends java.lang.ClassLoader


Load a class from the local file system or from the network.
When a class is loaded with this loader, this loader becomes the default loader.
So all the other classes that are associated with this classe (super classes, or used classes) will be loaded with this loader.


First a class is searched in the local cache (implemented in java.lang.ClassLoader), the in the local file system, and if it's still not found, it is searched in the net.
In the cache, only classes from the network are added.
If all classes are available locally, no connection is established with the server.

Example:
ClassLoader loader = new NetworkClassLoader(host, port);
Object main = loader.loadClass("myClass").newInstance();

Since:
JDK1.2
Version:
1.2 - August 2000
Author:
Lorenzo Bettini - bettini@dsi.unifi.it, Donato Cappetta - cappetta@infomedia.it
See Also:
ClassLoader

Constructor Summary
NetworkClassLoader()
          default constructor
NetworkClassLoader(java.lang.String hostName, int serverPort)
          Costructor.
 
Method Summary
protected  void connect()
          connect to the ClassServer
protected  java.io.File createLocalResourceFile(java.lang.String name, byte[] bytes)
          create the local file for the resource and return the URL.
protected  java.lang.String createLocalResourceName(java.lang.String name)
          it creates a name for the local file that will store the resource contents.
protected  void disconnect()
          Close the connection to the ClassServer
protected  java.lang.Class findClass(java.lang.String className)
          Required method.
protected  java.net.URL findResource(java.lang.String name)
           
protected  java.net.URL getLocalResourceURL(java.io.File file)
          open the local file for the resource and return the URL.
 java.lang.Class loadClass(java.lang.String name, boolean resolve)
          Not required.
protected  byte[] loadClassFromServer(java.lang.String className)
          Read the class from the network and return a byte array.
protected  byte[] loadResourceFromServer(java.lang.String resourceName, java.lang.String type)
          Read the class from the network and return a byte array.
protected  void Print(java.lang.String s)
          Print on the screen with indentation to show the nesting of the calls.
protected  void sendRequest(java.lang.String name, java.lang.String type)
          send a request to the server
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResources, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkClassLoader

public NetworkClassLoader()
default constructor

NetworkClassLoader

public NetworkClassLoader(java.lang.String hostName,
                          int serverPort)
Costructor.
Parameters:
hostName - host to load the classes from
serverPort - port of the server
Method Detail

findClass

protected java.lang.Class findClass(java.lang.String className)
                             throws ConnectClassServerException,
                                    JavaPackageException,
                                    java.lang.ClassNotFoundException,
                                    java.lang.ClassFormatError
Required method. It is invoked directly from the parent class loadClass(String name) in java.lang.ClassLoader, if the class was not found int the cache and it couldn't be loaded by the system loader.
In this implementation the class is requested to the remote ClassServer.
Overrides:
findClass in class java.lang.ClassLoader
Parameters:
name - class name
Returns:
Class - the requested class.
Throws:
ConnectClassServerException - Error connecting to the ClassServer.
java.lang.ClassNotFoundException -  
java.lang.ClassFormatError - in converting from byte array to a Class.

findResource

protected java.net.URL findResource(java.lang.String name)
Overrides:
findResource in class java.lang.ClassLoader

getLocalResourceURL

protected java.net.URL getLocalResourceURL(java.io.File file)
                                    throws java.net.MalformedURLException
open the local file for the resource and return the URL.
Parameters:
file - the local File for the resource
Returns:
the URL for this file
Throws:
java.net.MalformedURLException - see java.net.MalformedURLException

createLocalResourceFile

protected java.io.File createLocalResourceFile(java.lang.String name,
                                               byte[] bytes)
                                        throws java.net.MalformedURLException,
                                               java.io.FileNotFoundException,
                                               java.io.IOException
create the local file for the resource and return the URL. The file will be deleted when the virtual machine terminates.
Parameters:
name - the name of the resource
bytes - the binary contents of this resource
Returns:
the new File object
Throws:
java.net.MalformedURLException - see java.net.MalformedURLException
java.io.FileNotFoundException - see java.io.FileNotFoundException
java.io.IOException - see java.io.IOException

createLocalResourceName

protected java.lang.String createLocalResourceName(java.lang.String name)
it creates a name for the local file that will store the resource contents.
Parameters:
name - the resource name
Returns:
a name for the local file for this resource

loadClass

public java.lang.Class loadClass(java.lang.String name,
                                 boolean resolve)
                          throws java.lang.ClassNotFoundException
Not required. It is here just to print on the screen when a class is being loaded.
Overrides:
loadClass in class java.lang.ClassLoader

Print

protected void Print(java.lang.String s)
Print on the screen with indentation to show the nesting of the calls.

connect

protected void connect()
                throws java.net.UnknownHostException,
                       java.io.IOException
connect to the ClassServer
Throws:
java.net.UnknownHostException - see java.net.UnknownHostException.
java.io.IOException - see java.io.IOException.

disconnect

protected void disconnect()
Close the connection to the ClassServer

loadClassFromServer

protected byte[] loadClassFromServer(java.lang.String className)
                              throws java.lang.ClassNotFoundException,
                                     java.net.SocketException,
                                     java.io.IOException
Read the class from the network and return a byte array.
Parameters:
className - class name
Returns:
byte[] byte array for the class.
Throws:
java.lang.ClassNotFoundException - class not found.
java.net.SocketException - see java.net.SocketException.
java.io.IOException - see java.io.IOException.

loadResourceFromServer

protected byte[] loadResourceFromServer(java.lang.String resourceName,
                                        java.lang.String type)
                                 throws java.io.FileNotFoundException,
                                        java.lang.ClassNotFoundException,
                                        java.net.SocketException,
                                        java.io.IOException
Read the class from the network and return a byte array.
Parameters:
resourceName - resource name
type - resource type
Returns:
byte[] byte array for the class.
Throws:
java.io.FileNotFoundException - class not found.
java.net.SocketException - see java.net.SocketException.
java.io.IOException - see java.io.IOException.

sendRequest

protected void sendRequest(java.lang.String name,
                           java.lang.String type)
                    throws java.io.IOException
send a request to the server
Parameters:
name - the name of the requested resource
type - the type of the requested resource
Throws:
java.io.IOException - see java.io.IOException.