Skip to content

Commit 3531071

Browse files
committed
Polish 'Refactor ApplicationResourceLoader preferFileResolution support'
See gh-44535
1 parent 7800d24 commit 3531071

File tree

6 files changed

+43
-60
lines changed

6 files changed

+43
-60
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ApplicationResourceLoader.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static ResourceLoader get(ResourceLoader resourceLoader) {
131131
* class loader at the time this call is made.
132132
* @param resourceLoader the delegate resource loader
133133
* @param preferFileResolution if file based resolution is preferred when a suitable
134-
* {@link ResourceFilePathResolver} support the resource
134+
* {@link FilePathResolver} support the resource
135135
* @return a {@link ResourceLoader} instance
136136
* @since 3.4.1
137137
*/
@@ -160,8 +160,8 @@ private static ResourceLoader get(ResourceLoader resourceLoader, SpringFactories
160160
Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
161161
Assert.notNull(springFactoriesLoader, "'springFactoriesLoader' must not be null");
162162
List<ProtocolResolver> protocolResolvers = springFactoriesLoader.load(ProtocolResolver.class);
163-
List<ResourceFilePathResolver> filePathResolvers = (preferFileResolution)
164-
? springFactoriesLoader.load(ResourceFilePathResolver.class) : Collections.emptyList();
163+
List<FilePathResolver> filePathResolvers = (preferFileResolution)
164+
? springFactoriesLoader.load(FilePathResolver.class) : Collections.emptyList();
165165
return new ProtocolResolvingResourceLoader(resourceLoader, protocolResolvers, filePathResolvers);
166166
}
167167

@@ -188,6 +188,28 @@ static ResourceLoader get(ClassLoader classLoader) {
188188

189189
}
190190

191+
/**
192+
* Strategy interface registered in {@code spring.factories} and used by
193+
* {@link ApplicationResourceLoader} to determine the file path of loaded resource
194+
* when it can also be represented as a {@link FileSystemResource}.
195+
*
196+
* @author Phillip Webb
197+
* @since 3.4.5
198+
*/
199+
public interface FilePathResolver {
200+
201+
/**
202+
* Return the {@code path} of the given resource if it can also be represented as
203+
* a {@link FileSystemResource}.
204+
* @param location the location used to create the resource
205+
* @param resource the resource to check
206+
* @return the file path of the resource or {@code null} if the it is not possible
207+
* to represent the resource as a {@link FileSystemResource}.
208+
*/
209+
String resolveFilePath(String location, Resource resource);
210+
211+
}
212+
191213
/**
192214
* An application {@link Resource}.
193215
*/
@@ -214,10 +236,10 @@ private static class ProtocolResolvingResourceLoader implements ResourceLoader {
214236

215237
private final List<ProtocolResolver> protocolResolvers;
216238

217-
private final List<ResourceFilePathResolver> filePathResolvers;
239+
private final List<FilePathResolver> filePathResolvers;
218240

219241
ProtocolResolvingResourceLoader(ResourceLoader resourceLoader, List<ProtocolResolver> protocolResolvers,
220-
List<ResourceFilePathResolver> filePathResolvers) {
242+
List<FilePathResolver> filePathResolvers) {
221243
this.resourceLoader = resourceLoader;
222244
this.protocolResolvers = protocolResolvers;
223245
this.filePathResolvers = filePathResolvers;
@@ -239,12 +261,12 @@ public Resource getResource(String location) {
239261
}
240262
}
241263
Resource resource = this.resourceLoader.getResource(location);
242-
String fileSystemPath = getFileSystemPath(location, resource);
243-
return (fileSystemPath != null) ? new ApplicationResource(fileSystemPath) : resource;
264+
String filePath = getFilePath(location, resource);
265+
return (filePath != null) ? new ApplicationResource(filePath) : resource;
244266
}
245267

246-
private String getFileSystemPath(String location, Resource resource) {
247-
for (ResourceFilePathResolver filePathResolver : this.filePathResolvers) {
268+
private String getFilePath(String location, Resource resource) {
269+
for (FilePathResolver filePathResolver : this.filePathResolvers) {
248270
String filePath = filePathResolver.resolveFilePath(location, resource);
249271
if (filePath != null) {
250272
return filePath;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ClassPathResourceFilePathResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package org.springframework.boot.io;
1818

19+
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
1920
import org.springframework.core.io.ClassPathResource;
2021
import org.springframework.core.io.Resource;
2122
import org.springframework.core.io.ResourceLoader;
2223

2324
/**
24-
* {@link ResourceFilePathResolver} for {@link ClassPathResource}.
25+
* {@link FilePathResolver} for {@link ClassPathResource}.
2526
*
2627
* @author Phillip Webb
2728
*/
28-
class ClassPathResourceFilePathResolver implements ResourceFilePathResolver {
29+
class ClassPathResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
2930

3031
@Override
3132
public String resolveFilePath(String location, Resource resource) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ResourceFilePathResolver.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServletContextResourceFilePathResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
package org.springframework.boot.web.context;
1818

19-
import org.springframework.boot.io.ResourceFilePathResolver;
19+
import org.springframework.boot.io.ApplicationResourceLoader;
20+
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
2021
import org.springframework.core.io.Resource;
2122
import org.springframework.util.ClassUtils;
2223
import org.springframework.web.context.support.ServletContextResource;
2324

2425
/**
25-
* {@link ResourceFilePathResolver} for {@link ServletContextResource}.
26+
* {@link FilePathResolver} for {@link ServletContextResource}.
2627
*
2728
* @author Phillip Webb
2829
*/
29-
class ServletContextResourceFilePathResolver implements ResourceFilePathResolver {
30+
class ServletContextResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
3031

3132
private static final String RESOURCE_CLASS_NAME = "org.springframework.web.context.support.ServletContextResource";
3233

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/FilteredReactiveWebContextResourceFilePathResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
package org.springframework.boot.web.reactive.context;
1818

19-
import org.springframework.boot.io.ResourceFilePathResolver;
19+
import org.springframework.boot.io.ApplicationResourceLoader;
20+
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
2021
import org.springframework.core.io.Resource;
2122

2223
/**
23-
* {@link ResourceFilePathResolver} for {@link FilteredReactiveWebContextResource}.
24+
* {@link FilePathResolver} for {@link FilteredReactiveWebContextResource}.
2425
*
2526
* @author Dmytro Nosan
2627
*/
27-
class FilteredReactiveWebContextResourceFilePathResolver implements ResourceFilePathResolver {
28+
class FilteredReactiveWebContextResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
2829

2930
@Override
3031
public String resolveFilePath(String location, Resource resource) {

spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ org.springframework.core.io.ProtocolResolver=\
107107
org.springframework.boot.io.Base64ProtocolResolver
108108

109109
# Resource File Path Resolvers
110-
org.springframework.boot.io.ResourceFilePathResolver=\
110+
org.springframework.boot.io.ApplicationResourceLoader$FilePathResolver=\
111111
org.springframework.boot.io.ClassPathResourceFilePathResolver,\
112112
org.springframework.boot.web.context.ServletContextResourceFilePathResolver,\
113113
org.springframework.boot.web.reactive.context.FilteredReactiveWebContextResourceFilePathResolver

0 commit comments

Comments
 (0)