src/java.base/share/classes/jdk/internal/jimage/ImageReader.java
author erikj
Wed, 07 Mar 2018 13:26:15 -0800
changeset 49357 aaedb8343784
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198243: Add build time check for global operator new/delete in object files Reviewed-by: tbell, kbarrett, dholmes, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
package jdk.internal.jimage;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
import java.io.IOException;
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    28
import java.io.InputStream;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    29
import java.io.UncheckedIOException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    30
import java.nio.ByteBuffer;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
import java.nio.ByteOrder;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    32
import java.nio.IntBuffer;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    33
import java.nio.file.Files;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    34
import java.nio.file.attribute.BasicFileAttributes;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    35
import java.nio.file.attribute.FileTime;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    36
import java.nio.file.Path;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    37
import java.util.ArrayList;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    38
import java.util.Collections;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    39
import java.util.HashMap;
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    40
import java.util.HashSet;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    41
import java.util.List;
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    42
import java.util.Map;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    43
import java.util.Objects;
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    44
import java.util.Set;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    45
import java.util.function.Consumer;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    47
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    48
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    49
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    50
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    51
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    52
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    53
 */
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    54
public final class ImageReader implements AutoCloseable {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    55
    private final SharedImageReader reader;
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    56
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    57
    private volatile boolean closed;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    59
    private ImageReader(SharedImageReader reader) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    60
        this.reader = reader;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    61
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    62
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    63
    public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
    64
        Objects.requireNonNull(imagePath);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
    65
        Objects.requireNonNull(byteOrder);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
    66
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    67
        return SharedImageReader.open(imagePath, byteOrder);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    68
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    69
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    70
    public static ImageReader open(Path imagePath) throws IOException {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
        return open(imagePath, ByteOrder.nativeOrder());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    73
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    74
    @Override
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    75
    public void close() throws IOException {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    76
        if (closed) {
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    77
            throw new IOException("image file already closed");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    78
        }
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    79
        reader.close(this);
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    80
        closed = true;
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    81
    }
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    82
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    83
    private void ensureOpen() throws IOException {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    84
        if (closed) {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    85
            throw new IOException("image file closed");
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    86
        }
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    87
    }
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    88
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    89
    private void requireOpen() {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    90
        if (closed) {
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    91
            throw new IllegalStateException("image file closed");
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    92
        }
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    93
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    94
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    95
    // directory management interface
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    96
    public Directory getRootDirectory() throws IOException {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
    97
        ensureOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    98
        return reader.getRootDirectory();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
    99
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   100
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   101
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   102
    public Node findNode(String name) throws IOException {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   103
        ensureOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   104
        return reader.findNode(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   105
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   106
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   107
    public byte[] getResource(Node node) throws IOException {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   108
        ensureOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   109
        return reader.getResource(node);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   110
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   111
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   112
    public byte[] getResource(Resource rs) throws IOException {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   113
        ensureOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   114
        return reader.getResource(rs);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   115
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   116
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   117
    public ImageHeader getHeader() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   118
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   119
        return reader.getHeader();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   120
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   121
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   122
    public static void releaseByteBuffer(ByteBuffer buffer) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   123
        BasicImageReader.releaseByteBuffer(buffer);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   124
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   125
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   126
    public String getName() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   127
        requireOpen();
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   128
        return reader.getName();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   129
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   130
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   131
    public ByteOrder getByteOrder() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   132
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   133
        return reader.getByteOrder();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   134
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   135
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   136
    public Path getImagePath() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   137
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   138
        return reader.getImagePath();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   139
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   140
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   141
    public ImageStringsReader getStrings() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   142
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   143
        return reader.getStrings();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   144
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   145
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   146
    public ImageLocation findLocation(String mn, String rn) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   147
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   148
        return reader.findLocation(mn, rn);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   149
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   150
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   151
    public ImageLocation findLocation(String name) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   152
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   153
        return reader.findLocation(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   154
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   155
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   156
    public String[] getEntryNames() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   157
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   158
        return reader.getEntryNames();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   159
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   160
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   161
    public String[] getModuleNames() {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   162
        requireOpen();
38431
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   163
        int off = "/modules/".length();
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   164
        return reader.findNode("/modules")
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   165
                     .getChildren()
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   166
                     .stream()
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   167
                     .map(Node::getNameString)
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   168
                     .map(s -> s.substring(off, s.length()))
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   169
                     .toArray(String[]::new);
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   170
    }
ed73e79a0eb4 8157068: ExceptionInInitializerError if images build patched to use exploded version of jdk.internal.module.SystemModules
mchung
parents: 37614
diff changeset
   171
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   172
    public long[] getAttributes(int offset) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   173
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   174
        return reader.getAttributes(offset);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   175
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   176
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   177
    public String getString(int offset) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   178
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   179
        return reader.getString(offset);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   180
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   181
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   182
    public byte[] getResource(String name) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   183
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   184
        return reader.getResource(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   185
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   186
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   187
    public byte[] getResource(ImageLocation loc) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   188
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   189
        return reader.getResource(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   190
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   191
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   192
    public ByteBuffer getResourceBuffer(ImageLocation loc) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   193
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   194
        return reader.getResourceBuffer(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   195
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   196
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   197
    public InputStream getResourceStream(ImageLocation loc) {
43804
96fd6bf9c69b 8175010: ImageReader is not thread-safe
redestad
parents: 38582
diff changeset
   198
        requireOpen();
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   199
        return reader.getResourceStream(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   200
    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   201
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   202
    private final static class SharedImageReader extends BasicImageReader {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   203
        static final int SIZE_OF_OFFSET = Integer.BYTES;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   204
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   205
        static final Map<Path, SharedImageReader> OPEN_FILES = new HashMap<>();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   206
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   207
        // List of openers for this shared image.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   208
        final Set<ImageReader> openers;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   209
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   210
        // attributes of the .jimage file. jimage file does not contain
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   211
        // attributes for the individual resources (yet). We use attributes
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   212
        // of the jimage file itself (creation, modification, access times).
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   213
        // Iniitalized lazily, see {@link #imageFileAttributes()}.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   214
        BasicFileAttributes imageFileAttributes;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   215
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   216
        // directory management implementation
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   217
        final HashMap<String, Node> nodes;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   218
        volatile Directory rootDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   219
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   220
        Directory packagesDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   221
        Directory modulesDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   222
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   223
        private SharedImageReader(Path imagePath, ByteOrder byteOrder) throws IOException {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   224
            super(imagePath, byteOrder);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   225
            this.openers = new HashSet<>();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   226
            this.nodes = new HashMap<>();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   227
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   228
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   229
        public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
   230
            Objects.requireNonNull(imagePath);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
   231
            Objects.requireNonNull(byteOrder);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
   232
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   233
            synchronized (OPEN_FILES) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   234
                SharedImageReader reader = OPEN_FILES.get(imagePath);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   235
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   236
                if (reader == null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   237
                    // Will fail with an IOException if wrong byteOrder.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   238
                    reader =  new SharedImageReader(imagePath, byteOrder);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   239
                    OPEN_FILES.put(imagePath, reader);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   240
                } else if (reader.getByteOrder() != byteOrder) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   241
                    throw new IOException("\"" + reader.getName() + "\" is not an image file");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   242
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   243
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   244
                ImageReader image = new ImageReader(reader);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   245
                reader.openers.add(image);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   246
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   247
                return image;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   248
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   249
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   250
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   251
        public void close(ImageReader image) throws IOException {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
   252
            Objects.requireNonNull(image);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 38431
diff changeset
   253
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   254
            synchronized (OPEN_FILES) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   255
                if (!openers.remove(image)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   256
                    throw new IOException("image file already closed");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   257
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   258
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   259
                if (openers.isEmpty()) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   260
                    close();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   261
                    nodes.clear();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   262
                    rootDir = null;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   263
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   264
                    if (!OPEN_FILES.remove(this.getImagePath(), this)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   265
                        throw new IOException("image file not found in open list");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   266
                    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   267
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   268
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   269
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   270
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   271
        void addOpener(ImageReader reader) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   272
            synchronized (OPEN_FILES) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   273
                openers.add(reader);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   274
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   275
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   276
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   277
        boolean removeOpener(ImageReader reader) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   278
            synchronized (OPEN_FILES) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   279
                return openers.remove(reader);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   280
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   281
        }
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   282
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   283
        // directory management interface
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   284
        Directory getRootDirectory() {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   285
            return buildRootDirectory();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   286
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   287
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   288
        /**
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   289
         * Lazily build a node from a name.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   290
        */
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   291
        synchronized Node buildNode(String name) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   292
            Node n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   293
            boolean isPackages = name.startsWith("/packages");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   294
            boolean isModules = !isPackages && name.startsWith("/modules");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   295
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   296
            if (!(isModules || isPackages)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   297
                return null;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   298
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   299
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   300
            ImageLocation loc = findLocation(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   301
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   302
            if (loc != null) { // A sub tree node
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   303
                if (isPackages) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   304
                    n = handlePackages(name, loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   305
                } else { // modules sub tree
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   306
                    n = handleModulesSubTree(name, loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   307
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   308
            } else { // Asking for a resource? /modules/java.base/java/lang/Object.class
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   309
                if (isModules) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   310
                    n = handleResource(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   311
                } else {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   312
                    // Possibly ask for /packages/java.lang/java.base
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   313
                    // although /packages/java.base not created
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   314
                    n = handleModuleLink(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   315
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   316
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   317
            return n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   318
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   319
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   320
        synchronized Directory buildRootDirectory() {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   321
            Directory root = rootDir; // volatile read
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   322
            if (root != null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   323
                return root;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   324
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   325
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   326
            root = newDirectory(null, "/");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   327
            root.setIsRootDir();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   328
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   329
            // /packages dir
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   330
            packagesDir = newDirectory(root, "/packages");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   331
            packagesDir.setIsPackagesDir();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   332
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   333
            // /modules dir
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   334
            modulesDir = newDirectory(root, "/modules");
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   335
            modulesDir.setIsModulesDir();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   336
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   337
            root.setCompleted(true);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   338
            return rootDir = root;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   339
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   340
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   341
        /**
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   342
         * To visit sub tree resources.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   343
         */
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   344
        interface LocationVisitor {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   345
            void visit(ImageLocation loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   346
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   347
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   348
        void visitLocation(ImageLocation loc, LocationVisitor visitor) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   349
            byte[] offsets = getResource(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   350
            ByteBuffer buffer = ByteBuffer.wrap(offsets);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   351
            buffer.order(getByteOrder());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   352
            IntBuffer intBuffer = buffer.asIntBuffer();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   353
            for (int i = 0; i < offsets.length / SIZE_OF_OFFSET; i++) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   354
                int offset = intBuffer.get(i);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   355
                ImageLocation pkgLoc = getLocation(offset);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   356
                visitor.visit(pkgLoc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   357
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   358
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   359
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   360
        void visitPackageLocation(ImageLocation loc) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   361
            // Retrieve package name
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   362
            String pkgName = getBaseExt(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   363
            // Content is array of offsets in Strings table
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   364
            byte[] stringsOffsets = getResource(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   365
            ByteBuffer buffer = ByteBuffer.wrap(stringsOffsets);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   366
            buffer.order(getByteOrder());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   367
            IntBuffer intBuffer = buffer.asIntBuffer();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   368
            // For each module, create a link node.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   369
            for (int i = 0; i < stringsOffsets.length / SIZE_OF_OFFSET; i++) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   370
                // skip empty state, useless.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   371
                intBuffer.get(i);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   372
                i++;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   373
                int offset = intBuffer.get(i);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   374
                String moduleName = getString(offset);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   375
                Node targetNode = findNode("/modules/" + moduleName);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   376
                if (targetNode != null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   377
                    String pkgDirName = packagesDir.getName() + "/" + pkgName;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   378
                    Directory pkgDir = (Directory) nodes.get(pkgDirName);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   379
                    newLinkNode(pkgDir, pkgDir.getName() + "/" + moduleName, targetNode);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   380
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   381
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   382
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   383
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   384
        Node handlePackages(String name, ImageLocation loc) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   385
            long size = loc.getUncompressedSize();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   386
            Node n = null;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   387
            // Only possiblities are /packages, /packages/package/module
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   388
            if (name.equals("/packages")) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   389
                visitLocation(loc, (childloc) -> {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   390
                    findNode(childloc.getFullName());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   391
                });
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   392
                packagesDir.setCompleted(true);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   393
                n = packagesDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   394
            } else {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   395
                if (size != 0) { // children are offsets to module in StringsTable
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   396
                    String pkgName = getBaseExt(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   397
                    Directory pkgDir = newDirectory(packagesDir, packagesDir.getName() + "/" + pkgName);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   398
                    visitPackageLocation(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   399
                    pkgDir.setCompleted(true);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   400
                    n = pkgDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   401
                } else { // Link to module
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   402
                    String pkgName = loc.getParent();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   403
                    String modName = getBaseExt(loc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   404
                    Node targetNode = findNode("/modules/" + modName);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   405
                    if (targetNode != null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   406
                        String pkgDirName = packagesDir.getName() + "/" + pkgName;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   407
                        Directory pkgDir = (Directory) nodes.get(pkgDirName);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   408
                        Node linkNode = newLinkNode(pkgDir, pkgDir.getName() + "/" + modName, targetNode);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   409
                        n = linkNode;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   410
                    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   411
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   412
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   413
            return n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   414
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   415
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   416
        // Asking for /packages/package/module although
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   417
        // /packages/<pkg>/ not yet created, need to create it
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   418
        // prior to return the link to module node.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   419
        Node handleModuleLink(String name) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   420
            // eg: unresolved /packages/package/module
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   421
            // Build /packages/package node
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   422
            Node ret = null;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   423
            String radical = "/packages/";
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   424
            String path = name;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   425
            if (path.startsWith(radical)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   426
                int start = radical.length();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   427
                int pkgEnd = path.indexOf('/', start);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   428
                if (pkgEnd != -1) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   429
                    String pkg = path.substring(start, pkgEnd);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   430
                    String pkgPath = radical + pkg;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   431
                    Node n = findNode(pkgPath);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   432
                    // If not found means that this is a symbolic link such as:
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   433
                    // /packages/java.util/java.base/java/util/Vector.class
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   434
                    // and will be done by a retry of the filesystem
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   435
                    for (Node child : n.getChildren()) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   436
                        if (child.name.equals(name)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   437
                            ret = child;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   438
                            break;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   439
                        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   440
                    }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   441
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   442
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   443
            return ret;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   444
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   445
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   446
        Node handleModulesSubTree(String name, ImageLocation loc) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   447
            Node n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   448
            assert (name.equals(loc.getFullName()));
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   449
            Directory dir = makeDirectories(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   450
            visitLocation(loc, (childloc) -> {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   451
                String path = childloc.getFullName();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   452
                if (path.startsWith("/modules")) { // a package
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   453
                    makeDirectories(path);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   454
                } else { // a resource
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   455
                    makeDirectories(childloc.buildName(true, true, false));
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   456
                    newResource(dir, childloc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   457
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   458
            });
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   459
            dir.setCompleted(true);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   460
            n = dir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   461
            return n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   462
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   463
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   464
        Node handleResource(String name) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   465
            Node n = null;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   466
            String locationPath = name.substring("/modules".length());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   467
            ImageLocation resourceLoc = findLocation(locationPath);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   468
            if (resourceLoc != null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   469
                Directory dir = makeDirectories(resourceLoc.buildName(true, true, false));
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   470
                Resource res = newResource(dir, resourceLoc);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   471
                n = res;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   472
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   473
            return n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   474
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   475
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   476
        String getBaseExt(ImageLocation loc) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   477
            String base = loc.getBase();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   478
            String ext = loc.getExtension();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   479
            if (ext != null && !ext.isEmpty()) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   480
                base = base + "." + ext;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   481
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   482
            return base;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   483
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   484
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   485
        synchronized Node findNode(String name) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   486
            buildRootDirectory();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   487
            Node n = nodes.get(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   488
            if (n == null || !n.isCompleted()) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   489
                n = buildNode(name);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   490
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   491
            return n;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   492
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   493
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   494
        /**
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   495
         * Returns the file attributes of the image file.
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   496
         */
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   497
        BasicFileAttributes imageFileAttributes() {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   498
            BasicFileAttributes attrs = imageFileAttributes;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   499
            if (attrs == null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   500
                try {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   501
                    Path file = getImagePath();
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   502
                    attrs = Files.readAttributes(file, BasicFileAttributes.class);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   503
                } catch (IOException ioe) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   504
                    throw new UncheckedIOException(ioe);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   505
                }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   506
                imageFileAttributes = attrs;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   507
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   508
            return attrs;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   509
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   510
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   511
        Directory newDirectory(Directory parent, String name) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   512
            Directory dir = Directory.create(parent, name, imageFileAttributes());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   513
            nodes.put(dir.getName(), dir);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   514
            return dir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   515
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   516
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   517
        Resource newResource(Directory parent, ImageLocation loc) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   518
            Resource res = Resource.create(parent, loc, imageFileAttributes());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   519
            nodes.put(res.getName(), res);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   520
            return res;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   521
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   522
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   523
        LinkNode newLinkNode(Directory dir, String name, Node link) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   524
            LinkNode linkNode = LinkNode.create(dir, name, link);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   525
            nodes.put(linkNode.getName(), linkNode);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   526
            return linkNode;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   527
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   528
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   529
        Directory makeDirectories(String parent) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   530
            Directory last = rootDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   531
            for (int offset = parent.indexOf('/', 1);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   532
                    offset != -1;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   533
                    offset = parent.indexOf('/', offset + 1)) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   534
                String dir = parent.substring(0, offset);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   535
                last = makeDirectory(dir, last);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   536
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   537
            return makeDirectory(parent, last);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   538
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   539
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   540
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   541
        Directory makeDirectory(String dir, Directory last) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   542
            Directory nextDir = (Directory) nodes.get(dir);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   543
            if (nextDir == null) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   544
                nextDir = newDirectory(last, dir);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   545
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   546
            return nextDir;
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   547
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   548
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   549
        byte[] getResource(Node node) throws IOException {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   550
            if (node.isResource()) {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   551
                return super.getResource(node.getLocation());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   552
            }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   553
            throw new IOException("Not a resource: " + node);
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   554
        }
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   555
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   556
        byte[] getResource(Resource rs) throws IOException {
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   557
            return super.getResource(rs.getLocation());
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   558
        }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   559
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   560
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   561
    // jimage file does not store directory structure. We build nodes
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   562
    // using the "path" strings found in the jimage file.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   563
    // Node can be a directory or a resource
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31673
diff changeset
   564
    public abstract static class Node {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   565
        private static final int ROOT_DIR = 0b0000_0000_0000_0001;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   566
        private static final int PACKAGES_DIR = 0b0000_0000_0000_0010;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   567
        private static final int MODULES_DIR = 0b0000_0000_0000_0100;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   568
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   569
        private int flags;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   570
        private final String name;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   571
        private final BasicFileAttributes fileAttrs;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   572
        private boolean completed;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   573
37365
9cc4eb4d7491 8147460: Clean-up jrtfs implementation
sherman
parents: 36511
diff changeset
   574
        protected Node(String name, BasicFileAttributes fileAttrs) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   575
            this.name = Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   576
            this.fileAttrs = Objects.requireNonNull(fileAttrs);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   577
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   578
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   579
        /**
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   580
         * A node is completed when all its direct children have been built.
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   581
         *
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   582
         * @return
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   583
         */
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   584
        public boolean isCompleted() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   585
            return completed;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   586
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   587
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   588
        public void setCompleted(boolean completed) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   589
            this.completed = completed;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   590
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   591
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   592
        public final void setIsRootDir() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   593
            flags |= ROOT_DIR;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   594
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   595
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   596
        public final boolean isRootDir() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   597
            return (flags & ROOT_DIR) != 0;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   598
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   599
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   600
        public final void setIsPackagesDir() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   601
            flags |= PACKAGES_DIR;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   602
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   603
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   604
        public final boolean isPackagesDir() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   605
            return (flags & PACKAGES_DIR) != 0;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   606
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   607
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   608
        public final void setIsModulesDir() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   609
            flags |= MODULES_DIR;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   610
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   611
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   612
        public final boolean isModulesDir() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   613
            return (flags & MODULES_DIR) != 0;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   614
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   615
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   616
        public final String getName() {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   617
            return name;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   618
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   619
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   620
        public final BasicFileAttributes getFileAttributes() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   621
            return fileAttrs;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   622
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   623
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   624
        // resolve this Node (if this is a soft link, get underlying Node)
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   625
        public final Node resolveLink() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   626
            return resolveLink(false);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   627
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   628
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   629
        public Node resolveLink(boolean recursive) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   630
            return this;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   631
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   632
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   633
        // is this a soft link Node?
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   634
        public boolean isLink() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   635
            return false;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   636
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   637
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   638
        public boolean isDirectory() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   639
            return false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   640
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   641
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   642
        public List<Node> getChildren() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   643
            throw new IllegalArgumentException("not a directory: " + getNameString());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   644
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   645
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   646
        public boolean isResource() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   647
            return false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   648
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   649
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   650
        public ImageLocation getLocation() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   651
            throw new IllegalArgumentException("not a resource: " + getNameString());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   652
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   653
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   654
        public long size() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   655
            return 0L;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   656
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   657
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   658
        public long compressedSize() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   659
            return 0L;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   660
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   661
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   662
        public String extension() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   663
            return null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   664
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   665
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   666
        public long contentOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   667
            return 0L;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   668
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   669
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   670
        public final FileTime creationTime() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   671
            return fileAttrs.creationTime();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   672
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   673
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   674
        public final FileTime lastAccessTime() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   675
            return fileAttrs.lastAccessTime();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   676
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   677
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   678
        public final FileTime lastModifiedTime() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   679
            return fileAttrs.lastModifiedTime();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   680
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   681
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   682
        public final String getNameString() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   683
            return name;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   684
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   685
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   686
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   687
        public final String toString() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   688
            return getNameString();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   689
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   690
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   691
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   692
        public final int hashCode() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   693
            return name.hashCode();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   694
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   695
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   696
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   697
        public final boolean equals(Object other) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   698
            if (this == other) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   699
                return true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   700
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   701
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   702
            if (other instanceof Node) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   703
                return name.equals(((Node) other).name);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   704
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   705
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   706
            return false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   707
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   708
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   709
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   710
    // directory node - directory has full path name without '/' at end.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   711
    static final class Directory extends Node {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   712
        private final List<Node> children;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   713
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   714
        private Directory(String name, BasicFileAttributes fileAttrs) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   715
            super(name, fileAttrs);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   716
            children = new ArrayList<>();
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   717
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   718
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   719
        static Directory create(Directory parent, String name, BasicFileAttributes fileAttrs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   720
            Directory d = new Directory(name, fileAttrs);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   721
            if (parent != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   722
                parent.addChild(d);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   723
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   724
            return d;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   725
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   726
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   727
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   728
        public boolean isDirectory() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   729
            return true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   730
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   731
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   732
        @Override
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   733
        public List<Node> getChildren() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   734
            return Collections.unmodifiableList(children);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   735
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   736
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   737
        void addChild(Node node) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   738
            children.add(node);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   739
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   740
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   741
        public void walk(Consumer<? super Node> consumer) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   742
            consumer.accept(this);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   743
            for ( Node child : children ) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   744
                if (child.isDirectory()) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   745
                    ((Directory)child).walk(consumer);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   746
                } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   747
                    consumer.accept(child);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   748
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   749
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   750
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   751
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   752
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   753
    // "resource" is .class or any other resource (compressed/uncompressed) in a jimage.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   754
    // full path of the resource is the "name" of the resource.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   755
    static class Resource extends Node {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   756
        private final ImageLocation loc;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   757
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   758
        private Resource(ImageLocation loc, BasicFileAttributes fileAttrs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   759
            super(loc.getFullName(true), fileAttrs);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   760
            this.loc = loc;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   761
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   762
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   763
        static Resource create(Directory parent, ImageLocation loc, BasicFileAttributes fileAttrs) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   764
            Resource rs = new Resource(loc, fileAttrs);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   765
            parent.addChild(rs);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   766
            return rs;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   767
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   768
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   769
        @Override
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   770
        public boolean isCompleted() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   771
            return true;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   772
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   773
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   774
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   775
        public boolean isResource() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   776
            return true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   777
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   778
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   779
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   780
        public ImageLocation getLocation() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   781
            return loc;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   782
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   783
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   784
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   785
        public long size() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   786
            return loc.getUncompressedSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   787
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   788
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   789
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   790
        public long compressedSize() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   791
            return loc.getCompressedSize();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   792
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   793
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   794
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   795
        public String extension() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   796
            return loc.getExtension();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   797
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   798
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   799
        @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   800
        public long contentOffset() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   801
            return loc.getContentOffset();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   802
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   803
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   804
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   805
    // represents a soft link to another Node
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   806
    static class LinkNode extends Node {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   807
        private final Node link;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   808
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   809
        private LinkNode(String name, Node link) {
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   810
            super(name, link.getFileAttributes());
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   811
            this.link = link;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   812
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   813
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   814
        static LinkNode create(Directory parent, String name, Node link) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   815
            LinkNode ln = new LinkNode(name, link);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   816
            parent.addChild(ln);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   817
            return ln;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   818
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   819
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   820
        @Override
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   821
        public boolean isCompleted() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   822
            return true;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   823
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   824
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   825
        @Override
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   826
        public Node resolveLink(boolean recursive) {
37614
99a3cdeb43a7 8153930: Compiler crashed (intermittently)
jlaskey
parents: 37365
diff changeset
   827
            return (recursive && link instanceof LinkNode) ? ((LinkNode)link).resolveLink(true) : link;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   828
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   829
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   830
        @Override
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   831
        public boolean isLink() {
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   832
            return true;
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   833
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   834
    }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   835
}