From c27d678b814cbe62dcd0d3b462cb2d055f13948c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 29 Sep 2017 11:45:57 +0100 Subject: [PATCH] Improve handling of absolute URLs in Class-Path manifest attribute Closes gh-10268 --- .../boot/devtools/restart/ChangeableUrls.java | 20 ++++++++----------- .../devtools/restart/ChangeableUrlsTests.java | 7 ++++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java index d0b122d8ce..cc8ca4ffba 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java @@ -104,7 +104,7 @@ final class ChangeableUrls implements Iterable { return Collections.emptyList(); } try { - return getUrlsFromManifestClassPathAttribute(jarFile); + return getUrlsFromManifestClassPathAttribute(url, jarFile); } catch (IOException ex) { throw new IllegalStateException( @@ -126,8 +126,8 @@ final class ChangeableUrls implements Iterable { return null; } - private static List getUrlsFromManifestClassPathAttribute(JarFile jarFile) - throws IOException { + private static List getUrlsFromManifestClassPathAttribute(URL jarUrl, + JarFile jarFile) throws IOException { Manifest manifest = jarFile.getManifest(); if (manifest == null) { return Collections.emptyList(); @@ -138,17 +138,13 @@ final class ChangeableUrls implements Iterable { return Collections.emptyList(); } String[] entries = StringUtils.delimitedListToStringArray(classPath, " "); - List urls = new ArrayList(entries.length); - File parent = new File(jarFile.getName()).getParentFile(); - List nonExistentEntries = new ArrayList(); + List urls = new ArrayList<>(entries.length); + List nonExistentEntries = new ArrayList<>(); for (String entry : entries) { try { - File referenced = new File(entry); - if (!referenced.isAbsolute()) { - referenced = new File(parent, entry); - } - if (referenced.exists()) { - urls.add(referenced.toURI().toURL()); + URL referenced = new URL(jarUrl, entry); + if (new File(referenced.getFile()).exists()) { + urls.add(referenced); } else { nonExistentEntries.add(referenced); diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ChangeableUrlsTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ChangeableUrlsTests.java index c49ddd23f6..19eca4ebc1 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ChangeableUrlsTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ChangeableUrlsTests.java @@ -75,11 +75,12 @@ public class ChangeableUrlsTests { @Test public void urlsFromJarClassPathAreConsidered() throws Exception { File relative = this.temporaryFolder.newFolder(); - File absolute = this.temporaryFolder.newFolder(); + File absoluteFile = this.temporaryFolder.newFolder(); + URL absoluteUrl = this.temporaryFolder.newFolder().toURI().toURL(); File jarWithClassPath = makeJarFileWithUrlsInManifestClassPath( "project-core/target/classes/", "project-web/target/classes/", "does-not-exist/target/classes", relative.getName() + "/", - absolute.getAbsolutePath() + "/"); + absoluteFile.getAbsolutePath() + "/", absoluteUrl); new File(jarWithClassPath.getParentFile(), "project-core/target/classes") .mkdirs(); new File(jarWithClassPath.getParentFile(), "project-web/target/classes").mkdirs(); @@ -89,7 +90,7 @@ public class ChangeableUrlsTests { assertThat(urls.toList()).containsExactly( new URL(jarWithClassPath.toURI().toURL(), "project-core/target/classes/"), new URL(jarWithClassPath.toURI().toURL(), "project-web/target/classes/"), - relative.toURI().toURL(), absolute.toURI().toURL()); + relative.toURI().toURL(), absoluteFile.toURI().toURL(), absoluteUrl); } private URL makeUrl(String name) throws IOException {