/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.tools.jlink.internal; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; import java.nio.file.Path; import java.util.Comparator; import java.util.Map; import java.util.Objects; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipFile; import jdk.tools.jlink.internal.Archive.Entry.EntryType; /** * An Archive backed by a jar file. */ public abstract class JarArchive implements Archive { /** * An entry located in a jar file. */ private class JarFileEntry extends Entry { private final long size; private final JarEntry entry; private final JarFile file; JarFileEntry(String path, String name, EntryType type, JarFile file, JarEntry entry) { super(JarArchive.this, path, name, type); this.entry = Objects.requireNonNull(entry); this.file = Objects.requireNonNull(file); size = entry.getSize(); } /** * Returns the number of uncompressed bytes for this entry. */ @Override public long size() { return size; } @Override public InputStream stream() throws IOException { return file.getInputStream(entry); } } private static final String MANIFEST = "META-INF/MANIFEST.MF"; private static final String MODULE_INFO = "module-info.class"; private static final String VERSIONS_DIR = "META-INF/versions/"; private static final int VERSIONS_DIR_LEN = VERSIONS_DIR.length(); private final Path file; private final String moduleName; // currently processed JarFile private JarFile jarFile; private int version; private int offset; protected JarArchive(String mn, Path file) { Objects.requireNonNull(mn); Objects.requireNonNull(file); this.moduleName = mn; this.file = file; } @Override public String moduleName() { return moduleName; } @Override public Path getPath() { return file; } @Override public Stream entries() { try { if (jarFile == null) { open(); } } catch (IOException ioe) { throw new UncheckedIOException(ioe); } if (jarFile.isMultiRelease()) { // sort entries in ascending version order, extract the base name // and add them to a map, then return the Entries as with regular jar return jarFile.stream() .filter(je -> !je.isDirectory()) .filter(je -> !je.getName().equals(MANIFEST)) .filter(this::versionAcceptable) .sorted(entryComparator) .collect(Collectors.toMap(this::extractBaseName, je -> je, (v1, v2) -> v2)) .entrySet() .stream().map(this::toEntry).filter(n -> n != null); } return jarFile.stream().map(this::toEntry).filter(n -> n != null); } abstract EntryType toEntryType(String entryName); abstract String getFileName(String entryName); // sort base entries before versioned entries private Comparator entryComparator = (je1, je2) -> { String s1 = je1.getName(); String s2 = je2.getName(); if (s1.equals(s2)) return 0; boolean b1 = s1.startsWith(VERSIONS_DIR); boolean b2 = s2.startsWith(VERSIONS_DIR); if (b1 && !b2) return 1; if (!b1 && b2) return -1; int n = 0; // starting char for String compare if (b1 && b2) { // normally strings would be sorted so "10" goes before "9", but // version number strings need to be sorted numerically n = VERSIONS_DIR.length(); // skip the common prefix int i1 = s1.indexOf('/', n); int i2 = s1.indexOf('/', n); if (i1 == -1) throw new RuntimeException(s1); // fixme, better message if (i2 == -1) throw new RuntimeException(s2); // fixme, better message // shorter version numbers go first if (i1 != i2) return i1 - i2; // otherwise, handle equal length numbers below } int l1 = s1.length(); int l2 = s2.length(); int lim = Math.min(l1, l2); for (int k = n; k < lim; k++) { char c1 = s1.charAt(k); char c2 = s2.charAt(k); if (c1 != c2) { return c1 - c2; } } return l1 - l2; }; // must be invoked after versionAcceptable private String extractBaseName(JarEntry je) { String name = je.getName(); if (name.startsWith(VERSIONS_DIR)) { return name.substring(VERSIONS_DIR_LEN + offset + 1); } return name; } private Entry toEntry(JarEntry je) { String name = je.getName(); return toEntry(name, je); } private Entry toEntry(Map.Entry entry) { String name = entry.getKey(); JarEntry je = entry.getValue(); return toEntry(name, je); } private Entry toEntry(String name, JarEntry je) { String fn = getFileName(name); if (je.isDirectory() || fn.startsWith("_")) { return null; } EntryType rt = toEntryType(name); if (fn.equals(MODULE_INFO)) { fn = moduleName + "/" + MODULE_INFO; } return new JarFileEntry(name, fn, rt, jarFile, je); } // must be invoked before extractBaseName private boolean versionAcceptable(JarEntry je) { String name = je.getName(); if (name.startsWith(VERSIONS_DIR)) { name = name.substring(VERSIONS_DIR_LEN); offset = name.indexOf('/'); if (offset == -1) throw new RuntimeException("");// fixme, better message if (Integer.parseInt(name.substring(0, offset)) <= version) { return true; } return false; } return true; } @Override public void close() throws IOException { if (jarFile != null) { jarFile.close(); } } @Override public void open() throws IOException { if (jarFile != null) { jarFile.close(); } // open this way to determine if it's a multi-release jar jarFile = new JarFile(file.toFile(), true, ZipFile.OPEN_READ, JarFile.runtimeVersion()); version = jarFile.getVersion().major(); } }