home *** CD-ROM | disk | FTP | other *** search
/ Performance Now / PFE4.iso / worldnet / ns3230d / disk2 / java.z / java_30 / netscape / applet / AppletClassLoader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-08-15  |  8.1 KB  |  314 lines

  1. package netscape.applet;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.Hashtable;
  9. import java.util.Vector;
  10.  
  11. final class AppletClassLoader extends ClassLoader {
  12.    Hashtable classes;
  13.    URL codeBaseURL;
  14.    URL archiveURL;
  15.    MozillaAppletContext context;
  16.    int nativeZipFile;
  17.    static boolean wantTiming;
  18.    long loaderTime;
  19.    boolean mayScript = false;
  20.    static Vector classloaders = new Vector(10);
  21.    int refCount = 0;
  22.  
  23.    static synchronized AppletClassLoader getClassLoader(MozillaAppletContext cx, URL codebase, URL archive, boolean mayScript) {
  24.       AppletClassLoader loader = null;
  25.  
  26.       for(int i = 0; i < classloaders.size(); ++i) {
  27.          AppletClassLoader test = (AppletClassLoader)classloaders.elementAt(i);
  28.          if (codebase.equals(test.codeBaseURL) && archive.equals(test.archiveURL) && mayScript == test.mayScript && (!mayScript || cx == test.context)) {
  29.             loader = test;
  30.             break;
  31.          }
  32.       }
  33.  
  34.       if (loader == null) {
  35.          loader = new AppletClassLoader(cx, codebase, archive);
  36.          loader.mayScript = mayScript;
  37.          classloaders.addElement(loader);
  38.       }
  39.  
  40.       ++loader.refCount;
  41.       return loader;
  42.    }
  43.  
  44.    void releaseClassLoader() {
  45.       if (this.refCount == 0) {
  46.          throw new InternalError("tried to release unshared classloader");
  47.       } else {
  48.          if (MozillaAppletContext.debug > 0) {
  49.             System.err.println("# released reference to: " + this);
  50.          }
  51.  
  52.          if (--this.refCount == 0) {
  53.             classloaders.removeElement(this);
  54.             if (MozillaAppletContext.debug > 0) {
  55.                System.err.println("# removed: " + this);
  56.             }
  57.          }
  58.  
  59.       }
  60.    }
  61.  
  62.    AppletClassLoader(MozillaAppletContext cx, URL codebase, URL archive) {
  63.       String file = archive.getFile();
  64.       if (file.endsWith(".zip")) {
  65.          try {
  66.             if (wantTiming) {
  67.                long before = System.currentTimeMillis();
  68.                this.nativeZipFile = this.openZipFile(archive.openStream());
  69.                long after = System.currentTimeMillis();
  70.                long duration = after - before;
  71.                this.loaderTime += duration;
  72.                System.err.println("# Zip download time: " + archive + ": " + duration + " (total = " + this.loaderTime + ")");
  73.             } else {
  74.                this.nativeZipFile = this.openZipFile(archive.openStream());
  75.             }
  76.  
  77.             if (this.context != null && MozillaAppletContext.debug > 0) {
  78.                System.err.println("# Loading classes from downloaded zip file: " + archive);
  79.             }
  80.          } catch (IOException var12) {
  81.             System.err.println("# Failed to pull over zip file " + archive);
  82.          }
  83.       }
  84.  
  85.       file = codebase.getFile();
  86.       int i = file.lastIndexOf(47);
  87.       if (i > 0 && i < file.length() - 1) {
  88.          try {
  89.             codebase = new URL(codebase, file.substring(0, i + 1));
  90.          } catch (MalformedURLException var11) {
  91.          }
  92.       }
  93.  
  94.       this.codeBaseURL = codebase;
  95.       this.archiveURL = archive;
  96.       this.context = cx;
  97.       this.classes = new Hashtable();
  98.    }
  99.  
  100.    native int openZipFile(InputStream var1) throws IOException;
  101.  
  102.    native byte[] loadFromZipFile(int var1, String var2) throws IOException;
  103.  
  104.    native void closeZipFile(int var1) throws IOException;
  105.  
  106.    void close() {
  107.       if (this.nativeZipFile != 0) {
  108.          try {
  109.             this.closeZipFile(this.nativeZipFile);
  110.             if (this.context != null && MozillaAppletContext.debug > 0) {
  111.                System.err.println("# Closed downloaded zip file: " + this.codeBaseURL);
  112.             }
  113.          } catch (IOException var1) {
  114.             if (this.context != null && MozillaAppletContext.debug > 0) {
  115.                System.err.println("# Failed to close downloaded zip file: " + this.codeBaseURL);
  116.             }
  117.          }
  118.  
  119.          this.nativeZipFile = 0;
  120.       }
  121.  
  122.    }
  123.  
  124.    protected void finalize() {
  125.       this.close();
  126.       if (MozillaAppletContext.debug > 0) {
  127.          System.err.println("# finalized: " + this);
  128.       }
  129.  
  130.    }
  131.  
  132.    boolean mayScript() {
  133.       return this.mayScript;
  134.    }
  135.  
  136.    private Class loadClass(String name, URL url) throws IOException {
  137.       byte[] data = this.readURL(url);
  138.       return ((ClassLoader)this).defineClass(name, data, 0, data.length);
  139.    }
  140.  
  141.    public Class loadClass(String name) throws ClassNotFoundException {
  142.       return this.loadClass(name, true);
  143.    }
  144.  
  145.    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
  146.       Class clazz;
  147.       if (wantTiming) {
  148.          long before = System.currentTimeMillis();
  149.          clazz = this.loadClass1(name, resolve);
  150.          long after = System.currentTimeMillis();
  151.          long duration = after - before;
  152.          this.loaderTime += duration;
  153.          System.err.println("# Class load time: " + name + ": " + duration + " (total = " + this.loaderTime + ")");
  154.       } else {
  155.          clazz = this.loadClass1(name, resolve);
  156.       }
  157.  
  158.       return clazz;
  159.    }
  160.  
  161.    Class loadClass1(String name, boolean resolve) throws ClassNotFoundException {
  162.       Class cl = (Class)this.classes.get(name);
  163.       if (cl == null) {
  164.          SecurityManager security = System.getSecurityManager();
  165.          if (security != null) {
  166.             int i = name.lastIndexOf(46);
  167.             if (i >= 0) {
  168.                security.checkPackageAccess(name.substring(0, i));
  169.             }
  170.          }
  171.  
  172.          try {
  173.             return ((ClassLoader)this).findSystemClass(name);
  174.          } catch (ClassNotFoundException var6) {
  175.             cl = this.findClass(name);
  176.          }
  177.       }
  178.  
  179.       if (cl == null) {
  180.          throw new ClassNotFoundException(name);
  181.       } else {
  182.          if (resolve) {
  183.             ((ClassLoader)this).resolveClass(cl);
  184.          }
  185.  
  186.          return cl;
  187.       }
  188.    }
  189.  
  190.    private synchronized Class findClass(String name) throws ClassNotFoundException {
  191.       Class cl = (Class)this.classes.get(name);
  192.       if (cl != null) {
  193.          return cl;
  194.       } else {
  195.          if (this.context != null && MozillaAppletContext.debug > 0) {
  196.             System.err.println("# Find class " + name);
  197.          }
  198.  
  199.          SecurityManager security = System.getSecurityManager();
  200.          if (security != null) {
  201.             int i = name.lastIndexOf(46);
  202.             if (i >= 0) {
  203.                security.checkPackageDefinition(name.substring(0, i));
  204.             }
  205.          }
  206.  
  207.          String cname = name.replace('.', '/') + ".class";
  208.          if (this.nativeZipFile != 0) {
  209.             try {
  210.                byte[] data = this.loadFromZipFile(this.nativeZipFile, cname);
  211.                if (data != null) {
  212.                   cl = ((ClassLoader)this).defineClass(name, data, 0, data.length);
  213.                   if (cl != null && this.context != null && MozillaAppletContext.debug > 0) {
  214.                      System.err.println("# Loaded " + cname + " from downloaded zip file.");
  215.                   }
  216.                }
  217.             } catch (IOException var8) {
  218.                if (MozillaAppletContext.debug > 0) {
  219.                   System.err.println("# Failed to load " + cname + " from downloaded zip file.");
  220.                }
  221.             }
  222.          }
  223.  
  224.          if (cl == null) {
  225.             URL url;
  226.             try {
  227.                url = new URL(this.codeBaseURL, cname);
  228.             } catch (MalformedURLException var7) {
  229.                throw new ClassNotFoundException(name);
  230.             }
  231.  
  232.             if (this.context != null && MozillaAppletContext.debug > 0) {
  233.                System.err.println("# Fetching " + url);
  234.             }
  235.  
  236.             try {
  237.                cl = this.loadClass(name, url);
  238.             } catch (IOException var6) {
  239.                throw new ClassNotFoundException(name);
  240.             }
  241.          }
  242.  
  243.          if (!name.equals(cl.getName())) {
  244.             Class oldcl = cl;
  245.             Class var9 = null;
  246.             throw new ClassFormatError(name + " != " + oldcl.getName());
  247.          } else {
  248.             this.classes.put(name, cl);
  249.             return cl;
  250.          }
  251.       }
  252.    }
  253.  
  254.    byte[] getResource(URL url) {
  255.       byte[] data = null;
  256.       String urlFile = url.getFile();
  257.       String codebaseFile = this.codeBaseURL.getFile();
  258.       System.out.println("url=" + url + " codebase=" + this.codeBaseURL);
  259.       if (!urlFile.startsWith(codebaseFile)) {
  260.          return null;
  261.       } else {
  262.          String resourcePath = urlFile.substring(codebaseFile.length());
  263.          System.out.println("resourcePath=" + resourcePath);
  264.  
  265.          try {
  266.             data = this.loadFromZipFile(this.nativeZipFile, resourcePath);
  267.             if (data != null && this.context != null && MozillaAppletContext.debug > 0) {
  268.                System.err.println("# Loaded " + resourcePath + " from downloaded zip file.");
  269.             }
  270.          } catch (IOException var6) {
  271.             if (MozillaAppletContext.debug > 0) {
  272.                System.err.println("# Failed to load " + resourcePath + " from downloaded zip file.");
  273.             }
  274.          }
  275.  
  276.          return data;
  277.       }
  278.    }
  279.  
  280.    byte[] readURL(URL url) throws IOException {
  281.       InputStream in = null;
  282.  
  283.       byte[] data;
  284.       try {
  285.          URLConnection c = url.openConnection();
  286.          c.setAllowUserInteraction(false);
  287.          in = c.getInputStream();
  288.          int len = c.getContentLength();
  289.          data = new byte[len == -1 ? 4096 : len];
  290.          int total = 0;
  291.  
  292.          int n;
  293.          while((n = in.read(data, total, data.length - total)) >= 0) {
  294.             if ((total += n) == data.length) {
  295.                if (len >= 0) {
  296.                   break;
  297.                }
  298.  
  299.                byte[] newdata = new byte[total * 2];
  300.                System.arraycopy(data, 0, newdata, 0, total);
  301.                data = newdata;
  302.             }
  303.          }
  304.       } finally {
  305.          if (in != null) {
  306.             in.close();
  307.          }
  308.  
  309.       }
  310.  
  311.       return data;
  312.    }
  313. }
  314.