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