1 package com.atlassian.plugin.osgi.factory.transform;
2
3 import com.atlassian.fugue.Suppliers;
4 import com.atlassian.plugin.util.collect.Function;
5 import com.google.common.collect.ImmutableList;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.util.jar.JarEntry;
10 import java.util.jar.JarFile;
11 import java.util.jar.Manifest;
12 import java.util.zip.ZipEntry;
13
14 import static com.atlassian.fugue.Option.option;
15 import static com.google.common.collect.Iterators.forEnumeration;
16
17
18
19
20
21
22 public final class JarUtils
23 {
24
25
26
27 private JarUtils()
28 {}
29
30
31
32
33
34
35
36 public static Manifest getManifest(final File file)
37 {
38 final Manifest result = withJar(file, ManifestExtractor.INSTANCE);
39 return (result == null) ? new Manifest() : result;
40 }
41
42 public static boolean hasManifestEntry(Manifest manifest, final String entryName)
43 {
44 return option(manifest).fold(Suppliers.alwaysFalse(), new com.google.common.base.Function<Manifest, Boolean>()
45 {
46 @Override
47 public Boolean apply(Manifest m)
48 {
49 return m.getMainAttributes().getValue(entryName) != null;
50 }
51 });
52 }
53
54
55
56
57
58
59
60 static Iterable<JarEntry> getEntries(final File file)
61 {
62 return withJar(file, JarEntryExtractor.INSTANCE);
63 }
64
65
66
67
68
69
70
71 static JarEntry getEntry(final File file, final String path)
72 {
73 return withJar(file, new Extractor<JarEntry>()
74 {
75 public JarEntry get(final JarFile jarFile)
76 {
77 return jarFile.getJarEntry(path);
78 }
79 });
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 public static <T> T withJar(final File file, final Extractor<T> extractor)
94 {
95 JarFile jarFile = null;
96 try
97 {
98 jarFile = new JarFile(file);
99 return extractor.get(jarFile);
100 }
101 catch (final IOException e)
102 {
103 throw new IllegalArgumentException("File must be a jar: " + file, e);
104 }
105 finally
106 {
107 closeQuietly(jarFile);
108 }
109 }
110
111
112
113
114
115
116 public static void closeQuietly(final JarFile jarFile)
117 {
118 if (jarFile != null)
119 {
120 try
121 {
122 jarFile.close();
123 }
124 catch (final IOException ignore)
125 {}
126 }
127 }
128
129 public interface Extractor<T> extends Function<JarFile, T>
130 {}
131
132 enum ManifestExtractor implements Extractor<Manifest>
133 {
134 INSTANCE;
135
136 public Manifest get(final JarFile input)
137 {
138 try
139 {
140 return input.getManifest();
141 }
142 catch (final IOException e)
143 {
144 throw new RuntimeException(e);
145 }
146 }
147 }
148
149 enum JarEntryExtractor implements Extractor<Iterable<JarEntry>>
150 {
151 INSTANCE;
152
153 public Iterable<JarEntry> get(final JarFile jarFile)
154 {
155 return ImmutableList.copyOf(forEnumeration(jarFile.entries()));
156 }
157 }
158 }