> Subject: Re: How to create a NetworkClassLoader in JDK1.1.x?
> Author:  Volker.Augustin ([EMAIL PROTECTED]) at lon-mime
> Date:    13/03/99 09:09

It tooks me absolutely ages to work
out how to do a simple class loader.
The documentation was terrible. Anyhow,
I did it in the end - see the attachment.

Rich.

-- 
-     Richard Jones. Bibliotech: http://www.bibliotech.co.uk/      -
-         Embryonic homepage at: http://www.annexia.org/           -
- You are currently the 3,119,344,290th visitor to this signature. -
-    Original message content Copyright (C) 1998 Richard Jones.    -
/*
 * FLEET Copyright (C) 1998-1999 Richard W.M. Jones.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 * $Id: URLClassLoader.java,v 1.2 1999/01/16 19:38:01 rich Exp $
 *
 * $Log: URLClassLoader.java,v $
 * Revision 1.2  1999/01/16 19:38:01  rich
 * Started to experiment with CORBA for front-end to
 * server communications.
 *
 * Revision 1.1  1999/01/13 17:33:27  rich
 * Started to do network update code.
 *
 */

import java.net.*;
import java.io.*;
import java.util.*;

/**
 * This is a specialized class loader for loading classes
 * over the network (or from local storage) from a URL.
 */
public class URLClassLoader extends ClassLoader
{
  Hashtable cache = new Hashtable ();
  URL codeBase;

  /**
   * Create a URLClassLoader, giving the
   * appropriate codebase (URL). Classes
   * are loaded from:
   *<pre>
   *   CODEBASE name .class
   *</pre>
   */
  public URLClassLoader (URL codeBase)
  {
    this.codeBase = codeBase;
  }

  /**
   * Load the class. If the class cannot
   * be found anywhere else, then we try
   * to load it over the network from
   *<pre>
   *   CODEBASE name .class
   *</pre>
   */
  public synchronized Class loadClass (String className, boolean resolve)
    throws ClassNotFoundException
  {
    // Is it in the cache?
    Class c = (Class) cache.get (className);

    if (c == null)
      {
        // Try with the superclass first.
        try {
          c = super.findSystemClass(className);
          return c;
        } catch (ClassNotFoundException e) {
          // Not a system class.
        }

        // Try to load it over the network.
        byte[] data;

        try {
          URL url = new URL (codeBase, className + ".class");
          URLConnection connection = url.openConnection ();
          int length = connection.getContentLength ();
          data = new byte [length];

          InputStream input = connection.getInputStream ();
          try {
            input.read (data);
          }
          finally {
            input.close ();
          }
        }
        catch (MalformedURLException ex) {
          ex.printStackTrace ();
          throw new ClassNotFoundException ("MalformedURLException while loading 
class");
        }
        catch (IOException ex) {
          throw new ClassNotFoundException ("IOException while loading class");
        }

        c = defineClass (data, 0, data.length);
        if (c == null)
          return null;

        cache.put (className, c);
      }

    // Resolve the class, if asked to.
    if (resolve)
      resolveClass (c);

    return c;
  }

}

Reply via email to