jdk/src/java.base/share/classes/java/lang/module/SystemModuleFinder.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package 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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.function.Supplier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.internal.jimage.ImageLocation;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.internal.jimage.ImageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.jimage.ImageReaderFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.internal.module.SystemModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.internal.module.ModulePatcher;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 * A {@code ModuleFinder} that finds modules that are linked into the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * The modules linked into the run-time image are assumed to have the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * ConcealedPackages attribute.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
class SystemModuleFinder implements ModuleFinder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    private static final PerfCounter initTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.initTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private static final PerfCounter moduleCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    private static final PerfCounter packageCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.packages");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    private static final PerfCounter exportsCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        = PerfCounter.newPerfCounter("jdk.module.finder.jimage.exports");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    // ImageReader used to access all modules in the image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    private static final ImageReader imageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    // the set of modules in the run-time image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    private static final Set<ModuleReference> modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    // maps module name to module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    private static final Map<String, ModuleReference> nameToModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     * For now, the module references are created eagerly on the assumption
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     * that service binding will require all modules to be located.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        imageReader = ImageReaderFactory.getImageReader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        String[] moduleNames = SystemModules.MODULE_NAMES;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        ModuleDescriptor[] descriptors = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        boolean fastLoad = System.getProperty("jdk.installed.modules.disable") == null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        if (fastLoad) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            // fast loading of ModuleDescriptor of installed modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            descriptors = SystemModules.modules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        int n = moduleNames.length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        moduleCount.add(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Set<ModuleReference> mods = new HashSet<>(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Map<String, ModuleReference> map = new HashMap<>(n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        for (int i = 0; i < n; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            String mn = moduleNames[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            if (fastLoad) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                md = descriptors[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                // fallback to read module-info.class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                // if fast loading of ModuleDescriptors is disabled
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                ImageLocation location = imageReader.findLocation(mn, "module-info.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                md = ModuleDescriptor.read(imageReader.getResourceBuffer(location));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            if (!md.name().equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                throw new InternalError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            // create the ModuleReference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            URI uri = URI.create("jrt:/" + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            Supplier<ModuleReader> readerSupplier = new Supplier<>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                public ModuleReader get() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    return new ImageModuleReader(mn, uri);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            ModuleReference mref = new ModuleReference(md, uri, readerSupplier);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            // may need a reference to a patched module if -Xpatch specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            mref = ModulePatcher.interposeIfNeeded(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            mods.add(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            map.put(mn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            // counters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            packageCount.add(md.packages().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            exportsCount.add(md.exports().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        modules = Collections.unmodifiableSet(mods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        nameToModule = map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        initTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    SystemModuleFinder() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        return Optional.ofNullable(nameToModule.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     * A ModuleReader for reading resources from a module linked into the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     * run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    static class ImageModuleReader implements ModuleReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        private final String module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        private volatile boolean closed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
         * If there is a security manager set then check permission to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
         * connect to the run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        private static void checkPermissionToConnect(URI uri) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                    URLConnection uc = uri.toURL().openConnection();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                    sm.checkPermission(uc.getPermission());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                    throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        ImageModuleReader(String module, URI uri) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            checkPermissionToConnect(uri);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            this.module = module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
         * Returns the ImageLocation for the given resource, {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
         * if not found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        private ImageLocation findImageLocation(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            if (closed)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                throw new IOException("ModuleReader is closed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            if (imageReader != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                return imageReader.findLocation(module, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                // not an images build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        }
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 Optional<URI> find(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            ImageLocation location = findImageLocation(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            if (location != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                URI u = URI.create("jrt:/" + module + "/" + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                return Optional.of(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        public Optional<InputStream> open(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            return read(name).map(this::toInputStream);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        private InputStream toInputStream(ByteBuffer bb) { // ## -> ByteBuffer?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                int rem = bb.remaining();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                byte[] bytes = new byte[rem];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                bb.get(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                return new ByteArrayInputStream(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                release(bb);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        public Optional<ByteBuffer> read(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            ImageLocation location = findImageLocation(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            if (location != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                return Optional.of(imageReader.getResourceBuffer(location));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        public void release(ByteBuffer bb) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            ImageReader.releaseByteBuffer(bb);
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 void close() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            // nothing else to do
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            closed = true;
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
}