16
16
package org .springframework .data .gemfire .config .annotation ;
17
17
18
18
import java .lang .annotation .Annotation ;
19
+ import java .util .Objects ;
19
20
import java .util .Optional ;
21
+ import java .util .function .Predicate ;
20
22
21
23
import org .apache .geode .cache .GemFireCache ;
22
24
import org .apache .geode .pdx .PdxSerializer ;
36
38
import org .springframework .data .gemfire .mapping .GemfireMappingContext ;
37
39
import org .springframework .data .gemfire .mapping .MappingPdxSerializer ;
38
40
import org .springframework .data .gemfire .support .NoOpBeanFactoryPostProcessor ;
41
+ import org .springframework .data .gemfire .util .ArrayUtils ;
39
42
import org .springframework .lang .NonNull ;
40
43
import org .springframework .util .StringUtils ;
41
44
57
60
* @see org.springframework.data.gemfire.CacheFactoryBean
58
61
* @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport
59
62
* @see org.springframework.data.gemfire.config.support.PdxDiskStoreAwareBeanFactoryPostProcessor
63
+ * @see org.springframework.data.gemfire.mapping.GemfireMappingContext
60
64
* @see org.springframework.data.gemfire.mapping.MappingPdxSerializer
61
65
* @see org.springframework.data.gemfire.support.NoOpBeanFactoryPostProcessor
62
66
* @since 2.1.0
@@ -76,6 +80,8 @@ public class PdxConfiguration extends AbstractAnnotationConfigSupport implements
76
80
private Boolean persistent ;
77
81
private Boolean readSerialized ;
78
82
83
+ private Class <?>[] includeDomainTypes = {};
84
+
79
85
private String diskStoreName ;
80
86
private String serializerBeanName ;
81
87
@@ -106,6 +112,8 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
106
112
? enablePdxAttributes .getBoolean ("ignoreUnreadFields" )
107
113
: DEFAULT_IGNORE_UNREAD_FIELDS ));
108
114
115
+ setIncludeDomainTypes (enablePdxAttributes .getClassArray ("includeDomainTypes" ));
116
+
109
117
setPersistent (resolveProperty (pdxProperty ("persistent" ),
110
118
enablePdxAttributes .containsKey ("persistent" )
111
119
? enablePdxAttributes .getBoolean ("persistent" )
@@ -139,6 +147,14 @@ protected boolean isIgnoreUnreadFields() {
139
147
return Boolean .TRUE .equals (this .ignoreUnreadFields );
140
148
}
141
149
150
+ void setIncludeDomainTypes (Class <?>[] includeDomainTypes ) {
151
+ this .includeDomainTypes = includeDomainTypes ;
152
+ }
153
+
154
+ protected Class <?>[] getIncludeDomainTypes () {
155
+ return ArrayUtils .nullSafeArray (this .includeDomainTypes , Class .class );
156
+ }
157
+
142
158
void setPersistent (Boolean persistent ) {
143
159
this .persistent = persistent ;
144
160
}
@@ -189,7 +205,7 @@ PeerCacheConfigurer peerCachePdxConfigurer() {
189
205
* @see org.springframework.data.gemfire.CacheFactoryBean
190
206
* @see <a href="https://geode.apache.org/docs/guide/113/developing/data_serialization/gemfire_pdx_serialization.html">Geode PDX Serialization</a>
191
207
*/
192
- protected void configurePdx (CacheFactoryBean cacheFactoryBean ) {
208
+ protected void configurePdx (@ NonNull CacheFactoryBean cacheFactoryBean ) {
193
209
194
210
getDiskStoreName ().ifPresent (cacheFactoryBean ::setPdxDiskStoreName );
195
211
@@ -240,15 +256,20 @@ protected Optional<GemfireMappingContext> resolveMappingContext() {
240
256
* @see org.apache.geode.pdx.PdxSerializer
241
257
* @see #getBeanFactory()
242
258
*/
243
- @ NonNull
244
- protected PdxSerializer resolvePdxSerializer () {
259
+ protected @ NonNull PdxSerializer resolvePdxSerializer () {
245
260
246
261
BeanFactory beanFactory = getBeanFactory ();
247
262
248
- return getSerializerBeanName ()
263
+ PdxSerializer serializer = getSerializerBeanName ()
249
264
.filter (beanFactory ::containsBean )
250
265
.map (beanName -> beanFactory .getBean (beanName , PdxSerializer .class ))
251
266
.orElseGet (this ::newPdxSerializer );
267
+
268
+ if (serializer instanceof MappingPdxSerializer mappingSerializer ) {
269
+ mappingSerializer .setIncludeTypeFilters (buildIncludeTypeFilters ());
270
+ }
271
+
272
+ return serializer ;
252
273
}
253
274
254
275
/**
@@ -258,11 +279,23 @@ protected PdxSerializer resolvePdxSerializer() {
258
279
* @return a new instance of {@link PdxSerializer}.
259
280
* @see org.apache.geode.pdx.PdxSerializer
260
281
*/
261
- @ NonNull
262
282
@ SuppressWarnings ("unchecked" )
263
- protected <T extends PdxSerializer > T newPdxSerializer () {
283
+ protected @ NonNull <T extends PdxSerializer > T newPdxSerializer () {
264
284
265
285
return (T ) MappingPdxSerializer .create (resolveMappingContext ().orElse (null ),
266
286
resolveConversionService ().orElse (null ));
267
287
}
288
+
289
+ private @ NonNull Predicate <Class <?>> buildIncludeTypeFilters () {
290
+
291
+ Predicate <Class <?>> includeTypeFilter = type -> false ;
292
+
293
+ for (Class <?> domainType : getIncludeDomainTypes ()) {
294
+ if (Objects .nonNull (domainType )) {
295
+ includeTypeFilter = includeTypeFilter .or (type -> domainType .isAssignableFrom (type ));
296
+ }
297
+ }
298
+
299
+ return includeTypeFilter ;
300
+ }
268
301
}
0 commit comments