home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / ClassLoader.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  177 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.  
  24. /**
  25.  * ClassLoader is an abstract Class that can be used to define a policy
  26.  * for loading Java classes into the runtime environment. By default,
  27.  * the runtime system loads classes that originate as files by reading 
  28.  * them from the directory defined by the <tt>CLASSPATH</tt> environment
  29.  * variable (this is platform dependent). The default mechanism does not involve
  30.  * a Class loader. <p>
  31.  *
  32.  * However, some classes may not originate from a file; they could be
  33.  * loaded from some other source, e.g., the network. Classes loaded
  34.  * from the network are an array of bytes. A ClassLoader can be used to
  35.  * tell the runtime system to convert an array of bytes into an instance
  36.  * of class Class.
  37.  * This conversion information is passed to the runtime using the defineClass()
  38.  * method.<p>
  39.  *
  40.  * Classes that are created through the defineClass() mechanism can 
  41.  * reference other classes by name. To resolve those names, the runtime
  42.  * system calls the ClassLoader that originally created the Class.
  43.  * The runtime system calls the abstract method loadClass() to load
  44.  * the referenced classes.<p>
  45.  * <pre>
  46.  *     ClassLoader loader = new NetworkClassLoader(host, port);
  47.  *      Object main = loader.loadClass("Main").newInstance();
  48.  *    ....
  49.  * </pre>
  50.  *
  51.  * The NetworkClassLoader subclass must define the method loadClass() to 
  52.  * load a Class from the network. Once it has downloaded the bytes
  53.  * that make up the Class it should use the method defineClass() to create a Class
  54.  * instance. A sample implementation could be:
  55.  * <pre>
  56.  *    class NetworkClassLoader {
  57.  *        String host;
  58.  *        int port;
  59.  *        Hashtable cache = new Hashtable();
  60.  *
  61.  *        private byte loadClassData(String name)[] {
  62.  *        // load the class data from the connection
  63.  *        ...
  64.  *        }
  65.  *
  66.  *        public synchronized Class loadClass(String name) {
  67.  *            Class c = cache.get(name);
  68.  *        if (c == null) {
  69.  *            byte data[] = loadClassData(name);
  70.  *            cache.put(name, defineClass(data, 0, data.length));
  71.  *        }
  72.  *        return c;
  73.  *        }
  74.  *    }
  75.  * </pre>
  76.  * @see        Class
  77.  * @version     1.30, 27 Mar 1996
  78.  * @author    Arthur van Hoff
  79.  */
  80.  
  81. public abstract class ClassLoader {
  82.  
  83.     /**
  84.      * If initialization succeed this is set to true and security checks will
  85.      * succeed. Otherwise the object is not initialized and the object is
  86.      * useless.
  87.      */
  88.     private boolean initialized = false;
  89.  
  90.     /**
  91.      * Constructs a new Class loader and initializes it.
  92.      */
  93.     protected ClassLoader() {
  94.     SecurityManager security = System.getSecurityManager();
  95.     if (security != null) {
  96.         security.checkCreateClassLoader();
  97.     }
  98.     init();
  99.     initialized = true;
  100.     }
  101.  
  102.     /**
  103.      * Resolves the specified name to a Class. The method loadClass() is 
  104.      * called by the virtual machine.
  105.      * As an abstract method, loadClass() must be defined in a subclass of 
  106.      * ClassLoader. By using a Hashtable, you can avoid loading the same 
  107.      * Class more than once. 
  108.      * @param    name    the name of the desired Class
  109.      * @param resolve true if the Class needs to be resolved
  110.      * @return        the resulting Class, or null if it was not found.
  111.      * @exception ClassNotFoundException 
  112.      *                         Cannot find a definition for the class
  113.      * @see        java.util.Hashtable
  114.      */
  115.     protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException;
  116.  
  117.     /**
  118.      * Converts an array of bytes to an instance of class Class. Before the
  119.      * Class can be used it must be resolved.
  120.      * @param    data    the bytes that make up the Class
  121.      * @param    offset    the start offset of the Class data
  122.      * @param    length    the length of the Class data
  123.      * @return        the Class object which was created from the data.
  124.      * @exception ClassFormatError If the data does not contain a valid 
  125.      * Class.
  126.      * @see         ClassLoader#loadClass
  127.      * @see         ClassLoader#resolveClass
  128.      */
  129.     protected final Class defineClass(byte data[], int offset, int length) { 
  130.     check();
  131.     return defineClass0(data, offset, length);
  132.     }
  133.  
  134.     /**
  135.      * Resolves classes referenced by this Class. This must be done before the
  136.      * Class can be used. Class names referenced by the resulting Class are
  137.      * resolved by calling loadClass().
  138.      * @param    c    the Class to be resolved
  139.      * @see         ClassLoader#defineClass
  140.      */
  141.     protected final void resolveClass(Class c) { 
  142.     check();
  143.     resolveClass0(c);
  144.     }
  145.  
  146.     /**
  147.      * Loads a system Class. A system Class is a class with the
  148.      * primordial Class loader (which is null).
  149.      * @param name the name of the system Class
  150.      * @exception NoClassDefFoundError If the Class is not found.
  151.      * @exception ClassNotFoundException 
  152.      *                         Cannot find a definition for the class
  153.      */
  154.     protected final Class findSystemClass(String name) 
  155.     throws ClassNotFoundException {
  156.     check();
  157.     return findSystemClass0(name);
  158.     }
  159.  
  160.  
  161.     /**
  162.      * Initializes the Class loader.
  163.      */
  164.     private native void init();
  165.     private native Class defineClass0(byte data[], int offset, int length);
  166.     private native void resolveClass0(Class c);
  167.     private native Class findSystemClass0(String name) 
  168.         throws ClassNotFoundException;
  169.  
  170.     private void check() { 
  171.     if (initialized == true) 
  172.         return;
  173.     throw new SecurityException("Security Exception: ClassLoader object not initialized.");
  174.     }
  175. }
  176.  
  177.