Skip to content

Commit

Permalink
Initialize the store candidate component provider with the actual app…
Browse files Browse the repository at this point in the history
…lication environment

- otherwise profiled stores are not evaluated properly

Fixes #78
  • Loading branch information
Paul Warren committed Sep 25, 2019
1 parent f9d90fc commit 69ab4b3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingC

String[] basePackages = this.getBasePackages();

Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes());
Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes());

this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected void registerContentStoreBeanDefinitions(

String[] basePackages = this.getBasePackages();

Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());
Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());

this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected void registerContentStoreBeanDefinitions(

String[] basePackages = this.getBasePackages();

Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());
Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());

this.buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void registerContentStoreBeanDefinitions(

String[] basePackages = this.getBasePackages();

Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());
Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getEnvironment(), this.getResourceLoader(), basePackages, multipleStoreImplementationsDetected(), getIdentifyingTypes());

buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
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;

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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,9 +50,9 @@ public static String getStoreBeanName(BeanDefinition definition) {
return Introspector.decapitalize(beanName);
}

public static Set<GenericBeanDefinition> getStoreCandidates(ResourceLoader loader, String[] basePackages, boolean multiStoreMode, Class<?>[] identifyingType) {
public static Set<GenericBeanDefinition> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -134,7 +138,7 @@ protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingC
AnnotationAttributes attributes = new AnnotationAttributes(importingClassMetadata.getAnnotationAttributes(getAnnotation().getName()));
String[] basePackages = this.getBasePackages(attributes, importingClassMetadata);

Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(resourceLoader, basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes());
Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(environment, resourceLoader, basePackages, multipleStoreImplementationsDetected(), this.getIdentifyingTypes());

buildAndRegisterDefinitions(importingClassMetadata, registry, attributes, basePackages, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<URI> {
}

@Profile("b")
public interface TestAssociativeStore extends AssociativeStore<Object, URI> {
}

@Profile("c")
public interface TestContentStore extends ContentStore<Object, URI> {
}

@Test
public void noop() {
}
}

0 comments on commit 69ab4b3

Please sign in to comment.