diff --git a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java index 09048cbe1f..cbbd3bfe62 100644 --- a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -23,6 +23,7 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.io.ProtocolResolver; /** * SPI interface to be implemented by most if not all application contexts. @@ -113,9 +114,9 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life * Add a new BeanFactoryPostProcessor that will get applied to the internal * bean factory of this application context on refresh, before any of the * bean definitions get evaluated. To be invoked during context configuration. - * @param beanFactoryPostProcessor the factory processor to register + * @param postProcessor the factory processor to register */ - void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor); + void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor); /** * Add a new ApplicationListener that will be notified on context events @@ -129,6 +130,15 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life */ void addApplicationListener(ApplicationListener listener); + /** + * Register the given resource resolver with this application context, + * allowing for additional resource protocols to be handled. + *

Any such resolver will be invoked ahead of this context's standard + * resolution rules. It may therefore also override any default rules. + * @since 4.3 + */ + void addResourceResolver(ProtocolResolver protocolHandler); + /** * Load or refresh the persistent representation of the configuration, * which might an XML file, properties file, or relational database schema. diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index a3d9778347..a2068695e0 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -461,8 +461,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader } @Override - public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) { - this.beanFactoryPostProcessors.add(beanFactoryPostProcessor); + public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) { + Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null"); + this.beanFactoryPostProcessors.add(postProcessor); } @@ -476,6 +477,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader @Override public void addApplicationListener(ApplicationListener listener) { + Assert.notNull(listener, "ApplicationListener must not be null"); if (this.applicationEventMulticaster != null) { this.applicationEventMulticaster.addApplicationListener(listener); } diff --git a/spring-core/src/main/java/org/springframework/core/io/DefaultResourceLoader.java b/spring-core/src/main/java/org/springframework/core/io/DefaultResourceLoader.java index 2f22b14c38..b7c5219f1b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/DefaultResourceLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/DefaultResourceLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -18,6 +18,9 @@ package org.springframework.core.io; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -42,6 +45,8 @@ public class DefaultResourceLoader implements ResourceLoader { private ClassLoader classLoader; + private final Set protocolResolvers = new LinkedHashSet(4); + /** * Create a new DefaultResourceLoader. @@ -84,10 +89,40 @@ public class DefaultResourceLoader implements ResourceLoader { return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader()); } + /** + * Register the given resolver with this resource loader, allowing for + * additional protocols to be handled. + *

Any such resolver will be invoked ahead of this loader's standard + * resolution rules. It may therefore also override any default rules. + * @since 4.3 + * @see #getProtocolResolvers() + */ + public void addProtocolResolver(ProtocolResolver resolver) { + Assert.notNull(resolver, "ProtocolResolver must not be null"); + this.protocolResolvers.add(resolver); + } + + /** + * Return the collection of currently registered protocol resolvers, + * allowing for introspection as well as modification. + * @since 4.3 + */ + public Collection getProtocolResolvers() { + return this.protocolResolvers; + } + @Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); + + for (ProtocolResolver protocolResolver : this.protocolResolvers) { + Resource resource = protocolResolver.resolve(location, this); + if (resource != null) { + return resource; + } + } + if (location.startsWith("/")) { return getResourceByPath(location); } diff --git a/spring-core/src/main/java/org/springframework/core/io/ProtocolResolver.java b/spring-core/src/main/java/org/springframework/core/io/ProtocolResolver.java new file mode 100644 index 0000000000..700c866e7f --- /dev/null +++ b/spring-core/src/main/java/org/springframework/core/io/ProtocolResolver.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2016 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 + * + * http://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.core.io; + +/** + * A resolution strategy for protocol-specific resource handles. + * + *

Used as an SPI for {@link DefaultResourceLoader}, allowing for + * custom protocols to be handled without subclassing the loader + * implementation (or application context implementation). + * + * @author Juergen Hoeller + * @since 4.3 + * @see DefaultResourceLoader#addProtocolResolver + */ +public interface ProtocolResolver { + + /** + * Resolve the given location against the given resource loader + * if this implementation's protocol matches. + * @param location the user-specified resource location + * @param resourceLoader the associated resource loader + * @return a corresponding {@code Resource} handle if the given location + * matches this resolver's protocol, or {@code null} otherwise + */ + Resource resolve(String location, ResourceLoader resourceLoader); + +}