Prevent Tomcat URL "reflective access" warnings

Update the jar `Handler` class to support a non-reflective fallback
mechanism when possible. The updated code attempts to capture a regular
jar URL before our handler is installed. It can then use that URL as
context when creating the a fallback URL. The JDK jar `Handler` will
be copied from the context URL to the fallback URL.

Without this commit, resolving new Tomcat URLs of the form
`jar:war:file:...` would result in an ugly "Illegal reflective access"
warning.

Fixes gh-18631
This commit is contained in:
Phillip Webb
2020-12-17 17:45:44 -08:00
parent 361198ebba
commit c4e41305d5
9 changed files with 288 additions and 10 deletions

View File

@@ -0,0 +1,66 @@
/*
* 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 org.springframework.boot.loader;
import java.io.File;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests loader that supports fat jars.
*
* @author Phillip Webb
*/
@Testcontainers(disabledWithoutDocker = true)
class LoaderIntegrationTests {
private static final DockerImageName JRE = DockerImageName.parse("adoptopenjdk:15-jre-hotspot");
private static ToStringConsumer output = new ToStringConsumer();
@Container
public static GenericContainer<?> container = new GenericContainer<>(JRE).withLogConsumer(output)
.withCopyFileToContainer(MountableFile.forHostPath(findApplication().toPath()), "/app.jar")
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5)))
.withCommand("java", "-jar", "app.jar");
private static File findApplication() {
File appJar = new File("build/app/build/libs/app.jar");
if (appJar.isFile()) {
return appJar;
}
throw new IllegalStateException(
"Could not find test application in build/app/build/libs directory. Have you built it?");
}
@Test
void readUrlsWithoutWarning() {
assertThat(output.toUtf8String()).contains(">>>>> 287649 BYTES from").doesNotContain("WARNING:")
.doesNotContain("illegal");
}
}