jdk/src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
author redestad
Tue, 28 Feb 2017 12:24:29 +0100
changeset 44003 dc26a57d2570
parent 43712 5dfd0950317c
child 44359 c6761862ca0b
permissions -rw-r--r--
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services Reviewed-by: alanb, mchung
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;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    32
import java.lang.module.ModuleDescriptor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleReader;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    35
import java.lang.ref.SoftReference;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.net.MalformedURLException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.security.CodeSigner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.security.CodeSource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.security.PermissionCollection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.security.PrivilegedActionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.security.PrivilegedExceptionAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.security.SecureClassLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.Enumeration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import java.util.concurrent.ConcurrentHashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.jar.Attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import java.util.jar.Manifest;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
    58
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    60
import jdk.internal.misc.VM;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import jdk.internal.module.ModulePatcher.PatchedModuleReader;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    62
import jdk.internal.module.SystemModules;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * The platform or application class loader. Resources loaded from modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * defined to the boot class loader are also loaded via an instance of this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * ClassLoader type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * <p> This ClassLoader supports loading of classes and resources from modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * method. Defining a module to this ClassLoader has the effect of making the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * types in the module visible. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * <p> This ClassLoader also supports loading of classes and resources from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * class path of URLs that are specified to the ClassLoader at construction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * time. The class path may expand at runtime (the Class-Path attribute in JAR
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * files or via instrumentation agents). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * <p> The delegation model used by this ClassLoader differs to the regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 * delegation model. When requested to load a class then this ClassLoader first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 * 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
    83
 * BuiltinClassLoader containing this package then the class loader delegates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
 * directly to that class loader. If there isn't a module containing the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * package then it delegates the search to the parent class loader and if not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * found in the parent then it searches the class path. The main difference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 * between this and the usual delegation model is that it allows the platform
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 * class loader to delegate to the application class loader, important with
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
 * upgraded modules defined to the platform class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
