From 69ab4b3383f5da1954596a9271ca5052cede86d0 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Wed, 25 Sep 2019 07:31:17 -0700 Subject: [PATCH] Initialize the store candidate component provider with the actual application environment - otherwise profiled stores are not evaluated properly Fixes #78 --- ...lesystemContentAutoConfigureRegistrar.java | 2 +- .../JpaContentAutoConfigureRegistrar.java | 2 +- .../MongoContentAutoConfigureRegistrar.java | 2 +- .../S3ContentAutoConfigureRegistrar.java | 2 +- .../StoreCandidateComponentProvider.java | 5 +- .../content/commons/utils/StoreUtils.java | 5 +- .../AbstractStoreBeanDefinitionRegistrar.java | 6 +- ...idateComponentProviderEnvironmentTest.java | 74 +++++++++++++++++++ 8 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 spring-content-commons/src/test/java/org/springframework/content/commons/repository/factory/stores/StoreCandidateComponentProviderEnvironmentTest.java diff --git a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/fs/boot/autoconfigure/FilesystemContentAutoConfigureRegistrar.java b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/fs/boot/autoconfigure/FilesystemContentAutoConfigureRegistrar.java index c20981c72..eb7655cb4 100644 --- a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/fs/boot/autoconfigure/FilesystemContentAutoConfigureRegistrar.java +++ b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/fs/boot/autoconfigure/FilesystemContentAutoConfigureRegistrar.java @@ -23,7 +23,7 @@ protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingC String[] basePackages = this.getBasePackages(); - Set definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes()); + Set definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes()); this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions); } diff --git a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/jpa/boot/autoconfigure/JpaContentAutoConfigureRegistrar.java b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/jpa/boot/autoconfigure/JpaContentAutoConfigureRegistrar.java index 797efa964..89e27064c 100644 --- a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/jpa/boot/autoconfigure/JpaContentAutoConfigureRegistrar.java +++ b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/jpa/boot/autoconfigure/JpaContentAutoConfigureRegistrar.java @@ -26,7 +26,7 @@ protected void registerContentStoreBeanDefinitions( String[] basePackages = this.getBasePackages(); - Set definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); + Set definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions); } diff --git a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/mongo/boot/autoconfigure/MongoContentAutoConfigureRegistrar.java b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/mongo/boot/autoconfigure/MongoContentAutoConfigureRegistrar.java index 32601233c..8c4f3e096 100644 --- a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/mongo/boot/autoconfigure/MongoContentAutoConfigureRegistrar.java +++ b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/mongo/boot/autoconfigure/MongoContentAutoConfigureRegistrar.java @@ -26,7 +26,7 @@ protected void registerContentStoreBeanDefinitions( String[] basePackages = this.getBasePackages(); - Set definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); + Set definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions); } diff --git a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/s3/boot/autoconfigure/S3ContentAutoConfigureRegistrar.java b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/s3/boot/autoconfigure/S3ContentAutoConfigureRegistrar.java index 72278441b..d23effc78 100644 --- a/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/s3/boot/autoconfigure/S3ContentAutoConfigureRegistrar.java +++ b/spring-content-autoconfigure/src/main/java/internal/org/springframework/content/s3/boot/autoconfigure/S3ContentAutoConfigureRegistrar.java @@ -24,7 +24,7 @@ protected void registerContentStoreBeanDefinitions( String[] basePackages = this.getBasePackages(); - Set definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); + Set definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes()); buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions); } diff --git a/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreCandidateComponentProvider.java b/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreCandidateComponentProvider.java index 16b29674e..7fbad0680 100644 --- a/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreCandidateComponentProvider.java +++ b/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreCandidateComponentProvider.java @@ -6,6 +6,7 @@ import org.springframework.content.commons.repository.ContentRepository; import org.springframework.content.commons.repository.Store; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.core.env.Environment; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AssignableTypeFilter; @@ -13,8 +14,8 @@ public class StoreCandidateComponentProvider extends ClassPathScanningCandidateComponentProvider { - public StoreCandidateComponentProvider(boolean useDefaultFilters) { - super(useDefaultFilters); + public StoreCandidateComponentProvider(boolean useDefaultFilters, Environment env) { + super(useDefaultFilters, env); this.addIncludeFilter(new InterfaceTypeFilter(ContentRepository.class)); this.addIncludeFilter(new InterfaceTypeFilter(Store.class)); } diff --git a/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreUtils.java b/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreUtils.java index d04d072f5..7a9f67c54 100644 --- a/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreUtils.java +++ b/spring-content-commons/src/main/java/internal/org/springframework/content/commons/utils/StoreUtils.java @@ -8,6 +8,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -49,9 +50,9 @@ public static String getStoreBeanName(BeanDefinition definition) { return Introspector.decapitalize(beanName); } - public static Set getStoreCandidates(ResourceLoader loader, String[] basePackages, boolean multiStoreMode, Class[] identifyingType) { + public static Set getStoreCandidates(Environment env, ResourceLoader loader, String[] basePackages, boolean multiStoreMode, Class[] identifyingType) { - StoreCandidateComponentProvider scanner = new StoreCandidateComponentProvider(false); + StoreCandidateComponentProvider scanner = new StoreCandidateComponentProvider(false, env); // scanner.setConsiderNestedRepositoryInterfaces(shouldConsiderNestedRepositories()); scanner.setResourceLoader(loader); // scanner.setEnvironment(environment); diff --git a/spring-content-commons/src/main/java/org/springframework/content/commons/config/AbstractStoreBeanDefinitionRegistrar.java b/spring-content-commons/src/main/java/org/springframework/content/commons/config/AbstractStoreBeanDefinitionRegistrar.java index a58bcefb9..3443732ad 100644 --- a/spring-content-commons/src/main/java/org/springframework/content/commons/config/AbstractStoreBeanDefinitionRegistrar.java +++ b/spring-content-commons/src/main/java/org/springframework/content/commons/config/AbstractStoreBeanDefinitionRegistrar.java @@ -73,6 +73,10 @@ public void setEnvironment(Environment env) { this.environment = env; } + public Environment getEnvironment() { + return this.environment; + } + public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @@ -134,7 +138,7 @@ protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingC AnnotationAttributes attributes = new AnnotationAttributes(importingClassMetadata.getAnnotationAttributes(getAnnotation().getName())); String[] basePackages = this.getBasePackages(attributes, importingClassMetadata); - Set definitions = StoreUtils.getStoreCandidates(resourceLoader, basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes()); + Set definitions = StoreUtils.getStoreCandidates(environment, resourceLoader, basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes()); buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions); } diff --git a/spring-content-commons/src/test/java/org/springframework/content/commons/repository/factory/stores/StoreCandidateComponentProviderEnvironmentTest.java b/spring-content-commons/src/test/java/org/springframework/content/commons/repository/factory/stores/StoreCandidateComponentProviderEnvironmentTest.java new file mode 100644 index 000000000..4bb5d93dc --- /dev/null +++ b/spring-content-commons/src/test/java/org/springframework/content/commons/repository/factory/stores/StoreCandidateComponentProviderEnvironmentTest.java @@ -0,0 +1,74 @@ +package org.springframework.content.commons.repository.factory.stores; + +import java.net.URI; + +import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration; +import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.content.commons.repository.AssociativeStore; +import org.springframework.content.commons.repository.ContentStore; +import org.springframework.content.commons.repository.Store; +import org.springframework.content.commons.repository.factory.testsupport.EnableTestStores; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; + +import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe; +import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(Ginkgo4jSpringRunner.class) +@Ginkgo4jConfiguration(threads = 1) +@ActiveProfiles(profiles = "c") +@ContextConfiguration(classes = StoreCandidateComponentProviderEnvironmentTest.StoreTestConfiguration.class) +public class StoreCandidateComponentProviderEnvironmentTest { + + @Autowired(required=false) + private TestStore store; + + @Autowired(required=false) + private TestAssociativeStore associativeStore; + + @Autowired(required=false) + private TestContentStore contentStore; + + { + Describe("given two stores with profiles", () -> { + + It("should have a store bean", () -> { + assertThat(store, is(not(nullValue()))); + assertThat(associativeStore, is(nullValue())); + assertThat(contentStore, is(not(nullValue()))); + }); + + }); + } + + @Configuration + @EnableTestStores + public static class StoreTestConfiguration { + } + + public interface TestStore extends Store { + } + + @Profile("b") + public interface TestAssociativeStore extends AssociativeStore { + } + + @Profile("c") + public interface TestContentStore extends ContentStore { + } + + @Test + public void noop() { + } +}