home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-27 | 5.3 KB | 196 lines |
- /*
- * @(#)AppletClassLoader.java 1.25 96/03/25
- *
- * Copyright (c) 1994-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.FileNotFoundException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.MalformedURLException;
- import sun.misc.MessageUtils;
-
- /**
- * This class defines an applet class loader.
- *
- * @version 1.25, 03/25/96
- * @author Arthur van Hoff
- */
- class AppletClassLoader extends ClassLoader {
- boolean verboseClassLoading = false;
-
- Hashtable classes = new Hashtable();
- URL base;
-
- /**
- * Load applets from a base URL.
- */
- AppletClassLoader(URL base) {
- this.base = base;
- }
-
- /**
- * Load a class from a URL.
- */
- private Class loadClass(URL url) throws IOException {
- InputStream in = null;
- try {
- try {
- URLConnection c = url.openConnection();
- c.setAllowUserInteraction(false);
- in = c.getInputStream();
-
- int len = c.getContentLength();
- byte data[] = new byte[(len == -1) ? 4096 : len];
- int total = 0, n;
-
- Thread thread = Thread.currentThread();
-
- while (((n = in.read(data, total, data.length - total)) >= 0) && !thread.isInterrupted()) {
- if ((total += n) == data.length) {
- if (len < 0) {
- byte newdata[] = new byte[total * 2];
- System.arraycopy(data, 0, newdata, 0, total);
- data = newdata;
- } else {
- break;
- }
- }
- }
- if (thread.isInterrupted()) {
- throw new IOException("class loading interrupted: " + url);
- }
-
- return defineClass(data, 0, total);
- } finally {
- if (in != null) {
- in.close();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- throw e;
- } catch (Throwable e) {
- e.printStackTrace();
- throw new IOException("class not loaded: " + url);
- }
- }
-
- /**
- * 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;
- }
-
- void errorMsg(String key, String arg) {
- System.err.println(MessageUtils.substProp("appletloader."+key,
- arg));
- }
-
- void errorMsg(String key, String arg1, String arg2) {
- System.err.println(MessageUtils.substProp("appletloader."+key,
- arg1, arg2));
- }
-
- /**
- * This method finds a class. The returned class
- * may be unresolved. This method has to be synchronized
- * to avoid two threads loading the same class at the same time.
- * Must be called with the actual class name.
- */
- private synchronized Class findClass(String name) {
- Class cl = (Class)classes.get(name);
- if (cl != null) {
- return cl;
- }
-
- if (verboseClassLoading)
- System.out.println(Thread.currentThread().getName() + " find class " + name);
-
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- int i = name.lastIndexOf('.');
- if (i >= 0) {
- security.checkPackageDefinition(name.substring(0, i));
- }
- }
-
- String cname = name.replace('.', '/') + ".class";
-
- try {
- URL url = new URL(base, cname);
- if (verboseClassLoading)
- System.out.println("Opening stream to: " + url + " to get " + name);
- cl = loadClass(url);
- if (!name.equals(cl.getName())) {
- Class oldcl = cl;
- cl = null;
- throw new ClassFormatError(name + " != " + oldcl.getName());
- }
- classes.put(name, cl);
- } catch (FileNotFoundException e) {
- errorMsg("filenotfound", name);
- } catch (ClassFormatError e) {
- errorMsg("fileformat", name);
- } catch (IOException e) {
- errorMsg("fileioexception", name);
- } catch (Exception e) {
- errorMsg("fileexception", e.toString(), name);
- } catch (ThreadDeath e) {
- errorMsg("filedeath", name);
- throw e;
- } catch (Error e) {
- errorMsg("fileerror", e.toString(), name);
- }
- return cl;
- }
- }
-