Propagate manifest to exploded jars

Update `LaunchedURLClassLoader` so that packages defined from exploded
archive folders have manifest attributes applied to them. Prior to this
calling `package.getImplementationTitle()` would only return the a
manifiest attribute when running non-exploded.

The root cause of this issue is the way that `URLClassLoader` handles
the different URL types. For URLs that reference a jar the manifest is
available. For URLs that reference a folder it isn't. When running
exploded we use a URL that references to the `BOOT-INF/classes` folder
directly. To fix the issue we now attempt to detect when `definePackage`
is being called directly, and replace `null` entries with actual
manifest values.

Fixes gh-21705
pull/21793/head
Phillip Webb 5 years ago
parent 400a168ce9
commit 9a235197dc

@ -171,11 +171,7 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
return this.archive.isExploded();
}
/**
* Return the root archive.
* @return the root archive
* @since 2.3.0
*/
@Override
protected final Archive getArchive() {
return this.archive;
}

@ -26,8 +26,11 @@ import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.function.Supplier;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.jar.Handler;
/**
@ -48,6 +51,12 @@ public class LaunchedURLClassLoader extends URLClassLoader {
private final boolean exploded;
private final Archive rootArchive;
private final Object packageLock = new Object();
private volatile DefinePackageCallType definePackageCallType;
/**
* Create a new {@link LaunchedURLClassLoader} instance.
* @param urls the URLs from which to load classes and resources
@ -64,8 +73,21 @@ public class LaunchedURLClassLoader extends URLClassLoader {
* @param parent the parent class loader for delegation
*/
public LaunchedURLClassLoader(boolean exploded, URL[] urls, ClassLoader parent) {
this(exploded, null, urls, parent);
}
/**
* Create a new {@link LaunchedURLClassLoader} instance.
* @param exploded if the underlying archive is exploded
* @param rootArchive the root archive or {@code null}
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader for delegation
* @since 2.3.1
*/
public LaunchedURLClassLoader(boolean exploded, Archive rootArchive, URL[] urls, ClassLoader parent) {
super(urls, parent);
this.exploded = exploded;
this.rootArchive = rootArchive;
}
@Override
@ -219,6 +241,58 @@ public class LaunchedURLClassLoader extends URLClassLoader {
}
}
@Override
protected Package definePackage(String name, Manifest man, URL url) throws IllegalArgumentException {
if (!this.exploded) {
return super.definePackage(name, man, url);
}
synchronized (this.packageLock) {
return doDefinePackage(DefinePackageCallType.MANIFEST, () -> super.definePackage(name, man, url));
}
}
@Override
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor,
String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException {
if (!this.exploded) {
return super.definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor,
sealBase);
}
synchronized (this.packageLock) {
if (this.definePackageCallType == null) {
// We're not part of a call chain which means that the URLClassLoader
// is trying to define a package for our exploded JAR. We use the
// manifest version to ensure package attributes are set
Manifest manifest = getManifest(this.rootArchive);
if (manifest != null) {
return definePackage(name, manifest, sealBase);
}
}
return doDefinePackage(DefinePackageCallType.ATTRIBUTES, () -> super.definePackage(name, specTitle,
specVersion, specVendor, implTitle, implVersion, implVendor, sealBase));
}
}
private Manifest getManifest(Archive archive) {
try {
return (archive != null) ? archive.getManifest() : null;
}
catch (IOException ex) {
return null;
}
}
private <T> T doDefinePackage(DefinePackageCallType type, Supplier<T> call) {
DefinePackageCallType existingType = this.definePackageCallType;
try {
this.definePackageCallType = type;
return call.get();
}
finally {
this.definePackageCallType = existingType;
}
}
/**
* Clear URL caches.
*/
@ -280,4 +354,22 @@ public class LaunchedURLClassLoader extends URLClassLoader {
}
/**
* The different types of call made to define a package. We track these for exploded
* jars so that we can detect packages that should have manifest attributes applied.
*/
private enum DefinePackageCallType {
/**
* A define package call from a resource that has a manifest.
*/
MANIFEST,
/**
* A define package call with a direct set of attributes.
*/
ATTRIBUTES
}
}

