jdk/src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
author mchung
Thu, 03 Nov 2016 18:08:28 -0700
changeset 41911 b3bb62588635
parent 41817 b90ad1de93ea
child 42338 a60f280f803c
permissions -rw-r--r--
6479237: (cl) Add support for classloader names Reviewed-by: alanb, bchristi, coleenp, dfuchs, lfoltan, psandoz, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package jdk.internal.loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.FilePermission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.net.MalformedURLException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.security.CodeSigner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.security.CodeSource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.security.PermissionCollection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.security.PrivilegedActionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.security.PrivilegedExceptionAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.security.SecureClassLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.Enumeration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import java.util.concurrent.ConcurrentHashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import java.util.jar.Attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import java.util.jar.Manifest;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
    56
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.internal.module.ModulePatcher.PatchedModuleReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.internal.misc.VM;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * The platform or application class loader. Resources loaded from modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * defined to the boot class loader are also loaded via an instance of this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * ClassLoader type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * <p> This ClassLoader supports loading of classes and resources from modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * method. Defining a module to this ClassLoader has the effect of making the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * types in the module visible. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * <p> This ClassLoader also supports loading of classes and resources from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * class path of URLs that are specified to the ClassLoader at construction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * time. The class path may expand at runtime (the Class-Path attribute in JAR
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * files or via instrumentation agents). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * <p> The delegation model used by this ClassLoader differs to the regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * delegation model. When requested to load a class then this ClassLoader first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 * maps the class name to its package name. If there is a module defined to a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * BuiltinClassLoader containing this package then the class loader delegates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 * directly to that class loader. If there isn't a module containing the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 * package then it delegates the search to the parent class loader and if not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 * found in the parent then it searches the class path. The main difference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * between this and the usual delegation model is that it allows the platform
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * class loader to delegate to the application class loader, important with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * upgraded modules defined to the platform class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
public class BuiltinClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    extends SecureClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        if (!ClassLoader.registerAsParallelCapable())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            throw new InternalError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    // parent ClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    private final BuiltinClassLoader parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    // the URL class path or null if there is no class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    private final URLClassPath ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
     * A module defined/loaded by a built-in class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     * A LoadedModule encapsulates a ModuleReference along with its CodeSource
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36674
diff changeset
   108
     * URL to avoid needing to create this URL when defining classes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    private static class LoadedModule {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        private final BuiltinClassLoader loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        private final ModuleReference mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        private final URL codeSourceURL;          // may be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            URL url = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            if (mref.location().isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                    url = mref.location().get().toURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                } catch (MalformedURLException e) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            this.loader = loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            this.mref = mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            this.codeSourceURL = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        BuiltinClassLoader loader() { return loader; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        ModuleReference mref() { return mref; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        String name() { return mref.descriptor().name(); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        URL codeSourceURL() { return codeSourceURL; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    // maps package name to loaded module for modules in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    private static final Map<String, LoadedModule> packageToModule
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        = new ConcurrentHashMap<>(1024);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    // maps a module name to a module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    private final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    // maps a module reference to a module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    private final Map<ModuleReference, ModuleReader> moduleToReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     * Create a new instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     */
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   148
    BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        // ensure getParent() returns null when the parent is the boot loader
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   150
        super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        this.ucp = ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        this.nameToModule = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        this.moduleToReader = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     * Register a module this this class loader. This has the effect of making
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     * the types in the module visible.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    public void loadModule(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        String mn = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        if (nameToModule.putIfAbsent(mn, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            throw new InternalError(mn + " already defined to this loader");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        LoadedModule loadedModule = new LoadedModule(this, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        for (String pn : mref.descriptor().packages()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            if (other != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
                throw new InternalError(pn + " in modules " + mn + " and "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                                        + other.mref().descriptor().name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     * Returns the {@code ModuleReference} for the named module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * this class loader; or {@code null} if not defined.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     * @param name The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    protected ModuleReference findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        return nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
    // -- finding resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
     * Returns a URL to a resource of the given name in a module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
     * this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
    public URL findResource(String mn, String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        URL url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
            url = AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                new PrivilegedExceptionAction<URL>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                    public URL run() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                        URI u = moduleReaderFor(mref).find(name).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                        if (u != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                                return u.toURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                            } catch (MalformedURLException e) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        } catch (PrivilegedActionException pae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
            throw (IOException) pae.getCause();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        // check access to the URL
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        return checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     * Returns an input stream to a resource of the given name in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     * defined to this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
    public InputStream findResourceAsStream(String mn, String name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
        // Need URL to resource when running with a security manager so that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        // the right permission check is done.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            URL url = findResource(mn, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            return (url != null) ? url.openStream() : null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                    new PrivilegedExceptionAction<InputStream>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                        public InputStream run() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                            return moduleReaderFor(mref).open(name).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                    });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
            } catch (PrivilegedActionException pae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                throw (IOException) pae.getCause();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     * Finds the resource with the given name on the class path of this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     * loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    public URL findResource(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
            PrivilegedAction<URL> pa = () -> ucp.findResource(name, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            URL url = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            return checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
     * Returns an enumeration of URL objects to all the resources with the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     * given name on the class path of this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
    public Enumeration<URL> findResources(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
            List<URL> result = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            PrivilegedAction<Enumeration<URL>> pa = () -> ucp.findResources(name, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            Enumeration<URL> e = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            while (e.hasMoreElements()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                URL url = checkURL(e.nextElement());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                if (url != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                    result.add(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
            return Collections.enumeration(result); // checked URLs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            return Collections.emptyEnumeration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
    // -- finding/loading classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
     * Finds the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    protected Class<?> findClass(String cn) throws ClassNotFoundException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        // no class loading until VM is fully initialized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
        if (!VM.isModuleSystemInited())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            if (loadedModule.loader() == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            // search class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
        // not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     * Finds the class with the specified binary name in a given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
     * This method returns {@code null} if the class cannot be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
    protected Class<?> findClass(String mn, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
        ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        if (loadedModule == null || !loadedModule.name().equals(mn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
            return null;   // module name does not match
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
        // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
        assert loadedModule.loader() == this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
        return findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
     * Loads the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
    protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
        throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
        Class<?> c = loadClassOrNull(cn, resolve);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
    protected Class<?> loadClassOrNull(String cn, boolean resolve) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
        synchronized (getClassLoadingLock(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            // check if already loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                    // package is in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                    BuiltinClassLoader loader = loadedModule.loader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                    if (loader == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                        if (VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                        // delegate to the other loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                        c = loader.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
                    // check parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                    if (parent != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                        c = parent.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                    // check class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                    if (c == null && ucp != null && VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                        c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
            if (resolve && c != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
            return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
    protected  Class<?> loadClassOrNull(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
        return loadClassOrNull(cn, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
     * Find the candidate loaded module for the given class name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
     * Returns {@code null} if none of the modules defined to this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
     * class loader contain the API package for the class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
    private LoadedModule findLoadedModule(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        if (pos < 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
            return null; // unnamed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        return packageToModule.get(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
     * Finds the class with the specified binary name if in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     * defined to this ClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
    private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        return AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
     * Finds the class with the specified binary name on the class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
    private Class<?> findClassOnClassPathOrNull(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
        return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
            new PrivilegedAction<Class<?>>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
                public Class<?> run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                    String path = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                    Resource res = ucp.getResource(path, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                    if (res != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                            return defineClass(cn, res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                    return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
     * bytes from the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
     * @return the resulting Class or {@code null} if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
    private Class<?> defineClass(String cn, LoadedModule loadedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
        ModuleReference mref = loadedModule.mref();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
        ModuleReader reader = moduleReaderFor(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
            ByteBuffer bb = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
            URL csURL = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            // locate class file, special handling for patched modules to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            // avoid locating the resource twice
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            String rn = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            if (reader instanceof PatchedModuleReader) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                Resource r = ((PatchedModuleReader)reader).findResource(rn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                if (r != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                    bb = r.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    csURL = r.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                bb = reader.read(rn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                csURL = loadedModule.codeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
            if (bb == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
                // class not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
            CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
                // define class to VM
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
                return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                reader.release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
     * bytes via the given Resource object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
     * @return the resulting Class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
     * @throws IOException if reading the resource fails
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
    private Class<?> defineClass(String cn, Resource res) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        URL url = res.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        // if class is in a named package then ensure that the package is defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        if (pos != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
            Manifest man = res.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            defineOrCheckPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
        // defines the class to the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
        ByteBuffer bb = res.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
        if (bb != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
            byte[] b = res.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
            return defineClass(cn, b, 0, b.length, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
    // -- packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
     * Defines a package in this ClassLoader. If the package is already defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
     * then its sealing needs to be checked if sealed by the legacy sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
     * mechanism.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
    protected Package defineOrCheckPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
        Package pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        if (pkg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
                if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
                    pkg = definePackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
                    pkg = definePackage(pn, null, null, null, null, null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
            } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
                // defined by another thread so need to re-verify
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
                pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
                if (pkg == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                    throw new InternalError("Cannot find package: " + pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
     * Get the Package with the specified package name. If defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
     * then verify that it against the manifest and code source.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
    private Package getAndVerifyPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
        Package pkg = getDefinedPackage(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
        if (pkg != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
            if (pkg.isSealed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                if (!pkg.isSealed(url)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                        "sealing violation: package " + pn + " is sealed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                // can't seal package if already defined without sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
                if ((man != null) && isSealed(pn, man)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
                        "sealing violation: can't seal package " + pn +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                        ": already defined");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
     * Defines a new package in this ClassLoader. The attributes in the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
     * Manifest are use to get the package version and sealing information.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
     * @throws IllegalArgumentException if the package name duplicates an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
     * existing package either in this class loader or one of its ancestors
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
    private Package definePackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
        String specTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        String specVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        String specVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
        String implTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
        String implVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        String implVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
        URL sealBase = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
        if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
            Attributes attr = man.getAttributes(pn.replace('.', '/').concat("/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
                sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
            attr = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
                if (specTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
                    specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
                if (specVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
                    specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
                if (specVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
                    specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
                if (implTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                    implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
                if (implVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
                    implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
                if (implVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                    implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
                if (sealed == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
                    sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
            // package is sealed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
            if ("true".equalsIgnoreCase(sealed))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
                sealBase = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
        return definePackage(pn,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
                             specTitle,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                             specVersion,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
                             specVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
                             implTitle,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                             implVersion,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                             implVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                             sealBase);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
     * Returns {@code true} if the specified package name is sealed according to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
     * the given manifest.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
    private boolean isSealed(String pn, Manifest man) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
        String path = pn.replace('.', '/').concat("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
        Attributes attr = man.getAttributes(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
        if (attr != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
        if (sealed == null && (attr = man.getMainAttributes()) != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
        return "true".equalsIgnoreCase(sealed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
    // -- permissions
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
     * Returns the permissions for the given CodeSource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
    protected PermissionCollection getPermissions(CodeSource cs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
        PermissionCollection perms = super.getPermissions(cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
        // add the permission to access the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
        URL url = cs.getLocation();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
        if (url == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
            return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
        Permission p = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
            p = url.openConnection().getPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
            if (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
                // for directories then need recursive access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                if (p instanceof FilePermission) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
                    String path = p.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
                    if (path.endsWith(File.separator)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                        path += "-";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
                        p = new FilePermission(path, "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                perms.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
        } catch (IOException ioe) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
        return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
    // -- miscellaneous supporting methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
     * Returns the ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
    private ModuleReader moduleReaderFor(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
        return moduleToReader.computeIfAbsent(mref, m -> createModuleReader(mref));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
     * Creates a ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
    private ModuleReader createModuleReader(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            return mref.open();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
            // Return a null module reader to avoid a future class load
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            // attempting to open the module again.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
            return new NullModuleReader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
     * A ModuleReader that doesn't read any resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
    private static class NullModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
        public Optional<URI> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
        @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   753
        public Stream<String> list() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   754
            return Stream.empty();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   755
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   756
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
            throw new InternalError("Should not get here");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
     * Checks access to the given URL. We use URLClassPath for consistent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
     * checking with java.net.URLClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
    private static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
        return URLClassPath.checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
}