DATACMNS-836 - Improve reactive repository configuration.

RepositoryConfigurationExtensionSupport now exposes a ….useRepositoryConfiguration(RepositoryMetadata) so that extensions can opt in or out of an interface taking part in configuration more easily. Turned loadRepositoryInterface(…) private again as extensions should be able to just inspect the RepositoryMetadata now.

Some parameter rename in ReactiveWrapperConverters to avoid confusion.
This commit is contained in:
Oliver Gierke
2016-11-14 12:08:17 +01:00
parent 5d591db848
commit 9bca55a8c4
3 changed files with 46 additions and 19 deletions

View File

@@ -87,15 +87,25 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
for (BeanDefinition candidate : configSource.getCandidates(loader)) {
RepositoryConfiguration<T> configuration = getRepositoryConfiguration(candidate, configSource);
Class<?> repositoryInterface = loadRepositoryInterface(configuration, loader);
if (repositoryInterface == null) {
result.add(configuration);
continue;
}
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);
if (!useRepositoryConfiguration(metadata)) {
continue;
}
if (!strictMatchesOnly || configSource.usesExplicitFilters()) {
result.add(configuration);
continue;
}
Class<?> repositoryInterface = loadRepositoryInterface(configuration, loader);
if (repositoryInterface == null || isStrictRepositoryCandidate(repositoryInterface)) {
if (isStrictRepositoryCandidate(metadata)) {
result.add(configuration);
}
}
@@ -246,7 +256,7 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
}
/**
* Returns whether the given repository interface is a candidate for bean definition creation in the strict repository
* Returns whether the given repository metadata is a candidate for bean definition creation in the strict repository
* detection mode. The default implementation inspects the domain type managed for a set of well-known annotations
* (see {@link #getIdentifyingAnnotations()}). If none of them is found, the candidate is discarded. Implementations
* should make sure, the only return {@literal true} if they're really sure the interface handed to the method is
@@ -256,11 +266,10 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
* @return
* @since 1.9
*/
protected boolean isStrictRepositoryCandidate(Class<?> repositoryInterface) {
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);
protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {
Collection<Class<?>> types = getIdentifyingTypes();
Class<?> repositoryInterface = metadata.getRepositoryInterface();
for (Class<?> type : types) {
if (type.isAssignableFrom(repositoryInterface)) {
@@ -286,6 +295,16 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
return false;
}
/**
* Return whether to use the configuration for the repository with the given metadata. Defaults to {@literal true}.
*
* @param metadata will never be {@literal null}.
* @return
*/
protected boolean useRepositoryConfiguration(RepositoryMetadata metadata) {
return true;
}
/**
* Loads the repository interface contained in the given {@link RepositoryConfiguration} using the given
* {@link ResourceLoader}.
@@ -294,7 +313,7 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
* @param loader must not be {@literal null}.
* @return the repository interface or {@literal null} if it can't be loaded.
*/
protected Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuration, ResourceLoader loader) {
private Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuration, ResourceLoader loader) {
String repositoryInterface = configuration.getRepositoryInterface();
ClassLoader classLoader = loader.getClassLoader();

View File

@@ -193,21 +193,21 @@ public class ReactiveWrapperConverters {
/**
* Maps elements of a reactive element stream to other elements.
*
* @param stream must not be {@literal null}.
* @param reactiveObject must not be {@literal null}.
* @param converter must not be {@literal null}.
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T map(Object stream, Function<Object, Object> converter) {
public static <T> T map(Object reactiveObject, Function<Object, Object> converter) {
Assert.notNull(stream, "Stream must not be null!");
Assert.notNull(reactiveObject, "Reactive source object must not be null!");
Assert.notNull(converter, "Converter must not be null!");
return REACTIVE_WRAPPERS.stream()//
.filter(it -> ClassUtils.isAssignable(it.getWrapperClass(), stream.getClass()))//
.filter(it -> ClassUtils.isAssignable(it.getWrapperClass(), reactiveObject.getClass()))//
.findFirst()//
.map(it -> (T) it.map(stream, converter))//
.orElseThrow(() -> new IllegalStateException(String.format("Cannot apply converter to %s", stream)));
.map(it -> (T) it.map(reactiveObject, converter))//
.orElseThrow(() -> new IllegalStateException(String.format("Cannot apply converter to %s", reactiveObject)));
}
// -------------------------------------------------------------------------