@ -94,7 +94,7 @@ public abstract class Launcher {
* @throws Exception if the classloader cannot be created
*/
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
return new LaunchedURLClassLoader(isExploded(), urls, getClass().getClassLoader());
return new LaunchedURLClassLoader(isExploded(), getArchive(), urls, getClass().getClassLoader());
}
/**
@ -175,4 +175,13 @@ public abstract class Launcher {
return true;
}
/**
* Return the root archive.
* @return the root archive
* @since 2.3.1
*/
protected Archive getArchive() {
return null;
}
}

@ -33,6 +33,7 @@ import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
@ -59,19 +60,31 @@ public abstract class AbstractExecutableArchiveLauncherTests {
@SuppressWarnings("resource")
protected File createJarArchive(String name, String entryPrefix, boolean indexed, List<String> extraLibs)
throws IOException {
return createJarArchive(name, null, entryPrefix, indexed, extraLibs);
}
@SuppressWarnings("resource")
protected File createJarArchive(String name, Manifest manifest, String entryPrefix, boolean indexed,
List<String> extraLibs) throws IOException {
File archive = new File(this.tempDir, name);
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
if (manifest != null) {
jarOutputStream.putNextEntry(new JarEntry("META-INF/"));
jarOutputStream.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));
manifest.write(jarOutputStream);
jarOutputStream.closeEntry();
}
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/"));
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/classes/"));
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/lib/"));
if (indexed) {
JarEntry indexEntry = new JarEntry(entryPrefix + "/classpath.idx");
jarOutputStream.putNextEntry(indexEntry);
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/classpath.idx"));
Writer writer = new OutputStreamWriter(jarOutputStream, StandardCharsets.UTF_8);
writer.write("- \"BOOT-INF/lib/foo.jar\"\n");
writer.write("- \"BOOT-INF/lib/bar.jar\"\n");
writer.write("- \"BOOT-INF/lib/baz.jar\"\n");
writer.flush();
jarOutputStream.closeEntry();
}
addNestedJars(entryPrefix, "/lib/foo.jar", jarOutputStream);
addNestedJars(entryPrefix, "/lib/bar.jar", jarOutputStream);

@ -24,12 +24,17 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.ExplodedArchive;
import org.springframework.boot.loader.archive.JarFileArchive;
import org.springframework.boot.testsupport.compiler.TestCompiler;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -95,6 +100,26 @@ class JarLauncherTests extends AbstractExecutableArchiveLauncherTests {
assertThat(urls).containsExactly(expectedFileUrls);
}
@Test
void explodedJarDefinedPackagesIncludeManifestAttributes() throws Exception {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Name.MANIFEST_VERSION, "1.0");
attributes.put(Name.IMPLEMENTATION_TITLE, "test");
File explodedRoot = explode(
createJarArchive("archive.jar", manifest, "BOOT-INF", true, Collections.emptyList()));
TestCompiler compiler = new TestCompiler(new File(explodedRoot, "BOOT-INF/classes"));
File source = new File(this.tempDir, "explodedsample/ExampleClass.java");
source.getParentFile().mkdirs();
FileCopyUtils.copy(new File("src/test/resources/explodedsample/ExampleClass.txt"), source);
compiler.getTask(Collections.singleton(source)).call();
JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
Class<?> loaded = classLoader.loadClass("explodedsample.ExampleClass");
assertThat(loaded.getPackage().getImplementationTitle()).isEqualTo("test");
}
protected final URL[] getExpectedFileUrls(File explodedRoot) {
return getExpectedFiles(explodedRoot).stream().map(this::toUrl).toArray(URL[]::new);
}

@ -0,0 +1,26 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package explodedsample;
/**
* Example class used to test class loading.
*
* @author Phillip Webb
*/
public class ExampleClass {
}
Loading…
Cancel
Save