Skip to content

Commit 87de7e6

Browse files
committed
Return the correct manifest for a JarFile create from a nested directory
Previously, if a JarFile was created from a directory nested inside another jar file, it would look for the manifest in pathFromRoot/META-INF/MANIFEST.MF. This is incorrect as, unlike a JarFile created from a jar file, the archives are one and the same so the manifests should be too. This commit updates JarFile so that its aware of how it was created (direct from a file, from a nested directory, from a nested jar). If it was created from a file or from a nested jar, it uses its manifest. If it was created from a nested directory, it uses the manifest of the root archive. Closes gh-5609
1 parent 268641d commit 87de7e6

File tree

1 file changed

+29
-14
lines changed
  • spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar

1 file changed

+29
-14
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

+29-14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class JarFile extends java.util.jar.JarFile {
6464

6565
private final RandomAccessData data;
6666

67+
private final JarFileType type;
68+
6769
private URL url;
6870

6971
private JarFileEntries entries;
@@ -87,7 +89,7 @@ public JarFile(File file) throws IOException {
8789
* @throws IOException if the file cannot be read
8890
*/
8991
JarFile(RandomAccessDataFile file) throws IOException {
90-
this(file, "", file);
92+
this(file, "", file, JarFileType.DIRECT);
9193
}
9294

9395
/**
@@ -96,22 +98,25 @@ public JarFile(File file) throws IOException {
9698
* @param rootFile the root jar file
9799
* @param pathFromRoot the name of this file
98100
* @param data the underlying data
101+
* @param type the type of the jar file
99102
* @throws IOException if the file cannot be read
100103
*/
101104
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
102-
RandomAccessData data) throws IOException {
103-
this(rootFile, pathFromRoot, data, null);
105+
RandomAccessData data, JarFileType type) throws IOException {
106+
this(rootFile, pathFromRoot, data, null, type);
104107
}
105108

106109
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
107-
RandomAccessData data, JarEntryFilter filter) throws IOException {
110+
RandomAccessData data, JarEntryFilter filter, JarFileType type)
111+
throws IOException {
108112
super(rootFile.getFile());
109113
this.rootFile = rootFile;
110114
this.pathFromRoot = pathFromRoot;
111115
CentralDirectoryParser parser = new CentralDirectoryParser();
112116
this.entries = parser.addVisitor(new JarFileEntries(this, filter));
113117
parser.addVisitor(centralDirectoryVisitor());
114118
this.data = parser.parse(data, filter == null);
119+
this.type = type;
115120
}
116121

117122
private CentralDirectoryVisitor centralDirectoryVisitor() {
@@ -151,15 +156,21 @@ RandomAccessData getData() {
151156
public Manifest getManifest() throws IOException {
152157
Manifest manifest = (this.manifest == null ? null : this.manifest.get());
153158
if (manifest == null) {
154-
InputStream inputStream = getInputStream(MANIFEST_NAME, ResourceAccess.ONCE);
155-
if (inputStream == null) {
156-
return null;
157-
}
158-
try {
159-
manifest = new Manifest(inputStream);
159+
if (this.type == JarFileType.NESTED_DIRECTORY) {
160+
manifest = new JarFile(this.getRootJarFile()).getManifest();
160161
}
161-
finally {
162-
inputStream.close();
162+
else {
163+
InputStream inputStream = getInputStream(MANIFEST_NAME,
164+
ResourceAccess.ONCE);
165+
if (inputStream == null) {
166+
return null;
167+
}
168+
try {
169+
manifest = new Manifest(inputStream);
170+
}
171+
finally {
172+
inputStream.close();
173+
}
163174
}
164175
this.manifest = new SoftReference<Manifest>(manifest);
165176
}
@@ -259,7 +270,7 @@ public AsciiBytes apply(AsciiBytes name) {
259270
return new JarFile(this.rootFile,
260271
this.pathFromRoot + "!/"
261272
+ entry.getName().substring(0, sourceName.length() - 1),
262-
this.data, filter);
273+
this.data, filter, JarFileType.NESTED_DIRECTORY);
263274
}
264275

265276
private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
@@ -271,7 +282,7 @@ private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
271282
}
272283
RandomAccessData entryData = this.entries.getEntryData(entry.getName());
273284
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(),
274-
entryData);
285+
entryData, JarFileType.NESTED_JAR);
275286
}
276287

277288
@Override
@@ -376,4 +387,8 @@ private static void resetCachedUrlHandlers() {
376387
}
377388
}
378389

390+
private enum JarFileType {
391+
DIRECT, NESTED_DIRECTORY, NESTED_JAR
392+
}
393+
379394
}

0 commit comments

Comments
 (0)