Skip to content

Commit a839477

Browse files
committed
Explicit check for duplicates in addClassPathManifestEntries
Issue: SPR-15989 (cherry picked from commit 9d8e3d4)
1 parent 53a9697 commit a839477

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ protected void addAllClassLoaderJarRoots(ClassLoader classLoader, Set<Resource>
368368
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
369369
try {
370370
UrlResource jarResource = new UrlResource(
371-
ResourceUtils.JAR_URL_PREFIX + url.toString() + ResourceUtils.JAR_URL_SEPARATOR);
371+
ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR);
372372
if (jarResource.exists()) {
373373
result.add(jarResource);
374374
}
@@ -420,11 +420,11 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
420420
for (String path : StringUtils.delimitedListToStringArray(
421421
javaClassPathProperty, System.getProperty("path.separator"))) {
422422
try {
423-
File file = new File(path);
423+
String filePath = new File(path).getAbsolutePath();
424424
UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX +
425-
ResourceUtils.FILE_URL_PREFIX + file.getAbsolutePath() +
426-
ResourceUtils.JAR_URL_SEPARATOR);
427-
if (jarResource.exists()) {
425+
ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
426+
// Potentially overlapping with URLClassLoader.getURLs() result above!
427+
if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
428428
result.add(jarResource);
429429
}
430430
}
@@ -443,6 +443,29 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
443443
}
444444
}
445445

446+
/**
447+
* Check whether the given file path has a duplicate but differently structured entry
448+
* in the existing result, i.e. with or without a leading slash.
449+
* @param filePath the file path (with or without a leading slash)
450+
* @param result the current result
451+
* @return {@code true} if there is a duplicate (i.e. to ignore the given file path),
452+
* {@code false} to proceed with adding a corresponding resource to the current result
453+
*/
454+
private boolean hasDuplicate(String filePath, Set<Resource> result) {
455+
if (result.isEmpty()) {
456+
return false;
457+
}
458+
String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath);
459+
try {
460+
return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX +
461+
duplicatePath + ResourceUtils.JAR_URL_SEPARATOR));
462+
}
463+
catch (MalformedURLException ex) {
464+
// Ignore: just for testing against duplicate.
465+
return false;
466+
}
467+
}
468+
446469
/**
447470
* Find all resources that match the given location pattern via the
448471
* Ant-style PathMatcher. Supports resources in jar files and zip files

0 commit comments

Comments
 (0)