AppletPanel.java revision 215
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This code is free software; you can redistribute it and/or modify it
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * under the terms of the GNU General Public License version 2 only, as
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * published by the Free Software Foundation. Sun designates this
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * particular file as subject to the "Classpath" exception as provided
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * by Sun in the LICENSE file that accompanied this code.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This code is distributed in the hope that it will be useful, but WITHOUT
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * version 2 for more details (a copy is included in the LICENSE file that
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * accompanied this code).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You should have received a copy of the GNU General Public License version
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * 2 along with this work; if not, write to the Free Software Foundation,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CA 95054 USA or visit www.sun.com if you need additional information or
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * have any questions.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweimport java.lang.reflect.InvocationTargetException;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Applet panel class. The panel manages and manipulates the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * applet as it is being loaded. It forks a separate thread in a new
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * thread group to call the applet's init(), start(), stop(), and
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * destroy() methods.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * @author Arthur van Hoff
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweabstract class AppletPanel extends Panel implements AppletStub, Runnable {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The applet (if loaded).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Applet will allow initialization. Should be
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * set to false if loading a serialized applet
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * that was pickled in the init=true state.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected boolean doInit = true;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The classloader for the applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* applet event ids */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* send to the parent to force relayout */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* sent to a (distant) parent to indicate that the applet is being
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * loaded or as completed loading
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public final static int APPLET_LOADING = 51235;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public final static int APPLET_LOADING_COMPLETED = 51236;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The current status. One of:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_DISPOSE,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_LOAD,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_INIT,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_START,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_STOP,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_DESTROY,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_ERROR.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected int status;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The thread for the applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The initial applet size.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Dimension defaultAppletSize = new Dimension(10, 10);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The current applet size.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Dimension currentAppletSize = new Dimension(10, 10);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The thread to use during applet loading
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Flag to indicate that a loading has been cancelled
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe boolean loadAbortRequest = false;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* abstract classes */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe abstract protected String getSerializedObject();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe abstract public int getWidth();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe abstract public int getHeight();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe abstract public boolean hasInitialFocus();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected void setupAppletAppContext() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // do nothing
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Creates a thread to run the applet. This method is called
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * each time an applet is loaded and reloaded.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe synchronized void createAppletThread() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Create a thread group for the applet, and start a new
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // thread to load the applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // 4668479: Option to turn off codebase lookup in AppletClassLoader
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // during resource requests. [stanley.ho]
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe String param = getParameter("codebase_lookup");
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ThreadGroup appletGroup = loader.getThreadGroup();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe handler = new Thread(appletGroup, this, "thread " + nm);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // set the context class loader for this thread
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe AccessController.doPrivileged(new PrivilegedAction() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe void joinAppletThread() throws InterruptedException {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Construct an applet viewer and start the applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void init() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Get the width (if any)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe currentAppletSize.width = defaultAppletSize.width;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Get the height (if any)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe currentAppletSize.height = defaultAppletSize.height;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Turn on the error flag and let TagAppletPanel
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // do the right thing.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Minimum size
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Preferred size
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * AppletEvent Queue
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe synchronized public void addAppletListener(AppletListener l) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe listeners = AppletEventMulticaster.add(listeners, l);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe synchronized public void removeAppletListener(AppletListener l) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe listeners = AppletEventMulticaster.remove(listeners, l);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Dispatch event to the listeners..
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void dispatchAppletEvent(int id, Object argument) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //System.out.println("SEND= " + id);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe AppletEvent evt = new AppletEvent(this, id, argument);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Send an event. Queue it for execution by the handler thread.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe synchronized(this) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //System.out.println("SEND0= " + id);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe joinAppletThread(); // Let the applet event handler exit
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // AppletClassLoader.release() must be called by a Thread
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // not within the applet's ThreadGroup
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Get an event from the queue.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe synchronized AppletEvent getNextEvent() throws InterruptedException {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return new AppletEvent(this, eventId.intValue(), null);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return true;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return false;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This kludge is specific to get over AccessControlException thrown during
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Applet.stop() or destroy() when static thread is suspended. Set a flag
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * in AppletClassLoader to indicate that an
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * AccessControlException for RuntimePermission "modifyThread" or
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * "modifyThreadGroup" had occurred.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe private void setExceptionStatus(AccessControlException e) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (p instanceof RuntimePermission) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Execute applet events.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Here is the state transition diagram
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Note: (XXX) is the action
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_XXX is the state
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * (applet code loaded) --> APPLET_LOAD -- (applet init called)--> APPLET_INIT -- (
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * applet start called) --> APPLET_START -- (applet stop called) -->APPLET_STOP --(applet
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * destroyed called) --> APPLET_DESTROY -->(applet gets disposed) -->
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_DISPOSE -->....
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * In the legacy lifecycle model. The applet gets loaded, inited and started. So it stays
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * in the APPLET_START state unless the applet goes away(refresh page or leave the page).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * So the applet stop method called and the applet enters APPLET_STOP state. Then if the applet
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * is revisited, it will call applet start method and enter the APPLET_START state and stay there.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * In the modern lifecycle model. When the applet first time visited, it is same as legacy lifecycle
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * model. However, when the applet page goes away. It calls applet stop method and enters APPLET_STOP
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * state and then applet destroyed method gets called and enters APPLET_DESTROY state.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This code is also called by AppletViewer. In AppletViewer "Restart" menu, the applet is jump from
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_STOP to APPLET_DESTROY and to APPLET_INIT .
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Also, the applet can jump from APPLET_INIT state to APPLET_DESTROY (in Netscape/Mozilla case).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Same as APPLET_LOAD to
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * APPLET_DISPOSE since all of this are triggered by browser.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // if we are in the loader thread, cause
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // loading to occur. We may exit this with
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // status being APPLET_DISPOSE, APPLET_ERROR,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // or APPLET_LOAD
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe boolean disposed = false;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe while (!disposed && !curThread.isInterrupted()) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //showAppletStatus("EVENT = " + evt.getID());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // This complexity allows loading of applets to be
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // interruptable. The actual thread loading runs
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // in a separate thread, so it can be interrupted
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // without harming the applet thread.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // So that we don't have to worry about
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // concurrency issues, the main applet thread waits
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // until the loader thread terminates.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // (one way or another).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND: do we want a name?
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //System.out.println("------------------- loading applet");
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // we get to go to sleep while this runs
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND: issue an error -- this case should never
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // AppletViewer "Restart" will jump from destroy method to
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // init, that is why we need to check status w/ APPLET_DESTROY
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (status != APPLET_LOAD && status != APPLET_DESTROY) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //Need the default(fallback) font to be created in this AppContext
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH)) &&
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe f.getSize() == 12 && f.getStyle() == Font.PLAIN) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Validate the applet in event dispatch thread
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // to avoid deadlock.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe final AppletPanel p = this;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (status != APPLET_INIT && status != APPLET_STOP) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Validate and show the applet in event dispatch thread
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // to avoid deadlock.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe final AppletPanel p = this;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Fix for BugTraq ID 4041703.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Set the default focus for an applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Hide the applet in event dispatch thread
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // to avoid deadlock.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run()
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // During Applet.stop(), any AccessControlException on an involved Class remains in
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // the "memory" of the AppletClassLoader. If the same instance of the ClassLoader is
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // reused, the same exception will occur during class loading. Set the AppletClassLoader's
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // exceptionStatusSet flag to allow recognition of what had happened
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // when reusing AppletClassLoader object.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } catch (java.security.AccessControlException e) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // rethrow exception to be handled as it normally would be.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (status != APPLET_STOP && status != APPLET_INIT) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // During Applet.destroy(), any AccessControlException on an involved Class remains in
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // the "memory" of the AppletClassLoader. If the same instance of the ClassLoader is
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // reused, the same exception will occur during class loading. Set the AppletClassLoader's
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // exceptionStatusSet flag to allow recognition of what had happened
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // when reusing AppletClassLoader object.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } catch (java.security.AccessControlException e) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // rethrow exception to be handled as it normally would be.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (status != APPLET_DESTROY && status != APPLET_LOAD) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run()
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe showAppletStatus("exception2", e.getClass().getName(),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe showAppletStatus("exception", e.getClass().getName());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } catch (Error e) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe showAppletStatus("error2", e.getClass().getName(),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe showAppletStatus("error", e.getClass().getName());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Gets most recent focus owner component associated with the given window.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * It does that without calling Window.getMostRecentFocusOwner since it
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * provides its own logic contradicting with setDefautlFocus. Instead, it
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * calls KeyboardFocusManager directly.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe private Component getMostRecentFocusOwnerForWindow(Window w) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Method meth = (Method)AccessController.doPrivileged(new PrivilegedAction() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe meth = KeyboardFocusManager.class.getDeclaredMethod("getMostRecentFocusOwner", new Class[] {Window.class});
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Must never happen
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Meth refers static method
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return (Component)meth.invoke(null, new Object[] {w});
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Must never happen
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Will get here if exception was thrown or meth is null
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Fix for BugTraq ID 4041703.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Set the focus to a reasonable default for an Applet.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe private void setDefaultFocus() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe toFocus = getMostRecentFocusOwnerForWindow((Window)parent);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ((EmbeddedFrame)parent).synthesizeWindowActivation(true);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // EmbeddedFrame might have focus before the applet was added.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Thus after its activation the most recent focus owner will be
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // restored. We need the applet's initial focusabled component to
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // be focused here.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Load the applet into memory.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Runs in a seperate (and interruptible) thread from the rest of the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * applet event processing so that it can be gracefully interrupted from
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * things like HotJava.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe private void runLoader() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND -- might be cool to visually indicate loading here --
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // maybe do animation?
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Create a class loader
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Load the archives if present.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND - this probably should be done in a separate thread,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // or at least the additional archives (epll).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // setup applet AppContext
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // this must be called before loadJarFiles
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // sbb -- I added a return here
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } catch (Error e) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // notify that loading is no longer going on
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Fixed #4508194: NullPointerException thrown during
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // quick page switch
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Stick it in the frame
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe IllegalAccessException, IOException, InstantiationException, InterruptedException {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe System.err.println(amh.getMessage("runloader.err"));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe// return null;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe throw new InstantiationException("Either \"code\" or \"object\" should be specified, but not both.");
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe applet = (Applet)loader.loadCode(code).newInstance();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // serName is not null;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Determine the JDK level that the applet targets.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // This is critical for enabling certain backward
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // compatibility switch if an applet is a JDK 1.1
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // applet. [stanley.ho]
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND: This may not be exactly the right thing: the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // status is set by the stop button and not necessarily
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Thread.currentThread().interrupt(); // resignal interrupt
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected void loadJarFiles(AppletClassLoader loader) throws IOException,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // Load the archives if present.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND - this probably should be done in a separate thread,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // or at least the additional archives (epll).
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // bad archive name
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Request that the loading of the applet be stopped.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected synchronized void stopLoading() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // REMIND: fill in the body
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe //System.out.println("Interrupting applet loader thread: " + loaderThread);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected synchronized boolean okToLoad() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected synchronized void clearLoadAbortRequest() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected synchronized void setLoadAbortRequest() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe private synchronized void setLoaderThread(Thread loaderThread) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Return true when the applet has been started.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public boolean isActive() {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Is called when the applet wants to be resized.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void appletResize(int width, int height) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe final Dimension currentSize = new Dimension(currentAppletSize.width,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe appEvtQ = (java.awt.EventQueue)appCtxt.get(AppContext.EVENT_QUEUE_KEY);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe appEvtQ.postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void run(){
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ap.dispatchAppletEvent(APPLET_RESIZE, currentSize);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe public void setBounds(int x, int y, int width, int height) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Status line. Called by the AppletPanel to provide
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * feedback on the Applet's state.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected void showAppletStatus(String status) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe getAppletContext().showStatus(amh.getMessage(status));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe protected void showAppletStatus(String status, Object arg) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe getAppletContext().showStatus(amh.getMessage(status, arg));
t.printStackTrace();
repaint();
public static synchronized void flushClassLoaders() {
if (c == null) {
c = (AppletClassLoader)
synchronized (getClass()) {
return ac;
return res;
},acc);
if (p != null) {
return null;
Permission p;
p = null;
if (p != null)
if (p instanceof FilePermission) {
return acc;
return handler;
public int getAppletWidth() {
public int getAppletHeight() {
synchronized (Window.class)
Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)oldAppContext.get(Window.class);
private boolean jdk11Applet = false;
private boolean jdk12Applet = false;
synchronized(appletClass) {
catch (IOException e) {
jdk11Applet = true;
jdk12Applet = true;
protected boolean isJDK11Applet() {
return jdk11Applet;
protected boolean isJDK12Applet() {
return jdk12Applet;
private int readByte(byte b) {