Skip to content

Commit

Permalink
Convert file: URL to FileSystemResource for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Sep 28, 2022
1 parent 508be4e commit 0e22c3d
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -765,9 +766,9 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
files.forEach(file -> {
if (getPathMatcher().match(patternPath.toString(), file.toString())) {
try {
result.add(new UrlResource(file.toUri()));
result.add(convertToResource(file.toUri()));
}
catch (MalformedURLException ex) {
catch (Exception ex) {
// ignore
}
}
Expand Down Expand Up @@ -849,14 +850,12 @@ protected Set<Resource> findAllModulePathResources(String locationPattern) throw
}

@Nullable
private static Resource findResource(ModuleReader moduleReader, String name) {
private Resource findResource(ModuleReader moduleReader, String name) {
try {
return moduleReader.find(name)
// If it's a "file:" URI, use FileSystemResource to avoid duplicates
// for the same path discovered via class-path scanning.
.map(uri -> ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ?
new FileSystemResource(uri.getPath()) :
UrlResource.from(uri))
.map(this::convertToResource)
.orElse(null);
}
catch (Exception ex) {
Expand All @@ -867,6 +866,12 @@ private static Resource findResource(ModuleReader moduleReader, String name) {
}
}

private Resource convertToResource(URI uri) {
return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ?
new FileSystemResource(uri.getPath()) :
UrlResource.from(uri);
}

private static String stripLeadingSlash(String path) {
return (path.startsWith("/") ? path.substring(1) : path);
}
Expand Down

0 comments on commit 0e22c3d

Please sign in to comment.