home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-27 | 4.9 KB | 173 lines |
- /*
- * @(#)AppletZipClassLoader.java 1.3 96/03/15 David Connelly
- *
- * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package sun.applet;
-
- import java.util.Hashtable;
- import java.io.InputStream;
- import java.io.IOException;
- import java.io.File;
- import java.net.URL;
- import java.net.URLConnection;
- import java.lang.Thread;
- import java.net.MalformedURLException;
- import sun.tools.zip.ZipEntry;
- import sun.tools.zip.ZipReader;
- import sun.tools.zip.ZipFormatException;
-
- /**
- * This class defines an applet class loader for loading classes from
- * a zip file.
- *
- * @version 1.3, 03/15/96
- * @author David Connelly
- */
- class AppletZipClassLoader extends AppletClassLoader {
- Hashtable classBytes = new Hashtable();
-
- /**
- * Load applets from a base URL.
- */
- AppletZipClassLoader(URL base) throws IOException {
- super(base);
- System.out.println("loading zip file from " + base);
-
- // We might be reading the zip file from some arbitrary "distance"
- // away, so be prepared to stop graceuflly upon the user's request.
- Thread thread = Thread.currentThread();
-
- try {
- URLConnection c = base.openConnection();
- c.setAllowUserInteraction(false);
- ZipReader zr = new ZipReader(c.getInputStream());
- while (zr.nextEntry() && !thread.isInterrupted()) {
- ZipEntry ze = zr.getEntry();
- String path = ze.getPath();
- if (path.endsWith(".class")) {
- InputStream is = zr.getInputStream();
- int len = (int)ze.length();
- byte data[] = new byte[len];
- System.out.println(" " + path + " (" + len + " bytes)");
- int off = 0;
-
- while ((off < len) && !thread.isInterrupted()) {
- int n = is.read(data, off, len - off);
- if (n == -1) {
- throw new ZipFormatException("bad entry: " + path);
- }
- off += n;
- }
-
- if (thread.isInterrupted()) {
- break;
- }
-
-
- String name = path.substring(0, path.length() - 6)
- .replace('/', '.');
- classBytes.put(name, data);
- }
- }
-
- // Flow out to interrupt test.
- } catch (Throwable e) {
- e.printStackTrace();
- throw new IOException("could not load zip file: " + base);
- }
-
- if (thread.isInterrupted()) {
- // Repair any corrupted state
- classBytes = new Hashtable();
- throw new IOException("zip loading interrupted: " + base);
- }
- }
-
- /**
- * Load a class from this class loader.
- */
- public Class loadClass(String name) throws ClassNotFoundException {
- return loadClass(name, true);
- }
-
- /**
- * Load and resolve a class.
- */
- protected Class loadClass(String name, boolean resolve)
- throws ClassNotFoundException {
- Class cl = (Class)classes.get(name);
- if (cl == null) {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- int i = name.lastIndexOf('.');
- if (i >= 0) {
- security.checkPackageAccess(name.substring(0, i));
- }
- }
- try {
- return findSystemClass(name);
- } catch (Throwable e) {
- }
- cl = findClass(name);
- }
- if (cl == null) {
- throw new ClassNotFoundException(name);
- }
- if (resolve) {
- resolveClass(cl);
- }
- return cl;
- }
-
- /**
- * This method finds a class, and must be synchronized to avoid two
- * threads loading the same class at the same time. The returned class
- * may be unresolved.
- */
- private synchronized Class findClass(String name) {
- Class cl = (Class)classes.get(name);
- if (cl == null) {
- SecurityManager security = System.getSecurityManager();
- System.out.println(Thread.currentThread().getName() +
- " find class " + name);
-
- if (security != null) {
- int i = name.lastIndexOf('.');
- if (i >= 0) {
- security.checkPackageDefinition(name.substring(0, i));
- }
- }
- byte data[] = (byte [])classBytes.get(name);
- if (data != null) {
- try {
- cl = defineClass(data, 0, data.length);
- if (!name.equals(cl.getName())) {
- throw new ClassFormatError();
- }
- } catch (ClassFormatError e) {
- System.err.println("Class format error: " + name);
- return null;
- }
- classes.put(name, cl);
- classBytes.remove(name);
- }
- }
- return cl;
- }
- }
-