1318 lines · 46.2 KB · Java
RawDownload
                      break;                  }                 case APPLET_STOP:                    if (status != APPLET_START) {                        showAppletStatus("notstarted");                        break;                    }                    status = APPLET_STOP;                     // Hide the applet in event dispatch thread                    // to avoid deadlock.                    try {                        final Applet a = applet;                         EventQueue.invokeAndWait(new Runnable() {                                public void run()                                {                                    a.setVisible(false);                                }                            });                    }                    catch(InterruptedException ie) {                    }                    catch(InvocationTargetException ite) {                    }                      // During Applet.stop(), any AccessControlException on an involved Class remains in                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's                    // exceptionStatusSet flag to allow recognition of what had happened                    // when reusing AppletClassLoader object.                    try {                        applet.stop();                    } catch (java.security.AccessControlException e) {                        setExceptionStatus(e);                        // rethrow exception to be handled as it normally would be.                        throw e;                    }                    showAppletStatus("stopped");                    break;                 case APPLET_DESTROY:                    if (status != APPLET_STOP && status != APPLET_INIT) {                        showAppletStatus("notstopped");                        break;                    }                    status = APPLET_DESTROY;                     // During Applet.destroy(), any AccessControlException on an involved Class remains in                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's                    // exceptionStatusSet flag to allow recognition of what had happened                    // when reusing AppletClassLoader object.                    try {                        applet.destroy();                    } catch (java.security.AccessControlException e) {                        setExceptionStatus(e);                        // rethrow exception to be handled as it normally would be.                        throw e;                    }                    showAppletStatus("destroyed");                    break;                 case APPLET_DISPOSE:                    if (status != APPLET_DESTROY && status != APPLET_LOAD) {                        showAppletStatus("notdestroyed");                        break;                    }                    status = APPLET_DISPOSE;                     try                    {                        final Applet a = applet;                         EventQueue.invokeAndWait(new Runnable()                        {                            public void run()                            {                                remove(a);                            }                        });                    }                    catch(InterruptedException ie)                    {                    }                    catch(InvocationTargetException ite)                    {                    }                    applet = null;                    showAppletStatus("disposed");                    disposed = true;                    break;                 case APPLET_QUIT:                    return;                }            } catch (Exception e) {                status = APPLET_ERROR;                if (e.getMessage() != null) {                    showAppletStatus("exception2", e.getClass().getName(),                                     e.getMessage());                } else {                    showAppletStatus("exception", e.getClass().getName());                }                showAppletException(e);            } catch (ThreadDeath e) {                showAppletStatus("death");                return;            } catch (Error e) {                status = APPLET_ERROR;                if (e.getMessage() != null) {                    showAppletStatus("error2", e.getClass().getName(),                                     e.getMessage());                } else {                    showAppletStatus("error", e.getClass().getName());                }                showAppletException(e);            }            clearLoadAbortRequest();        }    }     /**     * Gets most recent focus owner component associated with the given window.     * It does that without calling Window.getMostRecentFocusOwner since it     * provides its own logic contradicting with setDefautlFocus. Instead, it     * calls KeyboardFocusManager directly.     */    private Component getMostRecentFocusOwnerForWindow(Window w) {        Method meth = (Method)AccessController.doPrivileged(new PrivilegedAction() {                public Object run() {                    Method meth = null;                    try {                        meth = KeyboardFocusManager.class.getDeclaredMethod("getMostRecentFocusOwner", new Class[] {Window.class});                        meth.setAccessible(true);                    } catch (Exception e) {                        // Must never happen                        e.printStackTrace();                    }                    return meth;                }            });        if (meth != null) {            // Meth refers static method            try {                return (Component)meth.invoke(null, new Object[] {w});            } catch (Exception e) {                // Must never happen                e.printStackTrace();            }        }        // Will get here if exception was thrown or meth is null        return w.getMostRecentFocusOwner();    }     /*     * Fix for BugTraq ID 4041703.     * Set the focus to a reasonable default for an Applet.     */    private void setDefaultFocus() {        Component toFocus = null;        Container parent = getParent();         if(parent != null) {            if (parent instanceof Window) {                toFocus = getMostRecentFocusOwnerForWindow((Window)parent);                if (toFocus == parent || toFocus == null) {                    toFocus = parent.getFocusTraversalPolicy().                        getInitialComponent((Window)parent);                }            } else if (parent.isFocusCycleRoot()) {                toFocus = parent.getFocusTraversalPolicy().                    getDefaultComponent(parent);            }        }         if (toFocus != null) {            if (parent instanceof EmbeddedFrame) {                ((EmbeddedFrame)parent).synthesizeWindowActivation(true);            }            // EmbeddedFrame might have focus before the applet was added.            // Thus after its activation the most recent focus owner will be            // restored. We need the applet's initial focusabled component to            // be focused here.            toFocus.requestFocusInWindow();        }    }     /**     * Load the applet into memory.     * Runs in a seperate (and interruptible) thread from the rest of the     * applet event processing so that it can be gracefully interrupted from     * things like HotJava.     */    private void runLoader() {        if (status != APPLET_DISPOSE) {            showAppletStatus("notdisposed");            return;        }         dispatchAppletEvent(APPLET_LOADING, null);         // REMIND -- might be cool to visually indicate loading here --        // maybe do animation?        status = APPLET_LOAD;         // Create a class loader        loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());         // Load the archives if present.        // REMIND - this probably should be done in a separate thread,        // or at least the additional archives (epll).         String code = getCode();         // setup applet AppContext        // this must be called before loadJarFiles        setupAppletAppContext();         try {            loadJarFiles(loader);            applet = createApplet(loader);        } catch (ClassNotFoundException e) {            status = APPLET_ERROR;            showAppletStatus("notfound", code);            showAppletLog("notfound", code);            showAppletException(e);            return;        } catch (InstantiationException e) {            status = APPLET_ERROR;            showAppletStatus("nocreate", code);            showAppletLog("nocreate", code);            showAppletException(e);            return;        } catch (IllegalAccessException e) {            status = APPLET_ERROR;            showAppletStatus("noconstruct", code);            showAppletLog("noconstruct", code);            showAppletException(e);            // sbb -- I added a return here            return;        } catch (Exception e) {            status = APPLET_ERROR;            showAppletStatus("exception", e.getMessage());            showAppletException(e);            return;        } catch (ThreadDeath e) {            status = APPLET_ERROR;            showAppletStatus("death");            return;        } catch (Error e) {            status = APPLET_ERROR;            showAppletStatus("error", e.getMessage());            showAppletException(e);            return;        } finally {            // notify that loading is no longer going on            dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);        }         // Fixed #4508194: NullPointerException thrown during        // quick page switch        //        if (applet != null)        {            // Stick it in the frame            applet.setStub(this);            applet.hide();            add("Center", applet);            showAppletStatus("loaded");            validate();        }    }     protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,                                                                         IllegalAccessException, IOException, InstantiationException, InterruptedException {        final String serName = getSerializedObject();        String code = getCode();         if (code != null && serName != null) {            System.err.println(amh.getMessage("runloader.err"));//          return null;            throw new InstantiationException("Either \"code\" or \"object\" should be specified, but not both.");        }        if (code == null && serName == null) {            String msg = "nocode";            status = APPLET_ERROR;            showAppletStatus(msg);            showAppletLog(msg);            repaint();        }        if (code != null) {            applet = (Applet)loader.loadCode(code).newInstance();            doInit = true;        } else {            // serName is not null;            InputStream is = (InputStream)                java.security.AccessController.doPrivileged(                                                            new java.security.PrivilegedAction() {                                                                public Object run() {                                                                    return loader.getResourceAsStream(serName);                                                                }                                                            });            ObjectInputStream ois =                new AppletObjectInputStream(is, loader);            Object serObject = ois.readObject();            applet = (Applet) serObject;            doInit = false; // skip over the first init        }         // Determine the JDK level that the applet targets.        // This is critical for enabling certain backward        // compatibility switch if an applet is a JDK 1.1        // applet. [stanley.ho]        findAppletJDKLevel(applet);         if (Thread.interrupted()) {            try {                status = APPLET_DISPOSE; // APPLET_ERROR?                applet = null;                // REMIND: This may not be exactly the right thing: the                // status is set by the stop button and not necessarily                // here.                showAppletStatus("death");            } finally {                Thread.currentThread().interrupt(); // resignal interrupt            }            return null;        }        return applet;    }     protected void loadJarFiles(AppletClassLoader loader) throws IOException,                                                                 InterruptedException {        // Load the archives if present.        // REMIND - this probably should be done in a separate thread,        // or at least the additional archives (epll).        String jarFiles = getJarFiles();         if (jarFiles != null) {            StringTokenizer st = new StringTokenizer(jarFiles, ",", false);            while(st.hasMoreTokens()) {                String tok = st.nextToken().trim();                try {                    loader.addJar(tok);                } catch (IllegalArgumentException e) {                    // bad archive name                    continue;                }            }        }    }     /**     * Request that the loading of the applet be stopped.     */    protected synchronized void stopLoading() {        // REMIND: fill in the body        if (loaderThread != null) {            //System.out.println("Interrupting applet loader thread: " + loaderThread);            loaderThread.interrupt();        } else {            setLoadAbortRequest();        }    }      protected synchronized boolean okToLoad() {        return !loadAbortRequest;    }     protected synchronized void clearLoadAbortRequest() {        loadAbortRequest = false;    }     protected synchronized void setLoadAbortRequest() {        loadAbortRequest = true;    }      private synchronized void setLoaderThread(Thread loaderThread) {        this.loaderThread = loaderThread;    }     /**     * Return true when the applet has been started.     */    public boolean isActive() {        return status == APPLET_START;    }      private EventQueue appEvtQ = null;    /**     * Is called when the applet wants to be resized.     */    public void appletResize(int width, int height) {        currentAppletSize.width = width;        currentAppletSize.height = height;        final Dimension currentSize = new Dimension(currentAppletSize.width,                                                    currentAppletSize.height);         if(loader != null) {            AppContext appCtxt = loader.getAppContext();            if(appCtxt != null)                appEvtQ = (java.awt.EventQueue)appCtxt.get(AppContext.EVENT_QUEUE_KEY);        }         final AppletPanel ap = this;        if (appEvtQ != null){            appEvtQ.postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),                                                  new Runnable(){                                                      public void run(){                                                          if(ap != null)                                                          {                                                              ap.dispatchAppletEvent(APPLET_RESIZE, currentSize);                                                          }                                                      }                                                  }));        }    }     public void setBounds(int x, int y, int width, int height) {        super.setBounds(x, y, width, height);        currentAppletSize.width = width;        currentAppletSize.height = height;    }     public Applet getApplet() {        return applet;    }     /**     * Status line. Called by the AppletPanel to provide     * feedback on the Applet's state.     */    protected void showAppletStatus(String status) {        getAppletContext().showStatus(amh.getMessage(status));    }     protected void showAppletStatus(String status, Object arg) {        getAppletContext().showStatus(amh.getMessage(status, arg));    }    protected void showAppletStatus(String status, Object arg1, Object arg2) {        getAppletContext().showStatus(amh.getMessage(status, arg1, arg2));    }     /**     * Called by the AppletPanel to print to the log.     */    protected void showAppletLog(String msg) {        System.out.println(amh.getMessage(msg));    }     protected void showAppletLog(String msg, Object arg) {        System.out.println(amh.getMessage(msg, arg));    }     /**     * Called by the AppletPanel to provide     * feedback when an exception has happened.     */    protected void showAppletException(Throwable t) {        t.printStackTrace();        repaint();    }     /**     * Get caching key for classloader cache     */    public String getClassLoaderCacheKey()    {        /**         * Fixed #4501142: Classlaoder sharing policy doesn't         * take "archive" into account. This will be overridden         * by Java Plug-in.                     [stanleyh]         */        return getCodeBase().toString();    }     /**     * The class loaders     */    private static HashMap classloaders = new HashMap();     /**     * Flush a class loader.     */    public static synchronized void flushClassLoader(String key) {        classloaders.remove(key);    }     /**     * Flush all class loaders.     */    public static synchronized void flushClassLoaders() {        classloaders = new HashMap();    }

Lines 501–1000 of 1318. Previous lines