Browse Source

Polish 'Refactor ApplicationResourceLoader `preferFileResolution` support'

See gh-44535
pull/45582/head
Phillip Webb 8 months ago
parent
commit
35310716ab
  1. 40
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ApplicationResourceLoader.java
  2. 5
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ClassPathResourceFilePathResolver.java
  3. 42
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/io/ResourceFilePathResolver.java
  4. 7
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServletContextResourceFilePathResolver.java
  5. 7
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/FilteredReactiveWebContextResourceFilePathResolver.java
  6. 2
      spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

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

@ -131,7 +131,7 @@ public class ApplicationResourceLoader extends DefaultResourceLoader { @@ -131,7 +131,7 @@ public class ApplicationResourceLoader extends DefaultResourceLoader {
* class loader at the time this call is made.
* @param resourceLoader the delegate resource loader
* @param preferFileResolution if file based resolution is preferred when a suitable
* {@link ResourceFilePathResolver} support the resource
* {@link FilePathResolver} support the resource
* @return a {@link ResourceLoader} instance
* @since 3.4.1
*/
@ -160,8 +160,8 @@ public class ApplicationResourceLoader extends DefaultResourceLoader { @@ -160,8 +160,8 @@ public class ApplicationResourceLoader extends DefaultResourceLoader {
Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
Assert.notNull(springFactoriesLoader, "'springFactoriesLoader' must not be null");
List<ProtocolResolver> protocolResolvers = springFactoriesLoader.load(ProtocolResolver.class);
List<ResourceFilePathResolver> filePathResolvers = (preferFileResolution)
? springFactoriesLoader.load(ResourceFilePathResolver.class) : Collections.emptyList();
List<FilePathResolver> filePathResolvers = (preferFileResolution)
? springFactoriesLoader.load(FilePathResolver.class) : Collections.emptyList();
return new ProtocolResolvingResourceLoader(resourceLoader, protocolResolvers, filePathResolvers);
}
@ -188,6 +188,28 @@ public class ApplicationResourceLoader extends DefaultResourceLoader { @@ -188,6 +188,28 @@ public class ApplicationResourceLoader extends DefaultResourceLoader {
}
/**
* Strategy interface registered in {@code spring.factories} and used by
* {@link ApplicationResourceLoader} to determine the file path of loaded resource
* when it can also be represented as a {@link FileSystemResource}.
*
* @author Phillip Webb
* @since 3.4.5
*/
public interface FilePathResolver {
/**
* Return the {@code path} of the given resource if it can also be represented as
* a {@link FileSystemResource}.
* @param location the location used to create the resource
* @param resource the resource to check
* @return the file path of the resource or {@code null} if the it is not possible
* to represent the resource as a {@link FileSystemResource}.
*/
String resolveFilePath(String location, Resource resource);
}
/**
* An application {@link Resource}.
*/
@ -214,10 +236,10 @@ public class ApplicationResourceLoader extends DefaultResourceLoader { @@ -214,10 +236,10 @@ public class ApplicationResourceLoader extends DefaultResourceLoader {
private final List<ProtocolResolver> protocolResolvers;
private final List<ResourceFilePathResolver> filePathResolvers;
private final List<FilePathResolver> filePathResolvers;
ProtocolResolvingResourceLoader(ResourceLoader resourceLoader, List<ProtocolResolver> protocolResolvers,
List<ResourceFilePathResolver> filePathResolvers) {
List<FilePathResolver> filePathResolvers) {
this.resourceLoader = resourceLoader;
this.protocolResolvers = protocolResolvers;
this.filePathResolvers = filePathResolvers;
@ -239,12 +261,12 @@ public class ApplicationResourceLoader extends DefaultResourceLoader { @@ -239,12 +261,12 @@ public class ApplicationResourceLoader extends DefaultResourceLoader {
}
}
Resource resource = this.resourceLoader.getResource(location);
String fileSystemPath = getFileSystemPath(location, resource);
return (fileSystemPath != null) ? new ApplicationResource(fileSystemPath) : resource;
String filePath = getFilePath(location, resource);
return (filePath != null) ? new ApplicationResource(filePath) : resource;
}
private String getFileSystemPath(String location, Resource resource) {
for (ResourceFilePathResolver filePathResolver : this.filePathResolvers) {
private String getFilePath(String location, Resource resource) {
for (FilePathResolver filePathResolver : this.filePathResolvers) {
String filePath = filePathResolver.resolveFilePath(location, resource);
if (filePath != null) {
return filePath;

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

@ -16,16 +16,17 @@ @@ -16,16 +16,17 @@
package org.springframework.boot.io;
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
/**
* {@link ResourceFilePathResolver} for {@link ClassPathResource}.
* {@link FilePathResolver} for {@link ClassPathResource}.
*
* @author Phillip Webb
*/
class ClassPathResourceFilePathResolver implements ResourceFilePathResolver {
class ClassPathResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
@Override
public String resolveFilePath(String location, Resource resource) {

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

@ -1,42 +0,0 @@ @@ -1,42 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.io;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
/**
* Strategy interface registered in {@code spring.factories} and used by
* {@link ApplicationResourceLoader} to determine the file path of loaded resource when it
* can also be represented as a {@link FileSystemResource}.
*
* @author Phillip Webb
* @since 3.4.5
*/
public interface ResourceFilePathResolver {
/**
* Return the {@code path} of the given resource if it can also be represented as a
* {@link FileSystemResource}.
* @param location the location used to create the resource
* @param resource the resource to check
* @return the file path of the resource or {@code null} if the it is not possible to
* represent the resource as a {@link FileSystemResource}.
*/
String resolveFilePath(String location, Resource resource);
}

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

@ -16,17 +16,18 @@ @@ -16,17 +16,18 @@
package org.springframework.boot.web.context;
import org.springframework.boot.io.ResourceFilePathResolver;
import org.springframework.boot.io.ApplicationResourceLoader;
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.support.ServletContextResource;
/**
* {@link ResourceFilePathResolver} for {@link ServletContextResource}.
* {@link FilePathResolver} for {@link ServletContextResource}.
*
* @author Phillip Webb
*/
class ServletContextResourceFilePathResolver implements ResourceFilePathResolver {
class ServletContextResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
private static final String RESOURCE_CLASS_NAME = "org.springframework.web.context.support.ServletContextResource";

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

@ -16,15 +16,16 @@ @@ -16,15 +16,16 @@
package org.springframework.boot.web.reactive.context;
import org.springframework.boot.io.ResourceFilePathResolver;
import org.springframework.boot.io.ApplicationResourceLoader;
import org.springframework.boot.io.ApplicationResourceLoader.FilePathResolver;
import org.springframework.core.io.Resource;
/**
* {@link ResourceFilePathResolver} for {@link FilteredReactiveWebContextResource}.
* {@link FilePathResolver} for {@link FilteredReactiveWebContextResource}.
*
* @author Dmytro Nosan
*/
class FilteredReactiveWebContextResourceFilePathResolver implements ResourceFilePathResolver {
class FilteredReactiveWebContextResourceFilePathResolver implements ApplicationResourceLoader.FilePathResolver {
@Override
public String resolveFilePath(String location, Resource resource) {

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

@ -107,7 +107,7 @@ org.springframework.core.io.ProtocolResolver=\ @@ -107,7 +107,7 @@ org.springframework.core.io.ProtocolResolver=\
org.springframework.boot.io.Base64ProtocolResolver
# Resource File Path Resolvers
org.springframework.boot.io.ResourceFilePathResolver=\
org.springframework.boot.io.ApplicationResourceLoader$FilePathResolver=\
org.springframework.boot.io.ClassPathResourceFilePathResolver,\
org.springframework.boot.web.context.ServletContextResourceFilePathResolver,\
org.springframework.boot.web.reactive.context.FilteredReactiveWebContextResourceFilePathResolver

Loading…
Cancel
Save