jdk/src/share/classes/java/net/URLClassLoader.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
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 1997-2007 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 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URLStreamHandlerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.jar.Attributes.Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.SecureClassLoader;
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.misc.Resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.misc.URLClassPath;
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 loader is used to load classes and resources from a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * path of URLs referring to both JAR files and directories. Any URL that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * ends with a '/' is assumed to refer to a directory. Otherwise, the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * is assumed to refer to a JAR file which will be opened as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * The AccessControlContext of the thread that created the instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * URLClassLoader will be used when subsequently loading classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * The classes that are loaded are by default granted permission only to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * access the URLs specified when the URLClassLoader was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class URLClassLoader extends SecureClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* The search path for classes and resources */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    URLClassPath ucp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* The context to be used when loading classes and resources */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Constructs a new URLClassLoader for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * searched in the order specified for classes and resources after first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * searching in the specified parent class loader. Any URL that ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * a '/' is assumed to refer to a directory. Otherwise, the URL is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * to refer to a JAR file which will be downloaded and opened as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * calls the security manager's <code>checkCreateClassLoader</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *             <code>checkCreateClassLoader</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public URLClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ucp = new URLClassPath(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        acc = AccessController.getContext();
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
     * Constructs a new URLClassLoader for the specified URLs using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * default delegation parent <code>ClassLoader</code>. The URLs will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * be searched in the order specified for classes and resources after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * first searching in the parent class loader. Any URL that ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * a '/' is assumed to refer to a directory. Otherwise, the URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * assumed to refer to a JAR file which will be downloaded and opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * calls the security manager's <code>checkCreateClassLoader</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *             <code>checkCreateClassLoader</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public URLClassLoader(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ucp = new URLClassPath(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Constructs a new URLClassLoader for the specified URLs, parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * class loader, and URLStreamHandlerFactory. The parent argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * will be used as the parent class loader for delegation. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * factory argument will be used as the stream handler factory to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * obtain protocol handlers when creating new jar URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>If there is a security manager, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * calls the security manager's <code>checkCreateClassLoader</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param factory the URLStreamHandlerFactory to use when creating URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *             <code>checkCreateClassLoader</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public URLClassLoader(URL[] urls, ClassLoader parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                          URLStreamHandlerFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ucp = new URLClassPath(urls, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Appends the specified URL to the list of URLs to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If the URL specified is <code>null</code> or is already in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * list of URLs, then invoking this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param url the URL to be added to the search path of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected void addURL(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        ucp.addURL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This includes the original list of URLs specified to the constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * along with any URLs subsequently appended by the addURL() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return ucp.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Finds and loads the class with the specified name from the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * path. Any URLs referring to JAR files are loaded and opened as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * until the class is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param name the name of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return the resulting class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception ClassNotFoundException if the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected Class<?> findClass(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   208
            return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   209
                new PrivilegedExceptionAction<Class>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   210
                    public Class run() throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        String path = name.replace('.', '/').concat(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        Resource res = ucp.getResource(path, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                return defineClass(name, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                throw new ClassNotFoundException(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw (ClassNotFoundException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Defines a Class using the class bytes obtained from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Resource. The resulting Class must be resolved before it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private Class defineClass(String name, Resource res) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        URL url = res.getCodeSourceURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            String pkgname = name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // Check if package already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            Package pkg = getPackage(pkgname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Manifest man = res.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // Package found, so check package sealing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (pkg.isSealed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    // Verify that code source URL is the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    if (!pkg.isSealed(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            "sealing violation: package " + pkgname + " is sealed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    // Make sure we are not attempting to seal the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    // at this code source URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if ((man != null) && isSealed(pkgname, man)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            "sealing violation: can't seal package " + pkgname +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            ": already loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    definePackage(pkgname, man, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    definePackage(pkgname, null, null, null, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Now read the class bytes and define the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        java.nio.ByteBuffer bb = res.getByteBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (bb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // Use (direct) ByteBuffer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            CodeSource cs = new CodeSource(url, signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return defineClass(name, bb, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            byte[] b = res.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // must read certificates AFTER reading bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            CodeSource cs = new CodeSource(url, signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return defineClass(name, b, 0, b.length, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Defines a new package by name in this ClassLoader. The attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * contained in the specified Manifest will be used to obtain package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * version and sealing information. For sealed packages, the additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * URL specifies the code source URL from which the package was loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param name  the package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param man   the Manifest containing package version and sealing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *              information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param url   the code source url for the package, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @exception   IllegalArgumentException if the package name duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *              an existing package either in this class loader or one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *              of its ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return the newly defined Package object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    protected Package definePackage(String name, Manifest man, URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        String specTitle = null, specVersion = null, specVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        String implTitle = null, implVersion = null, implVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        URL sealBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            specTitle   = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            specVendor  = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            implTitle   = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            implVendor  = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            sealed      = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (specTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (specVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (specVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (implTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (implVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (implVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if ("true".equalsIgnoreCase(sealed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            sealBase = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return definePackage(name, specTitle, specVersion, specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                             implTitle, implVersion, implVendor, sealBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Returns true if the specified package name is sealed according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * given manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private boolean isSealed(String name, Manifest man) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if ((attr = man.getMainAttributes()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return "true".equalsIgnoreCase(sealed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Finds the resource with the specified name on the URL search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param name the name of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return a <code>URL</code> for the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * if the resource could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public URL findResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * The same restriction to finding classes applies to resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   379
        URL url = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   380
            new PrivilegedAction<URL>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   381
                public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    return ucp.findResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return url != null ? ucp.checkURL(url) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns an Enumeration of URLs representing all of the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * on the URL search path having the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return an <code>Enumeration</code> of <code>URL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public Enumeration<URL> findResources(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   400
        final Enumeration<URL> e = ucp.findResources(name, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return new Enumeration<URL>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   410
                    URL u = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   411
                        new PrivilegedAction<URL>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   412
                            public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                if (!e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    url = ucp.checkURL(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } while (url == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                return url != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            public URL nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Returns the permissions for the given codesource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * The implementation of this method first calls super.getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * and then adds permissions based on the URL of the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * If the protocol of this URL is "jar", then the permission granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * is based on the permission that is required by the URL of the Jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * If the protocol is "file" and there is an authority component, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * permission to connect to and accept connections from that authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * may be granted. If the protocol is "file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * and the path specifies a file, then permission to read that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * file is granted. If protocol is "file" and the path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * a directory, permission is granted to read all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * and (recursively) all files and subdirectories contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * that directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * If the protocol is not "file", then permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * to connect to and accept connections from the URL's host is granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param codesource the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return the permissions granted to the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        URL url = codesource.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        URLConnection urlConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            urlConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // if the permission has a separator char on the end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            // it means the codebase is a directory, and we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            // to add an additional permission to read recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else if ((p == null) && (url.getProtocol().equals("file"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (path.endsWith(File.separator))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            p =  new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
             * Not loading from a 'file:' URL so we want to give the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
             * permission to connect to and accept from the remote host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             * after we've made sure the host is the correct one and is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            String host = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (host != null && (host.length() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                p = new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                         SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // make sure the person that created this class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // would have this permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                final Permission fp = p;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   518
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   519
                    public Void run() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        sm.checkPermission(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * URLs and parent class loader. If a security manager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * installed, the <code>loadClass</code> method of the URLClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * returned by this method will invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <code>SecurityManager.checkPackageAccess</code> method before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param urls the URLs to search for classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public static URLClassLoader newInstance(final URL[] urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                             final ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        // Save the caller's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   547
        URLClassLoader ucl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   548
            new PrivilegedAction<URLClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   549
                public URLClassLoader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return new FactoryURLClassLoader(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // Now set the context on the loader using the one we saved,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // not the one inside the privileged block...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        ucl.acc = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * URLs and default parent class loader. If a security manager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * installed, the <code>loadClass</code> method of the URLClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * returned by this method will invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <code>SecurityManager.checkPackageAccess</code> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param urls the URLs to search for classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public static URLClassLoader newInstance(final URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // Save the caller's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   574
        URLClassLoader ucl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   575
            new PrivilegedAction<URLClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   576
                public URLClassLoader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    return new FactoryURLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // Now set the context on the loader using the one we saved,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // not the one inside the privileged block...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        ucl.acc = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        sun.misc.SharedSecrets.setJavaNetAccess (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            new sun.misc.JavaNetAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                public URLClassPath getURLClassPath (URLClassLoader u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    return u.ucp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
final class FactoryURLClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    FactoryURLClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        super(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    FactoryURLClassLoader(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        super(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public final synchronized Class loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        // First check if we have permission to access the package. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // should go away once we've added support for exported packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return super.loadClass(name, resolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
}