jdk/src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 36674 7ab530dd6f10
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import jdk.internal.module.ModulePatcher.PatchedModuleReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.internal.misc.VM;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import sun.misc.URLClassPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import sun.misc.Resource;
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
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * The platform or application class loader. Resources loaded from modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * defined to the boot class loader are also loaded via an instance of this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * ClassLoader type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * <p> This ClassLoader supports loading of classes and resources from modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * method. Defining a module to this ClassLoader has the effect of making the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * types in the module visible. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * <p> This ClassLoader also supports loading of classes and resources from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * class path of URLs that are specified to the ClassLoader at construction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * time. The class path may expand at runtime (the Class-Path attribute in JAR
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * files or via instrumentation agents). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * <p> The delegation model used by this ClassLoader differs to the regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 * delegation model. When requested to load a class then this ClassLoader first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * 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
    81
 * BuiltinClassLoader containing this package then the class loader delegates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 * directly to that class loader. If there isn't a module containing the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 * package then it delegates the search to the parent class loader and if not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * found in the parent then it searches the class path. The main difference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * between this and the usual delegation model is that it allows the platform
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * class loader to delegate to the application class loader, important with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 * upgraded modules defined to the platform class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
public class BuiltinClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    extends SecureClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        if (!ClassLoader.registerAsParallelCapable())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            throw new InternalError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    // parent ClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    private final BuiltinClassLoader parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    // the URL class path or null if there is no class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    private final URLClassPath ucp;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     * A module defined/loaded by a built-in class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     * A LoadedModule encapsulates a ModuleReference along with its CodeSource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     * URL to avoid needing to create this URL when define classes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    private static class LoadedModule {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        private final BuiltinClassLoader loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        private final ModuleReference mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        private final URL codeSourceURL;          // may be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            URL url = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            if (mref.location().isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                    url = mref.location().get().toURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                } catch (MalformedURLException e) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            this.loader = loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            this.mref = mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            this.codeSourceURL = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        BuiltinClassLoader loader() { return loader; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        ModuleReference mref() { return mref; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        String name() { return mref.descriptor().name(); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        URL codeSourceURL() { return codeSourceURL; }
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    // maps package name to loaded module for modules in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
    private static final Map<String, LoadedModule> packageToModule
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        = new ConcurrentHashMap<>(1024);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    // maps a module name to a module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    private final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    // maps a module reference to a module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    private final Map<ModuleReference, ModuleReader> moduleToReader;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     * Create a new instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    BuiltinClassLoader(BuiltinClassLoader parent, URLClassPath ucp) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        // ensure getParent() returns null when the parent is the boot loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        super(parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        this.ucp = ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        this.nameToModule = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        this.moduleToReader = new ConcurrentHashMap<>();
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     * Register a module this this class loader. This has the effect of making
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
     * the types in the module visible.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    public void loadModule(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        String mn = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        if (nameToModule.putIfAbsent(mn, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            throw new InternalError(mn + " already defined to this loader");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        LoadedModule loadedModule = new LoadedModule(this, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        for (String pn : mref.descriptor().packages()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            if (other != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                throw new InternalError(pn + " in modules " + mn + " and "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                                        + other.mref().descriptor().name());
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * Returns the {@code ModuleReference} for the named module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     * this class loader; or {@code null} if not defined.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     * @param name The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
    protected ModuleReference findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        return nameToModule.get(name);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
    // -- finding resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
     * Returns a URL to a resource of the given name in a module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
     * this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    public URL findResource(String mn, String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        URL url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            url = AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                new PrivilegedExceptionAction<URL>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                    public URL run() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                        URI u = moduleReaderFor(mref).find(name).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                        if (u != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                                return u.toURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                            } catch (MalformedURLException e) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        } catch (PrivilegedActionException pae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
            throw (IOException) pae.getCause();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        // check access to the URL
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        return checkURL(url);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     * Returns an input stream to a resource of the given name in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     * defined to this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
    public InputStream findResourceAsStream(String mn, String name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        // Need URL to resource when running with a security manager so that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        // the right permission check is done.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            URL url = findResource(mn, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            return (url != null) ? url.openStream() : null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                    new PrivilegedExceptionAction<InputStream>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                        public InputStream run() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                            return moduleReaderFor(mref).open(name).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                    });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
            } catch (PrivilegedActionException pae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                throw (IOException) pae.getCause();
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     * Finds the resource with the given name on the class path of this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     * loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
    public URL findResource(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            PrivilegedAction<URL> pa = () -> ucp.findResource(name, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            URL url = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
            return checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            return null;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     * Returns an enumeration of URL objects to all the resources with the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     * given name on the class path of this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    public Enumeration<URL> findResources(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            List<URL> result = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            PrivilegedAction<Enumeration<URL>> pa = () -> ucp.findResources(name, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            Enumeration<URL> e = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            while (e.hasMoreElements()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                URL url = checkURL(e.nextElement());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                if (url != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                    result.add(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            return Collections.enumeration(result); // checked URLs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            return Collections.emptyEnumeration();
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
    // -- finding/loading classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
     * Finds the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
    protected Class<?> findClass(String cn) throws ClassNotFoundException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
        // no class loading until VM is fully initialized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        if (!VM.isModuleSystemInited())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            if (loadedModule.loader() == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            // search class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            if (ucp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                c = findClassOnClassPathOrNull(cn);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        // not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        return c;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
     * Finds the class with the specified binary name in a given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
     * This method returns {@code null} if the class cannot be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
    protected Class<?> findClass(String mn, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        ModuleReference mref = nameToModule.get(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        if (mref == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            return null;   // not defined to this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        if (loadedModule == null || !loadedModule.name().equals(mn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            return null;   // module name does not match
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
        // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
        assert loadedModule.loader() == this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        return findClassInModuleOrNull(loadedModule, cn);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
     * Loads the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
    protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
        throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
        Class<?> c = loadClassOrNull(cn, resolve);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
        return c;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
    protected Class<?> loadClassOrNull(String cn, boolean resolve) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
        synchronized (getClassLoadingLock(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
            // check if already loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                    // package is in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                    BuiltinClassLoader loader = loadedModule.loader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                    if (loader == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                        if (VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                        // delegate to the other loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                        c = loader.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                    // check parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                    if (parent != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                        c = parent.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                    // check class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                    if (c == null && ucp != null && VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                        c = findClassOnClassPathOrNull(cn);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            if (resolve && c != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
            return c;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
    protected  Class<?> loadClassOrNull(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        return loadClassOrNull(cn, false);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
     * Find the candidate loaded module for the given class name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
     * Returns {@code null} if none of the modules defined to this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
     * class loader contain the API package for the class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
    private LoadedModule findLoadedModule(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
        if (pos < 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
            return null; // unnamed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
        return packageToModule.get(pn);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     * Finds the class with the specified binary name if in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     * defined to this ClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
    private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        return AccessController.doPrivileged(pa);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
     * Finds the class with the specified binary name on the class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
    private Class<?> findClassOnClassPathOrNull(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
            new PrivilegedAction<Class<?>>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                public Class<?> run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                    String path = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                    Resource res = ucp.getResource(path, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                    if (res != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                            return defineClass(cn, res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                    return null;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
     * bytes from the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
     * @return the resulting Class or {@code null} if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    private Class<?> defineClass(String cn, LoadedModule loadedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
        ModuleReference mref = loadedModule.mref();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
        ModuleReader reader = moduleReaderFor(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
            ByteBuffer bb = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
            URL csURL = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            // locate class file, special handling for patched modules to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            // avoid locating the resource twice
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            String rn = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
            if (reader instanceof PatchedModuleReader) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                Resource r = ((PatchedModuleReader)reader).findResource(rn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                if (r != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    bb = r.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                    csURL = r.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                bb = reader.read(rn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
                csURL = loadedModule.codeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
            if (bb == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
                // class not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
                // define class to VM
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
                return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                reader.release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
            return null;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
     * bytes via the given Resource object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
     * @return the resulting Class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
     * @throws IOException if reading the resource fails
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
    private Class<?> defineClass(String cn, Resource res) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        URL url = res.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        // if class is in a named package then ensure that the package is defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        if (pos != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
            String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            Manifest man = res.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            defineOrCheckPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
        // defines the class to the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
        ByteBuffer bb = res.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        if (bb != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
            return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            byte[] b = res.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            return defineClass(cn, b, 0, b.length, cs);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
    // -- packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
     * Defines a package in this ClassLoader. If the package is already defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
     * then its sealing needs to be checked if sealed by the legacy sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
     * mechanism.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
    protected Package defineOrCheckPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        Package pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
        if (pkg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
                if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
                    pkg = definePackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
                    pkg = definePackage(pn, null, null, null, null, null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
            } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
                // defined by another thread so need to re-verify
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
                pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                if (pkg == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
                    throw new InternalError("Cannot find package: " + pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
        return pkg;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
     * Get the Package with the specified package name. If defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
     * then verify that it against the manifest and code source.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
    private Package getAndVerifyPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
        Package pkg = getDefinedPackage(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
        if (pkg != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            if (pkg.isSealed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
                if (!pkg.isSealed(url)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                        "sealing violation: package " + pn + " is sealed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
                // can't seal package if already defined without sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
                if ((man != null) && isSealed(pn, man)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                        "sealing violation: can't seal package " + pn +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                        ": already defined");
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
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        return pkg;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
     * Defines a new package in this ClassLoader. The attributes in the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
     * Manifest are use to get the package version and sealing information.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
     * @throws IllegalArgumentException if the package name duplicates an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
     * existing package either in this class loader or one of its ancestors
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
    private Package definePackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        String specTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        String specVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
        String specVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
        String implTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        String implVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
        String implVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
        URL sealBase = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
        if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            Attributes attr = man.getAttributes(pn.replace('.', '/').concat("/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
                implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
                sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
            attr = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
                if (specTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
                    specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
                if (specVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
                    specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
                if (specVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
                    specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                if (implTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
                    implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
                if (implVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
                    implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                if (implVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
                    implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
                if (sealed == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
                    sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
            // package is sealed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
            if ("true".equalsIgnoreCase(sealed))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
                sealBase = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
        return definePackage(pn,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                             specTitle,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
                             specVersion,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
                             specVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                             implTitle,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                             implVersion,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                             implVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
                             sealBase);
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
     * Returns {@code true} if the specified package name is sealed according to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
     * the given manifest.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
    private boolean isSealed(String pn, Manifest man) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
        String path = pn.replace('.', '/').concat("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
        Attributes attr = man.getAttributes(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
        if (attr != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
        if (sealed == null && (attr = man.getMainAttributes()) != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
        return "true".equalsIgnoreCase(sealed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
    // -- permissions
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
     * Returns the permissions for the given CodeSource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
    protected PermissionCollection getPermissions(CodeSource cs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
        PermissionCollection perms = super.getPermissions(cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
        // add the permission to access the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
        URL url = cs.getLocation();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
        if (url == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
            return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
        Permission p = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
            p = url.openConnection().getPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            if (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                // for directories then need recursive access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
                if (p instanceof FilePermission) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
                    String path = p.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                    if (path.endsWith(File.separator)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
                        path += "-";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                        p = new FilePermission(path, "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
                perms.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
        } catch (IOException ioe) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
        return perms;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
    // -- miscellaneous supporting methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
     * Returns the ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
    private ModuleReader moduleReaderFor(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
        return moduleToReader.computeIfAbsent(mref, m -> createModuleReader(mref));
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
     * Creates a ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
    private ModuleReader createModuleReader(ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            return mref.open();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            // Return a null module reader to avoid a future class load
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
            // attempting to open the module again.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
            return new NullModuleReader();
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
     * A ModuleReader that doesn't read any resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
    private static class NullModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
        public Optional<URI> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
            throw new InternalError("Should not get here");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
     * Checks access to the given URL. We use URLClassPath for consistent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
     * checking with java.net.URLClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
    private static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
        return URLClassPath.checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
}