Skip to content

Commit 37de0b2

Browse files
committed
ProtocolResolver SPI allows for custom resource resolution
Issue: SPR-12857 Issue: SPR-13905
1 parent a1e9e6f commit 37de0b2

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2424
import org.springframework.core.env.ConfigurableEnvironment;
2525
import org.springframework.core.env.Environment;
26+
import org.springframework.core.io.ProtocolResolver;
2627

2728
/**
2829
* SPI interface to be implemented by most if not all application contexts.
@@ -113,9 +114,9 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
113114
* Add a new BeanFactoryPostProcessor that will get applied to the internal
114115
* bean factory of this application context on refresh, before any of the
115116
* bean definitions get evaluated. To be invoked during context configuration.
116-
* @param beanFactoryPostProcessor the factory processor to register
117+
* @param postProcessor the factory processor to register
117118
*/
118-
void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor);
119+
void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);
119120

120121
/**
121122
* Add a new ApplicationListener that will be notified on context events
@@ -129,6 +130,15 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
129130
*/
130131
void addApplicationListener(ApplicationListener<?> listener);
131132

133+
/**
134+
* Register the given resource resolver with this application context,
135+
* allowing for additional resource protocols to be handled.
136+
* <p>Any such resolver will be invoked ahead of this context's standard
137+
* resolution rules. It may therefore also override any default rules.
138+
* @since 4.3
139+
*/
140+
void addResourceResolver(ProtocolResolver protocolHandler);
141+
132142
/**
133143
* Load or refresh the persistent representation of the configuration,
134144
* which might an XML file, properties file, or relational database schema.

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -461,8 +461,9 @@ public void setParent(ApplicationContext parent) {
461461
}
462462

463463
@Override
464-
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) {
465-
this.beanFactoryPostProcessors.add(beanFactoryPostProcessor);
464+
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
465+
Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
466+
this.beanFactoryPostProcessors.add(postProcessor);
466467
}
467468

468469

@@ -476,6 +477,7 @@ public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
476477

477478
@Override
478479
public void addApplicationListener(ApplicationListener<?> listener) {
480+
Assert.notNull(listener, "ApplicationListener must not be null");
479481
if (this.applicationEventMulticaster != null) {
480482
this.applicationEventMulticaster.addApplicationListener(listener);
481483
}

spring-core/src/main/java/org/springframework/core/io/DefaultResourceLoader.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import java.net.MalformedURLException;
2020
import java.net.URL;
21+
import java.util.Collection;
22+
import java.util.LinkedHashSet;
23+
import java.util.Set;
2124

2225
import org.springframework.util.Assert;
2326
import org.springframework.util.ClassUtils;
@@ -42,6 +45,8 @@ public class DefaultResourceLoader implements ResourceLoader {
4245

4346
private ClassLoader classLoader;
4447

48+
private final Set<ProtocolResolver> protocolResolvers = new LinkedHashSet<ProtocolResolver>(4);
49+
4550

4651
/**
4752
* Create a new DefaultResourceLoader.
@@ -84,10 +89,40 @@ public ClassLoader getClassLoader() {
8489
return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
8590
}
8691

92+
/**
93+
* Register the given resolver with this resource loader, allowing for
94+
* additional protocols to be handled.
95+
* <p>Any such resolver will be invoked ahead of this loader's standard
96+
* resolution rules. It may therefore also override any default rules.
97+
* @since 4.3
98+
* @see #getProtocolResolvers()
99+
*/
100+
public void addProtocolResolver(ProtocolResolver resolver) {
101+
Assert.notNull(resolver, "ProtocolResolver must not be null");
102+
this.protocolResolvers.add(resolver);
103+
}
104+
105+
/**
106+
* Return the collection of currently registered protocol resolvers,
107+
* allowing for introspection as well as modification.
108+
* @since 4.3
109+
*/
110+
public Collection<ProtocolResolver> getProtocolResolvers() {
111+
return this.protocolResolvers;
112+
}
113+
87114

88115
@Override
89116
public Resource getResource(String location) {
90117
Assert.notNull(location, "Location must not be null");
118+
119+
for (ProtocolResolver protocolResolver : this.protocolResolvers) {
120+
Resource resource = protocolResolver.resolve(location, this);
121+
if (resource != null) {
122+
return resource;
123+
}
124+
}
125+
91126
if (location.startsWith("/")) {
92127
return getResourceByPath(location);
93128
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io;
18+
19+
/**
20+
* A resolution strategy for protocol-specific resource handles.
21+
*
22+
* <p>Used as an SPI for {@link DefaultResourceLoader}, allowing for
23+
* custom protocols to be handled without subclassing the loader
24+
* implementation (or application context implementation).
25+
*
26+
* @author Juergen Hoeller
27+
* @since 4.3
28+
* @see DefaultResourceLoader#addProtocolResolver
29+
*/
30+
public interface ProtocolResolver {
31+
32+
/**
33+
* Resolve the given location against the given resource loader
34+
* if this implementation's protocol matches.
35+
* @param location the user-specified resource location
36+
* @param resourceLoader the associated resource loader
37+
* @return a corresponding {@code Resource} handle if the given location
38+
* matches this resolver's protocol, or {@code null} otherwise
39+
*/
40+
Resource resolve(String location, ResourceLoader resourceLoader);
41+
42+
}

0 commit comments

Comments
 (0)