jdk/src/share/classes/sun/applet/AppletPanel.java
author herrick
Fri, 12 Jun 2009 14:56:32 -0400
changeset 3111 fefdeafb7ab9
parent 2645 5dfb43cecd32
child 5506 202f599c92aa
permissions -rw-r--r--
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7 6845973: Update JDK7 with deployment changes in 6u13, 6u14 4802695: Support 64-bit Java Plug-in and Java webstart on Windows/Linux on AMD64 6825019: DownloadManager should not be loaded and referenced for full JRE 6738770: REGRESSION:JSException throws when use LiveConnect javascript facility 6772884: plugin2 : java.lang.OutOfMemoryError or crash 6707535: Crossing domain hole affecting multiple sites/domains using plug-in 6728071: Non-verification of Update files may allow unintended updates 6704154: Code loaded from local filesystem should not get access to localhost 6727081: Web Start security restrictions bypass using special extension jnlp 6727079: Java Web Start Socket() restriction bypass 6727071: Cache location/user name information disclosure in SingleInstanceImpl. 6716217: AppletClassLoader adds permissions based on codebase regardless of CS 6694892: Java Webstart inclusion via system properties override [CVE-2008-2086] 6704074: localhost socket access due to cache location exposed 6703909: Java webstart arbitrary file creation using nativelib 6665315: browser crashes when deployment.properties has more slashes ( / ) 6660121: Encoding values in JNLP files can cause buffer overflow 6606110: URLConnection.setProxiedHost for resources that are loaded via proxy 6581221: SSV(VISTA): Redirection FAILS to work if user does a downgrade install 6609756: Buffer Overflow in Java ActiveX component 6608712: Bypassing the same origin policy in Java with crafted names 6534630: "gnumake clobber" doesn't 6849953: JDK7 - replacement of bufferoverflowU.lib on amd64 breaks build 6849029: Need some JDK7 merge clean-up after comments on the webrev 6847582: Build problem on JDK7 with isSecureProperty in merge 6827935: JDK 7 deployment merging - problem in Compiler-msvm.gmk 6823215: latest merge fixes from 6u12 -> JDK7 6816153: further mergers for JDK7 deployment integration 6807074: Fix Java Kernel and JQS in initial JDK7 builds Summary: Initial changeset for implementing 6uX Deployment Features into JDK7 Reviewed-by: dgu, billyh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.MemoryImageSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.awt.EmbeddedFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.misc.MessageUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.misc.PerformanceLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.misc.Queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Applet panel class. The panel manages and manipulates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * applet as it is being loaded. It forks a separate thread in a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * thread group to call the applet's init(), start(), stop(), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * destroy() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
abstract class AppletPanel extends Panel implements AppletStub, Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The applet (if loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Applet applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Applet will allow initialization.  Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * set to false if loading a serialized applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * that was pickled in the init=true state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected boolean doInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The classloader for the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2645
diff changeset
    83
    protected AppletClassLoader loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* applet event ids */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public final static int APPLET_DISPOSE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public final static int APPLET_LOAD = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public final static int APPLET_INIT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public final static int APPLET_START = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public final static int APPLET_STOP = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public final static int APPLET_DESTROY = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public final static int APPLET_QUIT = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public final static int APPLET_ERROR = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* send to the parent to force relayout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public final static int APPLET_RESIZE = 51234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* sent to a (distant) parent to indicate that the applet is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * loaded or as completed loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public final static int APPLET_LOADING = 51235;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public final static int APPLET_LOADING_COMPLETED = 51236;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The current status. One of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *    APPLET_DISPOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *    APPLET_LOAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *    APPLET_INIT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *    APPLET_START,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *    APPLET_STOP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *    APPLET_DESTROY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *    APPLET_ERROR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The thread for the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2645
diff changeset
   119
    protected Thread handler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The initial applet size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    Dimension defaultAppletSize = new Dimension(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * The current applet size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    Dimension currentAppletSize = new Dimension(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    MessageUtils mu = new MessageUtils();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The thread to use during applet loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    Thread loaderThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Flag to indicate that a loading has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    boolean loadAbortRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /* abstract classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    abstract protected String getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    abstract protected String getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    abstract protected String getSerializedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    abstract public int    getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    abstract public int    getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    abstract public boolean hasInitialFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static int threadGroupNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected void setupAppletAppContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Creates a thread to run the applet. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * each time an applet is loaded and reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    synchronized void createAppletThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Create a thread group for the applet, and start a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // thread to load the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        String nm = "applet-" + getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        loader.grab(); // Keep this puppy around!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String param = getParameter("codebase_lookup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (param != null && param.equals("false"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            loader.setCodebaseLookup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            loader.setCodebaseLookup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ThreadGroup appletGroup = loader.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        handler = new Thread(appletGroup, this, "thread " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // set the context class loader for this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    handler.setContextClassLoader(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        handler.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    void joinAppletThread() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            handler.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            loader.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Construct an applet viewer and start the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // Get the width (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            defaultAppletSize.width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            currentAppletSize.width = defaultAppletSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // Get the height (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            defaultAppletSize.height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            currentAppletSize.height = defaultAppletSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // Turn on the error flag and let TagAppletPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // do the right thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            showAppletStatus("badattribute.exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            showAppletLog("badattribute.exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        createAppletThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Dimension minimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return new Dimension(defaultAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                             defaultAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public Dimension preferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return new Dimension(currentAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                             currentAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private AppletListener listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * AppletEvent Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private Queue queue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    synchronized public void addAppletListener(AppletListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        listeners = AppletEventMulticaster.add(listeners, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    synchronized public void removeAppletListener(AppletListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        listeners = AppletEventMulticaster.remove(listeners, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Dispatch event to the listeners..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void dispatchAppletEvent(int id, Object argument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        //System.out.println("SEND= " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            AppletEvent evt = new AppletEvent(this, id, argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            listeners.appletStateChanged(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Send an event. Queue it for execution by the handler thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public void sendEvent(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                //System.out.println("SEND0= " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                queue = new Queue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   287
            Integer eventId = Integer.valueOf(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            queue.enqueue(eventId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (id == APPLET_QUIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                joinAppletThread(); // Let the applet event handler exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // AppletClassLoader.release() must be called by a Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // not within the applet's ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Get an event from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    synchronized AppletEvent getNextEvent() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        while (queue == null || queue.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        Integer eventId = (Integer)queue.dequeue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return new AppletEvent(this, eventId.intValue(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    boolean emptyEventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if ((queue == null) || (queue.isEmpty()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * This kludge is specific to get over AccessControlException thrown during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Applet.stop() or destroy() when static thread is suspended.  Set a flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * in AppletClassLoader to indicate that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * AccessControlException for RuntimePermission "modifyThread" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * "modifyThreadGroup" had occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     private void setExceptionStatus(AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     Permission p = e.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     if (p instanceof RuntimePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         if (p.getName().startsWith("modifyThread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
             if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                 loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
             loader.setExceptionStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Execute applet events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Here is the state transition diagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *   Note: (XXX) is the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         APPLET_XXX is the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *  (applet code loaded) --> APPLET_LOAD -- (applet init called)--> APPLET_INIT -- (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *   applet start called) --> APPLET_START -- (applet stop called) -->APPLET_STOP --(applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *   destroyed called) --> APPLET_DESTROY -->(applet gets disposed) -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *   APPLET_DISPOSE -->....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * In the legacy lifecycle model. The applet gets loaded, inited and started. So it stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * in the APPLET_START state unless the applet goes away(refresh page or leave the page).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * So the applet stop method called and the applet enters APPLET_STOP state. Then if the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * is revisited, it will call applet start method and enter the APPLET_START state and stay there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * In the modern lifecycle model. When the applet first time visited, it is same as legacy lifecycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * model. However, when the applet page goes away. It calls applet stop method and enters APPLET_STOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * state and then applet destroyed method gets called and enters APPLET_DESTROY state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * This code is also called by AppletViewer. In AppletViewer "Restart" menu, the applet is jump from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * APPLET_STOP to APPLET_DESTROY and to APPLET_INIT .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Also, the applet can jump from APPLET_INIT state to APPLET_DESTROY (in Netscape/Mozilla case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * Same as APPLET_LOAD to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * APPLET_DISPOSE since all of this are triggered by browser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Thread curThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (curThread == loaderThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // if we are in the loader thread, cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // loading to occur.  We may exit this with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // status being APPLET_DISPOSE, APPLET_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // or APPLET_LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            runLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        boolean disposed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        while (!disposed && !curThread.isInterrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            AppletEvent evt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                evt = getNextEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                showAppletStatus("bail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            //showAppletStatus("EVENT = " + evt.getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                switch (evt.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                  case APPLET_LOAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                      if (!okToLoad()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                      // This complexity allows loading of applets to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                      // interruptable.  The actual thread loading runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                      // in a separate thread, so it can be interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                      // without harming the applet thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                      // So that we don't have to worry about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                      // concurrency issues, the main applet thread waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                      // until the loader thread terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                      // (one way or another).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                      if (loaderThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                          // REMIND: do we want a name?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                          //System.out.println("------------------- loading applet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                          setLoaderThread(new Thread(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                          loaderThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                          // we get to go to sleep while this runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                          loaderThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                          setLoaderThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                          // REMIND: issue an error -- this case should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                          // occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                  case APPLET_INIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    // AppletViewer "Restart" will jump from destroy method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    // init, that is why we need to check status w/ APPLET_DESTROY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                      if (status != APPLET_LOAD && status != APPLET_DESTROY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                          showAppletStatus("notloaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                      applet.resize(defaultAppletSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                      if (doInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                          if (PerformanceLogger.loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                              PerformanceLogger.setTime("Applet Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                              PerformanceLogger.outputLog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                          applet.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                      //Need the default(fallback) font to be created in this AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                      Font f = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                      if (f == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                          "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                          f.getSize() == 12 && f.getStyle() == Font.PLAIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                          setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                      doInit = true;    // allow restarts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                      // Validate the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                      // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                          final AppletPanel p = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
2645
5dfb43cecd32 6633354: AppletPanel loads Swing classes
dcherepanov
parents: 715
diff changeset
   452
                          EventQueue.invokeAndWait(new Runnable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                  public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                      p.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                              });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                      catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                      catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                      status = APPLET_INIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                      showAppletStatus("inited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                  case APPLET_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                      if (status != APPLET_INIT && status != APPLET_STOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                          showAppletStatus("notinited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                      applet.resize(currentAppletSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                      applet.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                      // Validate and show the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                      // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                          final AppletPanel p = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                          final Applet a = applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
2645
5dfb43cecd32 6633354: AppletPanel loads Swing classes
dcherepanov
parents: 715
diff changeset
   482
                          EventQueue.invokeAndWait(new Runnable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                  public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                      p.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                      a.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                      // Fix for BugTraq ID 4041703.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                      // Set the default focus for an applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                      if (hasInitialFocus())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                        setDefaultFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                              });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                      catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                      catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                      status = APPLET_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                      showAppletStatus("started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                case APPLET_STOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    if (status != APPLET_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        showAppletStatus("notstarted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    status = APPLET_STOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    // Hide the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        final Applet a = applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
2645
5dfb43cecd32 6633354: AppletPanel loads Swing classes
dcherepanov
parents: 715
diff changeset
   516
                        EventQueue.invokeAndWait(new Runnable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                public void run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                    a.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    // During Applet.stop(), any AccessControlException on an involved Class remains in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    // exceptionStatusSet flag to allow recognition of what had happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    // when reusing AppletClassLoader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        applet.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    } catch (java.security.AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        setExceptionStatus(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        // rethrow exception to be handled as it normally would be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    showAppletStatus("stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                case APPLET_DESTROY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    if (status != APPLET_STOP && status != APPLET_INIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        showAppletStatus("notstopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    status = APPLET_DESTROY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    // During Applet.destroy(), any AccessControlException on an involved Class remains in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    // exceptionStatusSet flag to allow recognition of what had happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    // when reusing AppletClassLoader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        applet.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    } catch (java.security.AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        setExceptionStatus(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        // rethrow exception to be handled as it normally would be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    showAppletStatus("destroyed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                case APPLET_DISPOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (status != APPLET_DESTROY && status != APPLET_LOAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        showAppletStatus("notdestroyed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    status = APPLET_DISPOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        final Applet a = applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        EventQueue.invokeAndWait(new Runnable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            public void run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    catch(InterruptedException ie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    catch(InvocationTargetException ite)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    applet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    showAppletStatus("disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    disposed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                case APPLET_QUIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    showAppletStatus("exception2", e.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    showAppletStatus("exception", e.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } catch (ThreadDeath e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    showAppletStatus("error2", e.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    showAppletStatus("error", e.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            clearLoadAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Gets most recent focus owner component associated with the given window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * It does that without calling Window.getMostRecentFocusOwner since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * provides its own logic contradicting with setDefautlFocus. Instead, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * calls KeyboardFocusManager directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    private Component getMostRecentFocusOwnerForWindow(Window w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        Method meth = (Method)AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    Method meth = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        meth = KeyboardFocusManager.class.getDeclaredMethod("getMostRecentFocusOwner", new Class[] {Window.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        meth.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        // Must never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    return meth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (meth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            // Meth refers static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                return (Component)meth.invoke(null, new Object[] {w});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                // Must never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // Will get here if exception was thrown or meth is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return w.getMostRecentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Fix for BugTraq ID 4041703.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Set the focus to a reasonable default for an Applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private void setDefaultFocus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        Component toFocus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (parent instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                toFocus = getMostRecentFocusOwnerForWindow((Window)parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (toFocus == parent || toFocus == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    toFocus = parent.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        getInitialComponent((Window)parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            } else if (parent.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                toFocus = parent.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    getDefaultComponent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (toFocus != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            if (parent instanceof EmbeddedFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                ((EmbeddedFrame)parent).synthesizeWindowActivation(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            // EmbeddedFrame might have focus before the applet was added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            // Thus after its activation the most recent focus owner will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            // restored. We need the applet's initial focusabled component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            // be focused here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            toFocus.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Load the applet into memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Runs in a seperate (and interruptible) thread from the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * applet event processing so that it can be gracefully interrupted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * things like HotJava.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    private void runLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (status != APPLET_DISPOSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            showAppletStatus("notdisposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        dispatchAppletEvent(APPLET_LOADING, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // REMIND -- might be cool to visually indicate loading here --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // maybe do animation?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        status = APPLET_LOAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // Create a class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        // Load the archives if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // REMIND - this probably should be done in a separate thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        // or at least the additional archives (epll).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        String code = getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // setup applet AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // this must be called before loadJarFiles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        setupAppletAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            loadJarFiles(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            applet = createApplet(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            showAppletStatus("notfound", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            showAppletLog("notfound", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            showAppletStatus("nocreate", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            showAppletLog("nocreate", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            showAppletStatus("noconstruct", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            showAppletLog("noconstruct", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            // sbb -- I added a return here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            showAppletStatus("exception", e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        } catch (ThreadDeath e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            showAppletStatus("error", e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            // notify that loading is no longer going on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // Fixed #4508194: NullPointerException thrown during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // quick page switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (applet != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            // Stick it in the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            applet.setStub(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            applet.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            add("Center", applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            showAppletStatus("loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                                                         IllegalAccessException, IOException, InstantiationException, InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        final String serName = getSerializedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        String code = getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (code != null && serName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            System.err.println(amh.getMessage("runloader.err"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
//          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            throw new InstantiationException("Either \"code\" or \"object\" should be specified, but not both.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (code == null && serName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            String msg = "nocode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            showAppletStatus(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            showAppletLog(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (code != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            applet = (Applet)loader.loadCode(code).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            doInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // serName is not null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            InputStream is = (InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                                            new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                                                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                                                    return loader.getResourceAsStream(serName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            ObjectInputStream ois =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                new AppletObjectInputStream(is, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            Object serObject = ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            applet = (Applet) serObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            doInit = false; // skip over the first init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // Determine the JDK level that the applet targets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // This is critical for enabling certain backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // compatibility switch if an applet is a JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // applet. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        findAppletJDKLevel(applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if (Thread.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                status = APPLET_DISPOSE; // APPLET_ERROR?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                applet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                // REMIND: This may not be exactly the right thing: the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                // status is set by the stop button and not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                // here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                Thread.currentThread().interrupt(); // resignal interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    protected void loadJarFiles(AppletClassLoader loader) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                                                 InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // Load the archives if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // REMIND - this probably should be done in a separate thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // or at least the additional archives (epll).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        String jarFiles = getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (jarFiles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            while(st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                String tok = st.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    loader.addJar(tok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    // bad archive name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Request that the loading of the applet be stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    protected synchronized void stopLoading() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        // REMIND: fill in the body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (loaderThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            //System.out.println("Interrupting applet loader thread: " + loaderThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            loaderThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            setLoadAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    protected synchronized boolean okToLoad() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return !loadAbortRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    protected synchronized void clearLoadAbortRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        loadAbortRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    protected synchronized void setLoadAbortRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        loadAbortRequest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    private synchronized void setLoaderThread(Thread loaderThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        this.loaderThread = loaderThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Return true when the applet has been started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return status == APPLET_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    private EventQueue appEvtQ = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Is called when the applet wants to be resized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public void appletResize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        currentAppletSize.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        currentAppletSize.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        final Dimension currentSize = new Dimension(currentAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                                    currentAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if(loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            AppContext appCtxt = loader.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            if(appCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                appEvtQ = (java.awt.EventQueue)appCtxt.get(AppContext.EVENT_QUEUE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        final AppletPanel ap = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (appEvtQ != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            appEvtQ.postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                                                  new Runnable(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                                                      public void run(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                                                          if(ap != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                                                          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                                                              ap.dispatchAppletEvent(APPLET_RESIZE, currentSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                                  }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public void setBounds(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        super.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        currentAppletSize.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        currentAppletSize.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public Applet getApplet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Status line. Called by the AppletPanel to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * feedback on the Applet's state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    protected void showAppletStatus(String status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        getAppletContext().showStatus(amh.getMessage(status));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    protected void showAppletStatus(String status, Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        getAppletContext().showStatus(amh.getMessage(status, arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    protected void showAppletStatus(String status, Object arg1, Object arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        getAppletContext().showStatus(amh.getMessage(status, arg1, arg2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * Called by the AppletPanel to print to the log.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    protected void showAppletLog(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        System.out.println(amh.getMessage(msg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    protected void showAppletLog(String msg, Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        System.out.println(amh.getMessage(msg, arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Called by the AppletPanel to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * feedback when an exception has happened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    protected void showAppletException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * Get caching key for classloader cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    public String getClassLoaderCacheKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         * Fixed #4501142: Classlaoder sharing policy doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * take "archive" into account. This will be overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         * by Java Plug-in.                     [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        return getCodeBase().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * The class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    private static HashMap classloaders = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Flush a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public static synchronized void flushClassLoader(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        classloaders.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Flush all class loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public static synchronized void flushClassLoaders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        classloaders = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * This method actually creates an AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * It can be override by subclasses (such as the Plug-in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * to provide different classloaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    protected AppletClassLoader createClassLoader(final URL codebase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        return new AppletClassLoader(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Get a class loader. Create in a restricted context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    synchronized AppletClassLoader getClassLoader(final URL codebase, final String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        AppletClassLoader c = (AppletClassLoader)classloaders.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                getAccessControlContext(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            c = (AppletClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                            AppletClassLoader ac = createClassLoader(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                            /* Should the creation of the classloader be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                             * within the class synchronized block?  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                             * this class is used by the plugin, take care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                             * to avoid deadlocks, or specialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                             * AppletPanel within the plugin.  It may take
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                             * an arbitrary amount of time to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                             * class loader (involving getting Jar files
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                             * etc.) and may block unrelated applets from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                             * finishing createAppletThread (due to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                             * class synchronization). If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                             * createAppletThread does not finish quickly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                             * the applet cannot process other messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                             * particularly messages such as destroy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                             * (which timeout when called from the browser).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                            synchronized (getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                AppletClassLoader res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                                    (AppletClassLoader)classloaders.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                    classloaders.put(key, ac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                    return ac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    },acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * get the context for the AppletClassLoader we are creating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * the context is granted permission to create the class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * connnect to the codebase, and whatever else the policy grants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * to all codebases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    private AccessControlContext getAccessControlContext(final URL codebase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        PermissionCollection perms = (PermissionCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        Policy p = java.security.Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                            return p.getPermissions(new CodeSource(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                                                                   (java.security.cert.Certificate[]) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        //XXX: this is needed to be able to create the classloader itself!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        java.net.URLConnection urlConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            urlConnection = codebase.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if (endIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                path = path.substring(0, endIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                perms.add(new FilePermission(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                             SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            URL locUrl = codebase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            String host = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (host != null && (host.length() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                perms.add(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                                               SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        ProtectionDomain domain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            new ProtectionDomain(new CodeSource(codebase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                                (java.security.cert.Certificate[]) null), perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            new AccessControlContext(new ProtectionDomain[] { domain });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public Thread getAppletHandlerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    public int getAppletWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return currentAppletSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    public int getAppletHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        return currentAppletSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public static void changeFrameAppContext(Frame frame, AppContext newAppContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        // Fixed #4754451: Applet can have methods running on main
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        // thread event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        // The cause of this bug is that the frame of the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        // is created in main thread group. Thus, when certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        // AWT/Swing events are generated, the events will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        // dispatched through the wrong event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        // To fix this, we rearrange the AppContext with the frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        // so the proper event queue will be looked up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // Swing also maintains a Frame list for the AppContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // so we will have to rearrange it as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        // Check if frame's AppContext has already been set properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        AppContext oldAppContext = SunToolkit.targetToAppContext(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (oldAppContext == newAppContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        // Synchronization on Window.class is needed for locking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        // critical section of the window list in AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        synchronized (Window.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            WeakReference weakRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            // Remove frame from the Window list in wrong AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                // Lookup current frame's AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)oldAppContext.get(Window.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                if (windowList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    for (WeakReference ref : windowList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                        if (ref.get() == frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                            weakRef = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    // Remove frame from wrong AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    if (weakRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                        windowList.remove(weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            // Put the frame into the applet's AppContext map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            SunToolkit.insertTargetMapping(frame, newAppContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            // Insert frame into the Window list in the applet's AppContext map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                Vector<WeakReference<Window>> windowList = (Vector)newAppContext.get(Window.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                if (windowList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    windowList = new Vector<WeakReference<Window>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    newAppContext.put(Window.class, windowList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                // use the same weakRef here as it is used elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                windowList.add(weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    // Flag to indicate if applet is targeted for JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    private boolean jdk11Applet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    // Flag to indicate if applet is targeted for JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    private boolean jdk12Applet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Determine JDK level of an applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    private void findAppletJDKLevel(Applet applet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        // To determine the JDK level of an applet, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        // most reliable way is to check the major version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        // of the applet class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // synchronized on applet class object, so calling from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        // different instances of the same applet will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        Class appletClass = applet.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        synchronized(appletClass)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            // Determine if the JDK level of an applet has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            // checked before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            Boolean jdk11Target = (Boolean) loader.isJDK11Target(appletClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            Boolean jdk12Target = (Boolean) loader.isJDK12Target(appletClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            // if applet JDK level has been checked before, retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            // value and return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (jdk11Target != null || jdk12Target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                jdk11Applet = (jdk11Target == null) ? false : jdk11Target.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                jdk12Applet = (jdk12Target == null) ? false : jdk12Target.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            String name = appletClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            // first convert any '.' to '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            name = name.replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            // append .class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            final String resourceName = name + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            byte[] classHeader = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                is = (InputStream) java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                            return loader.getResourceAsStream(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                // Read the first 8 bytes of the class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                int byteRead = is.read(classHeader, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                // return if the header is not read in entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                // for some reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                if (byteRead != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            catch (IOException e)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            // Check major version in class file header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            int major_version = readShort(classHeader, 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            // Major version in class file is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            //   45 - JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            //   46 - JDK 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            //   47 - JDK 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            //   48 - JDK 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            //   49 - JDK 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            if (major_version < 46)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                jdk11Applet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            else if (major_version == 46)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                jdk12Applet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            // Store applet JDK level in AppContext for later lookup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            // e.g. page switch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            loader.setJDK11Target(appletClass, jdk11Applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            loader.setJDK12Target(appletClass, jdk12Applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * Return true if applet is targeted to JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    protected boolean isJDK11Applet()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        return jdk11Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * Return true if applet is targeted to JDK1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    protected boolean isJDK12Applet()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        return jdk12Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * Read short from byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    private int readShort(byte[] b, int off)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        int hi = readByte(b[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        int lo = readByte(b[off + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return (hi << 8) | lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    private int readByte(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        return ((int)b) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    private static AppletMessageHandler amh = new AppletMessageHandler("appletpanel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
}