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