jdk/src/share/classes/java/net/URLClassLoader.java
author mchung
Mon, 14 Sep 2009 13:37:26 -0700
changeset 3849 34469964aa98
parent 3228 d7c00dbea781
child 5506 202f599c92aa
permissions -rw-r--r--
6878481: Add performance counters in the JDK Summary: Added new performance counters in the JDK to track performance metrics Reviewed-by: alanb, dholmes, iris, forax, andrew
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    34
import java.io.Closeable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URLStreamHandlerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Enumeration;
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    39
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.jar.Attributes.Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.CodeSigner;
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.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.SecureClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.misc.Resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.misc.URLClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * This class loader is used to load classes and resources from a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * path of URLs referring to both JAR files and directories. Any URL that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * ends with a '/' is assumed to refer to a directory. Otherwise, the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * is assumed to refer to a JAR file which will be opened as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The AccessControlContext of the thread that created the instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * URLClassLoader will be used when subsequently loading classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The classes that are loaded are by default granted permission only to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * access the URLs specified when the URLClassLoader was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    75
public class URLClassLoader extends SecureClassLoader implements Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* The search path for classes and resources */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
    77
    private final URLClassPath ucp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* The context to be used when loading classes and resources */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
    80
    private final AccessControlContext acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Constructs a new URLClassLoader for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * searched in the order specified for classes and resources after first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * searching in the specified parent class loader. Any URL that ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * a '/' is assumed to refer to a directory. Otherwise, the URL is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * to refer to a JAR file which will be downloaded and opened as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>If there is a security manager, this method first
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    90
     * calls the security manager's {@code checkCreateClassLoader} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception  SecurityException  if a security manager exists and its
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    96
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public URLClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   108
        this.acc = AccessController.getContext();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   109
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   110
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   111
    URLClassLoader(URL[] urls, ClassLoader parent,
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   112
                   AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   113
        super(parent);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   114
        // this is to make the stack depth consistent with 1.1
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   115
        SecurityManager security = System.getSecurityManager();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   116
        if (security != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   117
            security.checkCreateClassLoader();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   118
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   119
        ucp = new URLClassPath(urls);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   120
        this.acc = acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Constructs a new URLClassLoader for the specified URLs using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * default delegation parent <code>ClassLoader</code>. The URLs will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * be searched in the order specified for classes and resources after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * first searching in the parent class loader. Any URL that ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * a '/' is assumed to refer to a directory. Otherwise, the URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * assumed to refer to a JAR file which will be downloaded and opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * calls the security manager's <code>checkCreateClassLoader</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *             <code>checkCreateClassLoader</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public URLClassLoader(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   151
        this.acc = AccessController.getContext();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   152
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   153
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   154
    URLClassLoader(URL[] urls, AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   155
        super();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   156
        // this is to make the stack depth consistent with 1.1
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   157
        SecurityManager security = System.getSecurityManager();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   158
        if (security != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   159
            security.checkCreateClassLoader();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   160
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   161
        ucp = new URLClassPath(urls);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   162
        this.acc = acc;
2
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
     * Constructs a new URLClassLoader for the specified URLs, parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * class loader, and URLStreamHandlerFactory. The parent argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * will be used as the parent class loader for delegation. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * factory argument will be used as the stream handler factory to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * obtain protocol handlers when creating new jar URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * calls the security manager's <code>checkCreateClassLoader</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param factory the URLStreamHandlerFactory to use when creating URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *             <code>checkCreateClassLoader</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public URLClassLoader(URL[] urls, ClassLoader parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                          URLStreamHandlerFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ucp = new URLClassPath(urls, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   197
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   198
   /**
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   199
    * Closes this URLClassLoader, so that it can no longer be used to load
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   200
    * new classes or resources that are defined by this loader.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   201
    * Classes and resources defined by any of this loader's parents in the
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   202
    * delegation hierarchy are still accessible. Also, any classes or resources
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   203
    * that are already loaded, are still accessible.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   204
    * <p>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   205
    * In the case of jar: and file: URLs, it also closes any class files,
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   206
    * or JAR files that were opened by it. If another thread is loading a
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   207
    * class when the {@code close} method is invoked, then the result of
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   208
    * that load is undefined.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   209
    * <p>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   210
    * The method makes a best effort attempt to close all opened files,
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   211
    * by catching {@link IOException}s internally. Unchecked exceptions
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   212
    * and errors are not caught. Calling close on an already closed
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   213
    * loader has no effect.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   214
    * <p>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   215
    * @throws IOException if closing any file opened by this class loader
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   216
    * resulted in an IOException. Any such exceptions are caught, and a
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   217
    * single IOException is thrown after the last file has been closed.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   218
    * If only one exception was thrown, it will be set as the <i>cause</i>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   219
    * of this IOException.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   220
    *
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   221
    * @throws SecurityException if a security manager is set, and it denies
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   222
    *   {@link RuntimePermission}<tt>("closeClassLoader")</tt>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   223
    *
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   224
    * @since 1.7
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   225
    */
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   226
    public void close() throws IOException {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   227
        SecurityManager security = System.getSecurityManager();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   228
        if (security != null) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   229
            security.checkPermission(new RuntimePermission("closeClassLoader"));
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   230
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   231
        List<IOException> errors = ucp.closeLoaders();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   232
        if (errors.isEmpty()) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   233
            return;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   234
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   235
        if (errors.size() == 1) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   236
            throw new IOException (
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   237
                "Error closing URLClassLoader resource",
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   238
                errors.get(0)
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   239
            );
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   240
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   241
        // Several exceptions. So, just combine the error messages
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   242
        String errormsg = "Error closing resources: ";
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   243
        for (IOException error: errors) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   244
            errormsg = errormsg + "[" + error.toString() + "] ";
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   245
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   246
        throw new IOException (errormsg);
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   247
    }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   248
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Appends the specified URL to the list of URLs to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If the URL specified is <code>null</code> or is already in the
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   254
     * list of URLs, or if this loader is closed, then invoking this
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   255
     * method has no effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param url the URL to be added to the search path of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    protected void addURL(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        ucp.addURL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * This includes the original list of URLs specified to the constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * along with any URLs subsequently appended by the addURL() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return ucp.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Finds and loads the class with the specified name from the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * path. Any URLs referring to JAR files are loaded and opened as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * until the class is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param name the name of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the resulting class
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   280
     * @exception ClassNotFoundException if the class could not be found,
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   281
     *            or if the loader is closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected Class<?> findClass(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   287
            return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   288
                new PrivilegedExceptionAction<Class>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   289
                    public Class run() throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        String path = name.replace('.', '/').concat(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        Resource res = ucp.getResource(path, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                return defineClass(name, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                throw new ClassNotFoundException(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw (ClassNotFoundException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /*
3228
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   309
     * Retrieve the package using the specified package name.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   310
     * If non-null, verify the package using the specified code
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   311
     * source and manifest.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   312
     */
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   313
    private Package getAndVerifyPackage(String pkgname,
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   314
                                        Manifest man, URL url) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   315
        Package pkg = getPackage(pkgname);
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   316
        if (pkg != null) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   317
            // Package found, so check package sealing.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   318
            if (pkg.isSealed()) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   319
                // Verify that code source URL is the same.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   320
                if (!pkg.isSealed(url)) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   321
                    throw new SecurityException(
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   322
                        "sealing violation: package " + pkgname + " is sealed");
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   323
                }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   324
            } else {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   325
                // Make sure we are not attempting to seal the package
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   326
                // at this code source URL.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   327
                if ((man != null) && isSealed(pkgname, man)) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   328
                    throw new SecurityException(
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   329
                        "sealing violation: can't seal package " + pkgname +
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   330
                        ": already loaded");
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   331
                }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   332
            }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   333
        }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   334
        return pkg;
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   335
    }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   336
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   337
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Defines a Class using the class bytes obtained from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Resource. The resulting Class must be resolved before it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private Class defineClass(String name, Resource res) throws IOException {
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3228
diff changeset
   343
        long t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        URL url = res.getCodeSourceURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            String pkgname = name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // Check if package already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            Manifest man = res.getManifest();
3228
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   350
            if (getAndVerifyPackage(pkgname, man, url) == null) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   351
                try {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   352
                    if (man != null) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   353
                        definePackage(pkgname, man, url);
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   354
                    } else {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   355
                        definePackage(pkgname, null, null, null, null, null, null, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
3228
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   357
                } catch (IllegalArgumentException iae) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   358
                    // parallel-capable class loaders: re-verify in case of a
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   359
                    // race condition
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   360
                    if (getAndVerifyPackage(pkgname, man, url) == null) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   361
                        // Should never happen
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   362
                        throw new AssertionError("Cannot find package " +
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   363
                                                 pkgname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // Now read the class bytes and define the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        java.nio.ByteBuffer bb = res.getByteBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (bb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // Use (direct) ByteBuffer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            CodeSource cs = new CodeSource(url, signers);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3228
diff changeset
   374
            sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return defineClass(name, bb, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            byte[] b = res.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // must read certificates AFTER reading bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            CodeSource cs = new CodeSource(url, signers);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3228
diff changeset
   381
            sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return defineClass(name, b, 0, b.length, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Defines a new package by name in this ClassLoader. The attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * contained in the specified Manifest will be used to obtain package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * version and sealing information. For sealed packages, the additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * URL specifies the code source URL from which the package was loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param name  the package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param man   the Manifest containing package version and sealing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *              information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param url   the code source url for the package, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @exception   IllegalArgumentException if the package name duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *              an existing package either in this class loader or one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *              of its ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return the newly defined Package object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    protected Package definePackage(String name, Manifest man, URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        String specTitle = null, specVersion = null, specVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        String implTitle = null, implVersion = null, implVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        URL sealBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            specTitle   = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            specVendor  = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            implTitle   = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            implVendor  = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            sealed      = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (specTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (specVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (specVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (implTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (implVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (implVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if ("true".equalsIgnoreCase(sealed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            sealBase = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return definePackage(name, specTitle, specVersion, specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                             implTitle, implVersion, implVendor, sealBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Returns true if the specified package name is sealed according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * given manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private boolean isSealed(String name, Manifest man) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if ((attr = man.getMainAttributes()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return "true".equalsIgnoreCase(sealed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Finds the resource with the specified name on the URL search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param name the name of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return a <code>URL</code> for the resource, or <code>null</code>
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   475
     * if the resource could not be found, or if the loader is closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public URL findResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * The same restriction to finding classes applies to resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   481
        URL url = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   482
            new PrivilegedAction<URL>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   483
                public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    return ucp.findResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return url != null ? ucp.checkURL(url) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Returns an Enumeration of URLs representing all of the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * on the URL search path having the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return an <code>Enumeration</code> of <code>URL</code>s
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   498
     *         If the loader is closed, the Enumeration will be empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public Enumeration<URL> findResources(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   503
        final Enumeration<URL> e = ucp.findResources(name, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return new Enumeration<URL>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   513
                    URL u = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   514
                        new PrivilegedAction<URL>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   515
                            public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                if (!e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    url = ucp.checkURL(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                } while (url == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return url != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            public URL nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Returns the permissions for the given codesource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * The implementation of this method first calls super.getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * and then adds permissions based on the URL of the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * If the protocol of this URL is "jar", then the permission granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * is based on the permission that is required by the URL of the Jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * If the protocol is "file" and there is an authority component, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * permission to connect to and accept connections from that authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * may be granted. If the protocol is "file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * and the path specifies a file, then permission to read that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * file is granted. If protocol is "file" and the path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * a directory, permission is granted to read all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * and (recursively) all files and subdirectories contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * that directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * If the protocol is not "file", then permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * to connect to and accept connections from the URL's host is granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param codesource the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return the permissions granted to the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        URL url = codesource.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        URLConnection urlConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            urlConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // if the permission has a separator char on the end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            // it means the codebase is a directory, and we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // to add an additional permission to read recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        } else if ((p == null) && (url.getProtocol().equals("file"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (path.endsWith(File.separator))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            p =  new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
             * Not loading from a 'file:' URL so we want to give the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
             * permission to connect to and accept from the remote host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
             * after we've made sure the host is the correct one and is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            String host = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (host != null && (host.length() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                p = new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                         SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // make sure the person that created this class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // would have this permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                final Permission fp = p;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   621
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   622
                    public Void run() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                        sm.checkPermission(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * URLs and parent class loader. If a security manager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * installed, the <code>loadClass</code> method of the URLClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * returned by this method will invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <code>SecurityManager.checkPackageAccess</code> method before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param urls the URLs to search for classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public static URLClassLoader newInstance(final URL[] urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                             final ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // Save the caller's context
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   648
        final AccessControlContext acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   650
        URLClassLoader ucl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   651
            new PrivilegedAction<URLClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   652
                public URLClassLoader run() {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   653
                    return new FactoryURLClassLoader(urls, parent, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * URLs and default parent class loader. If a security manager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * installed, the <code>loadClass</code> method of the URLClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * returned by this method will invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <code>SecurityManager.checkPackageAccess</code> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param urls the URLs to search for classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public static URLClassLoader newInstance(final URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // Save the caller's context
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   672
        final AccessControlContext acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   674
        URLClassLoader ucl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   675
            new PrivilegedAction<URLClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   676
                public URLClassLoader run() {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   677
                    return new FactoryURLClassLoader(urls, acc);
2
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 ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        sun.misc.SharedSecrets.setJavaNetAccess (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            new sun.misc.JavaNetAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                public URLClassPath getURLClassPath (URLClassLoader u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    return u.ucp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        );
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   691
        ClassLoader.registerAsParallelCapable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
final class FactoryURLClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   697
    static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   698
        ClassLoader.registerAsParallelCapable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   701
    FactoryURLClassLoader(URL[] urls, ClassLoader parent,
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   702
                          AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   703
        super(urls, parent, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   706
    FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   707
        super(urls, acc);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   708
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   709
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   710
    public final Class loadClass(String name, boolean resolve)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // First check if we have permission to access the package. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        // should go away once we've added support for exported packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return super.loadClass(name, resolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}