Commit 87de7e63 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 268641d3
...@@ -64,6 +64,8 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -64,6 +64,8 @@ public class JarFile extends java.util.jar.JarFile {
private final RandomAccessData data; private final RandomAccessData data;
private final JarFileType type;
private URL url; private URL url;
private JarFileEntries entries; private JarFileEntries entries;
...@@ -87,7 +89,7 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -87,7 +89,7 @@ public class JarFile extends java.util.jar.JarFile {
* @throws IOException if the file cannot be read * @throws IOException if the file cannot be read
*/ */
JarFile(RandomAccessDataFile file) throws IOException { JarFile(RandomAccessDataFile file) throws IOException {
this(file, "", file); this(file, "", file, JarFileType.DIRECT);
} }
/** /**
...@@ -96,15 +98,17 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -96,15 +98,17 @@ public class JarFile extends java.util.jar.JarFile {
* @param rootFile the root jar file * @param rootFile the root jar file
* @param pathFromRoot the name of this file * @param pathFromRoot the name of this file
* @param data the underlying data * @param data the underlying data
* @param type the type of the jar file
* @throws IOException if the file cannot be read * @throws IOException if the file cannot be read
*/ */
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
RandomAccessData data) throws IOException { RandomAccessData data, JarFileType type) throws IOException {
this(rootFile, pathFromRoot, data, null); this(rootFile, pathFromRoot, data, null, type);
} }
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
RandomAccessData data, JarEntryFilter filter) throws IOException { RandomAccessData data, JarEntryFilter filter, JarFileType type)
throws IOException {
super(rootFile.getFile()); super(rootFile.getFile());
this.rootFile = rootFile; this.rootFile = rootFile;
this.pathFromRoot = pathFromRoot; this.pathFromRoot = pathFromRoot;
...@@ -112,6 +116,7 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -112,6 +116,7 @@ public class JarFile extends java.util.jar.JarFile {
this.entries = parser.addVisitor(new JarFileEntries(this, filter)); this.entries = parser.addVisitor(new JarFileEntries(this, filter));
parser.addVisitor(centralDirectoryVisitor()); parser.addVisitor(centralDirectoryVisitor());
this.data = parser.parse(data, filter == null); this.data = parser.parse(data, filter == null);
this.type = type;
} }
private CentralDirectoryVisitor centralDirectoryVisitor() { private CentralDirectoryVisitor centralDirectoryVisitor() {
...@@ -151,7 +156,12 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -151,7 +156,12 @@ public class JarFile extends java.util.jar.JarFile {
public Manifest getManifest() throws IOException { public Manifest getManifest() throws IOException {
Manifest manifest = (this.manifest == null ? null : this.manifest.get()); Manifest manifest = (this.manifest == null ? null : this.manifest.get());
if (manifest == null) { if (manifest == null) {
InputStream inputStream = getInputStream(MANIFEST_NAME, ResourceAccess.ONCE); if (this.type == JarFileType.NESTED_DIRECTORY) {
manifest = new JarFile(this.getRootJarFile()).getManifest();
}
else {
InputStream inputStream = getInputStream(MANIFEST_NAME,
ResourceAccess.ONCE);
if (inputStream == null) { if (inputStream == null) {
return null; return null;
} }
...@@ -161,6 +171,7 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -161,6 +171,7 @@ public class JarFile extends java.util.jar.JarFile {
finally { finally {
inputStream.close(); inputStream.close();
} }
}
this.manifest = new SoftReference<Manifest>(manifest); this.manifest = new SoftReference<Manifest>(manifest);
} }
return manifest; return manifest;
...@@ -259,7 +270,7 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -259,7 +270,7 @@ public class JarFile extends java.util.jar.JarFile {
return new JarFile(this.rootFile, return new JarFile(this.rootFile,
this.pathFromRoot + "!/" this.pathFromRoot + "!/"
+ entry.getName().substring(0, sourceName.length() - 1), + entry.getName().substring(0, sourceName.length() - 1),
this.data, filter); this.data, filter, JarFileType.NESTED_DIRECTORY);
} }
private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException { private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
...@@ -271,7 +282,7 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -271,7 +282,7 @@ public class JarFile extends java.util.jar.JarFile {
} }
RandomAccessData entryData = this.entries.getEntryData(entry.getName()); RandomAccessData entryData = this.entries.getEntryData(entry.getName());
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(),
entryData); entryData, JarFileType.NESTED_JAR);
} }
@Override @Override
...@@ -376,4 +387,8 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -376,4 +387,8 @@ public class JarFile extends java.util.jar.JarFile {
} }
} }
private enum JarFileType {
DIRECT, NESTED_DIRECTORY, NESTED_JAR
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment