src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JmodArchive.java
author mr
Tue, 29 Oct 2019 08:26:55 -0700
changeset 58842 6c255334120d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232080: jlink plugins for vendor information and run-time options Reviewed-by: ihse, alanb, kvn, bobv, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    28
import java.io.IOException;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    29
import java.io.InputStream;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    30
import java.io.UncheckedIOException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Objects;
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    33
import java.util.stream.Stream;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    34
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    35
import jdk.internal.jmod.JmodFile;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.tools.jlink.internal.Archive.Entry.EntryType;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * An Archive backed by a jmod file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 */
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    41
public class JmodArchive implements Archive {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
    42
    private static final String JMOD_EXT    = ".jmod";
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    43
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    44
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    45
     * An entry located in a jmod file.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    46
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    47
    public class JmodEntry extends Entry {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    48
        private final JmodFile.Entry entry;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    49
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    50
        JmodEntry(String path, String name, EntryType type,
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    51
                  JmodFile.Entry entry) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    52
            super(JmodArchive.this, path, name, type);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    53
            this.entry = Objects.requireNonNull(entry);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    54
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    55
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    56
        /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    57
         * Returns the number of uncompressed bytes for this entry.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    58
         */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    59
        @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    60
        public long size() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    61
            return entry.size();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    62
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    63
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    64
        @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    65
        public InputStream stream() throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    66
            return jmodFile.getInputStream(entry.section(), entry.name());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    67
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    68
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    69
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    70
    private final Path file;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    71
    private final String moduleName;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    72
    private JmodFile jmodFile;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public JmodArchive(String mn, Path jmod) {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    75
        Objects.requireNonNull(mn);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    76
        Objects.requireNonNull(jmod.getFileName());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    77
        String filename = jmod.toString();
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
    78
        if (!filename.endsWith(JMOD_EXT)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            throw new UnsupportedOperationException("Unsupported format: " + filename);
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
    80
        }
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    81
        this.moduleName = mn;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    82
        this.file = jmod;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    83
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    84
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    85
    @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    86
    public String moduleName() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    87
        return moduleName;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    88
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    89
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    90
    @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    91
    public Path getPath() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    92
        return file;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    93
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    94
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    95
    @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    96
    public Stream<Entry> entries() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    97
        ensureOpen();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    98
        return jmodFile.stream()
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    99
                       .map(this::toEntry);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    @Override
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   103
    public void open() throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   104
        if (jmodFile != null) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   105
            jmodFile.close();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   106
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   107
        this.jmodFile = new JmodFile(file);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   108
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   109
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   110
    @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   111
    public void close() throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   112
        if (jmodFile != null) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   113
            jmodFile.close();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   114
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   115
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   116
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   117
    private void ensureOpen() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   118
        if (jmodFile == null) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   119
            try {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   120
                open();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   121
            } catch(IOException ioe){
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   122
                throw new UncheckedIOException(ioe);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   123
            }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   124
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   125
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   126
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   127
    private EntryType toEntryType(JmodFile.Section section) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        switch (section) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            case CLASSES:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                return EntryType.CLASS_OR_RESOURCE;
41561
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41352
diff changeset
   131
            case CONFIG:
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41352
diff changeset
   132
                return EntryType.CONFIG;
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   133
            case HEADER_FILES:
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   134
                return EntryType.HEADER_FILE;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   135
            case LEGAL_NOTICES:
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   136
                return EntryType.LEGAL_NOTICE;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   137
            case MAN_PAGES:
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   138
                return EntryType.MAN_PAGE;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            case NATIVE_LIBS:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                return EntryType.NATIVE_LIB;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            case NATIVE_CMDS:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                return EntryType.NATIVE_CMD;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            default:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                throw new InternalError("unexpected entry: " + section);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   148
    private Entry toEntry(JmodFile.Entry entry) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   149
        EntryType type = toEntryType(entry.section());
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   150
        String prefix = entry.section().jmodDir();
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   151
        String name = entry.name();
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   152
        String path = prefix + "/" + name;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   153
        String resourceName = name;
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   154
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   155
        // The resource name represents the path of ResourcePoolEntry
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   156
        // and its subpath defines the ultimate path to be written
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   157
        // to the image relative to the directory corresponding to that
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   158
        // resource type.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   159
        //
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   160
        // For classes and resources, the resource name does not have
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   161
        // a prefix (<package>/<name>). They will be written to the jimage.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   162
        //
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   163
        // For other kind of entries, it will keep the section name as
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   164
        // the prefix for unique identification.  The subpath (taking
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   165
        // out the section name) is the pathname to be written to the
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   166
        // corresponding directory in the image.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   167
        //
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   168
        if (type == EntryType.LEGAL_NOTICE) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   169
            // legal notices are written to per-module directory
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   170
            resourceName = prefix + "/" + moduleName + "/" + name;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 41561
diff changeset
   171
        } else if (type != EntryType.CLASS_OR_RESOURCE) {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   172
            resourceName = path;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   175
        return new JmodEntry(path, resourceName, type, entry);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
}