jdk/src/share/classes/sun/applet/AppletClassLoader.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7956 34a05c4d244f
child 21328 13dbd66f4c09
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7956
diff changeset
     2
 * Copyright (c) 1995, 2011, 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: 4188
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: 4188
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: 4188
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
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.lang.NullPointerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.awt.SunToolkit;
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 3111
diff changeset
    54
import sun.misc.IOUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * This class defines the class loader for loading applet classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * resources. It extends URLClassLoader to search the applet code base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * for the class or resource after checking any loaded JAR files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class AppletClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private URL base;   /* applet code base URL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private CodeSource codesource; /* codesource for the base URL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean exceptionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final Object threadGroupSynchronizer = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final Object grabReleaseSynchronizer = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean codebaseLookup = true;
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    73
    private volatile boolean allowRecursiveDirectoryRead = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Creates a new AppletClassLoader for the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected AppletClassLoader(URL base) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(new URL[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.base = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.codesource =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            new CodeSource(base, (java.security.cert.Certificate[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    86
    public void disableRecursiveDirectoryRead() {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    87
        allowRecursiveDirectoryRead = false;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    88
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    89
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
    90
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Set the codebase lookup flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    void setCodebaseLookup(boolean codebaseLookup)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.codebaseLookup = codebaseLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns the applet code base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    URL getBaseURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Returns the URLs used for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        URL[] jars = super.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        URL[] urls = new URL[jars.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.arraycopy(jars, 0, urls, 0, jars.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        urls[urls.length - 1] = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Adds the specified JAR file to the search path of loaded JAR files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Changed modifier to protected in order to be able to overwrite addJar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * in PluginClassLoader.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void addJar(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            url = new URL(base, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        addURL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        //URL[] urls = getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        //for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        //    System.out.println("url[" + i + "] = " + urls[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Override loadClass so that class loading errors can be caught in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * order to print better error messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public synchronized Class loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // First check if we have permission to access the package. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // should go away once we've added support for exported packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return super.loadClass(name, resolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            //printError(name, e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            //printError(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            //printError(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Finds the applet class with the specified name. First searches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * loaded JAR files then the applet code base for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected Class findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int index = name.indexOf(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String cookie = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if(index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                cookie = name.substring(index, name.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                name = name.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // check loaded JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return super.findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Otherwise, try loading the class from the code base URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (codebaseLookup == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
//      final String path = name.replace('.', '/').concat(".class").concat(cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        String encodedName = ParseUtil.encodePath(name.replace('.', '/'), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        final String path = (new StringBuffer(encodedName)).append(".class").append(cookie).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            byte[] b = (byte[]) AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                               new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                public Object run() throws IOException {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   198
                   try {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   199
                        URL finalURL = new URL(base, path);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   200
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   201
                        // Make sure the codebase won't be modified
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   202
                        if (base.getProtocol().equals(finalURL.getProtocol()) &&
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   203
                            base.getHost().equals(finalURL.getHost()) &&
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   204
                            base.getPort() == finalURL.getPort()) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   205
                            return getBytes(finalURL);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   206
                        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   207
                        else {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   208
                            return null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   209
                        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   210
                    } catch (Exception e) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   211
                        return null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   212
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return defineClass(name, b, 0, b.length, codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new ClassNotFoundException(name, e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns the permissions for the given codesource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * The implementation of this method first calls super.getPermissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * to get the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * granted by the super class, and then adds additional permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * based on the URL of the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * If the protocol is "file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * and the path specifies a file, permission is granted to read all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * and (recursively) all files and subdirectories contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * that directory. This is so applets with a codebase of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * file:/blah/some.jar can read in file:/blah/, which is needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * be backward compatible. We also add permission to connect back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * the "localhost".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param codesource the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the permissions granted to the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        final PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        URL url = codesource.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            p = url.openConnection().getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else if ((p == null) && (url.getProtocol().equals("file"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (path != null) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   267
            final String rawPath = path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (!path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (endIndex != -1) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   271
                        path = path.substring(0, endIndex + 1) + "-";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        perms.add(new FilePermission(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   276
            final File f = new File(rawPath);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   277
            final boolean isDirectory = f.isDirectory();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   278
            // grant codebase recursive read permission
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   279
            // this should only be granted to non-UNC file URL codebase and
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   280
            // the codesource path must either be a directory, or a file
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   281
            // that ends with .jar or .zip
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   282
            if (allowRecursiveDirectoryRead && (isDirectory ||
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   283
                    rawPath.toLowerCase().endsWith(".jar") ||
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   284
                    rawPath.toLowerCase().endsWith(".zip"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Permission bperm;
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   287
                try {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   288
                    bperm = base.openConnection().getPermission();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   289
                } catch (java.io.IOException ioe) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   290
                    bperm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   292
                if (bperm instanceof FilePermission) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   293
                    String bpath = bperm.getName();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   294
                    if (bpath.endsWith(File.separator)) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   295
                        bpath += "-";
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   296
                    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   297
                    perms.add(new FilePermission(bpath,
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   298
                        SecurityConstants.FILE_READ_ACTION));
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   299
                } else if ((bperm == null) && (base.getProtocol().equals("file"))) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   300
                    String bpath = base.getFile().replace('/', File.separatorChar);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   301
                    bpath = ParseUtil.decode(bpath);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   302
                    if (bpath.endsWith(File.separator)) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   303
                        bpath += "-";
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   304
                    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   305
                    perms.add(new FilePermission(bpath, SecurityConstants.FILE_READ_ACTION));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   307
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns the contents of the specified URL as an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static byte[] getBytes(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (uc instanceof java.net.HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            int code = huc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                throw new IOException("open HTTP connection failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int len = uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Use buffered input stream [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        InputStream in = new BufferedInputStream(uc.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 3111
diff changeset
   335
            b = IOUtils.readFully(in, len, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    // Object for synchronization around getResourceAsStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private Object syncResourceAsStream = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private Object syncResourceAsStreamFromJar = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    // Flag to indicate getResourceAsStream() is in call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private boolean resourceAsStreamInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private boolean resourceAsStreamFromJarInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * #getResource(String)}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param  name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return an input stream for reading the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public InputStream getResourceAsStream(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // The following is used to avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // AppletClassLoader.findResource() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // super.getResourceAsStream(). Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // unnecessary connection will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            synchronized(syncResourceAsStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                resourceAsStreamInCall = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // Call super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                is = super.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                resourceAsStreamInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (codebaseLookup == true && is == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // If resource cannot be obtained,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // try to download it from codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                URL url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                is = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        catch (Exception e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Returns an input stream for reading the specified resource from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the loaded jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * #getResource(String)}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param  name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return an input stream for reading the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public InputStream getResourceAsStreamFromJar(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            synchronized(syncResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                resourceAsStreamFromJarInCall = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                // Call super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                is = super.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                resourceAsStreamFromJarInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Finds the applet resource with the specified name. First checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * loaded JAR files then the applet code base for the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // check loaded JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        URL url = super.findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // 6215746:  Disable META-INF/* lookup from codebase in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // applet/plugin classloader. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (name.startsWith("META-INF/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (codebaseLookup == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (url == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            //#4805170, if it is a call from Applet.getImage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            //we should check for the image only in the archives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            boolean insideGetResourceAsStreamFromJar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                synchronized(syncResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                insideGetResourceAsStreamFromJar = resourceAsStreamFromJarInCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (insideGetResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // Check if getResourceAsStream is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            boolean insideGetResourceAsStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            synchronized(syncResourceAsStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                insideGetResourceAsStream = resourceAsStreamInCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            // If getResourceAsStream is called, don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            // trigger the following code. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // unnecessary connection will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (insideGetResourceAsStream == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                // otherwise, try the code base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    // check if resource exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    if(!resourceExists(url))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    // all exceptions, including security exceptions, are caught
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private boolean resourceExists(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Check if the resource exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // It almost works to just try to do an openConnection() but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // HttpURLConnection will return true on HTTP_BAD_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // when the requested name ends in ".html", ".htm", and ".txt"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // and we want to be able to handle these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // Also, cannot just open a connection for things like FileURLConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // because they succeed when connecting to a nonexistent file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // So, in those cases we open and close an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            URLConnection conn = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (conn instanceof java.net.HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                java.net.HttpURLConnection hconn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    (java.net.HttpURLConnection) conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                // To reduce overhead, using http HEAD method instead of GET method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                hconn.setRequestMethod("HEAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                int code = hconn.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                if (code == java.net.HttpURLConnection.HTTP_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 * Fix for #4182052 - stanleyh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 * The same connection should be reused to avoid multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                 * HTTP connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // our best guess for the other cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                InputStream is = conn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Returns an enumeration of all the applet resources with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * name. First checks loaded JAR files then the applet code base for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * available resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public Enumeration findResources(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        final Enumeration e = super.findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        // 6215746:  Disable META-INF/* lookup from codebase in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // applet/plugin classloader. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (name.startsWith("META-INF/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (codebaseLookup == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        URL u = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (!resourceExists(u)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        final URL url = u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return new Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            private boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                return !done && (e.hasMoreElements() || url != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Load and resolve the file specified by the applet tag CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * attribute. The argument can either be the relative path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * of the class file itself or just the name of the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    Class loadCode(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // first convert any '/' or native file separator to .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        name = name.replace('/', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        name = name.replace(File.separatorChar, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // deal with URL rewriting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        String cookie = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int index = name.indexOf(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if(index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                cookie = name.substring(index, name.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                name = name.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        // save that name for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        String fullName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // then strip off any suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (name.endsWith(".class") || name.endsWith(".java")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            name = name.substring(0, name.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if(cookie != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        name = (new StringBuffer(name)).append(cookie).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // then if it didn't end with .java or .class, or in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // really pathological case of a class named class or java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if(cookie != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                fullName = (new StringBuffer(fullName)).append(cookie).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return loadClass(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * The threadgroup that the applets loaded by this classloader live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * in. In the sun.* implementation of applets, the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * (AppletSecurity) getThreadGroup returns the thread group of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * first applet on the stack, which is the applet's thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    private AppletThreadGroup threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private AppContext appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public ThreadGroup getThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
      synchronized (threadGroupSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (threadGroup == null || threadGroup.isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    threadGroup = new AppletThreadGroup(base + "-threadGroup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    // threadGroup.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    // threadGroup is now destroyed by AppContext.dispose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    // Create the new AppContext from within a Thread belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    // to the newly created ThreadGroup, and wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    // creation to complete before returning from this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    AppContextCreator creatorThread = new AppContextCreator(threadGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    // Since this thread will later be used to launch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    // applet's AWT-event dispatch thread and we want the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    // code executing the AWT callbacks to use their own class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    // loader rather than the system class loader, explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    // set the context class loader to the AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    creatorThread.setContextClassLoader(AppletClassLoader.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   666
                    creatorThread.start();
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   667
                    try {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   668
                        synchronized(creatorThread.syncObject) {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   669
                            while (!creatorThread.created) {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   670
                                creatorThread.syncObject.wait();
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   671
                            }
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   672
                        }
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   673
                    } catch (InterruptedException e) { }
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   674
                    appContext = creatorThread.appContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
      }
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
     * Get the AppContext, if any, corresponding to this AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public AppContext getAppContext()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    int usageCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Grab this AppletClassLoader and its ThreadGroup/AppContext, so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * won't be destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   696
public     void grab() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        synchronized(grabReleaseSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            usageCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        getThreadGroup(); // Make sure ThreadGroup/AppContext exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    protected void setExceptionStatus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        exceptionStatus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public boolean getExceptionStatus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return exceptionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Release this AppletClassLoader and its ThreadGroup/AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * If nothing else has grabbed this AppletClassLoader, its ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * and AppContext will be destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Because this method may destroy the AppletClassLoader's ThreadGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * this method should NOT be called from within the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * Changed modifier to protected in order to be able to overwrite this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * function in PluginClassLoader.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    protected void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        AppContext tempAppContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        synchronized(grabReleaseSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (usageCount > 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                --usageCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                synchronized(threadGroupSynchronizer) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   734
                    tempAppContext = resetAppContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // Dispose appContext outside any sync block to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // prevent potential deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (tempAppContext != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                tempAppContext.dispose(); // nuke the world!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            } catch (IllegalThreadStateException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   748
    /*
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   749
     * reset classloader's AppContext and ThreadGroup
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   750
     * This method is for subclass PluginClassLoader to
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   751
     * reset superclass's AppContext and ThreadGroup but do
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   752
     * not dispose the AppContext. PluginClassLoader does not
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   753
     * use UsageCount to decide whether to dispose AppContext
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   754
     *
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   755
     * @return previous AppContext
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   756
     */
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   757
    protected AppContext resetAppContext() {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   758
        AppContext tempAppContext = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   759
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   760
        synchronized(threadGroupSynchronizer) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   761
            // Store app context in temp variable
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   762
            tempAppContext = appContext;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   763
            usageCount = 0;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   764
            appContext = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   765
            threadGroup = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   766
        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   767
        return tempAppContext;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   768
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   769
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   770
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    // Hash map to store applet compatibility info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private HashMap jdk11AppletInfo = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private HashMap jdk12AppletInfo = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Set applet target level as JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param clazz Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @param bool true if JDK is targeted for JDK 1.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *             false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    void setJDK11Target(Class clazz, boolean bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         jdk11AppletInfo.put(clazz.toString(), Boolean.valueOf(bool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Set applet target level as JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param clazz Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param bool true if JDK is targeted for JDK 1.2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *             false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    void setJDK12Target(Class clazz, boolean bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        jdk12AppletInfo.put(clazz.toString(), Boolean.valueOf(bool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Determine if applet is targeted for JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param applet Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @return TRUE if applet is targeted for JDK 1.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *         FALSE if applet is not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *         null if applet is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    Boolean isJDK11Target(Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        return (Boolean) jdk11AppletInfo.get(clazz.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Determine if applet is targeted for JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @param applet Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @return TRUE if applet is targeted for JDK 1.2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *         FALSE if applet is not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *         null if applet is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    Boolean isJDK12Target(Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        return (Boolean) jdk12AppletInfo.get(clazz.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    private static AppletMessageHandler mh =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        new AppletMessageHandler("appletclassloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Prints a class loading error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    private static void printError(String name, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        String s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            s = mh.getMessage("filenotfound", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } else if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            s = mh.getMessage("fileioexception", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        } else if (e instanceof ClassFormatError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            s = mh.getMessage("fileformat", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        } else if (e instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            s = mh.getMessage("filedeath", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } else if (e instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            s = mh.getMessage("fileerror", e.toString(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            System.err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
 * The AppContextCreator class is used to create an AppContext from within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
 * a Thread belonging to the new AppContext's ThreadGroup.  To wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
 * this operation to complete before continuing, wait for the notifyAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
 * operation on the syncObject to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
class AppContextCreator extends Thread  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    Object syncObject = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    AppContext appContext = null;
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   859
    volatile boolean created = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    AppContextCreator(ThreadGroup group)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        super(group, "AppContextCreator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    public void run()  {
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   866
        appContext = SunToolkit.createNewAppContext();
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   867
        created = true;
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   868
        synchronized(syncObject) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            syncObject.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
} // class AppContextCreator