1 package com.atlassian.plugin.osgi.factory.transform;
2
3 import com.google.common.collect.ImmutableList;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.function.Function;
8 import java.util.jar.JarEntry;
9 import java.util.jar.JarFile;
10 import java.util.jar.Manifest;
11 import java.util.zip.ZipEntry;
12
13 import static com.google.common.collect.Iterators.forEnumeration;
14
15
16
17
18
19
20 public final class JarUtils {
21
22
23
24 private JarUtils() {
25 }
26
27
28
29
30
31
32
33 public static Manifest getManifest(final File file) {
34 final Manifest result = withJar(file, ManifestExtractor.INSTANCE);
35 return (result == null) ? new Manifest() : result;
36 }
37
38 public static boolean hasManifestEntry(Manifest manifest, final String entryName) {
39 return manifest != null && manifest.getMainAttributes().getValue(entryName) != null;
40 }
41
42
43
44
45
46
47
48 static Iterable<JarEntry> getEntries(final File file) {
49 return withJar(file, JarEntryExtractor.INSTANCE);
50 }
51
52
53
54
55
56
57
58 static JarEntry getEntry(final File file, final String path) {
59 return withJar(file, jarFile -> jarFile.getJarEntry(path));
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73 public static <T> T withJar(final File file, final Extractor<T> extractor) {
74 try (JarFile jarFile = new JarFile(file)){
75 return extractor.apply(jarFile);
76 } catch (final IOException e) {
77 throw new IllegalArgumentException("File must be a jar: " + file, e);
78 }
79 }
80
81
82
83
84
85
86 public static void closeQuietly(final JarFile jarFile) {
87 if (jarFile != null) {
88 try {
89 jarFile.close();
90 } catch (final IOException ignore) {
91 }
92 }
93 }
94
95 public interface Extractor<T> extends Function<JarFile, T> {
96 }
97
98 enum ManifestExtractor implements Extractor<Manifest> {
99 INSTANCE;
100
101 public Manifest apply(final JarFile input) {
102 try {
103 return input.getManifest();
104 } catch (final IOException e) {
105 throw new RuntimeException(e);
106 }
107 }
108 }
109
110 enum JarEntryExtractor implements Extractor<Iterable<JarEntry>> {
111 INSTANCE;
112
113 public Iterable<JarEntry> apply(final JarFile jarFile) {
114 return ImmutableList.copyOf(forEnumeration(jarFile.entries()));
115 }
116 }
117 }