Skip to content

Commit 20df899

Browse files
committed
Merge pull request #6759 from hengyunab
* gh-6759: Polish “Avoid null handler package in JarFile protocol handler registration” Avoid null handler package in JarFile protocol handler registration
2 parents 63b8e82 + eb1c349 commit 20df899

File tree

2 files changed

+43
-2
lines changed
  • spring-boot-tools/spring-boot-loader/src

2 files changed

+43
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -449,7 +449,7 @@ public String getName() {
449449
* {@link URLStreamHandler} will be located to deal with jar URLs.
450450
*/
451451
public static void registerUrlProtocolHandler() {
452-
String handlers = System.getProperty(PROTOCOL_HANDLER);
452+
String handlers = System.getProperty(PROTOCOL_HANDLER, "");
453453
System.setProperty(PROTOCOL_HANDLER, ("".equals(handlers) ? HANDLERS_PACKAGE
454454
: handlers + "|" + HANDLERS_PACKAGE));
455455
resetCachedUrlHandlers();

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
* @author Andy Wilkinson
6464
*/
6565
public class JarFileTests {
66+
private static final String PROTOCOL_HANDLER = "java.protocol.handler.pkgs";
67+
68+
private static final String HANDLERS_PACKAGE = "org.springframework.boot.loader";
6669

6770
@Rule
6871
public ExpectedException thrown = ExpectedException.none();
@@ -453,4 +456,42 @@ public void cannotLoadMissingJar() throws Exception {
453456
url.openConnection().getInputStream();
454457
}
455458

459+
@Test
460+
public void registerUrlProtocolHandlerWithNoExistingRegistration() {
461+
String original = System.getProperty(PROTOCOL_HANDLER);
462+
try {
463+
System.clearProperty(PROTOCOL_HANDLER);
464+
JarFile.registerUrlProtocolHandler();
465+
String protocolHandler = System.getProperty(PROTOCOL_HANDLER);
466+
assertThat(protocolHandler, equalTo(HANDLERS_PACKAGE));
467+
}
468+
finally {
469+
if (original == null) {
470+
System.clearProperty(PROTOCOL_HANDLER);
471+
}
472+
else {
473+
System.setProperty(PROTOCOL_HANDLER, original);
474+
}
475+
}
476+
}
477+
478+
@Test
479+
public void registerUrlProtocolHandlerAddsToExistingRegistration() {
480+
String original = System.getProperty(PROTOCOL_HANDLER);
481+
try {
482+
System.setProperty(PROTOCOL_HANDLER, "com.example");
483+
JarFile.registerUrlProtocolHandler();
484+
String protocolHandler = System.getProperty(PROTOCOL_HANDLER);
485+
assertThat(protocolHandler, equalTo("com.example|" + HANDLERS_PACKAGE));
486+
}
487+
finally {
488+
if (original == null) {
489+
System.clearProperty(PROTOCOL_HANDLER);
490+
}
491+
else {
492+
System.setProperty(PROTOCOL_HANDLER, original);
493+
}
494+
}
495+
}
496+
456497
}

0 commit comments

Comments
 (0)