home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Hashtable;
- import java.util.Vector;
-
- final class AppletClassLoader extends ClassLoader {
- Hashtable classes;
- URL codeBaseURL;
- URL archiveURL;
- MozillaAppletContext context;
- int nativeZipFile;
- static boolean wantTiming;
- long loaderTime;
- boolean mayScript = false;
- static Vector classloaders = new Vector(10);
- int refCount = 0;
-
- static synchronized AppletClassLoader getClassLoader(MozillaAppletContext cx, URL codebase, URL archive, boolean mayScript) {
- AppletClassLoader loader = null;
-
- for(int i = 0; i < classloaders.size(); ++i) {
- AppletClassLoader test = (AppletClassLoader)classloaders.elementAt(i);
- if (codebase.equals(test.codeBaseURL) && archive.equals(test.archiveURL) && mayScript == test.mayScript && (!mayScript || cx == test.context)) {
- loader = test;
- break;
- }
- }
-
- if (loader == null) {
- loader = new AppletClassLoader(cx, codebase, archive);
- loader.mayScript = mayScript;
- classloaders.addElement(loader);
- }
-
- ++loader.refCount;
- return loader;
- }
-
- void releaseClassLoader() {
- if (this.refCount == 0) {
- throw new InternalError("tried to release unshared classloader");
- } else {
- if (MozillaAppletContext.debug > 0) {
- System.err.println("# released reference to: " + this);
- }
-
- if (--this.refCount == 0) {
- classloaders.removeElement(this);
- if (MozillaAppletContext.debug > 0) {
- System.err.println("# removed: " + this);
- }
- }
-
- }
- }
-
- AppletClassLoader(MozillaAppletContext cx, URL codebase, URL archive) {
- String file = archive.getFile();
- if (file.endsWith(".zip")) {
- try {
- if (wantTiming) {
- long before = System.currentTimeMillis();
- this.nativeZipFile = this.openZipFile(archive.openStream());
- long after = System.currentTimeMillis();
- long duration = after - before;
- this.loaderTime += duration;
- System.err.println("# Zip download time: " + archive + ": " + duration + " (total = " + this.loaderTime + ")");
- } else {
- this.nativeZipFile = this.openZipFile(archive.openStream());
- }
-
- if (this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Loading classes from downloaded zip file: " + archive);
- }
- } catch (IOException var12) {
- System.err.println("# Failed to pull over zip file " + archive);
- }
- }
-
- file = codebase.getFile();
- int i = file.lastIndexOf(47);
- if (i > 0 && i < file.length() - 1) {
- try {
- codebase = new URL(codebase, file.substring(0, i + 1));
- } catch (MalformedURLException var11) {
- }
- }
-
- this.codeBaseURL = codebase;
- this.archiveURL = archive;
- this.context = cx;
- this.classes = new Hashtable();
- }
-
- native int openZipFile(InputStream var1) throws IOException;
-
- native byte[] loadFromZipFile(int var1, String var2) throws IOException;
-
- native void closeZipFile(int var1) throws IOException;
-
- void close() {
- if (this.nativeZipFile != 0) {
- try {
- this.closeZipFile(this.nativeZipFile);
- if (this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Closed downloaded zip file: " + this.codeBaseURL);
- }
- } catch (IOException var1) {
- if (this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Failed to close downloaded zip file: " + this.codeBaseURL);
- }
- }
-
- this.nativeZipFile = 0;
- }
-
- }
-
- protected void finalize() {
- this.close();
- if (MozillaAppletContext.debug > 0) {
- System.err.println("# finalized: " + this);
- }
-
- }
-
- boolean mayScript() {
- return this.mayScript;
- }
-
- private Class loadClass(String name, URL url) throws IOException {
- byte[] data = this.readURL(url);
- return ((ClassLoader)this).defineClass(name, data, 0, data.length);
- }
-
- public Class loadClass(String name) throws ClassNotFoundException {
- return this.loadClass(name, true);
- }
-
- protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
- Class clazz;
- if (wantTiming) {
- long before = System.currentTimeMillis();
- clazz = this.loadClass1(name, resolve);
- long after = System.currentTimeMillis();
- long duration = after - before;
- this.loaderTime += duration;
- System.err.println("# Class load time: " + name + ": " + duration + " (total = " + this.loaderTime + ")");
- } else {
- clazz = this.loadClass1(name, resolve);
- }
-
- return clazz;
- }
-
- Class loadClass1(String name, boolean resolve) throws ClassNotFoundException {
- Class cl = (Class)this.classes.get(name);
- if (cl == null) {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- int i = name.lastIndexOf(46);
- if (i >= 0) {
- security.checkPackageAccess(name.substring(0, i));
- }
- }
-
- try {
- return ((ClassLoader)this).findSystemClass(name);
- } catch (ClassNotFoundException var6) {
- cl = this.findClass(name);
- }
- }
-
- if (cl == null) {
- throw new ClassNotFoundException(name);
- } else {
- if (this.nativeZipFile != 0) {
- SecurityManager.checksURLConnect(this.archiveURL);
- }
-
- if (resolve) {
- ((ClassLoader)this).resolveClass(cl);
- }
-
- return cl;
- }
- }
-
- private synchronized Class findClass(String name) throws ClassNotFoundException {
- Class cl = (Class)this.classes.get(name);
- if (cl != null) {
- return cl;
- } else {
- if (this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Find class " + name);
- }
-
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- int i = name.lastIndexOf(46);
- if (i >= 0) {
- security.checkPackageDefinition(name.substring(0, i));
- }
- }
-
- String cname = name.replace('.', '/') + ".class";
- if (this.nativeZipFile != 0) {
- try {
- byte[] data = this.loadFromZipFile(this.nativeZipFile, cname);
- if (data != null) {
- cl = ((ClassLoader)this).defineClass(name, data, 0, data.length);
- if (cl != null && this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Loaded " + cname + " from downloaded zip file.");
- }
- }
- } catch (IOException var8) {
- if (MozillaAppletContext.debug > 0) {
- System.err.println("# Failed to load " + cname + " from downloaded zip file.");
- }
- }
- }
-
- if (cl == null) {
- URL url;
- try {
- url = new URL(this.codeBaseURL, cname);
- } catch (MalformedURLException var7) {
- throw new ClassNotFoundException(name);
- }
-
- if (this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Fetching " + url);
- }
-
- try {
- cl = this.loadClass(name, url);
- } catch (IOException var6) {
- throw new ClassNotFoundException(name);
- }
- }
-
- if (!name.equals(cl.getName())) {
- Class oldcl = cl;
- Class var9 = null;
- throw new ClassFormatError(name + " != " + oldcl.getName());
- } else {
- this.classes.put(name, cl);
- return cl;
- }
- }
- }
-
- byte[] getResource(URL url) {
- byte[] data = null;
- String urlFile = url.getFile();
- String codebaseFile = this.codeBaseURL.getFile();
- System.out.println("url=" + url + " codebase=" + this.codeBaseURL);
- if (!urlFile.startsWith(codebaseFile)) {
- return null;
- } else {
- String resourcePath = urlFile.substring(codebaseFile.length());
- System.out.println("resourcePath=" + resourcePath);
-
- try {
- data = this.loadFromZipFile(this.nativeZipFile, resourcePath);
- if (data != null && this.context != null && MozillaAppletContext.debug > 0) {
- System.err.println("# Loaded " + resourcePath + " from downloaded zip file.");
- }
- } catch (IOException var6) {
- if (MozillaAppletContext.debug > 0) {
- System.err.println("# Failed to load " + resourcePath + " from downloaded zip file.");
- }
- }
-
- return data;
- }
- }
-
- byte[] readURL(URL url) throws IOException {
- InputStream in = null;
-
- byte[] data;
- try {
- URLConnection c = url.openConnection();
- c.setAllowUserInteraction(false);
- in = c.getInputStream();
- int len = c.getContentLength();
- data = new byte[len == -1 ? 4096 : len];
- int total = 0;
-
- int n;
- while((n = in.read(data, total, data.length - total)) >= 0) {
- if ((total += n) == data.length) {
- if (len >= 0) {
- break;
- }
-
- byte[] newdata = new byte[total * 2];
- System.arraycopy(data, 0, newdata, 0, total);
- data = newdata;
- }
- }
- } finally {
- if (in != null) {
- in.close();
- }
-
- }
-
- return data;
- }
- }
-