From 9d8e3d4185b9f1158de31c4f9a53f2a036a041df Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 26 Sep 2017 11:49:49 +0200 Subject: [PATCH] Explicit check for duplicates in addClassPathManifestEntries Issue: SPR-15989 --- .../PathMatchingResourcePatternResolver.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 1500b179d3..a485a5b10f 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -371,7 +371,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol for (URL url : ((URLClassLoader) classLoader).getURLs()) { try { UrlResource jarResource = new UrlResource( - ResourceUtils.JAR_URL_PREFIX + url.toString() + ResourceUtils.JAR_URL_SEPARATOR); + ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR); if (jarResource.exists()) { result.add(jarResource); } @@ -423,11 +423,11 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol for (String path : StringUtils.delimitedListToStringArray( javaClassPathProperty, System.getProperty("path.separator"))) { try { - File file = new File(path); + String filePath = new File(path).getAbsolutePath(); UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + - ResourceUtils.FILE_URL_PREFIX + file.getAbsolutePath() + - ResourceUtils.JAR_URL_SEPARATOR); - if (jarResource.exists()) { + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR); + // Potentially overlapping with URLClassLoader.getURLs() result above! + if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) { result.add(jarResource); } } @@ -446,6 +446,29 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol } } + /** + * Check whether the given file path has a duplicate but differently structured entry + * in the existing result, i.e. with or without a leading slash. + * @param filePath the file path (with or without a leading slash) + * @param result the current result + * @return {@code true} if there is a duplicate (i.e. to ignore the given file path), + * {@code false} to proceed with adding a corresponding resource to the current result + */ + private boolean hasDuplicate(String filePath, Set result) { + if (result.isEmpty()) { + return false; + } + String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath); + try { + return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + + duplicatePath + ResourceUtils.JAR_URL_SEPARATOR)); + } + catch (MalformedURLException ex) { + // Ignore: just for testing against duplicate. + return false; + } + } + /** * Find all resources that match the given location pattern via the * Ant-style PathMatcher. Supports resources in jar files and zip files