jdk/src/share/classes/sun/applet/AppletClassLoader.java
author bpb
Tue, 22 Oct 2013 11:25:01 -0700
changeset 21328 13dbd66f4c09
parent 9035 1255eb81cc2f
child 23010 6dadb192ad81
permissions -rw-r--r--
7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE 6445180: URLClassLoader does not describe the behavior of several methods with respect to null arguments Summary: Document when a NPE will be thrown by URLClassLoader constructors, newInstance(), findClass(), and getPermissions(). Reviewed-by: alanb, mduigou, chegar, dholmes, jrose
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
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 9035
diff changeset
   242
     * @throws NullPointerException if {@code codesource} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return the permissions granted to the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        final PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        URL url = codesource.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            p = url.openConnection().getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } else if ((p == null) && (url.getProtocol().equals("file"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (path != null) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   268
            final String rawPath = path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (!path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (endIndex != -1) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   272
                        path = path.substring(0, endIndex + 1) + "-";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        perms.add(new FilePermission(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   277
            final File f = new File(rawPath);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   278
            final boolean isDirectory = f.isDirectory();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   279
            // grant codebase recursive read permission
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   280
            // 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
   281
            // 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
   282
            // that ends with .jar or .zip
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   283
            if (allowRecursiveDirectoryRead && (isDirectory ||
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   284
                    rawPath.toLowerCase().endsWith(".jar") ||
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   285
                    rawPath.toLowerCase().endsWith(".zip"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Permission bperm;
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   288
                try {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   289
                    bperm = base.openConnection().getPermission();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   290
                } catch (java.io.IOException ioe) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   291
                    bperm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   293
                if (bperm instanceof FilePermission) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   294
                    String bpath = bperm.getName();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   295
                    if (bpath.endsWith(File.separator)) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   296
                        bpath += "-";
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   297
                    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   298
                    perms.add(new FilePermission(bpath,
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   299
                        SecurityConstants.FILE_READ_ACTION));
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   300
                } 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
   301
                    String bpath = base.getFile().replace('/', File.separatorChar);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   302
                    bpath = ParseUtil.decode(bpath);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   303
                    if (bpath.endsWith(File.separator)) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   304
                        bpath += "-";
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   305
                    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   306
                    perms.add(new FilePermission(bpath, SecurityConstants.FILE_READ_ACTION));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   308
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns the contents of the specified URL as an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private static byte[] getBytes(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (uc instanceof java.net.HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int code = huc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new IOException("open HTTP connection failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int len = uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // Use buffered input stream [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        InputStream in = new BufferedInputStream(uc.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 3111
diff changeset
   336
            b = IOUtils.readFully(in, len, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // Object for synchronization around getResourceAsStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private Object syncResourceAsStream = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private Object syncResourceAsStreamFromJar = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    // Flag to indicate getResourceAsStream() is in call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private boolean resourceAsStreamInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private boolean resourceAsStreamFromJarInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * #getResource(String)}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param  name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return an input stream for reading the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public InputStream getResourceAsStream(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // The following is used to avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // AppletClassLoader.findResource() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // super.getResourceAsStream(). Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // unnecessary connection will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            synchronized(syncResourceAsStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                resourceAsStreamInCall = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                // Call super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                is = super.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                resourceAsStreamInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (codebaseLookup == true && is == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // If resource cannot be obtained,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                // try to download it from codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                URL url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                is = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        catch (Exception e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Returns an input stream for reading the specified resource from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * the loaded jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * #getResource(String)}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param  name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return an input stream for reading the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public InputStream getResourceAsStreamFromJar(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            synchronized(syncResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                resourceAsStreamFromJarInCall = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                // Call super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                is = super.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                resourceAsStreamFromJarInCall = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return null;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Finds the applet resource with the specified name. First checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * loaded JAR files then the applet code base for the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // check loaded JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        URL url = super.findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // 6215746:  Disable META-INF/* lookup from codebase in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // applet/plugin classloader. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (name.startsWith("META-INF/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (codebaseLookup == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (url == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            //#4805170, if it is a call from Applet.getImage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            //we should check for the image only in the archives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            boolean insideGetResourceAsStreamFromJar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                synchronized(syncResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                insideGetResourceAsStreamFromJar = resourceAsStreamFromJarInCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (insideGetResourceAsStreamFromJar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            // Fixed #4507227: Slow performance to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // class and resources. [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // Check if getResourceAsStream is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            boolean insideGetResourceAsStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            synchronized(syncResourceAsStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                insideGetResourceAsStream = resourceAsStreamInCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            // If getResourceAsStream is called, don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // trigger the following code. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // unnecessary connection will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (insideGetResourceAsStream == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                // otherwise, try the code base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    // check if resource exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    if(!resourceExists(url))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    // all exceptions, including security exceptions, are caught
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private boolean resourceExists(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // Check if the resource exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // It almost works to just try to do an openConnection() but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // HttpURLConnection will return true on HTTP_BAD_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // when the requested name ends in ".html", ".htm", and ".txt"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // and we want to be able to handle these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Also, cannot just open a connection for things like FileURLConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // because they succeed when connecting to a nonexistent file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // So, in those cases we open and close an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            URLConnection conn = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (conn instanceof java.net.HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                java.net.HttpURLConnection hconn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    (java.net.HttpURLConnection) conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // To reduce overhead, using http HEAD method instead of GET method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                hconn.setRequestMethod("HEAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                int code = hconn.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                if (code == java.net.HttpURLConnection.HTTP_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 * Fix for #4182052 - stanleyh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                 * The same connection should be reused to avoid multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                 * HTTP connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // our best guess for the other cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                InputStream is = conn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns an enumeration of all the applet resources with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * name. First checks loaded JAR files then the applet code base for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * available resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public Enumeration findResources(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        final Enumeration e = super.findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // 6215746:  Disable META-INF/* lookup from codebase in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // applet/plugin classloader. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (name.startsWith("META-INF/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (codebaseLookup == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        URL u = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (!resourceExists(u)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        final URL url = u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return new Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            private boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                return !done && (e.hasMoreElements() || url != null);
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Load and resolve the file specified by the applet tag CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * attribute. The argument can either be the relative path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * of the class file itself or just the name of the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    Class loadCode(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // first convert any '/' or native file separator to .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        name = name.replace('/', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        name = name.replace(File.separatorChar, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // deal with URL rewriting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        String cookie = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int index = name.indexOf(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if(index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                cookie = name.substring(index, name.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                name = name.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // save that name for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        String fullName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // then strip off any suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (name.endsWith(".class") || name.endsWith(".java")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            name = name.substring(0, name.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if(cookie != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        name = (new StringBuffer(name)).append(cookie).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // then if it didn't end with .java or .class, or in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // really pathological case of a class named class or java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if(cookie != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                fullName = (new StringBuffer(fullName)).append(cookie).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return loadClass(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * The threadgroup that the applets loaded by this classloader live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * in. In the sun.* implementation of applets, the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * (AppletSecurity) getThreadGroup returns the thread group of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * first applet on the stack, which is the applet's thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private AppletThreadGroup threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    private AppContext appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    public ThreadGroup getThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
      synchronized (threadGroupSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (threadGroup == null || threadGroup.isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    threadGroup = new AppletThreadGroup(base + "-threadGroup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    // threadGroup.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    // threadGroup is now destroyed by AppContext.dispose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    // Create the new AppContext from within a Thread belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    // to the newly created ThreadGroup, and wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    // creation to complete before returning from this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    AppContextCreator creatorThread = new AppContextCreator(threadGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    // Since this thread will later be used to launch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    // applet's AWT-event dispatch thread and we want the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    // code executing the AWT callbacks to use their own class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    // loader rather than the system class loader, explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    // set the context class loader to the AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    creatorThread.setContextClassLoader(AppletClassLoader.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   667
                    creatorThread.start();
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   668
                    try {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   669
                        synchronized(creatorThread.syncObject) {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   670
                            while (!creatorThread.created) {
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   671
                                creatorThread.syncObject.wait();
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
                        }
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   674
                    } catch (InterruptedException e) { }
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   675
                    appContext = creatorThread.appContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return threadGroup;
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
     * Get the AppContext, if any, corresponding to this AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public AppContext getAppContext()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    int usageCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Grab this AppletClassLoader and its ThreadGroup/AppContext, so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * won't be destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   697
public     void grab() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        synchronized(grabReleaseSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            usageCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        getThreadGroup(); // Make sure ThreadGroup/AppContext exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    protected void setExceptionStatus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        exceptionStatus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public boolean getExceptionStatus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        return exceptionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Release this AppletClassLoader and its ThreadGroup/AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * If nothing else has grabbed this AppletClassLoader, its ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * and AppContext will be destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Because this method may destroy the AppletClassLoader's ThreadGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * this method should NOT be called from within the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Changed modifier to protected in order to be able to overwrite this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * function in PluginClassLoader.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    protected void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        AppContext tempAppContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        synchronized(grabReleaseSynchronizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (usageCount > 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                --usageCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                synchronized(threadGroupSynchronizer) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   735
                    tempAppContext = resetAppContext();
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // Dispose appContext outside any sync block to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        // prevent potential deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (tempAppContext != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                tempAppContext.dispose(); // nuke the world!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            } catch (IllegalThreadStateException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   749
    /*
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   750
     * reset classloader's AppContext and ThreadGroup
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   751
     * This method is for subclass PluginClassLoader to
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   752
     * 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
   753
     * not dispose the AppContext. PluginClassLoader does not
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   754
     * 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
   755
     *
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   756
     * @return previous AppContext
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   757
     */
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   758
    protected AppContext resetAppContext() {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   759
        AppContext tempAppContext = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   760
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   761
        synchronized(threadGroupSynchronizer) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   762
            // Store app context in temp variable
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   763
            tempAppContext = appContext;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   764
            usageCount = 0;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   765
            appContext = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   766
            threadGroup = null;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   767
        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   768
        return tempAppContext;
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
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   771
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    // Hash map to store applet compatibility info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private HashMap jdk11AppletInfo = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    private HashMap jdk12AppletInfo = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Set applet target level as JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @param clazz Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param bool true if JDK is targeted for JDK 1.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *             false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    void setJDK11Target(Class clazz, boolean bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         jdk11AppletInfo.put(clazz.toString(), Boolean.valueOf(bool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Set applet target level as JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param clazz Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param bool true if JDK is targeted for JDK 1.2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *             false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    void setJDK12Target(Class clazz, boolean bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        jdk12AppletInfo.put(clazz.toString(), Boolean.valueOf(bool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Determine if applet is targeted for JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param applet Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @return TRUE if applet is targeted for JDK 1.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *         FALSE if applet is not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *         null if applet is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    Boolean isJDK11Target(Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        return (Boolean) jdk11AppletInfo.get(clazz.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Determine if applet is targeted for JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param applet Applet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @return TRUE if applet is targeted for JDK 1.2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *         FALSE if applet is not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *         null if applet is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    Boolean isJDK12Target(Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return (Boolean) jdk12AppletInfo.get(clazz.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    private static AppletMessageHandler mh =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        new AppletMessageHandler("appletclassloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Prints a class loading error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    private static void printError(String name, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        String s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            s = mh.getMessage("filenotfound", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        } else if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            s = mh.getMessage("fileioexception", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        } else if (e instanceof ClassFormatError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            s = mh.getMessage("fileformat", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else if (e instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            s = mh.getMessage("filedeath", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        } else if (e instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            s = mh.getMessage("fileerror", e.toString(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            System.err.println(s);
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
 * The AppContextCreator class is used to create an AppContext from within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
 * a Thread belonging to the new AppContext's ThreadGroup.  To wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
 * this operation to complete before continuing, wait for the notifyAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
 * operation on the syncObject to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
class AppContextCreator extends Thread  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    Object syncObject = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    AppContext appContext = null;
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   860
    volatile boolean created = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    AppContextCreator(ThreadGroup group)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        super(group, "AppContextCreator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public void run()  {
7956
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   867
        appContext = SunToolkit.createNewAppContext();
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   868
        created = true;
34a05c4d244f 7011442: AppletClassLoader.java needs to avoid spurious wakeup
dcherepanov
parents: 5506
diff changeset
   869
        synchronized(syncObject) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            syncObject.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
} // class AppContextCreator