home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / lang / ClassLoader.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  7.3 KB  |  197 lines

  1. /*
  2.  * @(#)ClassLoader.java    1.30 96/03/27 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.io.InputStream;
  23. import java.util.Hashtable;
  24.  
  25. /**
  26.  * ClassLoader is an abstract Class that can be used to define a policy
  27.  * for loading Java classes into the runtime environment. By default,
  28.  * the runtime system loads classes that originate as files by reading 
  29.  * them from the directory defined by the <tt>CLASSPATH</tt> environment
  30.  * variable (this is platform dependent). The default mechanism does not involve
  31.  * a Class loader. <p>
  32.  *
  33.  * However, some classes may not originate from a file; they could be
  34.  * loaded from some other source, e.g., the network. Classes loaded
  35.  * from the network are an array of bytes. A ClassLoader can be used to
  36.  * tell the runtime system to convert an array of bytes into an instance
  37.  * of class Class.
  38.  * This conversion information is passed to the runtime using the defineClass()
  39.  * method.<p>
  40.  *
  41.  * Classes that are created through the defineClass() mechanism can 
  42.  * reference other classes by name. To resolve those names, the runtime
  43.  * system calls the ClassLoader that originally created the Class.
  44.  * The runtime system calls the abstract method loadClass() to load
  45.  * the referenced classes.<p>
  46.  * <pre>
  47.  *     ClassLoader loader = new NetworkClassLoader(host, port);
  48.  *      Object main = loader.loadClass("Main").newInstance();
  49.  *    ....
  50.  * </pre>
  51.  *
  52.  * The NetworkClassLoader subclass must define the method loadClass() to 
  53.  * load a Class from the network. Once it has downloaded the bytes
  54.  * that make up the Class it should use the method defineClass() to create a Class
  55.  * instance. A sample implementation could be:
  56.  * <pre>
  57.  *    class NetworkClassLoader {
  58.  *        String host;
  59.  *        int port;
  60.  *        Hashtable cache = new Hashtable();
  61.  *
  62.  *        private byte loadClassData(String name)[] {
  63.  *        // load the class data from the connection
  64.  *        ...
  65.  *        }
  66.  *
  67.  *        public synchronized Class loadClass(String name) {
  68.  *            Class c = cache.get(name);
  69.  *        if (c == null) {
  70.  *            byte data[] = loadClassData(name);
  71.  *            cache.put(name, defineClass(data, 0, data.length));
  72.  *        }
  73.  *        return c;
  74.  *        }
  75.  *    }
  76.  * </pre>
  77.  * @see        Class
  78.  * @version     1.30, 27 Mar 1996
  79.  * @author    Arthur van Hoff
  80.  */
  81.  
  82. public abstract class ClassLoader {
  83.  
  84.     /**
  85.      * If initialization succeed this is set to true and security checks will
  86.      * succeed. Otherwise the object is not initialized and the object is
  87.      * useless.
  88.      */
  89.     private boolean initialized = false;
  90.     private int     PrivateData = 0;
  91.  
  92.     /**
  93.      * Constructs a new Class loader and initializes it.
  94.      */
  95.     protected ClassLoader() {
  96.     SecurityManager security = System.getSecurityManager();
  97.     if (security != null) {
  98.         security.checkCreateClassLoader();
  99.     }
  100.     init();
  101.     initialized = true;
  102.     }
  103.  
  104.     /**
  105.      * Resolves the specified name to a Class. The method loadClass() is 
  106.      * called by the virtual machine.
  107.      * As an abstract method, loadClass() must be defined in a subclass of 
  108.      * ClassLoader. By using a Hashtable, you can avoid loading the same 
  109.      * Class more than once. 
  110.      * @param    name    the name of the desired Class
  111.      * @param resolve true if the Class needs to be resolved
  112.      * @return        the resulting Class, or null if it was not found.
  113.      * @exception ClassNotFoundException 
  114.      *                         Cannot find a definition for the class
  115.      * @see        java.util.Hashtable
  116.      */
  117.     protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException;
  118.  
  119.     /**
  120.      * Converts an array of bytes to an instance of class Class. Before the
  121.      * Class can be used it must be resolved.
  122.      * @param    data    the bytes that make up the Class
  123.      * @param    offset    the start offset of the Class data
  124.      * @param    length    the length of the Class data
  125.      * @return        the Class object which was created from the data.
  126.      * @exception ClassFormatError If the data does not contain a valid 
  127.      * Class.
  128.      * @see         ClassLoader#loadClass
  129.      * @see         ClassLoader#resolveClass
  130.      */
  131.     protected final native Class defineClass(byte data[], int offset, int length);
  132.  
  133.     /**
  134.      * Resolves classes referenced by this Class. This must be done before the
  135.      * Class can be used. Class names referenced by the resulting Class are
  136.      * resolved by calling loadClass().
  137.      * @param    c    the Class to be resolved
  138.      * @see         ClassLoader#defineClass
  139.      */
  140.     protected final native void resolveClass(Class c);
  141.  
  142.     /**
  143.      * Loads a system Class. A system Class is a class with the
  144.      * primordial Class loader (which is null).
  145.      * @param name the name of the system Class
  146.      * @exception NoClassDefFoundError If the Class is not found.
  147.      * @exception ClassNotFoundException 
  148.      *                         Cannot find a definition for the class
  149.      */
  150.     protected final native Class findSystemClass(String name) throws ClassNotFoundException;
  151.  
  152.     /**
  153.      * Initializes the Class loader.
  154.      */
  155.     private native void init();
  156.  
  157.     private Hashtable arrays = new Hashtable();
  158.  
  159.     private final Class loadClassInternal(String name, boolean resolve) throws ClassNotFoundException
  160.     {
  161.         if (name.charAt(0) != '[') {
  162.             //  The specified name is not an array, so simply call the real
  163.             //  loadClass() method.
  164.             return loadClass(name, resolve);
  165.         } else {
  166.             Class cl = (Class)arrays.get(name);
  167.  
  168.             if (cl == null) {
  169.                 Class elementType;
  170.                 if (name.charAt(1) == '[') {
  171.                     elementType = loadClassInternal(name.substring(1), resolve);
  172.                 } else if (name.charAt(1) == 'L') {
  173.                     int end = name.indexOf(';');
  174.                     elementType = loadClass(name.substring(2, end), resolve);
  175.                 } else {
  176.                     throw new ClassNotFoundException(name);
  177.                 }
  178.  
  179.                 //  Guard against multiple threads attempting to
  180.                 //  create the same array class at the same time.
  181.                 synchronized(arrays) {
  182.                     //  Check if the class has already been created.
  183.                     cl = (Class)arrays.get(name);
  184.                     if (cl == null) {
  185.                         cl = createArrayClass(name, elementType);
  186.                         arrays.put(name, cl);
  187.                     }
  188.                 }
  189.             }
  190.             return cl;
  191.         }
  192.     }
  193.  
  194.     private final native Class createArrayClass(String name, Class elementType);
  195. }
  196.  
  197.