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