jdk/src/java.base/share/classes/java/security/SecureClassLoader.java
author mullan
Fri, 12 Jun 2015 12:50:41 -0400
changeset 31141 ef5ddf021137
parent 29492 a4bf9a570035
child 31183 eb0ffbecb4ba
permissions -rw-r--r--
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class extends ClassLoader with additional support for defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * classes with an associated code source and permissions which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * retrieved by the system policy by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class SecureClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * If initialization succeed this is set to true and security checks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * succeed. Otherwise the object is not initialized and the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    48
    private final boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    50
    /*
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    51
     * HashMap that maps the CodeSource URL (as a String) to ProtectionDomain.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    52
     * We use a String instead of a CodeSource/URL as the key to avoid
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    53
     * potential expensive name service lookups. This does mean that URLs that
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    54
     * are equivalent after nameservice lookup will be placed in separate
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    55
     * ProtectionDomains; however during policy enforcement these URLs will be
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    56
     * canonicalized and resolved resulting in a consistent set of granted
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    57
     * permissions.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    58
     */
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    59
    private final HashMap<String, ProtectionDomain> pdcache = new HashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final Debug debug = Debug.getInstance("scl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    63
    static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    64
        ClassLoader.registerAsParallelCapable();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    65
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a new SecureClassLoader using the specified parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * class loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>If there is a security manager, this method first
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    72
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * method  to ensure creation of a class loader is allowed.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
    74
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param parent the parent ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    77
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected SecureClassLoader(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Creates a new SecureClassLoader using the default parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>If there is a security manager, this method first
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    96
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * method  to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   100
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected SecureClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Converts an array of bytes into an instance of class Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * with an optional CodeSource. Before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * constructed and associated with the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   121
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   122
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param      b    the bytes that make up the class data. The bytes in
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   126
     *             positions {@code off} through {@code off+len-1}
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   127
     *             should have the format of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   128
     *             <cite>The Java&trade; Virtual Machine Specification</cite>.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   129
     * @param      off  the start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param      len  the length of the class data
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   131
     * @param      cs   the associated CodeSource, or {@code null} if none
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   132
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @exception  ClassFormatError if the data did not contain a valid class
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   135
     * @exception  IndexOutOfBoundsException if either {@code off} or
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   136
     *             {@code len} is negative, or if
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   137
     *             {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected final Class<?> defineClass(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                         byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   148
        return defineClass(name, b, off, len, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   152
     * Converts a {@link java.nio.ByteBuffer ByteBuffer}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   153
     * into an instance of class {@code Class}, with an optional CodeSource.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * constructed and associated with the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   158
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   159
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param      b    the bytes that make up the class data.  The bytes from positions
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   163
     *                  {@code b.position()} through {@code b.position() + b.limit() -1}
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   164
     *                  should have the format of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   165
     *                  <cite>The Java&trade; Virtual Machine Specification</cite>.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   166
     * @param      cs   the associated CodeSource, or {@code null} if none
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   167
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception  ClassFormatError if the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   180
        return defineClass(name, b, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the permissions for the given CodeSource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * This method is invoked by the defineClass method which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * a CodeSource as an argument when it is constructing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * ProtectionDomain for the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   189
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param codesource the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @return the permissions granted to the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return new Permissions(); // ProtectionDomain defers the binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returned cached ProtectionDomain for the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private ProtectionDomain getProtectionDomain(CodeSource cs) {
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   205
        if (cs == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return null;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ProtectionDomain pd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        synchronized (pdcache) {
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   211
            // Use a String form of the URL as the key. It should behave in the
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   212
            // same manner as the URL when compared for equality except that no
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   213
            // nameservice lookup is done on the hostname (String comparison
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   214
            // only), and the fragment is not considered.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   215
            String key = cs.getLocationNoFragString();
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   216
            pd = pdcache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                PermissionCollection perms = getPermissions(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                pd = new ProtectionDomain(cs, perms, this, null);
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   220
                pdcache.put(key, pd);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   221
                if (debug != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   222
                    debug.println(" getPermissions "+ pd);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   223
                    debug.println("");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Check to make sure the class loader has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private void check() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new SecurityException("ClassLoader object not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}