jdk/src/java.base/share/classes/java/lang/module/SystemModuleFinder.java
author alanb
Fri, 28 Oct 2016 10:18:07 +0100
changeset 41817 b90ad1de93ea
parent 41555 8a2a5a88376a
child 42338 a60f280f803c
permissions -rw-r--r--
8168789: ModuleReader.list and ModuleFinder.of update Reviewed-by: 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 java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.net.URLConnection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.ByteBuffer;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    35
import java.util.ArrayDeque;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Collections;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    37
import java.util.Deque;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashSet;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    40
import java.util.Iterator;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Set;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    45
import java.util.Spliterator;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    46
import java.util.function.Consumer;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.function.Supplier;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    48
import java.util.stream.Stream;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    49
import java.util.stream.StreamSupport;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.internal.jimage.ImageLocation;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.internal.jimage.ImageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.internal.jimage.ImageReaderFactory;
41555
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 40261
diff changeset
    54
import jdk.internal.misc.JavaNetUriAccess;
8a2a5a88376a 8168073: Speed up URI creation during module bootstrap
redestad
parents: 40261
diff changeset
    55
import jdk.internal.misc.SharedSecrets;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    56
import jdk.internal.module.ModuleHashes;
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
    57
import jdk.internal.module.ModuleHashes.HashSupplier;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.internal.module.SystemModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.internal.module.ModulePatcher;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * A {@code ModuleFinder} that finds modules that are linked into the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * The modules linked into the run-time image are assumed to have the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * ConcealedPackages attribute.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
class SystemModuleFinder implements ModuleFinder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    72
    private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
    73
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    private static final PerfCounter initTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.initTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    private static final PerfCounter moduleCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    private static final PerfCounter packageCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.packages");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    private static final PerfCounter exportsCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.exports");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    // ImageReader used to access all modules in the image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private static final ImageReader imageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    // the set of modules in the run-time image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    private static final Set<ModuleReference> modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    // maps module name to module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    private static final Map<String, ModuleReference> nameToModule;
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
     * For now, the module references are created eagerly on the assumption
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
     * that service binding will require all modules to be located.
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
        long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        imageReader = ImageReaderFactory.getImageReader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
    99
        String[] names = moduleNames();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   100
        ModuleDescriptor[] descriptors = descriptors(names);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   102
        int n = names.length;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        moduleCount.add(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        Set<ModuleReference> mods = new HashSet<>(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        Map<String, ModuleReference> map = new HashMap<>(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        for (int i = 0; i < n; i++) {
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   109
            ModuleDescriptor md = descriptors[i];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            // create the ModuleReference
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   112
            ModuleReference mref = toModuleReference(md, hashSupplier(i, names[i]));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            mods.add(mref);
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   115
            map.put(names[i], mref);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            // counters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            packageCount.add(md.packages().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            exportsCount.add(md.exports().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        modules = Collections.unmodifiableSet(mods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        nameToModule = map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        initTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   128
    /*
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   129
     * Returns an array of ModuleDescriptor of the given module names.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   130
     *
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   131
     * This obtains ModuleDescriptors from SystemModules class that is generated
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   132
     * from the jlink system-modules plugin.  ModuleDescriptors have already
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   133
     * been validated at link time.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   134
     *
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   135
     * If java.base is patched, or fastpath is disabled for troubleshooting
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   136
     * purpose, it will fall back to find system modules via jrt file system.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   137
     */
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   138
    private static ModuleDescriptor[] descriptors(String[] names) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   139
        // fastpath is enabled by default.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   140
        // It can be disabled for troubleshooting purpose.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   141
        boolean disabled =
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   142
            System.getProperty("jdk.system.module.finder.disabledFastPath") != null;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   143
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   144
        // fast loading of ModuleDescriptor of system modules
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   145
        if (isFastPathSupported() && !disabled)
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   146
            return SystemModules.modules();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   147
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   148
        // if fast loading of ModuleDescriptors is disabled
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   149
        // fallback to read module-info.class
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   150
        ModuleDescriptor[] descriptors = new ModuleDescriptor[names.length];
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   151
        for (int i = 0; i < names.length; i++) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   152
            String mn = names[i];
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   153
            ImageLocation loc = imageReader.findLocation(mn, "module-info.class");
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   154
            descriptors[i] = ModuleDescriptor.read(imageReader.getResourceBuffer(loc));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   155
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   156
            // add the recorded hashes of tied modules
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   157
            Hashes.add(descriptors[i]);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   158
        }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   159
        return descriptors;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   160
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   161
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   162
    private static boolean isFastPathSupported() {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   163
       return SystemModules.MODULE_NAMES.length > 0;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   164
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   165
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   166
    private static String[] moduleNames() {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   167
        if (isFastPathSupported())
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   168
            // module names recorded at link time
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   169
            return SystemModules.MODULE_NAMES;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   170
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   171
        // this happens when java.base is patched with java.base
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   172
        // from an exploded image
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   173
        return imageReader.getModuleNames();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   174
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   175
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   176
    private static ModuleReference toModuleReference(ModuleDescriptor md,
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   177
                                                     HashSupplier hash)
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   178
    {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   179
        String mn = md.name();
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   180
        URI uri = JNUA.create("jrt", "/".concat(mn));
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   181
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   182
        Supplier<ModuleReader> readerSupplier = new Supplier<>() {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   183
            @Override
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   184
            public ModuleReader get() {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   185
                return new ImageModuleReader(mn, uri);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   186
            }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   187
        };
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   188
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   189
        ModuleReference mref =
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   190
            new ModuleReference(md, uri, readerSupplier, hash);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   191
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38565
diff changeset
   192
        // may need a reference to a patched module if --patch-module specified
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   193
        mref = ModulePatcher.interposeIfNeeded(mref);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   194
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   195
        return mref;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   196
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   197
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   198
    private static HashSupplier hashSupplier(int index, String name) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   199
        if (isFastPathSupported()) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   200
            return new HashSupplier() {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   201
                @Override
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   202
                public String generate(String algorithm) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   203
                    return SystemModules.MODULES_TO_HASH[index];
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   204
                }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   205
            };
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   206
        } else {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   207
            return Hashes.hashFor(name);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   208
        }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   209
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   210
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   211
    /*
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   212
     * This helper class is only used when SystemModules is patched.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   213
     * It will get the recorded hashes from module-info.class.
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   214
     */
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   215
    private static class Hashes {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   216
        static Map<String, String> hashes = new HashMap<>();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   217
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   218
        static void add(ModuleDescriptor descriptor) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   219
            Optional<ModuleHashes> ohashes = descriptor.hashes();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   220
            if (ohashes.isPresent()) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   221
                hashes.putAll(ohashes.get().hashes());
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   222
            }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   223
        }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   224
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   225
        static HashSupplier hashFor(String name) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   226
            if (!hashes.containsKey(name))
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   227
                return null;
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   228
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   229
            return new HashSupplier() {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   230
                @Override
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   231
                public String generate(String algorithm) {
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   232
                    return hashes.get(name);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   233
                }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   234
            };
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37779
diff changeset
   235
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   236
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   237
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    SystemModuleFinder() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
    public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        return Optional.ofNullable(nameToModule.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
    public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     * A ModuleReader for reading resources from a module linked into the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     * run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
    static class ImageModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
        private final String module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        private volatile boolean closed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
         * If there is a security manager set then check permission to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
         * connect to the run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        private static void checkPermissionToConnect(URI uri) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                    URLConnection uc = uri.toURL().openConnection();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                    sm.checkPermission(uc.getPermission());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                    throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        ImageModuleReader(String module, URI uri) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            checkPermissionToConnect(uri);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            this.module = module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
         * Returns the ImageLocation for the given resource, {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
         * if not found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        private ImageLocation findImageLocation(String name) throws IOException {
38565
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 38431
diff changeset
   286
            Objects.requireNonNull(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            if (closed)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                throw new IOException("ModuleReader is closed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            if (imageReader != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                return imageReader.findLocation(module, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
                // not an images build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        public Optional<URI> find(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            ImageLocation location = findImageLocation(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            if (location != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                URI u = URI.create("jrt:/" + module + "/" + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                return Optional.of(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        public Optional<InputStream> open(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
            return read(name).map(this::toInputStream);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        private InputStream toInputStream(ByteBuffer bb) { // ## -> ByteBuffer?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
                int rem = bb.remaining();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
                byte[] bytes = new byte[rem];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                bb.get(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                return new ByteArrayInputStream(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        public Optional<ByteBuffer> read(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
            ImageLocation location = findImageLocation(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            if (location != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                return Optional.of(imageReader.getResourceBuffer(location));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        public void release(ByteBuffer bb) {
38565
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 38431
diff changeset
   336
            Objects.requireNonNull(bb);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            ImageReader.releaseByteBuffer(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   341
        public Stream<String> list() throws IOException {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   342
            if (closed)
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   343
                throw new IOException("ModuleReader is closed");
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   344
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   345
            Spliterator<String> s = new ModuleContentSpliterator(module);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   346
            return StreamSupport.stream(s, false);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   347
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   348
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   349
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        public void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            // nothing else to do
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
            closed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   356
    /**
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   357
     * A Spliterator for traversing the resources of a module linked into the
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   358
     * run-time image.
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   359
     */
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   360
    static class ModuleContentSpliterator implements Spliterator<String> {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   361
        final String moduleRoot;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   362
        final Deque<ImageReader.Node> stack;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   363
        Iterator<ImageReader.Node> iterator;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   364
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   365
        ModuleContentSpliterator(String module) throws IOException {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   366
            moduleRoot = "/modules/" + module;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   367
            stack = new ArrayDeque<>();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   368
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   369
            // push the root node to the stack to get started
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   370
            ImageReader.Node dir = imageReader.findNode(moduleRoot);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   371
            if (dir == null || !dir.isDirectory())
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   372
                throw new IOException(moduleRoot + " not a directory");
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   373
            stack.push(dir);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   374
            iterator = Collections.emptyIterator();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   375
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   376
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   377
        /**
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   378
         * Returns the name of the next non-directory node or {@code null} if
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   379
         * there are no remaining nodes to visit.
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   380
         */
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   381
        private String next() throws IOException {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   382
            for (;;) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   383
                while (iterator.hasNext()) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   384
                    ImageReader.Node node = iterator.next();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   385
                    String name = node.getName();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   386
                    if (node.isDirectory()) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   387
                        // build node
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   388
                        ImageReader.Node dir = imageReader.findNode(name);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   389
                        assert dir.isDirectory();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   390
                        stack.push(dir);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   391
                    } else {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   392
                        // strip /modules/$MODULE/ prefix
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   393
                        return name.substring(moduleRoot.length() + 1);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   394
                    }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   395
                }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   396
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   397
                if (stack.isEmpty()) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   398
                    return null;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   399
                } else {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   400
                    ImageReader.Node dir = stack.poll();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   401
                    assert dir.isDirectory();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   402
                    iterator = dir.getChildren().iterator();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   403
                }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   404
            }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   405
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   406
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   407
        @Override
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   408
        public boolean tryAdvance(Consumer<? super String> action) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   409
            String next;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   410
            try {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   411
                next = next();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   412
            } catch (IOException ioe) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   413
                throw new UncheckedIOException(ioe);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   414
            }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   415
            if (next != null) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   416
                action.accept(next);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   417
                return true;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   418
            } else {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   419
                return false;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   420
            }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   421
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   422
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   423
        @Override
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   424
        public Spliterator<String> trySplit() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   425
            return null;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   426
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   427
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   428
        @Override
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   429
        public int characteristics() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   430
            return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   431
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   432
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   433
        @Override
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   434
        public long estimateSize() {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   435
            return Long.MAX_VALUE;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   436
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41555
diff changeset
   437
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
}