public class BuiltinClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    extends SecureClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        if (!ClassLoader.registerAsParallelCapable())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    97
            throw new InternalError("Unable to register as parallel capable");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    // parent ClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    private final BuiltinClassLoader parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    // the URL class path or null if there is no class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    private final URLClassPath ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     * A module defined/loaded by a built-in class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     * A LoadedModule encapsulates a ModuleReference along with its CodeSource
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36674
diff changeset
   111
     * URL to avoid needing to create this URL when defining classes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    private static class LoadedModule {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        private final BuiltinClassLoader loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        private final ModuleReference mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        private final URL codeSourceURL;          // may be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            URL url = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            if (mref.location().isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    url = mref.location().get().toURL();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   123
                } catch (MalformedURLException | IllegalArgumentException e) { }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            this.loader = loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            this.mref = mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            this.codeSourceURL = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        BuiltinClassLoader loader() { return loader; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        ModuleReference mref() { return mref; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        String name() { return mref.descriptor().name(); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        URL codeSourceURL() { return codeSourceURL; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    // maps package name to loaded module for modules in the boot layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    private static final Map<String, LoadedModule> packageToModule
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   139
        = new ConcurrentHashMap<>(SystemModules.PACKAGES_IN_BOOT_LAYER);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    // maps a module name to a module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    private final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    // maps a module reference to a module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    private final Map<ModuleReference, ModuleReader> moduleToReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   147
    // cache of resource name -> list of URLs.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   148
    // used only for resources that are not in module packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   149
    private volatile SoftReference<Map<String, List<URL>>> resourceCache;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     * Create a new instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
     */
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   154
    BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        // ensure getParent() returns null when the parent is the boot loader
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41817
diff changeset
   156
        super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        this.ucp = ucp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        this.nameToModule = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        this.moduleToReader = new ConcurrentHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     * Register a module this this class loader. This has the effect of making
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
     * the types in the module visible.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
    public void loadModule(ModuleReference mref) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   170
        assert !VM.isModuleSystemInited();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   171
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        String mn = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        if (nameToModule.putIfAbsent(mn, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            throw new InternalError(mn + " already defined to this loader");
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
        LoadedModule loadedModule = new LoadedModule(this, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        for (String pn : mref.descriptor().packages()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            if (other != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                throw new InternalError(pn + " in modules " + mn + " and "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                                        + other.mref().descriptor().name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    }
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
     * Returns the {@code ModuleReference} for the named module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
     * this class loader; or {@code null} if not defined.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
     * @param name The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
    protected ModuleReference findModule(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        return nameToModule.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    // -- finding resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
     * Returns a URL to a resource of the given name in a module defined to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
     * this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
    public URL findResource(String mn, String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   206
        URL url = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   208
        if (mn != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   209
            // find in module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   210
            ModuleReference mref = nameToModule.get(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   211
            if (mref != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   212
                url = findResource(mref, name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   213
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   214
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   215
            // find on class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   216
            url = findResourceOnClassPath(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   219
        return checkURL(url);  // check access before returning
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * Returns an input stream to a resource of the given name in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     * defined to this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
    public InputStream findResourceAsStream(String mn, String name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        // Need URL to resource when running with a security manager so that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        // the right permission check is done.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   231
        if (System.getSecurityManager() != null || mn == null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
            URL url = findResource(mn, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            return (url != null) ? url.openStream() : null;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   234
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   236
        // find in module defined to this loader, no security manager
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   237
        ModuleReference mref = nameToModule.get(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   238
        if (mref != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   239
            return moduleReaderFor(mref).open(name).orElse(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   246
     * Finds a resource with the given name in the modules defined to this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   247
     * class loader or its class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   248
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   249
    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   250
    public URL findResource(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   251
        String pn = ResourceHelper.getPackageName(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   252
        LoadedModule module = packageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   253
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   254
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   255
            // resource is in a package of a module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   256
            if (module.loader() == this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   257
                && (name.endsWith(".class") || isOpen(module.mref(), pn))) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   258
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   259
                    return findResource(module.name(), name); // checks URL
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   260
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   261
                    return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   262
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   263
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   264
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   265
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   266
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   267
            // not in a module package but may be in module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   268
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   269
                List<URL> urls = findMiscResource(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   270
                if (!urls.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   271
                    URL url = urls.get(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   272
                    if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   273
                        return checkURL(url); // check access before returning
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   274
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   275
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   276
            } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   277
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   278
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   279
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   280
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   281
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   282
        // search class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   283
        URL url = findResourceOnClassPath(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   284
        return checkURL(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   285
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   286
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   287
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
     * Returns an enumeration of URL objects to all the resources with the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   289
     * given name in modules defined to this class loader or on the class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   290
     * path of this loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
    public Enumeration<URL> findResources(String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   294
        List<URL> checked = new ArrayList<>();  // list of checked URLs
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   295
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   296
        String pn = ResourceHelper.getPackageName(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   297
        LoadedModule module = packageToModule.get(pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   298
        if (module != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   299
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   300
            // resource is in a package of a module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   301
            if (module.loader() == this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   302
                && (name.endsWith(".class") || isOpen(module.mref(), pn))) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   303
                URL url = findResource(module.name(), name);  // checks URL
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                if (url != null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   305
                    checked.add(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   306
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   307
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   308
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   309
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   310
            // not in a package of a module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   311
            for (URL url : findMiscResource(name)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   312
                url = checkURL(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   313
                if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   314
                    checked.add(url);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   317
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   318
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   319
        // search class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   320
        Enumeration<URL> e = findResourcesOnClassPath(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   321
        while (e.hasMoreElements()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   322
            URL url = checkURL(e.nextElement());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   323
            if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   324
                checked.add(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   325
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   326
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   327
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   328
        return Collections.enumeration(checked);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   329
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   330
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   331
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   332
     * Returns the list of URLs to a "miscellaneous" resource in modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   333
     * defined to this loader. A miscellaneous resource is not in a module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   334
     * package, e.g. META-INF/services/p.S.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   335
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   336
     * The cache used by this method avoids repeated searching of all modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   337
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   338
    private List<URL> findMiscResource(String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   339
        SoftReference<Map<String, List<URL>>> ref = this.resourceCache;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   340
        Map<String, List<URL>> map = (ref != null) ? ref.get() : null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   341
        if (map != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   342
            List<URL> urls = map.get(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   343
            if (urls != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   344
                return urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   345
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   346
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   347
        // search all modules for the resource
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   348
        List<URL> urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   349
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   350
            urls = AccessController.doPrivileged(
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   351
                new PrivilegedExceptionAction<>() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   352
                    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   353
                    public List<URL> run() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   354
                        List<URL> result = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   355
                        for (ModuleReference mref : nameToModule.values()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   356
                            URI u = moduleReaderFor(mref).find(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   357
                            if (u != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   358
                                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   359
                                    result.add(u.toURL());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   360
                                } catch (MalformedURLException |
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   361
                                         IllegalArgumentException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   362
                                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   363
                            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   364
                        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   365
                        return result;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   366
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   367
                });
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   368
        } catch (PrivilegedActionException pae) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   369
            throw (IOException) pae.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   370
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   371
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   372
        // only cache resources after all modules have been defined
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   373
        if (VM.isModuleSystemInited()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   374
            if (map == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   375
                map = new ConcurrentHashMap<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   376
                this.resourceCache = new SoftReference<>(map);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   377
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   378
            if (urls.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   379
                urls = Collections.emptyList();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   380
            map.putIfAbsent(name, urls);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   381
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   382
        return urls;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   383
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   384
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   385
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   386
     * Returns the URL to a resource in a module or {@code null} if not found.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   387
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   388
    private URL findResource(ModuleReference mref, String name) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   389
        URI u;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   390
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   391
            u = moduleReaderFor(mref).find(name).orElse(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   393
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   394
                u = AccessController.doPrivileged(new PrivilegedExceptionAction<> () {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   395
                    @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   396
                    public URI run() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   397
                        return moduleReaderFor(mref).find(name).orElse(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   398
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   399
                });
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   400
            } catch (PrivilegedActionException pae) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   401
                throw (IOException) pae.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   402
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   403
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   404
        if (u != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   405
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   406
                return u.toURL();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   407
            } catch (MalformedURLException | IllegalArgumentException e) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   408
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   409
        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   410
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   411
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   412
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   413
     * Returns the URL to a resource in a module. Returns {@code null} if not found
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   414
     * or an I/O error occurs.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   415
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   416
    private URL findResourceOrNull(ModuleReference mref, String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   417
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   418
            return findResource(mref, name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   419
        } catch (IOException ignore) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   420
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   421
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   422
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   423
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   424
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   425
     * Returns a URL to a resource on the class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   426
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   427
    private URL findResourceOnClassPath(String name) {
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   428
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   429
            if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   430
                return ucp.findResource(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   431
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   432
                PrivilegedAction<URL> pa = () -> ucp.findResource(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   433
                return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   434
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   435
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   436
            // no class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   437
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   438
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   439
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   440
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   441
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   442
     * Returns the URLs of all resources of the given name on the class path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   443
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   444
    private Enumeration<URL> findResourcesOnClassPath(String name) {
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   445
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   446
            if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   447
                return ucp.findResources(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   448
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   449
                PrivilegedAction<Enumeration<URL>> pa;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   450
                pa = () -> ucp.findResources(name, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   451
                return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   452
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   453
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   454
            // no class path
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
            return Collections.emptyEnumeration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
    // -- finding/loading classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
     * Finds the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
    protected Class<?> findClass(String cn) throws ClassNotFoundException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
        // no class loading until VM is fully initialized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        if (!VM.isModuleSystemInited())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
        // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
        LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        Class<?> c = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
        if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
            // attempt to load class in module defined to this loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
            if (loadedModule.loader() == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
            // search class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   484
            if (hasClassPath()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
                c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
        // not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   498
     * Finds the class with the specified binary name in a module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   499
     * This method returns {@code null} if the class cannot be found
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   500
     * or not defined in the specified module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
    protected Class<?> findClass(String mn, String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   504
        if (mn != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   505
            // find the candidate module for this class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   506
            LoadedModule loadedModule = findLoadedModule(mn, cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   507
            if (loadedModule == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   508
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   509
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   511
            // attempt to load class in module defined to this loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   512
            assert loadedModule.loader() == this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   513
            return findClassInModuleOrNull(loadedModule, cn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   516
        // search class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   517
        if (hasClassPath()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   518
            return findClassOnClassPathOrNull(cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   519
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   520
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   521
        return null;
36511
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
     * Loads the class with the specified binary name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
    protected Class<?> loadClass(String cn, boolean resolve)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
        throws ClassNotFoundException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
        Class<?> c = loadClassOrNull(cn, resolve);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
        if (c == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            throw new ClassNotFoundException(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
    protected Class<?> loadClassOrNull(String cn, boolean resolve) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        synchronized (getClassLoadingLock(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            // check if already loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            Class<?> c = findLoadedClass(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                // find the candidate module for this class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                LoadedModule loadedModule = findLoadedModule(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
                if (loadedModule != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
                    // package is in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                    BuiltinClassLoader loader = loadedModule.loader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
                    if (loader == this) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
                        if (VM.isModuleSystemInited()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                            c = findClassInModuleOrNull(loadedModule, cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
                        // delegate to the other loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                        c = loader.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
                    // check parent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
                    if (parent != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
                        c = parent.loadClassOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
                    // check class path
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   572
                    if (c == null && hasClassPath() && VM.isModuleSystemInited()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
                        c = findClassOnClassPathOrNull(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
            if (resolve && c != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
                resolveClass(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
     * A variation of {@code loadCass} to load a class with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
     * binary name. This method returns {@code null} when the class is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
     * found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
    protected  Class<?> loadClassOrNull(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
        return loadClassOrNull(cn, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
     * Find the candidate loaded module for the given class name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
     * Returns {@code null} if none of the modules defined to this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
     * class loader contain the API package for the class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
    private LoadedModule findLoadedModule(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
        if (pos < 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
            return null; // unnamed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
        String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
        return packageToModule.get(pn);
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
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   610
     * Find the candidate loaded module for the given class name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   611
     * in the named module.  Returns {@code null} if the named module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   612
     * is not defined to this class loader or does not contain
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   613
     * the API package for the class.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   614
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   615
    private LoadedModule findLoadedModule(String mn, String cn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   616
        LoadedModule loadedModule = findLoadedModule(cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   617
        if (loadedModule != null && mn.equals(loadedModule.name())) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   618
            return loadedModule;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   619
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   620
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   621
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   622
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   623
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   624
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
     * Finds the class with the specified binary name if in a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
     * defined to this ClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
    private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   631
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   632
            return defineClass(cn, loadedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   633
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   634
            PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   635
            return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   636
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
     * Finds the class with the specified binary name on the class path.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
     * @return the resulting Class or {@code null} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
    private Class<?> findClassOnClassPathOrNull(String cn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   645
        String path = cn.replace('.', '/').concat(".class");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   646
        if (System.getSecurityManager() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   647
            Resource res = ucp.getResource(path, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   648
            if (res != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   649
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   650
                    return defineClass(cn, res);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   651
                } catch (IOException ioe) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   652
                    // TBD on how I/O errors should be propagated
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   653
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   654
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   655
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   656
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   657
            // avoid use of lambda here
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   658
            PrivilegedAction<Class<?>> pa = new PrivilegedAction<>() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
                public Class<?> run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
                    Resource res = ucp.getResource(path, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
                    if (res != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
                        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
                            return defineClass(cn, res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
                        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
                            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
                    return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   670
            };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   671
            return AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   672
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
     * bytes from the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
     * @return the resulting Class or {@code null} if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
    private Class<?> defineClass(String cn, LoadedModule loadedModule) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
        ModuleReference mref = loadedModule.mref();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
        ModuleReader reader = moduleReaderFor(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            ByteBuffer bb = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
            URL csURL = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
            // locate class file, special handling for patched modules to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
            // avoid locating the resource twice
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            String rn = cn.replace('.', '/').concat(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
            if (reader instanceof PatchedModuleReader) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
                Resource r = ((PatchedModuleReader)reader).findResource(rn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
                if (r != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
                    bb = r.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
                    csURL = r.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
                bb = reader.read(rn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
                csURL = loadedModule.codeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
            if (bb == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
                // class not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
            CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                // define class to VM
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
                return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                reader.release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
            // TBD on how I/O errors should be propagated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
     * Defines the given binary class name to the VM, loading the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
     * bytes via the given Resource object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
     * @return the resulting Class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
     * @throws IOException if reading the resource fails
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
    private Class<?> defineClass(String cn, Resource res) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
        URL url = res.getCodeSourceURL();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        // if class is in a named package then ensure that the package is defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
        int pos = cn.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
        if (pos != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            String pn = cn.substring(0, pos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
            Manifest man = res.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            defineOrCheckPackage(pn, man, url);
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
        // defines the class to the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
        ByteBuffer bb = res.getByteBuffer();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
        if (bb != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
            return defineClass(cn, bb, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
            byte[] b = res.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
            CodeSigner[] signers = res.getCodeSigners();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
            CodeSource cs = new CodeSource(url, signers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
            return defineClass(cn, b, 0, b.length, cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        }
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
    // -- packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
     * Defines a package in this ClassLoader. If the package is already defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
     * then its sealing needs to be checked if sealed by the legacy sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
     * mechanism.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
    protected Package defineOrCheckPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
        Package pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
        if (pkg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
                if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
                    pkg = definePackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
                    pkg = definePackage(pn, null, null, null, null, null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
            } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
                // defined by another thread so need to re-verify
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
                pkg = getAndVerifyPackage(pn, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
                if (pkg == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
                    throw new InternalError("Cannot find package: " + pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
     * Get the Package with the specified package name. If defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
     * then verify that it against the manifest and code source.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
     * @throws SecurityException if there is a sealing violation (JAR spec)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
    private Package getAndVerifyPackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   792
        Package pkg = getDefinedPackage(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
        if (pkg != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
            if (pkg.isSealed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                if (!pkg.isSealed(url)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
                        "sealing violation: package " + pn + " is sealed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   799
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
                // can't seal package if already defined without sealing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
                if ((man != null) && isSealed(pn, man)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
                    throw new SecurityException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
                        "sealing violation: can't seal package " + pn +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   804
                        ": already defined");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   805
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   807
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   808
        return pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   809
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   810
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
     * Defines a new package in this ClassLoader. The attributes in the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   813
     * Manifest are use to get the package version and sealing information.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   814
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   815
     * @throws IllegalArgumentException if the package name duplicates an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   816
     * existing package either in this class loader or one of its ancestors
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   817
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   818
    private Package definePackage(String pn, Manifest man, URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   819
        String specTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   820
        String specVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   821
        String specVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   822
        String implTitle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   823
        String implVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   824
        String implVendor = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   826
        URL sealBase = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   827
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   828
        if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   829
            Attributes attr = man.getAttributes(pn.replace('.', '/').concat("/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   830
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   831
                specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
                specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
                specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   834
                implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   835
                implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   836
                implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
                sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   838
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   839
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
            attr = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   841
            if (attr != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
                if (specTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   843
                    specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
                if (specVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   845
                    specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
                if (specVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
                    specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
                if (implTitle == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
                    implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
                if (implVersion == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
                    implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
                if (implVendor == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
                    implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
                if (sealed == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
                    sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
            // package is sealed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
            if ("true".equalsIgnoreCase(sealed))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
                sealBase = url;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
        return definePackage(pn,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   863
                specTitle,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   864
                specVersion,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   865
                specVendor,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   866
                implTitle,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   867
                implVersion,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   868
                implVendor,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   869
                sealBase);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   870
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   871
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
    /**
44003
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   873
     * Returns {@code true} if there is a class path associated with this
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   874
     * class loader.
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   875
     */
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   876
    boolean hasClassPath() {
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   877
        return ucp != null;
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   878
    }
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   879
dc26a57d2570 8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents: 43712
diff changeset
   880
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
     * Returns {@code true} if the specified package name is sealed according to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   882
     * the given manifest.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   883
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   884
    private boolean isSealed(String pn, Manifest man) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   885
        String path = pn.replace('.', '/').concat("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   886
        Attributes attr = man.getAttributes(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
        String sealed = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   888
        if (attr != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   889
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   890
        if (sealed == null && (attr = man.getMainAttributes()) != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   891
            sealed = attr.getValue(Attributes.Name.SEALED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   892
        return "true".equalsIgnoreCase(sealed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   893
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   894
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   895
    // -- permissions
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   896
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
     * Returns the permissions for the given CodeSource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   899
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   900
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   901
    protected PermissionCollection getPermissions(CodeSource cs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   902
        PermissionCollection perms = super.getPermissions(cs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   903
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   904
        // add the permission to access the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   905
        URL url = cs.getLocation();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   906
        if (url == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   907
            return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   908
        Permission p = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   909
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   910
            p = url.openConnection().getPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   911
            if (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   912
                // for directories then need recursive access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   913
                if (p instanceof FilePermission) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   914
                    String path = p.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   915
                    if (path.endsWith(File.separator)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   916
                        path += "-";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   917
                        p = new FilePermission(path, "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   918
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   919
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   920
                perms.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
        } catch (IOException ioe) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
        return perms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
    // -- miscellaneous supporting methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
     * Returns the ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   932
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   933
    private ModuleReader moduleReaderFor(ModuleReference mref) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   934
        return moduleToReader.computeIfAbsent(mref, BuiltinClassLoader::createModuleReader);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   935
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   936
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   937
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   938
     * Creates a ModuleReader for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   939
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   940
    private static ModuleReader createModuleReader(ModuleReference mref) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   941
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   942
            return mref.open();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   943
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   944
            // Return a null module reader to avoid a future class load
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   945
            // attempting to open the module again.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   946
            return new NullModuleReader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   947
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   948
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   949
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   950
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   951
     * A ModuleReader that doesn't read any resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   952
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   953
    private static class NullModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   954
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   955
        public Optional<URI> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   956
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   957
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   958
        @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   959
        public Stream<String> list() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   960
            return Stream.empty();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   961
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 37779
diff changeset
   962
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   963
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   964
            throw new InternalError("Should not get here");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   965
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   966
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   967
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   968
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   969
     * Returns true if the given module opens the given package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   970
     * unconditionally.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   971
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   972
     * @implNote This method currently iterates over each of the open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   973
     * packages. This will be replaced once the ModuleDescriptor.Opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   974
     * API is updated.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   975
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   976
    private boolean isOpen(ModuleReference mref, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   977
        ModuleDescriptor descriptor = mref.descriptor();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   978
        if (descriptor.isOpen())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   979
            return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   980
        for (ModuleDescriptor.Opens opens : descriptor.opens()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   981
            String source = opens.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   982
            if (!opens.isQualified() && source.equals(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   983
                return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   984
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   985
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   986
        return false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   987
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   988
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   989
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   990
     * Checks access to the given URL. We use URLClassPath for consistent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   991
     * checking with java.net.URLClassLoader.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   992
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   993
    private static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   994
        return URLClassPath.checkURL(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   995
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   996
}