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

  1. /*
  2.  * @(#)AppletZipClassLoader.java    1.3 96/03/15 David Connelly
  3.  *
  4.  * Copyright (c) 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.File;
  26. import java.net.URL;
  27. import java.net.URLConnection;
  28. import java.lang.Thread;
  29. import java.net.MalformedURLException;
  30. import sun.tools.zip.ZipEntry;
  31. import sun.tools.zip.ZipReader;
  32. import sun.tools.zip.ZipFormatException;
  33.  
  34. /**
  35.  * This class defines an applet class loader for loading classes from
  36.  * a zip file.
  37.  *
  38.  * @version     1.3, 03/15/96
  39.  * @author      David Connelly
  40.  */
  41. class AppletZipClassLoader extends AppletClassLoader {
  42.     Hashtable classBytes = new Hashtable();
  43.  
  44.     /**
  45.      * Load applets from a base URL.
  46.      */
  47.     AppletZipClassLoader(URL base) throws IOException {
  48.     super(base);
  49.     System.out.println("loading zip file from " + base);
  50.  
  51.     // We might be reading the zip file from some arbitrary "distance"
  52.     // away, so be prepared to stop graceuflly upon the user's request.
  53.     Thread thread = Thread.currentThread();
  54.  
  55.     try {
  56.         URLConnection c = base.openConnection();
  57.         c.setAllowUserInteraction(false);
  58.         ZipReader zr = new ZipReader(c.getInputStream());
  59.         while (zr.nextEntry() && !thread.isInterrupted()) {
  60.         ZipEntry ze = zr.getEntry();
  61.         String path = ze.getPath();
  62.         if (path.endsWith(".class")) {
  63.             InputStream is = zr.getInputStream();
  64.             int len = (int)ze.length();
  65.             byte data[] = new byte[len];
  66.             System.out.println("    " + path + " (" + len + " bytes)");
  67.             int off = 0;
  68.  
  69.             while ((off < len) && !thread.isInterrupted()) {
  70.             int n = is.read(data, off, len - off);
  71.             if (n == -1) {
  72.                 throw new ZipFormatException("bad entry: " + path);
  73.             }
  74.             off += n;
  75.             }
  76.  
  77.             if (thread.isInterrupted()) {
  78.             break;
  79.             }
  80.  
  81.  
  82.             String name = path.substring(0, path.length() - 6)
  83.                       .replace('/', '.');
  84.             classBytes.put(name, data);
  85.         }
  86.         }
  87.  
  88.         // Flow out to interrupt test.
  89.     } catch (Throwable e) {
  90.         e.printStackTrace();
  91.         throw new IOException("could not load zip file: " + base);
  92.     }
  93.  
  94.     if (thread.isInterrupted()) {
  95.         // Repair any corrupted state
  96.         classBytes = new Hashtable();
  97.         throw new IOException("zip loading interrupted: " + base);
  98.     }
  99.     }
  100.  
  101.     /**
  102.      * Load a class from this class loader.
  103.      */
  104.     public Class loadClass(String name) throws ClassNotFoundException {
  105.     return loadClass(name, true);
  106.     }
  107.  
  108.     /**
  109.      * Load and resolve a class.
  110.      */
  111.     protected Class loadClass(String name, boolean resolve)
  112.             throws ClassNotFoundException {
  113.     Class cl = (Class)classes.get(name);
  114.     if (cl == null) {
  115.         SecurityManager security = System.getSecurityManager();
  116.         if (security != null) {
  117.         int i = name.lastIndexOf('.');
  118.         if (i >= 0) {
  119.             security.checkPackageAccess(name.substring(0, i));
  120.         }
  121.         }
  122.         try {
  123.         return findSystemClass(name);
  124.         } catch (Throwable e) {
  125.         }
  126.         cl = findClass(name);
  127.     }
  128.     if (cl == null) {
  129.         throw new ClassNotFoundException(name);
  130.     }
  131.     if (resolve) {
  132.         resolveClass(cl);
  133.     }
  134.     return cl;
  135.     }
  136.  
  137.     /**
  138.      * This method finds a class, and must be synchronized to avoid two
  139.      * threads loading the same class at the same time. The returned class
  140.      * may be unresolved.
  141.      */
  142.     private synchronized Class findClass(String name) {
  143.     Class cl = (Class)classes.get(name);
  144.     if (cl == null) {
  145.         SecurityManager security = System.getSecurityManager();
  146.         System.out.println(Thread.currentThread().getName() +
  147.                    " find class " + name);
  148.  
  149.         if (security != null) {
  150.         int i = name.lastIndexOf('.');
  151.         if (i >= 0) {
  152.             security.checkPackageDefinition(name.substring(0, i));
  153.         }
  154.         }
  155.         byte data[] = (byte [])classBytes.get(name);
  156.         if (data != null) {
  157.         try {
  158.             cl = defineClass(data, 0, data.length);
  159.             if (!name.equals(cl.getName())) {
  160.             throw new ClassFormatError();
  161.             }
  162.         } catch (ClassFormatError e) {
  163.             System.err.println("Class format error: " + name);
  164.             return null;
  165.         }
  166.         classes.put(name, cl);
  167.         classBytes.remove(name);
  168.         }
  169.     }
  170.     return cl;
  171.     }
  172. }
  173.