Skip to content

Commit f84323f

Browse files
committed
Prevent duplicate jar entries from being written
Update the `AbstractJarWriter` so that it can directly build the layer index as entries are written. Prior to this commit, a layer tracking was handled by a decorator class which was broken because it didn't override enough methods. Since `AbstractJarWriter` has quite a complex API, it seems sensible to have it handle the layer index directly, removing the need for a decorator entirely. Fixes gh-23801
1 parent b37eecc commit f84323f

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ public abstract class AbstractJarWriter implements LoaderClassesWriter {
6060

6161
private final Set<String> writtenEntries = new HashSet<>();
6262

63+
private Layers layers;
64+
65+
private LayersIndex layersIndex;
66+
67+
/**
68+
* Update this writer to use specific layers.
69+
* @param layers the layers to use
70+
* @param layersIndex the layers index to update
71+
*/
72+
void useLayers(Layers layers, LayersIndex layersIndex) {
73+
this.layers = layers;
74+
this.layersIndex = layersIndex;
75+
}
76+
6377
/**
6478
* Write the specified manifest.
6579
* @param manifest the manifest to write
@@ -89,7 +103,7 @@ final void writeEntries(JarFile jarFile, EntryTransformer entryTransformer, Unpa
89103
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream);
90104
JarArchiveEntry transformedEntry = entryTransformer.transform(entry);
91105
if (transformedEntry != null) {
92-
writeEntry(transformedEntry, entryWriter, unpackHandler);
106+
writeEntry(transformedEntry, entryWriter, unpackHandler, true);
93107
}
94108
}
95109
}
@@ -144,7 +158,15 @@ public void writeNestedLibrary(String location, Library library) throws IOExcept
144158
entry.setTime(getNestedLibraryTime(library));
145159
new CrcAndSize(library::openStream).setupStoredEntry(entry);
146160
try (InputStream inputStream = library.openStream()) {
147-
writeEntry(entry, new InputStreamEntryWriter(inputStream), new LibraryUnpackHandler(library));
161+
writeEntry(entry, new InputStreamEntryWriter(inputStream), new LibraryUnpackHandler(library), false);
162+
updateLayerIndex(entry.getName(), library);
163+
}
164+
}
165+
166+
private void updateLayerIndex(String name, Library library) {
167+
if (this.layers != null) {
168+
Layer layer = this.layers.getLayer(library);
169+
this.layersIndex.add(layer, name);
148170
}
149171
}
150172

@@ -225,7 +247,7 @@ private boolean isClassEntry(JarEntry entry) {
225247
}
226248

227249
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter) throws IOException {
228-
writeEntry(entry, entryWriter, UnpackHandler.NEVER);
250+
writeEntry(entry, entryWriter, UnpackHandler.NEVER, true);
229251
}
230252

231253
/**
@@ -234,10 +256,11 @@ private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter) throws I
234256
* @param entry the entry to write
235257
* @param entryWriter the entry writer or {@code null} if there is no content
236258
* @param unpackHandler handles possible unpacking for the entry
259+
* @param updateLayerIndex if the layer index should be updated
237260
* @throws IOException in case of I/O errors
238261
*/
239-
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHandler unpackHandler)
240-
throws IOException {
262+
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHandler unpackHandler,
263+
boolean updateLayerIndex) throws IOException {
241264
String name = entry.getName();
242265
writeParentDirectoryEntries(name);
243266
if (this.writtenEntries.add(name)) {
@@ -248,18 +271,28 @@ private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHa
248271
entry.setSize(entryWriter.size());
249272
}
250273
entryWriter = addUnpackCommentIfNecessary(entry, entryWriter, unpackHandler);
274+
if (updateLayerIndex) {
275+
updateLayerIndex(entry);
276+
}
251277
writeToArchive(entry, entryWriter);
252278
}
253279
}
254280

281+
private void updateLayerIndex(JarArchiveEntry entry) {
282+
if (this.layers != null && !entry.getName().endsWith("/")) {
283+
Layer layer = this.layers.getLayer(entry.getName());
284+
this.layersIndex.add(layer, entry.getName());
285+
}
286+
}
287+
255288
protected abstract void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException;
256289

257290
private void writeParentDirectoryEntries(String name) throws IOException {
258291
String parent = name.endsWith("/") ? name.substring(0, name.length() - 1) : name;
259292
while (parent.lastIndexOf('/') != -1) {
260293
parent = parent.substring(0, parent.lastIndexOf('/'));
261294
if (!parent.isEmpty()) {
262-
writeEntry(new JarArchiveEntry(parent + "/"), null, UnpackHandler.NEVER);
295+
writeEntry(new JarArchiveEntry(parent + "/"), null, UnpackHandler.NEVER, false);
263296
}
264297
}
265298
}

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.jar.JarFile;
3030
import java.util.jar.Manifest;
3131
import java.util.stream.Collectors;
32-
import java.util.zip.ZipEntry;
3332

3433
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
3534

@@ -168,9 +167,7 @@ protected final boolean isAlreadyPackaged(File file) throws IOException {
168167
protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWriter writer) throws IOException {
169168
Assert.notNull(libraries, "Libraries must not be null");
170169
WritableLibraries writeableLibraries = new WritableLibraries(libraries);
171-
if (this.layers != null) {
172-
writer = new LayerTrackingEntryWriter(writer);
173-
}
170+
writer.useLayers(this.layers, this.layersIndex);
174171
writer.writeManifest(buildManifest(sourceJar));
175172
writeLoaderClasses(writer);
176173
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
@@ -422,35 +419,6 @@ private boolean isTransformable(JarArchiveEntry entry) {
422419

423420
}
424421

425-
/**
426-
* Decorator to track the layers as entries are written.
427-
*/
428-
private final class LayerTrackingEntryWriter extends AbstractJarWriter {
429-
430-
private final AbstractJarWriter writer;
431-
432-
private LayerTrackingEntryWriter(AbstractJarWriter writer) {
433-
this.writer = writer;
434-
}
435-
436-
@Override
437-
public void writeNestedLibrary(String location, Library library) throws IOException {
438-
this.writer.writeNestedLibrary(location, library);
439-
Layer layer = Packager.this.layers.getLayer(library);
440-
Packager.this.layersIndex.add(layer, location + library.getName());
441-
}
442-
443-
@Override
444-
protected void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException {
445-
this.writer.writeToArchive(entry, entryWriter);
446-
if (!entry.getName().endsWith("/")) {
447-
Layer layer = Packager.this.layers.getLayer(entry.getName());
448-
Packager.this.layersIndex.add(layer, entry.getName());
449-
}
450-
}
451-
452-
}
453-
454422
/**
455423
* An {@link UnpackHandler} that determines that an entry needs to be unpacked if a
456424
* library that requires unpacking has a matching entry name.

0 commit comments

Comments
 (0)