diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index 5b23b677bf6..f2623441b55 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -19,7 +19,6 @@ package org.springframework.boot.loader.jar; import java.io.File; import java.io.IOException; import java.lang.ref.SoftReference; -import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -58,20 +57,6 @@ public class Handler extends URLStreamHandler { private static final String[] FALLBACK_HANDLERS = { "sun.net.www.protocol.jar.Handler" }; - private static final Method OPEN_CONNECTION_METHOD; - - static { - Method method = null; - try { - method = URLStreamHandler.class.getDeclaredMethod("openConnection", - URL.class); - } - catch (Exception ex) { - // Swallow and ignore - } - OPEN_CONNECTION_METHOD = method; - } - private static SoftReference> rootFileCache; static { @@ -159,12 +144,7 @@ public class Handler extends URLStreamHandler { private URLConnection openConnection(URLStreamHandler handler, URL url) throws Exception { - if (OPEN_CONNECTION_METHOD == null) { - throw new IllegalStateException( - "Unable to invoke fallback open connection method"); - } - OPEN_CONNECTION_METHOD.setAccessible(true); - return (URLConnection) OPEN_CONNECTION_METHOD.invoke(handler, url); + return new URL(null, url.toExternalForm(), handler).openConnection(); } @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java index aedbc9d7292..6076146564a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -16,10 +16,16 @@ package org.springframework.boot.loader.jar; +import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import org.springframework.boot.loader.TestJarCreator; import static org.assertj.core.api.Assertions.assertThat; @@ -30,6 +36,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class HandlerTests { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private final Handler handler = new Handler(); @Test @@ -170,6 +179,20 @@ public class HandlerTests { "!/foo.txt?alpha"); } + @Test + public void fallbackToJdksJarUrlStreamHandler() throws Exception { + File testJar = this.temporaryFolder.newFile("test.jar"); + TestJarCreator.createTestJar(testJar); + URLConnection connection = new URL(null, + "jar:file:" + testJar.getAbsolutePath() + "!/nested.jar!/", this.handler) + .openConnection(); + assertThat(connection).isInstanceOf(JarURLConnection.class); + URLConnection jdkConnection = new URL(null, + "jar:file:file:" + testJar.getAbsolutePath() + "!/nested.jar!/", + this.handler).openConnection(); + assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class); + } + private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException { URL standardUrl = new URL(new URL("jar:" + context), spec);