15
15
*/
16
16
package org .springframework .data .map .repository .config ;
17
17
18
+ import java .lang .reflect .Constructor ;
18
19
import java .util .Map ;
19
20
20
21
import org .springframework .beans .BeanUtils ;
24
25
import org .springframework .core .type .AnnotationMetadata ;
25
26
import org .springframework .data .config .ParsingUtils ;
26
27
import org .springframework .data .keyvalue .core .KeyValueTemplate ;
28
+ import org .springframework .data .keyvalue .core .QueryEngine ;
29
+ import org .springframework .data .keyvalue .core .QueryEngineFactory ;
27
30
import org .springframework .data .keyvalue .core .SortAccessor ;
28
31
import org .springframework .data .keyvalue .repository .config .KeyValueRepositoryConfigurationExtension ;
29
32
import org .springframework .data .map .MapKeyValueAdapter ;
33
+ import org .springframework .data .repository .config .RepositoryConfigurationExtension ;
30
34
import org .springframework .data .repository .config .RepositoryConfigurationSource ;
31
35
import org .springframework .lang .Nullable ;
36
+ import org .springframework .util .ClassUtils ;
32
37
33
38
/**
39
+ * {@link RepositoryConfigurationExtension} for Map-based repositories.
40
+ *
34
41
* @author Christoph Strobl
42
+ * @author Mark Paluch
35
43
*/
44
+ @ SuppressWarnings ("unchecked" )
36
45
public class MapRepositoryConfigurationExtension extends KeyValueRepositoryConfigurationExtension {
37
46
38
47
@ Override
@@ -58,7 +67,11 @@ protected AbstractBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
58
67
adapterBuilder .addConstructorArgValue (getMapTypeToUse (configurationSource ));
59
68
60
69
SortAccessor <?> sortAccessor = getSortAccessor (configurationSource );
61
- if (sortAccessor != null ) {
70
+ QueryEngine <?, ?, ?> queryEngine = getQueryEngine (sortAccessor , configurationSource );
71
+
72
+ if (queryEngine != null ) {
73
+ adapterBuilder .addConstructorArgValue (queryEngine );
74
+ } else if (sortAccessor != null ) {
62
75
adapterBuilder .addConstructorArgValue (sortAccessor );
63
76
}
64
77
@@ -73,20 +86,60 @@ protected AbstractBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
73
86
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
74
87
private static Class <? extends Map > getMapTypeToUse (RepositoryConfigurationSource source ) {
75
88
76
- return (Class <? extends Map >) ((AnnotationMetadata ) source .getSource ()).getAnnotationAttributes (
77
- EnableMapRepositories .class .getName ()).get ("mapType" );
89
+ return (Class <? extends Map >) getAnnotationAttributes (source ).get ("mapType" );
78
90
}
79
91
80
92
@ Nullable
81
93
private static SortAccessor <?> getSortAccessor (RepositoryConfigurationSource source ) {
82
94
83
- Class <? extends SortAccessor <?>> sortAccessorType = (Class <? extends SortAccessor <?>>) (( AnnotationMetadata ) source . getSource ()). getAnnotationAttributes (
84
- EnableMapRepositories . class . getName () ).get ("sortAccessor" );
95
+ Class <? extends SortAccessor <?>> sortAccessorType = (Class <? extends SortAccessor <?>>) getAnnotationAttributes (
96
+ source ).get ("sortAccessor" );
85
97
86
- if (sortAccessorType != null && !sortAccessorType .isInterface ()) {
98
+ if (sortAccessorType != null && !sortAccessorType .isInterface ()) {
87
99
return BeanUtils .instantiateClass (sortAccessorType );
88
100
}
89
101
90
102
return null ;
91
103
}
104
+
105
+ @ Nullable
106
+ private static QueryEngine <?, ?, ?> getQueryEngine (@ Nullable SortAccessor <?> sortAccessor ,
107
+ RepositoryConfigurationSource source ) {
108
+
109
+ Class <? extends QueryEngineFactory > queryEngineFactoryType = (Class <? extends QueryEngineFactory >) getAnnotationAttributes (
110
+ source ).get ("queryEngineFactory" );
111
+
112
+ if (queryEngineFactoryType != null && !queryEngineFactoryType .isInterface ()) {
113
+
114
+ if (sortAccessor != null ) {
115
+ Constructor <? extends QueryEngineFactory > constructor = ClassUtils
116
+ .getConstructorIfAvailable (queryEngineFactoryType , SortAccessor .class );
117
+ if (constructor != null ) {
118
+ return BeanUtils .instantiateClass (constructor , sortAccessor ).create ();
119
+ }
120
+ }
121
+
122
+ return BeanUtils .instantiateClass (queryEngineFactoryType ).create ();
123
+ }
124
+
125
+ return null ;
126
+ }
127
+
128
+ private static Map <String , Object > getAnnotationAttributes (RepositoryConfigurationSource source ) {
129
+
130
+ AnnotationMetadata annotationSource = (AnnotationMetadata ) source .getSource ();
131
+
132
+ if (annotationSource == null ) {
133
+ throw new IllegalArgumentException ("AnnotationSource not available" );
134
+ }
135
+
136
+ Map <String , Object > annotationAttributes = annotationSource
137
+ .getAnnotationAttributes (EnableMapRepositories .class .getName ());
138
+
139
+ if (annotationAttributes == null ) {
140
+ throw new IllegalStateException ("No annotation attributes for @EnableMapRepositories" );
141
+ }
142
+
143
+ return annotationAttributes ;
144
+ }
92
145
}
0 commit comments