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