src/java.rmi/share/classes/sun/rmi/server/LoaderHandler.java
author rehn
Fri, 29 Nov 2019 12:09:25 +0100
changeset 59325 3636bab5e81e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234086: VM operation can be simplified Reviewed-by: kbarrett, dholmes, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 18196
diff changeset
     2
 * Copyright (c) 1996, 2013, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.Permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.Policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.server.LogStream;
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 21655
diff changeset
    50
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.IdentityHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.WeakHashMap;
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
    59
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>LoaderHandler</code> provides the implementation of the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * methods of the <code>java.rmi.server.RMIClassLoader</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author      Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author      Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 18196
diff changeset
    70
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public final class LoaderHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** RMI class loader log level */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int logLevel = LogStream.parseLevel(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    75
        java.security.AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 21655
diff changeset
    76
            (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.loader.logLevel")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /* loader system log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final Log loaderLog =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Log.getLog("sun.rmi.loader", "loader", LoaderHandler.logLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * value of "java.rmi.server.codebase" property, as cached at class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * initialization time.  It may contain malformed URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static String codebaseProperty = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    88
        String prop = java.security.AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 21655
diff changeset
    89
            (PrivilegedAction<String>) () -> System.getProperty("java.rmi.server.codebase"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (prop != null && prop.trim().length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            codebaseProperty = prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** list of URLs represented by the codebase property, if valid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static URL[] codebaseURLs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** table of class loaders that use codebase property for annotation */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    99
    private static final Map<ClassLoader, Void> codebaseLoaders =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   100
        Collections.synchronizedMap(new IdentityHashMap<ClassLoader, Void>(5));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (ClassLoader codebaseLoader = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             codebaseLoader != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             codebaseLoader = codebaseLoader.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            codebaseLoaders.put(codebaseLoader, null);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * table mapping codebase URL path and context class loader pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * to class loader instances.  Entries hold class loaders with weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * references, so this table does not prevent loaders from being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   116
    private static final HashMap<LoaderKey, LoaderEntry> loaderTable
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   117
        = new HashMap<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** reference queue for cleared class loader entries */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   120
    private static final ReferenceQueue<Loader> refQueue
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   121
        = new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Disallow anyone from creating one of these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private LoaderHandler() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns an array of URLs initialized with the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * java.rmi.server.codebase property as the URL path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static synchronized URL[] getDefaultCodebaseURLs()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * If it hasn't already been done, convert the codebase property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * into an array of URLs; this may throw a MalformedURLException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (codebaseURLs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (codebaseProperty != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                codebaseURLs = pathToURLs(codebaseProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                codebaseURLs = new URL[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return codebaseURLs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Load a class from a network location (one or more URLs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * but first try to resolve the named class through the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * "default loader".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   154
    public static Class<?> loadClass(String codebase, String name,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   155
                                     ClassLoader defaultLoader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws MalformedURLException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                "name = \"" + name + "\", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                "codebase = \"" + (codebase != null ? codebase : "") + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                (defaultLoader != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                 ", defaultLoader = " + defaultLoader : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        URL[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (codebase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            urls = pathToURLs(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            urls = getDefaultCodebaseURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (defaultLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
   175
                Class<?> c = loadClassForName(name, false, defaultLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        "class \"" + name + "\" found via defaultLoader, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        "defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } catch (ClassNotFoundException e) {
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
        return loadClass(urls, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Returns the class annotation (representing the location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * a class) that RMI will use to annotate the call stream when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * marshalling objects of the given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   194
    public static String getClassAnnotation(Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String name = cl.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * Class objects for arrays of primitive types never need an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * annotation, because they never need to be (or can be) downloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * REMIND: should we (not) be annotating classes that are in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * "java.*" packages?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int nameLength = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (nameLength > 0 && name.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // skip past all '[' characters (see bugid 4211906)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            while (nameLength > i && name.charAt(i) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (nameLength > i && name.charAt(i) != 'L') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * Get the class's class loader.  If it is null, the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * loader, an ancestor of the base class loader (such as the loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * for installed extensions), return the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * "java.rmi.server.codebase" property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ClassLoader loader = cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (loader == null || codebaseLoaders.containsKey(loader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return codebaseProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * Get the codebase URL path for the class loader, if it supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * such a notion (i.e., if it is a URLClassLoader or subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        String annotation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (loader instanceof Loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             * If the class loader is one of our RMI class loaders, we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             * already computed the class annotation string, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             * permissions are required to know the URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            annotation = ((Loader) loader).getClassAnnotation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else if (loader instanceof URLClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                URL[] urls = ((URLClassLoader) loader).getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (urls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                     * If the class loader is not one of our RMI class loaders,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                     * we must verify that the current access control context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                     * has permission to know all of these URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            Permission p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                urls[i].openConnection().getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                if (!perms.implies(p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                    sm.checkPermission(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                    perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    annotation = urlsToPath(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   266
            } catch (SecurityException | IOException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                /*
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   268
                 * SecurityException: If access was denied to the knowledge of
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   269
                 * the class loader's URLs, fall back to the default behavior.
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   270
                 *
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   271
                 * IOException: This shouldn't happen, although it is declared
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   272
                 * to be thrown by openConnection() and getPermission().  If it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                 * does happen, forget about this class loader's URLs and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                 * fall back to the default behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (annotation != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return codebaseProperty;    // REMIND: does this make sense??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns a classloader that loads classes from the given codebase URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * path.  The parent classloader of the returned classloader is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * context class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public static ClassLoader getClassLoader(String codebase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ClassLoader parent = getRMIContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        URL[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (codebase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            urls = pathToURLs(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            urls = getDefaultCodebaseURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * If there is a security manager, the current access control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * context must have the "getClassLoader" RuntimePermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            sm.checkPermission(new RuntimePermission("getClassLoader"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
             * But if no security manager is set, disable access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             * RMI class loaders and simply return the parent loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Loader loader = lookupLoader(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * Verify that the caller has permission to access this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            loader.checkPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Return the security context of the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public static Object getSecurityContext(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * REMIND: This is a bogus JDK1.1-compatible implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * This method should never be called by application code anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * (hence the deprecation), but should it do something different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * and perhaps more useful, like return a String or a URL[]?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (loader instanceof Loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            URL[] urls = ((Loader) loader).getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (urls.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return urls[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return null;
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
     * Register a class loader as one whose classes should always be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * annotated with the value of the "java.rmi.server.codebase" property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public static void registerCodebaseLoader(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        codebaseLoaders.put(loader, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Load a class from the RMI class loader corresponding to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * codebase URL path in the current execution context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   361
    private static Class<?> loadClass(URL[] urls, String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        ClassLoader parent = getRMIContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                "(thread context class loader: " + parent + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * If no security manager is set, disable access to RMI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * loaders and simply delegate request to the parent loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * (see bugid 4140511).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   378
                Class<?> c = Class.forName(name, false, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        "class \"" + name + "\" found via " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        "thread context class loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        "(no security manager: codebase disabled), " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        "defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        "class \"" + name + "\" not found via " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        "thread context class loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        "(no security manager: codebase disabled)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                throw new ClassNotFoundException(e.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    " (no security manager: RMI class loader disabled)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * Get or create the RMI class loader for this codebase URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * and parent class loader pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Loader loader = lookupLoader(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                 * Verify that the caller has permission to access this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                loader.checkPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
             * If the current access control context does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
             * to access all of the URLs in the codebase path, wrap the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
             * resulting security exception in a ClassNotFoundException, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
             * the caller can handle this outcome just like any other class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
             * loading failure (see bugid 4146529).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                 * But first, check to see if the named class could have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                 * resolved without the security-offending codebase anyway;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                 * if so, return successfully (see bugids 4191926 & 4349670).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                 */
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
   427
                Class<?> c = loadClassForName(name, false, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        "class \"" + name + "\" found via " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        "thread context class loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        "(access to codebase denied), " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        "defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } catch (ClassNotFoundException unimportant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                 * Presumably the security exception is the more important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                 * exception to report in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        "class \"" + name + "\" not found via " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        "thread context class loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        "(access to codebase denied)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw new ClassNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    "access to class loader denied", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        try {
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
   453
            Class<?> c = loadClassForName(name, false, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    "class \"" + name + "\" " + "found via codebase, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    "defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    "class \"" + name + "\" not found via codebase", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Define and return a dynamic proxy class in a class loader with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * URLs supplied in the given location.  The proxy class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * implement interface classes named by the given array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * interface names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   475
    public static Class<?> loadProxyClass(String codebase, String[] interfaces,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   476
                                          ClassLoader defaultLoader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        throws MalformedURLException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                "interfaces = " + Arrays.asList(interfaces) + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                "codebase = \"" + (codebase != null ? codebase : "") + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                (defaultLoader != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                 ", defaultLoader = " + defaultLoader : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * This method uses a fairly complex algorithm to load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * proxy class and its interface classes in order to maximize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * the likelihood that the proxy's codebase annotation will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * preserved.  The algorithm is (assuming that all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * proxy interface classes are public):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * If the default loader is not null, try to load the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * interfaces through that loader. If the interfaces can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * loaded in that loader, try to define the proxy class in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * RMI class loader (child of the context class loader) before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * trying to define the proxy in the default loader.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * attempt to define the proxy class succeeds, the codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * annotation is preserved.  If the attempt fails, try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * define the proxy class in the default loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * If the interface classes can not be loaded from the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * loader or the default loader is null, try to load them from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         * the RMI class loader.  Then try to define the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * in the RMI class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * Additionally, if any of the proxy interface classes are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * public, all of the non-public interfaces must reside in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * same class loader or it will be impossible to define the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * proxy class (an IllegalAccessError will be thrown).  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * attempt to load the interfaces from the default loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * made.  If the attempt fails, a second attempt will be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * to load the interfaces from the RMI loader. If all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * non-public interfaces classes do reside in the same class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * loader, then we attempt to define the proxy class in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * class loader of the non-public interfaces.  No other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * attempt to define the proxy class will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        ClassLoader parent = getRMIContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                "(thread context class loader: " + parent + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        URL[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (codebase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            urls = pathToURLs(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            urls = getDefaultCodebaseURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * If no security manager is set, disable access to RMI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * loaders and use the would-de parent instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   540
                Class<?> c = loadProxyClass(interfaces, defaultLoader, parent,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                         false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        "(no security manager: codebase disabled) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        "proxy class defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        "(no security manager: codebase disabled) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        "proxy class resolution failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                throw new ClassNotFoundException(e.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    " (no security manager: RMI class loader disabled)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    e.getException());
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
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * Get or create the RMI class loader for this codebase URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * and parent class loader pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        Loader loader = lookupLoader(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                 * Verify that the caller has permission to access this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                loader.checkPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
             * If the current access control context does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
             * to access all of the URLs in the codebase path, wrap the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
             * resulting security exception in a ClassNotFoundException, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
             * the caller can handle this outcome just like any other class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
             * loading failure (see bugid 4146529).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                 * But first, check to see if the proxy class could have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                 * resolved without the security-offending codebase anyway;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                 * if so, return successfully (see bugids 4191926 & 4349670).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                 */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   587
                Class<?> c = loadProxyClass(interfaces, defaultLoader, parent,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   588
                                            false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        "(access to codebase denied) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        "proxy class defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } catch (ClassNotFoundException unimportant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                 * Presumably the security exception is the more important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                 * exception to report in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        "(access to codebase denied) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        "proxy class resolution failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                throw new ClassNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    "access to class loader denied", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   611
            Class<?> c = loadProxyClass(interfaces, defaultLoader, loader, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                              "proxy class defined by " + c.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (loaderLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                loaderLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                              "proxy class resolution failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Define a proxy class in the default loader if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Define the class in an RMI class loader otherwise.  The proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * class will implement classes which are named in the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * interfaceNames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   632
    private static Class<?> loadProxyClass(String[] interfaceNames,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   633
                                           ClassLoader defaultLoader,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   634
                                           ClassLoader codebaseLoader,
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   635
                                           boolean preferCodebase)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        ClassLoader proxyLoader = null;
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   639
        Class<?>[] classObjs = new Class<?>[interfaceNames.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        boolean[] nonpublic = { false };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
      defaultLoaderCase:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (defaultLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                proxyLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    loadProxyInterfaces(interfaceNames, defaultLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                        classObjs, nonpublic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    ClassLoader[] definingLoaders =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        new ClassLoader[classObjs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    for (int i = 0; i < definingLoaders.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        definingLoaders[i] = classObjs[i].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        "proxy interfaces found via defaultLoader, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        "defined by " + Arrays.asList(definingLoaders));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                break defaultLoaderCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (!nonpublic[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                if (preferCodebase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                        return Proxy.getProxyClass(codebaseLoader, classObjs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                proxyLoader = defaultLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return loadProxyClass(proxyLoader, classObjs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        nonpublic[0] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        proxyLoader = loadProxyInterfaces(interfaceNames, codebaseLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                          classObjs, nonpublic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            ClassLoader[] definingLoaders = new ClassLoader[classObjs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            for (int i = 0; i < definingLoaders.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                definingLoaders[i] = classObjs[i].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                "proxy interfaces found via codebase, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                "defined by " + Arrays.asList(definingLoaders));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (!nonpublic[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            proxyLoader = codebaseLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return loadProxyClass(proxyLoader, classObjs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Define a proxy class in the given class loader.  The proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * class will implement the given interfaces Classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 19211
diff changeset
   695
    private static Class<?> loadProxyClass(ClassLoader loader, Class<?>[] interfaces)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            return Proxy.getProxyClass(loader, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            throw new ClassNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                "error creating dynamic proxy class", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Load Class objects for the names in the interfaces array fron
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * We pass classObjs and nonpublic arrays to avoid needing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * multi-element return value.  nonpublic is an array to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * the method to take a boolean argument by reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * nonpublic array is needed to signal when the return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * this method should be used as the proxy class loader.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * null represents a valid class loader, that value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * insufficient to signal that the return value should not be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * as the proxy class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    private static ClassLoader loadProxyInterfaces(String[] interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                                   ClassLoader loader,
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 19211
diff changeset
   722
                                                   Class<?>[] classObjs,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                                   boolean[] nonpublic)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        /* loader of a non-public interface class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        ClassLoader nonpublicLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        for (int i = 0; i < interfaces.length; i++) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   730
            Class<?> cl =
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
   731
                (classObjs[i] = loadClassForName(interfaces[i], false, loader));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (!Modifier.isPublic(cl.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                ClassLoader current = cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                if (loaderLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    loaderLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        "non-public interface \"" + interfaces[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        "\" defined by " + current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (!nonpublic[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    nonpublicLoader = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    nonpublic[0] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                } else if (current != nonpublicLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    throw new IllegalAccessError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        "non-public interfaces defined in different " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        "class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        return nonpublicLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Convert a string containing a space-separated list of URLs into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * corresponding array of URL objects, throwing a MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * if any of the URLs are invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private static URL[] pathToURLs(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        synchronized (pathToURLsCache) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   762
            Object[] v = pathToURLsCache.get(path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                return ((URL[])v[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        StringTokenizer st = new StringTokenizer(path); // divide by spaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        for (int i = 0; st.hasMoreTokens(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            urls[i] = new URL(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        synchronized (pathToURLsCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            pathToURLsCache.put(path,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   774
                                new Object[] {urls, new SoftReference<String>(path)});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /** map from weak(key=string) to [URL[], soft(key)] */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   780
    private static final Map<String, Object[]> pathToURLsCache
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
   781
        = new WeakHashMap<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Convert an array of URL objects into a corresponding string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * containing a space-separated list of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Note that if the array has zero elements, the return value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * null, not the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    private static String urlsToPath(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        } else if (urls.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return urls[0].toExternalForm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23333
diff changeset
   796
            StringBuilder path = new StringBuilder(urls[0].toExternalForm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            for (int i = 1; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                path.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                path.append(urls[i].toExternalForm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return path.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Return the class loader to be used as the parent for an RMI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * loader used in the current execution context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    private static ClassLoader getRMIContextClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * The current implementation simply uses the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * context class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Look up the RMI class loader for the given codebase URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * and the given parent class loader.  A new class loader instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * will be created and returned if no match is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    private static Loader lookupLoader(final URL[] urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                       final ClassLoader parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * If the requested codebase URL path is empty, the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * parent class loader will be sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * REMIND: To be conservative, this optimization is commented out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * for now so that it does not open a security hole in the future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         * by providing untrusted code with direct access to the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * loadClass() method of a class loader instance that it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * get a reference to.  (It's an unlikely optimization anyway.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         * if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         *     return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        LoaderEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        synchronized (LoaderHandler.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
             * Take this opportunity to remove from the table entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
             * whose weak references have been cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            while ((entry = (LoaderEntry) refQueue.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                if (!entry.removed) {   // ignore entries removed below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    loaderTable.remove(entry.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
             * Look up the codebase URL path and parent class loader pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
             * in the table of RMI class loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            LoaderKey key = new LoaderKey(urls, parent);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   859
            entry = loaderTable.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   861
            if (entry == null || (loader = entry.get()) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                 * If entry was in table but it's weak reference was cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                 * remove it from the table and mark it as explicitly cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                 * so that new matching entry that we put in the table will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                 * not be erroneously removed when this entry is processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                 * from the weak reference queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    loaderTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    entry.removed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                 * A matching loader was not found, so create a new class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                 * loader instance for the requested codebase URL path and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                 * parent class loader.  The instance is created within an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                 * access control context retricted to the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                 * necessary to load classes from its codebase URL path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                AccessControlContext acc = getLoaderAccessControlContext(urls);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   882
                loader = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   883
                    new java.security.PrivilegedAction<Loader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   884
                        public Loader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                            return new Loader(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                 * Finally, create an entry to hold the new loader with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                 * weak reference and store it in the table with the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                entry = new LoaderEntry(key, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                loaderTable.put(key, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * LoaderKey holds a codebase URL path and parent class loader pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * used to look up RMI class loader instances in its class loader cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    private static class LoaderKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        private URL[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        private int hashValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        public LoaderKey(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            this.urls = urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                hashValue = parent.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                hashValue ^= urls[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return hashValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (obj instanceof LoaderKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                LoaderKey other = (LoaderKey) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (parent != other.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                if (urls == other.urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                if (urls.length != other.urls.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    if (!urls[i].equals(other.urls[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * LoaderEntry contains a weak reference to an RMIClassLoader.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * weak reference is registered with the private static "refQueue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * queue.  The entry contains the codebase URL path and parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * loader key for the loader so that the mapping can be removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * the table efficiently when the weak reference is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   960
    private static class LoaderEntry extends WeakReference<Loader> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        public LoaderKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * set to true if the entry has been removed from the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * because it has been replaced, so it should not be attempted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * to be removed again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        public boolean removed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        public LoaderEntry(LoaderKey key, Loader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            super(loader, refQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Return the access control context that a loader for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * codebase URL path should execute with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    private static AccessControlContext getLoaderAccessControlContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        URL[] urls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         * The approach used here is taken from the similar method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * getAccessControlContext() in the sun.applet.AppletPanel class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        // begin with permissions granted to all code in current policy
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   989
        PermissionCollection perms =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   991
                new java.security.PrivilegedAction<PermissionCollection>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   992
                public PermissionCollection run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    CodeSource codesource = new CodeSource(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                        (java.security.cert.Certificate[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    Policy p = java.security.Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                        return p.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                        return new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // createClassLoader permission needed to create loader in context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        perms.add(new RuntimePermission("createClassLoader"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        // add permissions to read any "java.*" property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        perms.add(new java.util.PropertyPermission("java.*","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        // add permissions reuiqred to load from codebase URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        addPermissionsForURLs(urls, perms, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * Create an AccessControlContext that consists of a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * protection domain with only the permissions calculated above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        ProtectionDomain pd = new ProtectionDomain(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            new CodeSource((urls.length > 0 ? urls[0] : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                (java.security.cert.Certificate[]) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return new AccessControlContext(new ProtectionDomain[] { pd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * Adds to the specified permission collection the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * necessary to load classes from a loader with the specified URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * path; if "forLoader" is true, also adds URL-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * permissions necessary for the security context that such a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * loader operates within, such as permissions necessary for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * granting automatic permissions to classes defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * loader.  A given permission is only added to the collection if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * it is not already implied by the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
11039
4ee27839f531 7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents: 10913
diff changeset
  1034
    private static void addPermissionsForURLs(URL[] urls,
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
  1035
                                             PermissionCollection perms,
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
  1036
                                             boolean forLoader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            URL url = urls[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                URLConnection urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                Permission p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                         * If the codebase is a file, the permission required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                         * to actually read classes from the codebase URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                         * the permission to read all files beneath the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                         * directory in the file path, either because JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                         * files can refer to other JAR files in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                         * directory, or because permission to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                         * directory is not implied by permission to read the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                         * contents of a directory, which all that might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                         * granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                        String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                        int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                        if (endIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                            path = path.substring(0, endIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                            if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                            Permission p2 = new FilePermission(path, "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                            if (!perms.implies(p2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                perms.add(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                            perms.add(new FilePermission(path, "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                             * No directory separator: use permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                             * read the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                            if (!perms.implies(p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                                perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                        if (!perms.implies(p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                         * If the purpose of these permissions is to grant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                         * them to an instance of a URLClassLoader subclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                         * we must add permission to connect to and accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                         * from the host of non-"file:" URLs, otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                         * getPermissions() method of URLClassLoader will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                         * throw a security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                        if (forLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                            // get URL with meaningful host component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                            URL hostURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                            for (URLConnection conn = urlConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                 conn instanceof JarURLConnection;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                hostURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                    ((JarURLConnection) conn).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                conn = hostURL.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                            String host = hostURL.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                            if (host != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                                p.implies(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                                                               "resolve")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                Permission p2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                                    new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                                                         "connect,accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                if (!perms.implies(p2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                    perms.add(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                 * This shouldn't happen, although it is declared to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                 * thrown by openConnection() and getPermission().  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                 * does, don't bother granting or requiring any permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                 * for this URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * Loader is the actual class of the RMI class loaders created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * by the RMIClassLoader static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    private static class Loader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        /** parent class loader, kept here as an optimization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        /** string form of loader's codebase URL path, also an optimization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        private String annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        /** permissions required to access loader through public API */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        private Permissions permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        private Loader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            super(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
             * Precompute the permissions required to access the loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            permissions = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            addPermissionsForURLs(urls, permissions, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
             * Caching the value of class annotation string here assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
             * that the protected method addURL() is never called on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
             * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            annotation = urlsToPath(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         * Return the string to be annotated with all classes loaded from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
         * this class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        public String getClassAnnotation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            return annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * Check that the current access control context has all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         * permissions necessary to load classes from this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        private void checkPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            if (sm != null) {           // should never be null?
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
  1174
                Enumeration<Permission> enum_ = permissions.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                while (enum_.hasMoreElements()) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 11039
diff changeset
  1176
                    sm.checkPermission(enum_.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * Return the permissions to be granted to code loaded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         * given code source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        protected PermissionCollection getPermissions(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
             * Grant the same permissions that URLClassLoader would grant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         * Return a string representation of this loader (useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
         * debugging).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            return super.toString() + "[\"" + annotation + "\"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1200
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1201
        @Override
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1202
        protected Class<?> loadClass(String name, boolean resolve)
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1203
                throws ClassNotFoundException {
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1204
            if (parent == null) {
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1205
                ReflectUtil.checkPackageAccess(name);
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1206
            }
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1207
            return super.loadClass(name, resolve);
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1208
        }
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1209
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1210
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
18196
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1212
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1213
    private static Class<?> loadClassForName(String name,
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1214
                                              boolean initialize,
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1215
                                              ClassLoader loader)
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1216
            throws ClassNotFoundException
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1217
    {
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1218
        if (loader == null) {
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1219
            ReflectUtil.checkPackageAccess(name);
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1220
        }
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1221
        return Class.forName(name, initialize, loader);
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1222
    }
07dfacf3d73e 8001043: Clarify definition restrictions
dfuchs
parents: 14342
diff changeset
  1223
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
}