home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.applet.AudioClip;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.Window;
- import java.io.PrintStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import netscape.net.URLStreamHandlerFactory;
- import sun.awt.image.URLImageSource;
-
- class MozillaAppletContext implements AppletContext {
- int frameMWContext;
- HistoryElement history;
- Hashtable appletFrames = new Hashtable();
- Hashtable imageCache = new Hashtable();
- Hashtable audioClipCache = new Hashtable();
- Hashtable classLoaders = new Hashtable();
- URL documentURL;
- static int debug;
- static Console console;
- static int totalApplets;
- static final int MAX_APPLETS = 10;
- static final int APPLETS_PER_WINDOW = 5;
-
- void dumpState(PrintStream out, int i) {
- MozillaWindow.indent(out, i);
- out.println("MozillaAppletContext #frames=" + this.appletFrames.size() + " #images=" + this.imageCache.size() + " #audioClips=" + this.audioClipCache.size() + " #classLoaders=" + this.classLoaders.size() + " url=" + this.documentURL);
- Enumeration e = this.appletFrames.elements();
-
- while(e.hasMoreElements()) {
- EmbeddedAppletFrame eaf = (EmbeddedAppletFrame)e.nextElement();
- eaf.dumpState(out, i + 1);
- }
-
- if (this.imageCache.size() > 0) {
- MozillaWindow.indent(out, i + 1);
- out.println("image cache:");
- Enumeration e = this.imageCache.keys();
-
- while(e.hasMoreElements()) {
- URL u = (URL)e.nextElement();
- MozillaWindow.indent(out, i + 2);
- out.println(u.toString());
- }
- }
-
- if (this.audioClipCache.size() > 0) {
- MozillaWindow.indent(out, i + 1);
- out.println("audio clip cache:");
- Enumeration e = this.audioClipCache.keys();
-
- while(e.hasMoreElements()) {
- URL u = (URL)e.nextElement();
- MozillaWindow.indent(out, i + 2);
- out.println(u.toString());
- }
- }
-
- if (this.classLoaders.size() > 0) {
- MozillaWindow.indent(out, i + 1);
- out.println("class loader cache:");
- Enumeration e = this.classLoaders.keys();
-
- while(e.hasMoreElements()) {
- URL u = (URL)e.nextElement();
- MozillaWindow.indent(out, i + 2);
- out.println(u.toString());
- }
- }
-
- }
-
- void destroy() {
- this.frameMWContext = 0;
- this.appletFrames = null;
- this.imageCache = null;
- this.audioClipCache = null;
- }
-
- void trim() {
- this.destroyApplets();
- this.destroy();
- }
-
- Class loadClass(URL codebase, String code) throws ClassNotFoundException {
- AppletClassLoader loader = lookupClassLoader(this, codebase);
- return loader.loadClass(code);
- }
-
- public AudioClip getAudioClip(URL url) {
- AudioClip clip = lookupAudioClip(this, url);
- return clip;
- }
-
- public Image getImage(URL url) {
- Image img = lookupImage(this, url);
- return img;
- }
-
- public Applet getApplet(String name) {
- name = name.toLowerCase();
- Enumeration e = this.appletFrames.elements();
-
- while(e.hasMoreElements()) {
- EmbeddedAppletFrame p = (EmbeddedAppletFrame)e.nextElement();
- if (name.equals(p.applet.getParameter("name"))) {
- return p.applet;
- }
- }
-
- return null;
- }
-
- public Enumeration getApplets() {
- Vector v = new Vector();
- Enumeration e = this.appletFrames.elements();
-
- while(e.hasMoreElements()) {
- EmbeddedAppletFrame p = (EmbeddedAppletFrame)e.nextElement();
- v.addElement(p.applet);
- }
-
- return v.elements();
- }
-
- public void showDocument(URL url) {
- this.pShowDocument(url.toExternalForm(), url.getHost(), "_top");
- }
-
- public void showDocument(URL url, String target) {
- this.pShowDocument(url.toExternalForm(), url.getHost(), target);
- }
-
- public void showStatus(String status) {
- this.pShowStatus(status);
- }
-
- private native void pShowDocument(String var1, String var2, String var3);
-
- private native void pShowStatus(String var1);
-
- static synchronized AppletClassLoader lookupClassLoader(MozillaAppletContext incx, URL url) {
- Enumeration e = HistoryElement.elements();
-
- while(e.hasMoreElements()) {
- HistoryElement h = (HistoryElement)e.nextElement();
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- Object loader = acx.classLoaders.get(url);
- if (loader != null) {
- return (AppletClassLoader)loader;
- }
- }
- }
-
- AppletClassLoader loader = new AppletClassLoader(incx, url);
- incx.classLoaders.put(url, loader);
- if (debug > 0) {
- System.err.println("# New class loader: " + url);
- }
-
- return loader;
- }
-
- static synchronized AudioClip lookupAudioClip(MozillaAppletContext incx, URL url) {
- Enumeration e = HistoryElement.elements();
-
- while(e.hasMoreElements()) {
- HistoryElement h = (HistoryElement)e.nextElement();
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- Object clip = acx.audioClipCache.get(url);
- if (clip != null) {
- return (AudioClip)clip;
- }
- }
- }
-
- AudioClip clip = new AppletAudioClip(url);
- incx.audioClipCache.put(url, clip);
- if (debug > 0) {
- System.err.println("# New audio clip: " + url);
- }
-
- return clip;
- }
-
- static synchronized Image lookupImage(MozillaAppletContext incx, URL url) {
- Enumeration e = HistoryElement.elements();
-
- while(e.hasMoreElements()) {
- HistoryElement h = (HistoryElement)e.nextElement();
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- Object image = acx.imageCache.get(url);
- if (image != null) {
- return (Image)image;
- }
- }
- }
-
- Image image;
- try {
- URLImageSource source = new URLImageSource(url);
- image = Toolkit.getDefaultToolkit().createImage(source);
- incx.imageCache.put(url, image);
- if (debug > 0) {
- System.err.println("# New image: " + url);
- }
- } catch (Exception var6) {
- image = null;
- }
-
- return image;
- }
-
- synchronized void initApplet(int pLJAppletData, String[] argv) {
- Hashtable params = new Hashtable();
-
- for(int i = 1; i < argv.length; ++i) {
- String str = argv[i];
- int j = str.indexOf(61);
- if (j >= 0) {
- params.put(str.substring(0, j).toLowerCase(), str.substring(j + 1));
- }
- }
-
- String dbg = (String)params.get("debug");
-
- try {
- debug = Integer.parseInt(dbg);
- } catch (Exception var15) {
- }
-
- try {
- this.documentURL = new URL(argv[0]);
- } catch (MalformedURLException var16) {
- if (debug > 0) {
- System.err.println("# Malformed documentURL: " + argv[0]);
- }
-
- return;
- }
-
- String codebase = (String)params.get("codebase");
- if (codebase == null) {
- codebase = argv[0];
- int tail = codebase.lastIndexOf(47) + 1;
- codebase = codebase.substring(0, tail);
- } else {
- if (!codebase.endsWith("/")) {
- codebase = codebase + "/";
- }
-
- try {
- URL u = new URL(this.documentURL, codebase);
- codebase = u.toString();
- int tail = codebase.lastIndexOf(47) + 1;
- if (tail != codebase.length() - 1) {
- codebase = codebase.substring(0, tail);
- }
- } catch (MalformedURLException var14) {
- }
- }
-
- params.put("codebase", codebase);
- URL codebaseURL = this.documentURL;
-
- try {
- codebaseURL = new URL(codebase);
- } catch (MalformedURLException var13) {
- }
-
- Integer appKey = new Integer(pLJAppletData);
- EmbeddedAppletFrame frame = new EmbeddedAppletFrame(this.documentURL, codebaseURL, params, this, pLJAppletData);
- this.appletFrames.put(appKey, frame);
- ++totalApplets;
- if (debug > 0) {
- System.err.println("# Total applets " + totalApplets);
- }
-
- if (debug > 0) {
- String p = "";
-
- Object key;
- for(Enumeration e = params.keys(); e.hasMoreElements(); p = p + (String)key + "=" + (String)params.get(key) + " ") {
- key = e.nextElement();
- }
-
- System.err.println("# New applet: " + pLJAppletData + " at " + codebaseURL + " " + p);
- }
-
- frame.start();
- ((Window)frame).pack();
- ((Window)frame).show();
- frame.sendEvent(1);
- frame.sendEvent(2);
- }
-
- synchronized void startApplet(int pLJAppletData) {
- if (debug > 0) {
- System.err.println("# startApplet: appletID=" + pLJAppletData);
- }
-
- Integer appKey = new Integer(pLJAppletData);
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
- if (frame != null) {
- frame.sendEvent(3);
- this.history.start();
- } else {
- if (debug > 0) {
- System.err.println("# Warning: startApplet: appletID " + pLJAppletData + " not found!");
- }
-
- }
- }
-
- synchronized void stopApplet(int pLJAppletData) {
- if (debug > 0) {
- System.err.println("# stopApplet: appletID=" + pLJAppletData);
- }
-
- Integer appKey = new Integer(pLJAppletData);
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
- if (frame != null) {
- frame.sendEvent(4);
- this.history.stop();
- } else {
- if (debug > 0) {
- System.err.println("# Warning: stopApplet: appletID " + pLJAppletData + " not found!");
- }
-
- }
- }
-
- synchronized void destroyApplet(int pLJAppletData) {
- if (debug > 0) {
- System.err.println("# destroyApplet: appletID=" + pLJAppletData);
- }
-
- Integer appKey = new Integer(pLJAppletData);
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
- if (frame == null) {
- if (debug > 0) {
- System.err.println("# Warning: destroyApplet: appletID " + pLJAppletData + " not found!");
- }
-
- } else {
- --totalApplets;
- if (debug > 0) {
- System.err.println("# Total applets " + totalApplets);
- }
-
- this.appletFrames.remove(appKey);
- ThreadGroup group = frame.handler.getThreadGroup();
- synchronized(group){}
-
- try {
- frame.sendEvent(4);
- frame.sendEvent(5);
- frame.sendEvent(0);
-
- try {
- group.wait(1000L);
- } catch (InterruptedException var12) {
- }
-
- if (group.activeCount() > 0) {
- group.stop();
-
- try {
- group.wait(1000L);
- } catch (InterruptedException var11) {
- }
- }
-
- try {
- group.destroy();
- } catch (Exception var10) {
- }
- } catch (Throwable var13) {
- throw var13;
- }
-
- }
- }
-
- synchronized void iconifyApplets() {
- if (debug > 0) {
- System.err.println("# iconifyApplets");
- }
-
- Enumeration frames = this.appletFrames.elements();
-
- while(frames.hasMoreElements()) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)frames.nextElement();
- frame.sendEvent(4);
- this.history.stop();
- if (debug > 0) {
- System.err.println("# iconifyApplet: stopping appletID " + frame.pData);
- }
- }
-
- }
-
- synchronized void uniconifyApplets() {
- if (debug > 0) {
- System.err.println("# uniconifyApplets");
- }
-
- Enumeration frames = this.appletFrames.elements();
-
- while(frames.hasMoreElements()) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)frames.nextElement();
- frame.sendEvent(3);
- this.history.start();
- if (debug > 0) {
- System.err.println("# uniconifyApplet: starting appletID " + frame.pData);
- }
- }
-
- }
-
- synchronized void destroyApplets() {
- if (debug > 0) {
- System.err.println("# destroyApplets");
- }
-
- Enumeration e = this.appletFrames.keys();
-
- while(e.hasMoreElements()) {
- Integer frame = (Integer)e.nextElement();
- this.destroyApplet(frame);
- }
-
- }
-
- static void init() {
- console = new Console();
- System.setProperties(new AppletProperties());
- URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory());
- System.setSecurityManager(new AppletSecurity());
- }
-
- static void setConsoleState(int newstate) {
- setConsoleState0(newstate);
- }
-
- static native void setConsoleState0(int var0);
-
- static void showConsole() {
- console.show();
- }
-
- static void hideConsole() {
- console.hide();
- }
-
- static void trimOneFrame() {
- int numWindows = MozillaWindow.numWindows();
- int maxApplets = Math.max(numWindows * 5, 10);
- if (HistoryElement.hiddenElements != 0 && totalApplets > maxApplets) {
- HistoryElement.trim();
- }
-
- }
-
- static void initApplet(int parentContext, int frameContext, int historyID, int pLJAppletData, String[] argv) {
- if (debug > 0) {
- System.err.println("# initApplet: historyID=" + historyID + " appletID=" + pLJAppletData + " parentContext=" + parentContext + "frameContext=" + frameContext);
- }
-
- trimOneFrame();
- MozillaWindow win = MozillaWindow.lookupOrCreate(parentContext);
- HistoryElement h = HistoryElement.lookup(historyID);
- MozillaAppletContext acx;
- if (h == null) {
- MozillaFrame frame = win.lookupFrame(frameContext);
- if (frame == null) {
- frame = new MozillaFrame(win, frameContext);
- }
-
- acx = new MozillaAppletContext();
- h = new HistoryElement(historyID, frame, acx);
- } else if (h.appletContext == null) {
- acx = new MozillaAppletContext();
- h.resurrect(acx);
- } else {
- acx = h.appletContext;
- }
-
- acx.frameMWContext = frameContext;
- acx.history = h;
- acx.initApplet(pLJAppletData, argv);
- }
-
- static void startApplet(int historyID, int pLJAppletData, int newFrameMWContext) {
- if (debug > 0) {
- System.err.println("# startApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- acx.frameMWContext = newFrameMWContext;
- h.frame.setMWContext(newFrameMWContext);
- acx.startApplet(pLJAppletData);
- return;
- }
- } else if (debug > 0) {
- System.err.println("# Warning: startApplet: historyID " + historyID + " not found!");
- }
-
- }
-
- static void stopApplet(int historyID, int pLJAppletData) {
- if (debug > 0) {
- System.err.println("# stopApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- acx.stopApplet(pLJAppletData);
- acx.frameMWContext = 0;
- h.frame.setMWContext(0);
- return;
- }
- } else if (debug > 0) {
- System.err.println("# Warning: stopApplet: historyID " + historyID + " not found!");
- }
-
- }
-
- static void destroyApplet(int historyID, int pLJAppletData) {
- if (debug > 0) {
- System.err.println("# destroyApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- acx.destroyApplet(pLJAppletData);
- if (acx.appletFrames.isEmpty()) {
- if (debug > 0) {
- System.err.println("# destroy: historyID=" + historyID);
- }
-
- acx.destroy();
- h.destroy();
- return;
- }
- } else {
- h.destroy();
- }
- }
-
- }
-
- static void iconifyApplets(int historyID) {
- if (debug > 0) {
- System.err.println("# iconifyApplets: historyID=" + historyID);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- acx.iconifyApplets();
- }
- }
-
- }
-
- static void uniconifyApplets(int historyID) {
- if (debug > 0) {
- System.err.println("# uniconifyApplets: historyID=" + historyID);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- if (acx != null) {
- acx.uniconifyApplets();
- }
- }
-
- }
-
- static void destroyApplets(int historyID) {
- if (debug > 0) {
- System.err.println("# destroyApplets: historyID=" + historyID);
- }
-
- HistoryElement h = HistoryElement.lookup(historyID);
- if (h != null) {
- MozillaAppletContext acx = h.appletContext;
- acx.destroyApplets();
- if (debug > 0) {
- System.err.println("# destroyApplets: destroying historyID " + historyID);
- }
-
- acx.destroy();
- }
-
- }
-
- static void destroyAll() {
- if (debug > 0) {
- System.err.println("# destroyAll");
- }
-
- HistoryElement.destroyAll();
- }
- }
-