Skip to content

Commit

Permalink
Explicit check for duplicates in addClassPathManifestEntries
Browse files Browse the repository at this point in the history
Issue: SPR-15989
  • Loading branch information
jhoeller committed Sep 26, 2017
1 parent 95b83fe commit 9d8e3d4
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<
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);
}
Expand Down Expand Up @@ -423,11 +423,11 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
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);
}
}
Expand All @@ -446,6 +446,29 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
}
}

/**
* 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<Resource> 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
Expand Down

0 comments on commit 9d8e3d4

Please sign in to comment.