Skip to content

Commit c4e4130

Browse files
committed
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
1 parent 361198e commit c4e4130

File tree

9 files changed

+287
-9
lines changed

9 files changed

+287
-9
lines changed

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ include "spring-boot-project:spring-boot-test-autoconfigure"
7171
include "spring-boot-tests:spring-boot-deployment-tests"
7272
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-configuration-processor-tests"
7373
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-launch-script-tests"
74+
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-loader-tests"
7475
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-server-tests"
7576

7677
file("${rootDir}/spring-boot-project/spring-boot-starters").eachDirMatch(~/spring-boot-starter.*/) {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ public class Handler extends URLStreamHandler {
5757

5858
private static final String PARENT_DIR = "/../";
5959

60+
private static final String PROTOCOL_HANDLER = "java.protocol.handler.pkgs";
61+
6062
private static final String[] FALLBACK_HANDLERS = { "sun.net.www.protocol.jar.Handler" };
6163

64+
private static URL jarContextUrl;
65+
6266
private static SoftReference<Map<File, JarFile>> rootFileCache;
6367

6468
static {
@@ -98,7 +102,8 @@ private boolean isUrlInJarFile(URL url, JarFile jarFile) throws MalformedURLExce
98102

99103
private URLConnection openFallbackConnection(URL url, Exception reason) throws IOException {
100104
try {
101-
return openConnection(getFallbackHandler(), url);
105+
URLConnection connection = openFallbackContextConnection(url);
106+
return (connection != null) ? connection : openFallbackHandlerConnection(url);
102107
}
103108
catch (Exception ex) {
104109
if (reason instanceof IOException) {
@@ -113,16 +118,35 @@ private URLConnection openFallbackConnection(URL url, Exception reason) throws I
113118
}
114119
}
115120

116-
private void log(boolean warning, String message, Exception cause) {
121+
/**
122+
* Attempt to open a fallback connection by using a context URL captured before the
123+
* jar handler was replaced with our own version. Since this method doesn't use
124+
* reflection it won't trigger "illegal reflective access operation has occurred"
125+
* warnings on Java 13+.
126+
* @param url the URL to open
127+
* @return a {@link URLConnection} or {@code null}
128+
*/
129+
private URLConnection openFallbackContextConnection(URL url) {
117130
try {
118-
Level level = warning ? Level.WARNING : Level.FINEST;
119-
Logger.getLogger(getClass().getName()).log(level, message, cause);
131+
if (jarContextUrl != null) {
132+
return new URL(jarContextUrl, url.toExternalForm()).openConnection();
133+
}
120134
}
121135
catch (Exception ex) {
122-
if (warning) {
123-
System.err.println("WARNING: " + message);
124-
}
125136
}
137+
return null;
138+
}
139+
140+
/**
141+
* Attempt to open a fallback connection by using reflection to access Java's default
142+
* jar {@link URLStreamHandler}.
143+
* @param url the URL to open
144+
* @return the {@link URLConnection}
145+
* @throws Exception if not connection could be opened
146+
*/
147+
private URLConnection openFallbackHandlerConnection(URL url) throws Exception {
148+
URLStreamHandler fallbackHandler = getFallbackHandler();
149+
return new URL(null, url.toExternalForm(), fallbackHandler).openConnection();
126150
}
127151

128152
private URLStreamHandler getFallbackHandler() {
@@ -142,8 +166,16 @@ private URLStreamHandler getFallbackHandler() {
142166
throw new IllegalStateException("Unable to find fallback handler");
143167
}
144168

145-
private URLConnection openConnection(URLStreamHandler handler, URL url) throws Exception {
146-
return new URL(null, url.toExternalForm(), handler).openConnection();
169+
private void log(boolean warning, String message, Exception cause) {
170+
try {
171+
Level level = warning ? Level.WARNING : Level.FINEST;
172+
Logger.getLogger(getClass().getName()).log(level, message, cause);
173+
}
174+
catch (Exception ex) {
175+
if (warning) {
176+
System.err.println("WARNING: " + message);
177+
}
178+
}
147179
}
148180

149181
@Override
@@ -333,6 +365,53 @@ static void addToRootFileCache(File sourceFile, JarFile jarFile) {
333365
cache.put(sourceFile, jarFile);
334366
}
335367

368+
/**
369+
* If possible, capture a URL that is configured with the original jar handler so that
370+
* we can use it as a fallback context later. We can only do this if we know that we
371+
* can reset the handlers after.
372+
*/
373+
static void captureJarContextUrl() {
374+
if (canResetCachedUrlHandlers()) {
375+
String handlers = System.getProperty(PROTOCOL_HANDLER, "");
376+
try {
377+
System.clearProperty(PROTOCOL_HANDLER);
378+
try {
379+
resetCachedUrlHandlers();
380+
jarContextUrl = new URL("jar:file:context.jar!/");
381+
URLConnection connection = jarContextUrl.openConnection();
382+
if (connection instanceof JarURLConnection) {
383+
jarContextUrl = null;
384+
}
385+
}
386+
catch (Exception ex) {
387+
}
388+
}
389+
finally {
390+
if (handlers == null) {
391+
System.clearProperty(PROTOCOL_HANDLER);
392+
}
393+
else {
394+
System.setProperty(PROTOCOL_HANDLER, handlers);
395+
}
396+
}
397+
resetCachedUrlHandlers();
398+
}
399+
}
400+
401+
private static boolean canResetCachedUrlHandlers() {
402+
try {
403+
resetCachedUrlHandlers();
404+
return true;
405+
}
406+
catch (Error ex) {
407+
return false;
408+
}
409+
}
410+
411+
private static void resetCachedUrlHandlers() {
412+
URL.setURLStreamHandlerFactory(null);
413+
}
414+
336415
/**
337416
* Set if a generic static exception can be thrown when a URL cannot be connected.
338417
* This optimization is used during class loading to save creating lots of exceptions

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ JarFileType getType() {
411411
* {@link URLStreamHandler} will be located to deal with jar URLs.
412412
*/
413413
public static void registerUrlProtocolHandler() {
414+
Handler.captureJarContextUrl();
414415
String handlers = System.getProperty(PROTOCOL_HANDLER, "");
415416
System.setProperty(PROTOCOL_HANDLER,
416417
("".equals(handlers) ? HANDLERS_PACKAGE : handlers + "|" + HANDLERS_PACKAGE));

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ void fallbackToJdksJarUrlStreamHandler(@TempDir File tempDir) throws Exception {
163163
URLConnection jdkConnection = new URL(null, "jar:file:" + testJar.toURI().toURL() + "!/nested.jar!/",
164164
this.handler).openConnection();
165165
assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class);
166+
assertThat(jdkConnection.getClass().getName()).endsWith(".JarURLConnection");
166167
}
167168

168169
@Test
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot"
4+
}
5+
6+
apply plugin: "io.spring.dependency-management"
7+
8+
repositories {
9+
maven { url "file:${rootDir}/../int-test-maven-repository"}
10+
mavenCentral()
11+
maven { url "https://repo.spring.io/snapshot" }
12+
maven { url "https://repo.spring.io/milestone" }
13+
}
14+
15+
dependencies {
16+
implementation("org.springframework.boot:spring-boot-starter-web")
17+
implementation("org.webjars:jquery:3.5.0")
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pluginManagement {
2+
repositories {
3+
maven { url "file:${rootDir}/../int-test-maven-repository"}
4+
mavenCentral()
5+
maven { url "https://repo.spring.io/snapshot" }
6+
maven { url "https://repo.spring.io/milestone" }
7+
}
8+
resolutionStrategy {
9+
eachPlugin {
10+
if (requested.id.id == "org.springframework.boot") {
11+
useModule "org.springframework.boot:spring-boot-gradle-plugin:${requested.version}"
12+
}
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loaderapp;
18+
19+
import java.net.URL;
20+
import java.util.Arrays;
21+
22+
import javax.servlet.ServletContext;
23+
24+
import org.springframework.boot.CommandLineRunner;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.autoconfigure.SpringBootApplication;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.util.FileCopyUtils;
29+
30+
@SpringBootApplication
31+
public class LoaderTestApplication {
32+
33+
@Bean
34+
public CommandLineRunner commandLineRunner(ServletContext servletContext) {
35+
return (args) -> {
36+
URL resourceUrl = servletContext.getResource("webjars/jquery/3.5.0/jquery.js");
37+
byte[] resourceContent = FileCopyUtils.copyToByteArray(resourceUrl.openStream());
38+
URL directUrl = new URL(resourceUrl.toExternalForm());
39+
byte[] directContent = FileCopyUtils.copyToByteArray(directUrl.openStream());
40+
String message = (!Arrays.equals(resourceContent, directContent)) ? "NO MATCH"
41+
: directContent.length + " BYTES";
42+
System.out.println(">>>>> " + message + " from " + resourceUrl);
43+
};
44+
}
45+
46+
public static void main(String[] args) {
47+
SpringApplication.run(LoaderTestApplication.class, args).stop();
48+
}
49+
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
id "org.springframework.boot.integration-test"
5+
}
6+
7+
description = "Spring Boot Loader Integration Tests"
8+
9+
configurations {
10+
app
11+
}
12+
13+
dependencies {
14+
app project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository")
15+
app project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "mavenRepository")
16+
app project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-web", configuration: "mavenRepository")
17+
18+
intTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent")))
19+
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
20+
intTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
21+
intTestImplementation("org.testcontainers:junit-jupiter")
22+
intTestImplementation("org.testcontainers:testcontainers")
23+
}
24+
25+
task syncMavenRepository(type: Sync) {
26+
from configurations.app
27+
into "${buildDir}/int-test-maven-repository"
28+
}
29+
30+
task syncAppSource(type: Sync) {
31+
from "app"
32+
into "${buildDir}/app"
33+
filter { line ->
34+
line.replace("id \"org.springframework.boot\"", "id \"org.springframework.boot\" version \"${project.version}\"")
35+
}
36+
}
37+
38+
task buildApp(type: GradleBuild) {
39+
dependsOn syncAppSource, syncMavenRepository
40+
dir = "${buildDir}/app"
41+
startParameter.buildCacheEnabled = false
42+
tasks = ["build"]
43+
}
44+
45+
intTest {
46+
dependsOn buildApp
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader;
18+
19+
import java.io.File;
20+
import java.time.Duration;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.testcontainers.containers.GenericContainer;
24+
import org.testcontainers.containers.output.ToStringConsumer;
25+
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
import org.testcontainers.utility.DockerImageName;
29+
import org.testcontainers.utility.MountableFile;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Integration tests loader that supports fat jars.
35+
*
36+
* @author Phillip Webb
37+
*/
38+
@Testcontainers(disabledWithoutDocker = true)
39+
class LoaderIntegrationTests {
40+
41+
private static final DockerImageName JRE = DockerImageName.parse("adoptopenjdk:15-jre-hotspot");
42+
43+
private static ToStringConsumer output = new ToStringConsumer();
44+
45+
@Container
46+
public static GenericContainer<?> container = new GenericContainer<>(JRE).withLogConsumer(output)
47+
.withCopyFileToContainer(MountableFile.forHostPath(findApplication().toPath()), "/app.jar")
48+
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5)))
49+
.withCommand("java", "-jar", "app.jar");
50+
51+
private static File findApplication() {
52+
File appJar = new File("build/app/build/libs/app.jar");
53+
if (appJar.isFile()) {
54+
return appJar;
55+
}
56+
throw new IllegalStateException(
57+
"Could not find test application in build/app/build/libs directory. Have you built it?");
58+
}
59+
60+
@Test
61+
void readUrlsWithoutWarning() {
62+
assertThat(output.toUtf8String()).contains(">>>>> 287649 BYTES from").doesNotContain("WARNING:")
63+
.doesNotContain("illegal");
64+
}
65+
66+
}

0 commit comments

Comments
 (0)