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