Skip to content

Commit

Permalink
Merge branch '2.3.x'
Browse files Browse the repository at this point in the history
Closes gh-23819
  • Loading branch information
philwebb committed Oct 23, 2020
2 parents 1725594 + f84323f commit da75330
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
Expand Up @@ -60,6 +60,20 @@ public abstract class AbstractJarWriter implements LoaderClassesWriter {

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

private Layers layers;

private LayersIndex layersIndex;

/**
* Update this writer to use specific layers.
* @param layers the layers to use
* @param layersIndex the layers index to update
*/
void useLayers(Layers layers, LayersIndex layersIndex) {
this.layers = layers;
this.layersIndex = layersIndex;
}

/**
* Write the specified manifest.
* @param manifest the manifest to write
Expand Down Expand Up @@ -89,7 +103,7 @@ final void writeEntries(JarFile jarFile, EntryTransformer entryTransformer, Unpa
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream);
JarArchiveEntry transformedEntry = entryTransformer.transform(entry);
if (transformedEntry != null) {
writeEntry(transformedEntry, entryWriter, unpackHandler);
writeEntry(transformedEntry, entryWriter, unpackHandler, true);
}
}
}
Expand Down Expand Up @@ -144,7 +158,15 @@ public void writeNestedLibrary(String location, Library library) throws IOExcept
entry.setTime(getNestedLibraryTime(library));
new CrcAndSize(library::openStream).setupStoredEntry(entry);
try (InputStream inputStream = library.openStream()) {
writeEntry(entry, new InputStreamEntryWriter(inputStream), new LibraryUnpackHandler(library));
writeEntry(entry, new InputStreamEntryWriter(inputStream), new LibraryUnpackHandler(library), false);
updateLayerIndex(entry.getName(), library);
}
}

private void updateLayerIndex(String name, Library library) {
if (this.layers != null) {
Layer layer = this.layers.getLayer(library);
this.layersIndex.add(layer, name);
}
}

Expand Down Expand Up @@ -225,7 +247,7 @@ private boolean isClassEntry(JarEntry entry) {
}

private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter) throws IOException {
writeEntry(entry, entryWriter, UnpackHandler.NEVER);
writeEntry(entry, entryWriter, UnpackHandler.NEVER, true);
}

/**
Expand All @@ -234,10 +256,11 @@ private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter) throws I
* @param entry the entry to write
* @param entryWriter the entry writer or {@code null} if there is no content
* @param unpackHandler handles possible unpacking for the entry
* @param updateLayerIndex if the layer index should be updated
* @throws IOException in case of I/O errors
*/
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHandler unpackHandler)
throws IOException {
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHandler unpackHandler,
boolean updateLayerIndex) throws IOException {
String name = entry.getName();
writeParentDirectoryEntries(name);
if (this.writtenEntries.add(name)) {
Expand All @@ -248,18 +271,28 @@ private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHa
entry.setSize(entryWriter.size());
}
entryWriter = addUnpackCommentIfNecessary(entry, entryWriter, unpackHandler);
if (updateLayerIndex) {
updateLayerIndex(entry);
}
writeToArchive(entry, entryWriter);
}
}

private void updateLayerIndex(JarArchiveEntry entry) {
if (this.layers != null && !entry.getName().endsWith("/")) {
Layer layer = this.layers.getLayer(entry.getName());
this.layersIndex.add(layer, entry.getName());
}
}

protected abstract void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException;

private void writeParentDirectoryEntries(String name) throws IOException {
String parent = name.endsWith("/") ? name.substring(0, name.length() - 1) : name;
while (parent.lastIndexOf('/') != -1) {
parent = parent.substring(0, parent.lastIndexOf('/'));
if (!parent.isEmpty()) {
writeEntry(new JarArchiveEntry(parent + "/"), null, UnpackHandler.NEVER);
writeEntry(new JarArchiveEntry(parent + "/"), null, UnpackHandler.NEVER, false);
}
}
}
Expand Down
Expand Up @@ -29,7 +29,6 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;

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

Expand Down Expand Up @@ -168,25 +167,18 @@ protected final boolean isAlreadyPackaged(File file) throws IOException {
protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWriter writer) throws IOException {
Assert.notNull(libraries, "Libraries must not be null");
WritableLibraries writeableLibraries = new WritableLibraries(libraries);
if (isLayersEnabled()) {
writer = new LayerTrackingEntryWriter(writer);
if (isLayered()) {
writer.useLayers(this.layers, this.layersIndex);
}
writer.writeManifest(buildManifest(sourceJar));
writeLoaderClasses(writer);
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
writeableLibraries.write(writer);
if (isLayersEnabled()) {
if (isLayered()) {
writeLayerIndex(writer);
}
}

private boolean isLayersEnabled() {
if (!(getLayout() instanceof Layouts.Jar)) {
return false;
}
return this.layers != null;
}

private void writeLoaderClasses(AbstractJarWriter writer) throws IOException {
Layout layout = getLayout();
if (layout instanceof CustomLoaderLayout) {
Expand Down Expand Up @@ -339,7 +331,7 @@ private void addBootBootAttributesForRepackagingLayout(Attributes attributes, Re
attributes.putValue(BOOT_CLASSES_ATTRIBUTE, layout.getRepackagedClassesLocation());
putIfHasLength(attributes, BOOT_LIB_ATTRIBUTE, getLayout().getLibraryLocation("", LibraryScope.COMPILE));
putIfHasLength(attributes, BOOT_CLASSPATH_INDEX_ATTRIBUTE, layout.getClasspathIndexFileLocation());
if (isLayersEnabled()) {
if (isLayered()) {
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layout.getLayersIndexFileLocation());
}
}
Expand All @@ -355,6 +347,10 @@ private void putIfHasLength(Attributes attributes, String name, String value) {
}
}

private boolean isLayered() {
return this.layers != null && getLayout() instanceof Layouts.Jar;
}

/**
* Callback interface used to present a warning when finding the main class takes too
* long.
Expand Down Expand Up @@ -429,35 +425,6 @@ private boolean isTransformable(JarArchiveEntry entry) {

}

/**
* Decorator to track the layers as entries are written.
*/
private final class LayerTrackingEntryWriter extends AbstractJarWriter {

private final AbstractJarWriter writer;

private LayerTrackingEntryWriter(AbstractJarWriter writer) {
this.writer = writer;
}

@Override
public void writeNestedLibrary(String location, Library library) throws IOException {
this.writer.writeNestedLibrary(location, library);
Layer layer = Packager.this.layers.getLayer(library);
Packager.this.layersIndex.add(layer, location + library.getName());
}

@Override
protected void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException {
this.writer.writeToArchive(entry, entryWriter);
if (!entry.getName().endsWith("/")) {
Layer layer = Packager.this.layers.getLayer(entry.getName());
Packager.this.layersIndex.add(layer, entry.getName());
}
}

}

/**
* An {@link UnpackHandler} that determines that an entry needs to be unpacked if a
* library that requires unpacking has a matching entry name.
Expand All @@ -472,7 +439,7 @@ private final class WritableLibraries implements UnpackHandler {
addLibrary(library);
}
});
if (isLayersEnabled() && Packager.this.includeRelevantJarModeJars) {
if (isLayered() && Packager.this.includeRelevantJarModeJars) {
addLibrary(JarModeLibrary.LAYER_TOOLS);
}
}
Expand Down

0 comments on commit da75330

Please sign in to comment.