src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47987 85ea7e83af30
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.UncheckedIOException;
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;
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
    33
import java.util.jar.JarFile;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.zip.ZipEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.zip.ZipFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.tools.jlink.internal.Archive.Entry.EntryType;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * An Archive backed by a jar file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
public abstract class JarArchive implements Archive {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
     * An entry located in a jar file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
     */
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
    47
    public class JarEntry extends Entry {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        private final long size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        private final ZipEntry entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        private final ZipFile file;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        JarEntry(String path, String name, EntryType type, ZipFile file, ZipEntry entry) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            super(JarArchive.this, path, name, type);
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
    55
            this.entry = Objects.requireNonNull(entry);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
    56
            this.file = Objects.requireNonNull(file);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            size = entry.getSize();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
         * Returns the number of uncompressed bytes for this entry.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        public long size() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            return size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        public InputStream stream() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            return file.getInputStream(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    private final Path file;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    private final String moduleName;
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47216
diff changeset
    76
    private final Runtime.Version version;
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
    77
    // currently processed JarFile
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
    78
    private JarFile jarFile;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47216
diff changeset
    80
    protected JarArchive(String mn, Path file, Runtime.Version version) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        Objects.requireNonNull(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        Objects.requireNonNull(file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        this.moduleName = mn;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        this.file = file;
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47216
diff changeset
    85
        this.version = Objects.requireNonNull(version);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public String moduleName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        return moduleName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    public Path getPath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        return file;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    public Stream<Entry> entries() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        try {
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   101
            if (jarFile == null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                open();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        }
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47737
diff changeset
   107
        return jarFile.versionedStream()
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   108
                .filter(je -> !je.isDirectory())
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   109
                .map(this::toEntry);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    abstract EntryType toEntryType(String entryName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    abstract String getFileName(String entryName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 39308
diff changeset
   116
    abstract Entry toEntry(ZipEntry ze);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    public void close() throws IOException {
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   120
        if (jarFile != null) {
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   121
            jarFile.close();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    public void open() throws IOException {
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   127
        if (jarFile != null) {
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   128
            jarFile.close();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47216
diff changeset
   130
        jarFile = new JarFile(file.toFile(), true, ZipFile.OPEN_READ, version);
41828
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   131
    }
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   132
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   133
    protected JarFile getJarFile() {
0436ea0c6099 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents: 41352
diff changeset
   134
        return jarFile;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
}