home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ClassLoader.java < prev    next >
Text File  |  1998-01-23  |  16KB  |  417 lines

  1. /*
  2.  * @(#)ClassLoader.java    1.57 97/08/25
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.lang;
  24.  
  25. import java.io.InputStream;
  26. import java.util.Hashtable;
  27.  
  28. /**
  29.  * The class <code>ClassLoader</code> is an abstract class. 
  30.  * Applications implement subclasses of <code>ClassLoader</code> in 
  31.  * order to extend the manner in which the Java Virtual Machine 
  32.  * dynamically loads classes. 
  33.  * <p>
  34.  * Normally, the Java Virtual Machine loads classes from the local 
  35.  * file system in a platform-dependent manner. For example, on UNIX 
  36.  * systems, the Virtual Machine loads classes from the directory 
  37.  * defined by the <code>CLASSPATH</code> environment variable. 
  38.  * <p>
  39.  * However, some classes may not originate from a file; they may 
  40.  * originate from other sources, such as the network, or they could 
  41.  * be constructed by an application. The method 
  42.  * <code>defineClass</code> converts an array of bytes into an 
  43.  * instance of class <code>Class</code>. Instances of this newly 
  44.  * defined class can be created using the <code>newInstance</code> 
  45.  * method in class <code>Class</code>. 
  46.  * <p>
  47.  * The methods and constructors of objects created by a class loader 
  48.  * may reference other classes. To determine the class(es) referred 
  49.  * to, the Java Virtual Machine calls the <code>loadClass</code> 
  50.  * method of the class loader that originally created the class. If 
  51.  * the Java Virtual Machine only needs to determine if the class 
  52.  * exists and if it does exist to know its superclass, the 
  53.  * <code>resolve</code> flag is set to <code>false</code>. However, 
  54.  * if an instance of the class is being created or any of its methods 
  55.  * are being called, the class must also be resolved. In this case 
  56.  * the <code>resolve</code> flag is set to <code>true</code>, and the 
  57.  * <code>resolveClass</code> method should be called. 
  58.  * <p>
  59.  * For example, an application could create a network class loader 
  60.  * to download class files from a server. Sample code might look like:
  61.  * <ul><code>
  62.  *   ClassLoader loader = new NetworkClassLoader(host, port);<br>
  63.  *   Object main = loader.loadClass("Main", true).newInstance();<br>
  64.  *      . . .
  65.  * </code></ul>
  66.  * <p>
  67.  * The network class loader subclass must define the method 
  68.  * <code>loadClass</code> to load a class from the network. Once it 
  69.  * has downloaded the bytes that make up the class, it should use the 
  70.  * method <code>defineClass</code> to create a class instance. A 
  71.  * sample implementation is: 
  72.  * <p><hr><blockquote><pre>
  73.  *     class NetworkClassLoader {
  74.  *         String host;
  75.  *         int port;
  76.  *         Hashtable cache = new Hashtable();
  77.  *         private byte loadClassData(String name)[] {
  78.  *         // load the class data from the connection
  79.  *          . . .
  80.  *         }
  81.  * 
  82.  *         public synchronized Class loadClass(String name,
  83.  *                                             boolean resolve) {
  84.  *             Class c = cache.get(name);
  85.  *             if (c == null) {
  86.  *                 byte data[] = loadClassData(name);
  87.  *                 c = defineClass(data, 0, data.length);
  88.  *                 cache.put(name, c);
  89.  *             }
  90.  *             if (resolve)
  91.  *                 resolveClass(c);
  92.  *             return c;
  93.  *         }
  94.  *     }
  95.  * </pre></blockquote><hr>
  96.  *
  97.  * @author  Arthur van Hoff
  98.  * @version 1.57, 08/25/97
  99.  * @see     java.lang.Class
  100.  * @see     java.lang.Class#newInstance()
  101.  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
  102.  * @see     java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  103.  * @see     java.lang.ClassLoader#resolveClass(java.lang.Class)
  104.  * @since   JDK1.0
  105.  */
  106. public abstract class ClassLoader {
  107.     /**
  108.      * If initialization succeed this is set to true and security checks will
  109.      * succeed. Otherwise the object is not initialized and the object is
  110.      * useless.
  111.      */
  112.     private boolean initialized = false;
  113.  
  114.     /**
  115.      * This is a mapping of all String => Class done by this classloader.
  116.      * Each Class in the table will either have a null class loader, or 
  117.      * "this" as its classloader
  118.      */
  119.     private Hashtable classes = new Hashtable();
  120.  
  121.     /**
  122.      * Constructs a new class loader and initializes it. 
  123.      * <p>
  124.      * If there is a security manager, its 
  125.      * <code>checkCreateClassLoader</code> method is called. This may 
  126.      * result in a security exception. 
  127.      *
  128.      * @exception  SecurityException  if the current thread does not have
  129.      *               permission to create a new class loader.
  130.      * @see       java.lang.SecurityException
  131.      * @see       java.lang.SecurityManager#checkCreateClassLoader()
  132.      * @since     JDK1.0
  133.      */
  134.     protected ClassLoader() {
  135.     SecurityManager security = System.getSecurityManager();
  136.     if (security != null) {
  137.         security.checkCreateClassLoader();
  138.     }
  139.     init();
  140.     initialized = true;
  141.     }
  142.  
  143.     /**
  144.      * Requests the class loader to load and resolve a class with the specified 
  145.      * name. The <code>loadClass</code> method is called by the Java 
  146.      * Virtual Machine when a class loaded by a class loader first 
  147.      * references another class. Every subclass of class 
  148.      * <code>ClassLoader</code> must define this method. 
  149.      * <p>
  150.      * Class loaders should use a hashtable or other cache to avoid 
  151.      * defining classes with the same name multiple times. 
  152.      *
  153.      * @param      name      the name of the desired <code>Class</code>.
  154.      * @return     the resulting <code>Class</code>, or <code>null</code>
  155.      *             if it was not found.
  156.      * @exception  ClassNotFoundException  if the class loader cannot find
  157.      *               a definition for the class.
  158.      * @since      JDK1.1
  159.      */
  160.     public Class loadClass(String name) throws ClassNotFoundException
  161.     {
  162.     return loadClass(name, true);
  163.     }
  164.  
  165.     /**
  166.      * Resolves the specified name to a Class. The method loadClass() is 
  167.      * called by the virtual machine.
  168.      * <p>
  169.      * If the <code>resolve</code> flag is true, the method should call 
  170.      * the <code>resolveClass</code> method on the resulting class object.
  171.      * <p>
  172.      * As an abstract method, loadClass() must be defined in a subclass of 
  173.      * ClassLoader. By using a Hashtable, you can avoid loading the same 
  174.      * Class more than once.
  175.      * 
  176.      * @param       name     the name of the desired Class.
  177.      * @param      resolve  true if the Class needs to be resolved.
  178.      * @return       the resulting Class, or null if it was not found.
  179.      * @exception  ClassNotFoundException  if the class loader cannot find
  180.      *               a definition for the class.
  181.      * @see       java.util.Hashtable
  182.      * @since      JDK1.0
  183.      */
  184.     protected abstract Class loadClass(String name, boolean resolve)
  185.     throws ClassNotFoundException;
  186.  
  187.     /**
  188.      * Converts an array of bytes into an instance of class 
  189.      * <code>Class</code>. 
  190.      * Before the Class can be used it must be resolved.  This
  191.      * method is deprecated in favor of the version that takes a
  192.      * "name" as a first argument, and is more secure.
  193.      *
  194.      * @param      data     the bytes that make up the <code>Class</code>.
  195.      * @param      offset   the start offset of the <code>Class</code> data.
  196.      * @param      length   the length of the <code>Class</code> data.
  197.      * @return     the <code>Class</code> object that was created from the data.
  198.      * @exception  ClassFormatError  if the data does not contain a valid Class.
  199.      * @see        ClassLoader#loadClass(java.lang.String, boolean)
  200.      * @see        ClassLoader#resolveClass(java.lang.Class)
  201.      * @since      JDK1.0
  202.      * @deprecated Replaced by defineClass(java.lang.String, byte[], int, int).
  203.      */
  204.     protected final Class defineClass(byte data[], int offset, int length) { 
  205.     return defineClass(null, data, offset, length);
  206.     }
  207.  
  208.     /**
  209.      * Converts an array of bytes to an instance of class
  210.      * Class. Before the Class can be used it must be resolved.
  211.      *
  212.      * @param       name     the expected name of the class; null if unknown;
  213.      *                      using '.' and not '/' as separator, and without
  214.      *                      a trailing ".class" suffix.
  215.      * @param      data     the bytes that make up the <code>Class</code>.
  216.      * @param      offset   the start offset of the <code>Class</code> data.
  217.      * @param      length   the length of the <code>Class</code> data.
  218.      * @return     the <code>Class</code> object that was created from the data.
  219.      * @exception  ClassFormatError  if the data does not contain a valid Class.
  220.      * @see        ClassLoader#loadClass(java.lang.String, boolean)
  221.      * @see        ClassLoader#resolveClass(java.lang.Class)
  222.      * @since      JDK1.1
  223.      */
  224.     protected final Class defineClass(String name,
  225.                       byte data[], int offset, int length) { 
  226.     check();
  227.     Class result = defineClass0(name, data, offset, length);
  228.     if (result != null) 
  229.         classes.put(result.getName(), result);
  230.     return result;
  231.     }
  232.  
  233.     /**
  234.      * Resolves the class so that an instance of the class can be 
  235.      * created, or so that one of its methods can be called. This method 
  236.      * should be called by <code>loadClass</code> if the resolve flag is 
  237.      * <code>true</code>. 
  238.      *
  239.      * @param   c   the <code>Class</code> instance to be resolved.
  240.      * @see     java.lang.ClassLoader#defineClass(java.lang.String, byte[], int, int)
  241.      * @since   JDK1.0
  242.      */
  243.     protected final void resolveClass(Class c) { 
  244.     check();
  245.     resolveClass0(c);
  246.     }
  247.  
  248.     /**
  249.      * Finds the system class with the specified name, loading it in if 
  250.      * necessary. 
  251.      * <p>
  252.      * A system class is a class loaded from the local file system in a 
  253.      * platform- dependent way. It has no class loader. 
  254.      *
  255.      * @param      name   the name of the system <code>class</code>.
  256.      * @return     a system class with the given name.
  257.      * @exception  ClassNotFoundException  if it could not find a definition
  258.      *               for the class.
  259.      * @exception  NoClassDefFoundError    if the class is not found.
  260.      * @since      JDK1.0
  261.      */
  262.     protected final Class findSystemClass(String name) 
  263.     throws ClassNotFoundException {
  264.     check();
  265.     return findSystemClass0(name);
  266.     }
  267.  
  268.     /**
  269.      * Sets the signers of a class. This is called after defining a class,
  270.      * by signature-aware class loading code.
  271.      *
  272.      * @since   JDK1.1
  273.      */
  274.     protected final void setSigners(Class cl, Object[] signers) {
  275.         check();
  276.     // make a check which will take cl.getClassLoader and
  277.     // check if it is != this.
  278.     cl.setSigners(signers);
  279.     }
  280.  
  281.     /**
  282.      * Initializes the Class loader.
  283.      */
  284.     private native void init();
  285.     private native Class defineClass0(String name, 
  286.                       byte data[], int offset, int length);
  287.     private native void resolveClass0(Class c);
  288.     private native Class findSystemClass0(String name) 
  289.         throws ClassNotFoundException;
  290.  
  291.     private void check() { 
  292.     if (initialized == true) 
  293.         return;
  294.     throw new SecurityException("ClassLoader object not initialized.");
  295.     }
  296.  
  297.     /**
  298.      * @since   JDK1.1
  299.      */
  300.     final protected Class findLoadedClass(String name) { 
  301.     return (Class)classes.get(name);
  302.     }
  303.  
  304.     /**
  305.      * Load and resolve a class.
  306.      */
  307.     final Class loadClassInternal(String name, boolean resolve) 
  308.                     throws ClassNotFoundException {
  309.     name = name.replace('/', '.');
  310.     Class cl = (Class)classes.get(name);
  311.     if (cl == null) {
  312.         cl = loadClass(name, false);
  313.         if (cl == null) 
  314.         throw new ClassNotFoundException(name);
  315.         String realName = cl.getName();
  316.         if (!realName.equals(name)) { 
  317.         throw new ClassNotFoundException(name);
  318.         }
  319.         classes.put(realName, cl);
  320.     }
  321.     if (resolve) 
  322.         resolveClass(cl);
  323.     return cl;
  324.     }
  325.  
  326.     /**
  327.      * A resource is some data (images, audio, text, etc) that wants to be
  328.      * accessed by some class code in a way that is independent of the
  329.      * location of the code.  Resources are found with cooperation of the
  330.      * class loaders, since they are the only ones who know where the class
  331.      * actually came from. <p>
  332.      *
  333.      * System resources are those that are handled by the host implemenation
  334.      * directly.  For example, they may be located in the CLASSPATH.<p>
  335.      *
  336.      * The name of a resource is a "/"-separated sequence of identifiers.
  337.      * The class Class provides convenience methods for accessing resources;
  338.      * the methods implement a convention where the package name is prefixed
  339.      * to the short name of the resource.<p>
  340.      * 
  341.      * Resources can be accessed as an InputStream, or as a URL.
  342.      *
  343.      * @see    Class
  344.      */
  345.  
  346.     /**
  347.      * Get an InputStream on a given resource..  Will return null if no
  348.      * resource with this name is found. <p>
  349.      *
  350.      * The resource name may be any system resource (e.g. follows CLASSPATH
  351.      * order).
  352.      *
  353.      * @param    name    the name of the resource, to be used as is.
  354.      * @return    an InputStream on the resource, or null if not found.
  355.      * @since   JDK1.1
  356.      */
  357.     public static final InputStream getSystemResourceAsStream(String name) {
  358.     // REMIND - This is equivalent to getSystemResource() call plus a openStream()
  359.     return getSystemResourceAsStream0(name);
  360.     }
  361.  
  362.     /**
  363.      * Find a resource with a given name.  The return is a URL to the resource
  364.      * Doing a getContent() on the URL may return an Image, an AudioClip, or
  365.      * an InputStream.<p>
  366.      *
  367.      * The resource name may be any system resource (e.g. follows CLASSPATH
  368.      * order).
  369.      *
  370.      * @param    name    the name of the resource, to be used as is.
  371.      * @return    the URL on the resource, or null if not found.
  372.      * @since   JDK1.1
  373.      */
  374.     public static final java.net.URL getSystemResource(String name) {
  375.     String s = getSystemResourceAsName0(name);
  376.     java.net.URL back;
  377.     try {
  378.         back = new java.net.URL(s);
  379.     } catch (Exception ex) {
  380.         back = null;
  381.     }
  382.     return back;
  383.     }
  384.  
  385.     /**
  386.      * Get an InputStream on a given resource.  Will return null if no
  387.      * resource with this name is found. <p>
  388.      *
  389.      * The class loader can choose what to do to locate the resource.
  390.      *
  391.      * @param    name    the name of the resource, to be used as is.
  392.      * @return    an InputStream on the resource, or null if not found.
  393.      * @since   JDK1.1
  394.      */
  395.     public InputStream getResourceAsStream(String name) {
  396.     return null;
  397.     }
  398.  
  399.     /**
  400.      * Find a resource with a given name.  The return is a URL to the resource.
  401.      * Doing a getContent() on the URL may return an Image, an AudioClip,
  402.      * or an InputStream.<p>
  403.      *
  404.      * The class loader can choose what to do to locate the resource.
  405.      *
  406.      * @param    name    the name of the resource, to be used as is.
  407.      * @return    an InputStream on the resource, or null if not found.
  408.      * @since   JDK1.1
  409.      */
  410.     public java.net.URL getResource(String name) {
  411.     return null;
  412.     }
  413.  
  414.     private static native InputStream getSystemResourceAsStream0(String name);
  415.     private static native String getSystemResourceAsName0(String name);
  416. }
  417.