|
| 1 | +import java.io.BufferedInputStream; |
| 2 | +import java.io.BufferedWriter; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.OutputStreamWriter; |
| 8 | +import java.io.Writer; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Enumeration; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Properties; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.zip.ZipEntry; |
| 16 | +import java.util.zip.ZipException; |
| 17 | +import java.util.zip.ZipFile; |
| 18 | +import java.util.zip.ZipInputStream; |
| 19 | + |
| 20 | +/** |
| 21 | + * Walks a Java archive and every archive nested in it, writing what it finds as records for |
| 22 | + * generate-archive-facts.mts, which decides what they mean. |
| 23 | + * |
| 24 | + * <p>Records, one per line, tab-separated, fields escaped like records.mts ({@code \\ \t \n \r}). |
| 25 | + * An archive path is the chain of entry names from the scanned archive, joined by {@code !/}; |
| 26 | + * the scanned archive itself is the empty path. |
| 27 | + * |
| 28 | + * <pre> |
| 29 | + * archive path |
| 30 | + * pom archivePath entry groupId artifactId version (absent keys are empty) |
| 31 | + * malformed archivePath entry detail |
| 32 | + * unreadable archivePath detail |
| 33 | + * </pre> |
| 34 | + */ |
| 35 | +public final class ArchiveLoader { |
| 36 | + private final Set<String> archiveExtensions; |
| 37 | + private final Writer out; |
| 38 | + |
| 39 | + private ArchiveLoader(Set<String> archiveExtensions, Writer out) { |
| 40 | + this.archiveExtensions = archiveExtensions; |
| 41 | + this.out = out; |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args) throws IOException { |
| 45 | + if (args.length != 3) { |
| 46 | + System.err.println("Usage: java -jar archive-loader.jar <archive> <recordsFile> <ext,ext,...>"); |
| 47 | + System.exit(64); |
| 48 | + } |
| 49 | + Set<String> extensions = new HashSet<>(Arrays.asList(args[2].split(","))); |
| 50 | + ZipFile zip; |
| 51 | + try { |
| 52 | + zip = new ZipFile(new File(args[0])); |
| 53 | + } catch (ZipException e) { |
| 54 | + // generate-archive-facts.mts reports this exit as "not a zip archive". |
| 55 | + System.err.println(e.getMessage()); |
| 56 | + System.exit(2); |
| 57 | + return; |
| 58 | + } |
| 59 | + try (ZipFile root = zip; |
| 60 | + Writer out = |
| 61 | + new BufferedWriter( |
| 62 | + new OutputStreamWriter(new FileOutputStream(args[1]), StandardCharsets.UTF_8))) { |
| 63 | + ArchiveLoader loader = new ArchiveLoader(extensions, out); |
| 64 | + Enumeration<? extends ZipEntry> entries = root.entries(); |
| 65 | + while (entries.hasMoreElements()) { |
| 66 | + ZipEntry entry = entries.nextElement(); |
| 67 | + if (!entry.isDirectory() && loader.wants(entry.getName())) { |
| 68 | + try (InputStream in = root.getInputStream(entry)) { |
| 69 | + loader.visit("", entry.getName(), in); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // The same escaping as SocketSupport.escapeField in the Maven extension. |
| 77 | + private static String escape(String s) { |
| 78 | + return s.replace("\\", "\\\\").replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r"); |
| 79 | + } |
| 80 | + |
| 81 | + // ZipInputStream reads a non-zip as an empty archive. A zip starts with a local file header, or |
| 82 | + // when empty with its (zip64) end of central directory record (APPNOTE.TXT 4.3.6). |
| 83 | + private static boolean startsLikeZip(BufferedInputStream in) throws IOException { |
| 84 | + byte[] signature = new byte[4]; |
| 85 | + in.mark(signature.length); |
| 86 | + int read = 0; |
| 87 | + while (read < signature.length) { |
| 88 | + int n = in.read(signature, read, signature.length - read); |
| 89 | + if (n < 0) { |
| 90 | + break; |
| 91 | + } |
| 92 | + read += n; |
| 93 | + } |
| 94 | + in.reset(); |
| 95 | + return read == signature.length |
| 96 | + && signature[0] == 'P' |
| 97 | + && signature[1] == 'K' |
| 98 | + && ((signature[2] == 3 && signature[3] == 4) |
| 99 | + || (signature[2] == 5 && signature[3] == 6) |
| 100 | + || (signature[2] == 6 && signature[3] == 6)); |
| 101 | + } |
| 102 | + |
| 103 | + // maven-archiver writes META-INF/maven/<groupId>/<artifactId>/pom.properties. |
| 104 | + private static boolean isPomProperties(String entryName) { |
| 105 | + String[] segments = entryName.split("/", -1); |
| 106 | + return segments.length == 5 |
| 107 | + && segments[0].equals("META-INF") |
| 108 | + && segments[1].equals("maven") |
| 109 | + && segments[4].equals("pom.properties"); |
| 110 | + } |
| 111 | + |
| 112 | + private boolean isArchive(String entryName) { |
| 113 | + int dot = entryName.lastIndexOf('.'); |
| 114 | + return dot > entryName.lastIndexOf('/') |
| 115 | + && archiveExtensions.contains(entryName.substring(dot + 1)); |
| 116 | + } |
| 117 | + |
| 118 | + private boolean wants(String entryName) { |
| 119 | + return isPomProperties(entryName) || isArchive(entryName); |
| 120 | + } |
| 121 | + |
| 122 | + private void record(String... fields) throws IOException { |
| 123 | + for (int i = 0; i < fields.length; i++) { |
| 124 | + if (i > 0) { |
| 125 | + out.write('\t'); |
| 126 | + } |
| 127 | + out.write(escape(fields[i])); |
| 128 | + } |
| 129 | + out.write('\n'); |
| 130 | + } |
| 131 | + |
| 132 | + // `in` is positioned at the entry's data and is not closed here. |
| 133 | + private void visit(String archivePath, String entryName, InputStream in) throws IOException { |
| 134 | + if (isPomProperties(entryName)) { |
| 135 | + Properties properties = new Properties(); |
| 136 | + try { |
| 137 | + properties.load(in); |
| 138 | + } catch (IllegalArgumentException | IOException e) { |
| 139 | + record("malformed", archivePath, entryName, String.valueOf(e.getMessage())); |
| 140 | + return; |
| 141 | + } |
| 142 | + record( |
| 143 | + "pom", |
| 144 | + archivePath, |
| 145 | + entryName, |
| 146 | + properties.getProperty("groupId", ""), |
| 147 | + properties.getProperty("artifactId", ""), |
| 148 | + properties.getProperty("version", "")); |
| 149 | + } else if (isArchive(entryName)) { |
| 150 | + String nestedPath = archivePath.isEmpty() ? entryName : archivePath + "!/" + entryName; |
| 151 | + record("archive", nestedPath); |
| 152 | + BufferedInputStream buffered = new BufferedInputStream(in); |
| 153 | + if (!startsLikeZip(buffered)) { |
| 154 | + record("unreadable", nestedPath, "not a zip archive"); |
| 155 | + return; |
| 156 | + } |
| 157 | + // Not closed: closing a ZipInputStream closes the stream it reads from. |
| 158 | + ZipInputStream nested = new ZipInputStream(buffered, StandardCharsets.UTF_8); |
| 159 | + try { |
| 160 | + ZipEntry entry; |
| 161 | + while ((entry = nested.getNextEntry()) != null) { |
| 162 | + if (!entry.isDirectory()) { |
| 163 | + visit(nestedPath, entry.getName(), nested); |
| 164 | + } |
| 165 | + } |
| 166 | + } catch (IOException | IllegalArgumentException e) { |
| 167 | + record("unreadable", nestedPath, String.valueOf(e)); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments