21
21
import java .io .IOException ;
22
22
import java .io .UncheckedIOException ;
23
23
import java .nio .file .Path ;
24
+ import java .util .Collections ;
24
25
import java .util .List ;
25
26
26
- import org .springframework .core .io .ClassPathResource ;
27
27
import org .springframework .core .io .ContextResource ;
28
28
import org .springframework .core .io .DefaultResourceLoader ;
29
29
import org .springframework .core .io .FileSystemResource ;
32
32
import org .springframework .core .io .ResourceLoader ;
33
33
import org .springframework .core .io .support .SpringFactoriesLoader ;
34
34
import org .springframework .util .Assert ;
35
- import org .springframework .util .ClassUtils ;
36
35
import org .springframework .util .StringUtils ;
37
36
38
37
/**
@@ -153,9 +152,8 @@ public static ResourceLoader get(ResourceLoader resourceLoader) {
153
152
* {@code spring.factories}. The factories file will be resolved using the default
154
153
* class loader at the time this call is made.
155
154
* @param resourceLoader the delegate resource loader
156
- * @param preferFileResolution if file based resolution is preferred over
157
- * {@code ServletContextResource} or {@link ClassPathResource} when no resource prefix
158
- * is provided.
155
+ * @param preferFileResolution if file based resolution is preferred when a suitable
156
+ * {@link ResourceFilePathResolver} support the resource
159
157
* @return a {@link ResourceLoader} instance
160
158
* @since 3.4.1
161
159
*/
@@ -183,8 +181,10 @@ private static ResourceLoader get(ResourceLoader resourceLoader, SpringFactories
183
181
boolean preferFileResolution ) {
184
182
Assert .notNull (resourceLoader , "'resourceLoader' must not be null" );
185
183
Assert .notNull (springFactoriesLoader , "'springFactoriesLoader' must not be null" );
186
- return new ProtocolResolvingResourceLoader (resourceLoader , springFactoriesLoader .load (ProtocolResolver .class ),
187
- preferFileResolution );
184
+ List <ProtocolResolver > protocolResolvers = springFactoriesLoader .load (ProtocolResolver .class );
185
+ List <ResourceFilePathResolver > filePathResolvers = (preferFileResolution )
186
+ ? springFactoriesLoader .load (ResourceFilePathResolver .class ) : Collections .emptyList ();
187
+ return new ProtocolResolvingResourceLoader (resourceLoader , protocolResolvers , filePathResolvers );
188
188
}
189
189
190
190
/**
@@ -268,30 +268,22 @@ public String getPathWithinContext() {
268
268
*/
269
269
private static class ProtocolResolvingResourceLoader implements ResourceLoader {
270
270
271
- private static final String SERVLET_CONTEXT_RESOURCE_CLASS_NAME = "org.springframework.web.context.support.ServletContextResource" ;
272
-
273
271
private final ResourceLoader resourceLoader ;
274
272
275
273
private final List <ProtocolResolver > protocolResolvers ;
276
274
277
- private final boolean preferFileResolution ;
278
-
279
- private final Class <?> servletContextResourceClass ;
275
+ private final List <ResourceFilePathResolver > filePathResolvers ;
280
276
281
277
ProtocolResolvingResourceLoader (ResourceLoader resourceLoader , List <ProtocolResolver > protocolResolvers ,
282
- boolean preferFileResolution ) {
278
+ List < ResourceFilePathResolver > filePathResolvers ) {
283
279
this .resourceLoader = resourceLoader ;
284
280
this .protocolResolvers = protocolResolvers ;
285
- this .preferFileResolution = preferFileResolution ;
286
- this .servletContextResourceClass = resolveServletContextResourceClass (
287
- resourceLoader .getClass ().getClassLoader ());
281
+ this .filePathResolvers = filePathResolvers ;
288
282
}
289
283
290
- private static Class <?> resolveServletContextResourceClass (ClassLoader classLoader ) {
291
- if (!ClassUtils .isPresent (SERVLET_CONTEXT_RESOURCE_CLASS_NAME , classLoader )) {
292
- return null ;
293
- }
294
- return ClassUtils .resolveClassName (SERVLET_CONTEXT_RESOURCE_CLASS_NAME , classLoader );
284
+ @ Override
285
+ public ClassLoader getClassLoader () {
286
+ return this .resourceLoader .getClassLoader ();
295
287
}
296
288
297
289
@ Override
@@ -305,24 +297,18 @@ public Resource getResource(String location) {
305
297
}
306
298
}
307
299
Resource resource = this .resourceLoader .getResource (location );
308
- if (this .preferFileResolution
309
- && (isClassPathResourceByPath (location , resource ) || isServletResource (resource ))) {
310
- resource = new ApplicationResource (location );
311
- }
312
- return resource ;
300
+ String fileSystemPath = getFileSystemPath (location , resource );
301
+ return (fileSystemPath != null ) ? new ApplicationResource (fileSystemPath ) : resource ;
313
302
}
314
303
315
- private boolean isClassPathResourceByPath (String location , Resource resource ) {
316
- return (resource instanceof ClassPathResource ) && !location .startsWith (CLASSPATH_URL_PREFIX );
317
- }
318
-
319
- private boolean isServletResource (Resource resource ) {
320
- return this .servletContextResourceClass != null && this .servletContextResourceClass .isInstance (resource );
321
- }
322
-
323
- @ Override
324
- public ClassLoader getClassLoader () {
325
- return this .resourceLoader .getClassLoader ();
304
+ private String getFileSystemPath (String location , Resource resource ) {
305
+ for (ResourceFilePathResolver filePathResolver : this .filePathResolvers ) {
306
+ String filePath = filePathResolver .resolveFilePath (location , resource );
307
+ if (filePath != null ) {
308
+ return filePath ;
309
+ }
310
+ }
311
+ return null ;
326
312
}
327
313
328
314
}
0 commit comments