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

  1. /*
  2.  * @(#)AppletClassLoader.java    1.25 96/03/25  
  3.  *
  4.  * Copyright (c) 1994-1996 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 sun.applet;
  21.  
  22. import java.util.Hashtable;
  23. import java.io.InputStream;
  24. import java.io.IOException;
  25. import java.io.FileNotFoundException;
  26. import java.net.URL;
  27. import java.net.URLConnection;
  28. import java.net.MalformedURLException;
  29. import sun.misc.MessageUtils;
  30.  
  31. /**
  32.  * This class defines an applet class loader.
  33.  *
  34.  * @version     1.25, 03/25/96
  35.  * @author     Arthur van Hoff
  36.  */
  37. class AppletClassLoader extends ClassLoader {
  38.     boolean verboseClassLoading = false;
  39.     
  40.     Hashtable classes = new Hashtable();
  41.     URL base;
  42.  
  43.     /**
  44.      * Load applets from a base URL.
  45.      */
  46.     AppletClassLoader(URL base) {
  47.     this.base = base;
  48.     }
  49.  
  50.     /**
  51.      * Load a class from a URL.
  52.      */
  53.     private Class loadClass(URL url) throws IOException {
  54.     InputStream in = null;
  55.     try {
  56.         try {
  57.         URLConnection c = url.openConnection();
  58.         c.setAllowUserInteraction(false);
  59.         in = c.getInputStream();
  60.  
  61.         int len = c.getContentLength();
  62.         byte data[] = new byte[(len == -1) ? 4096 : len];
  63.         int total = 0, n;
  64.         
  65.         Thread thread = Thread.currentThread();
  66.         
  67.         while (((n = in.read(data, total, data.length - total)) >= 0) && !thread.isInterrupted()) {
  68.             if ((total += n) == data.length) {
  69.             if (len < 0) {
  70.                 byte newdata[] = new byte[total * 2];
  71.                 System.arraycopy(data, 0, newdata, 0, total);
  72.                 data = newdata;
  73.             } else {
  74.                 break;
  75.             }
  76.             }
  77.         }
  78.         if (thread.isInterrupted()) {
  79.             throw new IOException("class loading interrupted: " + url);
  80.         }
  81.  
  82.         return defineClass(data, 0, total);
  83.         } finally {
  84.         if (in != null) {
  85.             in.close();
  86.         }
  87.         }
  88.     } catch (IOException e) {
  89.         e.printStackTrace();
  90.         throw e;
  91.     } catch (Throwable e) {
  92.         e.printStackTrace();
  93.         throw new IOException("class not loaded: " + url);
  94.     }
  95.     }
  96.  
  97.     /**
  98.      * Load a class from this class loader.
  99.      */
  100.     public Class loadClass(String name) throws ClassNotFoundException {
  101.     return loadClass(name, true);
  102.     }
  103.  
  104.     /**
  105.      * Load and resolve a class.
  106.      */
  107.     protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
  108.     Class cl = (Class)classes.get(name);
  109.     if (cl == null) {
  110.         SecurityManager security = System.getSecurityManager();
  111.         if (security != null) {
  112.         int i = name.lastIndexOf('.');
  113.         if (i >= 0) {
  114.             security.checkPackageAccess(name.substring(0, i));
  115.         }
  116.         }
  117.         try {
  118.         return findSystemClass(name);
  119.         } catch (Throwable e) {
  120.         }
  121.  
  122.         cl = findClass(name);
  123.     }
  124.     if (cl == null) {
  125.         throw new ClassNotFoundException(name);
  126.     }
  127.     if (resolve) {
  128.         resolveClass(cl);
  129.     }
  130.     return cl;
  131.     }
  132.  
  133.     void errorMsg(String key, String arg) {
  134.     System.err.println(MessageUtils.substProp("appletloader."+key,
  135.                           arg));
  136.     }
  137.  
  138.     void errorMsg(String key, String arg1, String arg2) {
  139.     System.err.println(MessageUtils.substProp("appletloader."+key,
  140.                           arg1, arg2));
  141.     }
  142.  
  143.     /**
  144.      * This method finds a class. The returned class
  145.      * may be unresolved. This method has to be synchronized
  146.      * to avoid two threads loading the same class at the same time.
  147.      * Must be called with the actual class name.
  148.      */
  149.     private synchronized Class findClass(String name) {
  150.     Class cl = (Class)classes.get(name);
  151.     if (cl != null) {
  152.         return cl;
  153.     }
  154.  
  155.     if (verboseClassLoading)
  156.         System.out.println(Thread.currentThread().getName() + " find class " + name);
  157.  
  158.     SecurityManager security = System.getSecurityManager();
  159.     if (security != null) {
  160.         int i = name.lastIndexOf('.');
  161.         if (i >= 0) {
  162.         security.checkPackageDefinition(name.substring(0, i));
  163.         }
  164.     }
  165.  
  166.     String cname = name.replace('.', '/') + ".class";
  167.  
  168.     try {
  169.         URL url = new URL(base, cname);
  170.         if (verboseClassLoading)
  171.             System.out.println("Opening stream to: " + url + " to get " + name);
  172.         cl = loadClass(url);
  173.         if (!name.equals(cl.getName())) {
  174.         Class oldcl = cl;
  175.         cl = null;
  176.         throw new ClassFormatError(name + " != " + oldcl.getName());
  177.         }
  178.         classes.put(name, cl);
  179.     } catch (FileNotFoundException e) {
  180.         errorMsg("filenotfound", name);
  181.     } catch (ClassFormatError e) {
  182.         errorMsg("fileformat", name);
  183.     } catch (IOException e) {
  184.         errorMsg("fileioexception", name);
  185.     } catch (Exception e) {
  186.         errorMsg("fileexception", e.toString(), name);
  187.     } catch (ThreadDeath e) {
  188.         errorMsg("filedeath", name);
  189.         throw e;
  190.     } catch (Error e) {
  191.         errorMsg("fileerror", e.toString(), name);
  192.     }
  193.     return cl;
  194.     }
  195. }
  196.