Skip to content

Commit da75330

Browse files
committed
Merge branch '2.3.x'
Closes gh-23819
2 parents 1725594 + f84323f commit da75330

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
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: 9 additions & 42 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,25 +167,18 @@ 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 (isLayersEnabled()) {
172-
writer = new LayerTrackingEntryWriter(writer);
170+
if (isLayered()) {
171+
writer.useLayers(this.layers, this.layersIndex);
173172
}
174173
writer.writeManifest(buildManifest(sourceJar));
175174
writeLoaderClasses(writer);
176175
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
177176
writeableLibraries.write(writer);
178-
if (isLayersEnabled()) {
177+
if (isLayered()) {
179178
writeLayerIndex(writer);
180179
}
181180
}
182181

183-
private boolean isLayersEnabled() {
184-
if (!(getLayout() instanceof Layouts.Jar)) {
185-
return false;
186-
}
187-
return this.layers != null;
188-
}
189-
190182
private void writeLoaderClasses(AbstractJarWriter writer) throws IOException {
191183
Layout layout = getLayout();
192184
if (layout instanceof CustomLoaderLayout) {
@@ -339,7 +331,7 @@ private void addBootBootAttributesForRepackagingLayout(Attributes attributes, Re
339331
attributes.putValue(BOOT_CLASSES_ATTRIBUTE, layout.getRepackagedClassesLocation());
340332
putIfHasLength(attributes, BOOT_LIB_ATTRIBUTE, getLayout().getLibraryLocation("", LibraryScope.COMPILE));
341333
putIfHasLength(attributes, BOOT_CLASSPATH_INDEX_ATTRIBUTE, layout.getClasspathIndexFileLocation());
342-
if (isLayersEnabled()) {
334+
if (isLayered()) {
343335
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layout.getLayersIndexFileLocation());
344336
}
345337
}
@@ -355,6 +347,10 @@ private void putIfHasLength(Attributes attributes, String name, String value) {
355347
}
356348
}
357349

350+
private boolean isLayered() {
351+
return this.layers != null && getLayout() instanceof Layouts.Jar;
352+
}
353+
358354
/**
359355
* Callback interface used to present a warning when finding the main class takes too
360356
* long.
@@ -429,35 +425,6 @@ private boolean isTransformable(JarArchiveEntry entry) {
429425

430426
}
431427

432-
/**
433-
* Decorator to track the layers as entries are written.
434-
*/
435-
private final class LayerTrackingEntryWriter extends AbstractJarWriter {
436-
437-
private final AbstractJarWriter writer;
438-
439-
private LayerTrackingEntryWriter(AbstractJarWriter writer) {
440-
this.writer = writer;
441-
}
442-
443-
@Override
444-
public void writeNestedLibrary(String location, Library library) throws IOException {
445-
this.writer.writeNestedLibrary(location, library);
446-
Layer layer = Packager.this.layers.getLayer(library);
447-
Packager.this.layersIndex.add(layer, location + library.getName());
448-
}
449-
450-
@Override
451-
protected void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException {
452-
this.writer.writeToArchive(entry, entryWriter);
453-
if (!entry.getName().endsWith("/")) {
454-
Layer layer = Packager.this.layers.getLayer(entry.getName());
455-
Packager.this.layersIndex.add(layer, entry.getName());
456-
}
457-
}
458-
459-
}
460-
461428
/**
462429
* An {@link UnpackHandler} that determines that an entry needs to be unpacked if a
463430
* library that requires unpacking has a matching entry name.
@@ -472,7 +439,7 @@ private final class WritableLibraries implements UnpackHandler {
472439
addLibrary(library);
473440
}
474441
});
475-
if (isLayersEnabled() && Packager.this.includeRelevantJarModeJars) {
442+
if (isLayered() && Packager.this.includeRelevantJarModeJars) {
476443
addLibrary(JarModeLibrary.LAYER_TOOLS);
477444
}
478445
}

0 commit comments

Comments
 (0)