Skip to content

Commit b3abd25

Browse files
committed
Merge branch '2.1.x'
2 parents 45cc649 + 8a7958e commit b3abd25

File tree

2 files changed

+26
-23
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader/src

2 files changed

+26
-23
lines changed

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

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.lang.ref.SoftReference;
22-
import java.lang.reflect.Method;
2322
import java.net.MalformedURLException;
2423
import java.net.URL;
2524
import java.net.URLConnection;
@@ -58,20 +57,6 @@ public class Handler extends URLStreamHandler {
5857
private static final String[] FALLBACK_HANDLERS = {
5958
"sun.net.www.protocol.jar.Handler" };
6059

61-
private static final Method OPEN_CONNECTION_METHOD;
62-
63-
static {
64-
Method method = null;
65-
try {
66-
method = URLStreamHandler.class.getDeclaredMethod("openConnection",
67-
URL.class);
68-
}
69-
catch (Exception ex) {
70-
// Swallow and ignore
71-
}
72-
OPEN_CONNECTION_METHOD = method;
73-
}
74-
7560
private static SoftReference<Map<File, JarFile>> rootFileCache;
7661

7762
static {
@@ -159,12 +144,7 @@ private URLStreamHandler getFallbackHandler() {
159144

160145
private URLConnection openConnection(URLStreamHandler handler, URL url)
161146
throws Exception {
162-
if (OPEN_CONNECTION_METHOD == null) {
163-
throw new IllegalStateException(
164-
"Unable to invoke fallback open connection method");
165-
}
166-
OPEN_CONNECTION_METHOD.setAccessible(true);
167-
return (URLConnection) OPEN_CONNECTION_METHOD.invoke(handler, url);
147+
return new URL(null, url.toExternalForm(), handler).openConnection();
168148
}
169149

170150
@Override

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,16 @@
1616

1717
package org.springframework.boot.loader.jar;
1818

19+
import java.io.File;
1920
import java.net.MalformedURLException;
2021
import java.net.URL;
22+
import java.net.URLConnection;
2123

24+
import org.junit.Rule;
2225
import org.junit.Test;
26+
import org.junit.rules.TemporaryFolder;
27+
28+
import org.springframework.boot.loader.TestJarCreator;
2329

2430
import static org.assertj.core.api.Assertions.assertThat;
2531

@@ -30,6 +36,9 @@
3036
*/
3137
public class HandlerTests {
3238

39+
@Rule
40+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
41+
3342
private final Handler handler = new Handler();
3443

3544
@Test
@@ -170,6 +179,20 @@ public void urlWithQuery() throws MalformedURLException {
170179
"!/foo.txt?alpha");
171180
}
172181

182+
@Test
183+
public void fallbackToJdksJarUrlStreamHandler() throws Exception {
184+
File testJar = this.temporaryFolder.newFile("test.jar");
185+
TestJarCreator.createTestJar(testJar);
186+
URLConnection connection = new URL(null,
187+
"jar:file:" + testJar.getAbsolutePath() + "!/nested.jar!/", this.handler)
188+
.openConnection();
189+
assertThat(connection).isInstanceOf(JarURLConnection.class);
190+
URLConnection jdkConnection = new URL(null,
191+
"jar:file:file:" + testJar.getAbsolutePath() + "!/nested.jar!/",
192+
this.handler).openConnection();
193+
assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class);
194+
}
195+
173196
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec)
174197
throws MalformedURLException {
175198
URL standardUrl = new URL(new URL("jar:" + context), spec);

0 commit comments

Comments
 (0)