From 9a235197dc7d629fd7c24c34f88eee1ac215d376 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 5 Jun 2020 15:20:27 -0700 Subject: [PATCH] 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 --- .../loader/ExecutableArchiveLauncher.java | 6 +- .../boot/loader/LaunchedURLClassLoader.java | 92 +++++++++++++++++++ .../springframework/boot/loader/Launcher.java | 11 ++- ...bstractExecutableArchiveLauncherTests.java | 17 +++- .../boot/loader/JarLauncherTests.java | 25 +++++ .../resources/explodedsample/ExampleClass.txt | 26 ++++++ 6 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/resources/explodedsample/ExampleClass.txt diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExecutableArchiveLauncher.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExecutableArchiveLauncher.java index 95947250c6..07fad13e0b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExecutableArchiveLauncher.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExecutableArchiveLauncher.java @@ -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; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java index bf5ec924d8..75ac508150 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java @@ -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 doDefinePackage(DefinePackageCallType type, Supplier 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 + + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java index 57085dc454..7be83a3153 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java @@ -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; + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/AbstractExecutableArchiveLauncherTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/AbstractExecutableArchiveLauncherTests.java index 84a05eb25c..a461ba1f1d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/AbstractExecutableArchiveLauncherTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/AbstractExecutableArchiveLauncherTests.java @@ -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 extraLibs) throws IOException { + return createJarArchive(name, null, entryPrefix, indexed, extraLibs); + } + + @SuppressWarnings("resource") + protected File createJarArchive(String name, Manifest manifest, String entryPrefix, boolean indexed, + List 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); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/JarLauncherTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/JarLauncherTests.java index 0b265f3a3e..59cc53d2a2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/JarLauncherTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/JarLauncherTests.java @@ -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 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); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/resources/explodedsample/ExampleClass.txt b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/resources/explodedsample/ExampleClass.txt new file mode 100644 index 0000000000..c53100f90f --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/resources/explodedsample/ExampleClass.txt @@ -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 { + +}