Skip to content

Commit 7607544

Browse files
committed
DATACMNS-836 - Introduced base class for reactive repository factories.
Introduced ReactiveRepositoryFactorySupport so that validation for the presence of all required converters (e.g. RxJava 1) does not have to be repeated in individual stores.
1 parent 8fc0159 commit 7607544

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.repository.core.support;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.Arrays;
20+
21+
import org.reactivestreams.Publisher;
22+
import org.springframework.dao.InvalidDataAccessApiUsageException;
23+
import org.springframework.data.repository.core.RepositoryMetadata;
24+
import org.springframework.data.repository.util.ReactiveWrapperConverters;
25+
import org.springframework.data.repository.util.ReactiveWrappers;
26+
import org.springframework.util.ClassUtils;
27+
28+
/**
29+
* Base class for repository factories to use reactive support. Centralizes the validation of the classpath setup in
30+
* case a repository uses reactive types.
31+
*
32+
* @author Mark Paluch
33+
* @author Oliver Gierke
34+
* @since 2.0
35+
*/
36+
public abstract class ReactiveRepositoryFactorySupport extends RepositoryFactorySupport {
37+
38+
/*
39+
* (non-Javadoc)
40+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#validate(org.springframework.data.repository.core.RepositoryMetadata)
41+
*/
42+
@Override
43+
protected void validate(RepositoryMetadata repositoryMetadata) {
44+
45+
if (!ReactiveWrappers.isAvailable()) {
46+
47+
throw new InvalidDataAccessApiUsageException(
48+
String.format("Cannot implement repository %s without reactive library support.",
49+
repositoryMetadata.getRepositoryInterface().getName()));
50+
}
51+
52+
if (RxJavaOneConversionSetup.REACTIVE_STREAMS_PRESENT) {
53+
54+
Arrays.stream(repositoryMetadata.getRepositoryInterface().getMethods())
55+
.forEach(RxJavaOneConversionSetup::validate);
56+
}
57+
}
58+
59+
/**
60+
* We need to make sure that the necessary conversion libraries are in place if the repository interface uses RxJava 1
61+
* types.
62+
*
63+
* @author Mark Paluch
64+
* @author Oliver Gierke
65+
*/
66+
private static class RxJavaOneConversionSetup {
67+
68+
private static final boolean REACTIVE_STREAMS_PRESENT = ClassUtils.isPresent("org.reactivestreams.Publisher",
69+
RxJavaOneConversionSetup.class.getClassLoader());
70+
71+
/**
72+
* Reactive MongoDB support requires reactive wrapper support. If return type/parameters are reactive wrapper types,
73+
* then it's required to be able to convert these.
74+
*
75+
* @param method the method to validate.
76+
*/
77+
private static void validate(Method method) {
78+
79+
if (ReactiveWrappers.supports(method.getReturnType())
80+
&& !ClassUtils.isAssignable(Publisher.class, method.getReturnType())
81+
&& !ReactiveWrapperConverters.supports(method.getReturnType())) {
82+
83+
throw new InvalidDataAccessApiUsageException(
84+
String.format("No reactive type converter found for type %s used in %s, method %s.",
85+
method.getReturnType().getName(), method.getDeclaringClass().getName(), method));
86+
}
87+
88+
Arrays.stream(method.getParameterTypes()) //
89+
.filter(ReactiveWrappers::supports) //
90+
.filter(parameterType -> !ClassUtils.isAssignable(Publisher.class, parameterType)) //
91+
.filter(parameterType -> !ReactiveWrapperConverters.supports(parameterType)) //
92+
.forEach(parameterType -> {
93+
throw new InvalidDataAccessApiUsageException(
94+
String.format("No reactive type converter found for type %s used in %s, method %s.",
95+
parameterType.getName(), method.getDeclaringClass().getName(), method));
96+
});
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)