|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2022 the original author or authors |
| 2 | + * Copyright 2022 the original author or authors |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| - |
17 | 16 | package org.springframework.data.couchbase.core.convert;
|
18 | 17 |
|
| 18 | +import java.lang.annotation.Annotation; |
19 | 19 | import java.lang.reflect.Constructor;
|
20 |
| -import java.lang.reflect.InvocationTargetException; |
21 |
| -import java.util.HashMap; |
| 20 | +import java.lang.reflect.Method; |
22 | 21 | import java.util.Map;
|
| 22 | +import java.util.Optional; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
23 | 24 |
|
| 25 | +import org.springframework.beans.BeanUtils; |
24 | 26 | import org.springframework.data.convert.PropertyValueConverter;
|
25 | 27 | import org.springframework.data.convert.PropertyValueConverterFactory;
|
26 | 28 | import org.springframework.data.convert.ValueConversionContext;
|
27 | 29 | import org.springframework.data.mapping.PersistentProperty;
|
28 | 30 |
|
29 | 31 | import com.couchbase.client.core.encryption.CryptoManager;
|
30 | 32 | import com.couchbase.client.java.encryption.annotation.Encrypted;
|
| 33 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 34 | +import com.fasterxml.jackson.annotation.JsonValue; |
31 | 35 |
|
32 | 36 | /**
|
33 |
| - * Accept the Couchbase @Encrypted annotation in addition to @ValueConverter |
| 37 | + * Accept the Couchbase @Encrypted and @JsonValue annotations in addition to @ValueConverter annotation.<br> |
| 38 | + * There can only be one propertyValueConverter for a property. Although there maybe be multiple annotations, |
| 39 | + * getConverter(property) only returns one converter (a ChainedPropertyValueConverter might be useful). Note that |
| 40 | + * valueConversions.afterPropertiesSet() (see |
| 41 | + * {@link org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration#customConversions(CryptoManager)} |
| 42 | + * encapsulates this in a CachingPropertyValueConverterFactory which caches by 'property'. Although |
| 43 | + * CachingPropertyValueConverterFactory does have the functionality to cache by a type, it only caches by the type |
| 44 | + * specified on an @ValueConverter annotation.To avoid having identical converter instances for each instance of a class |
| 45 | + * containing an @JsonValue annotation, converterCacheForType is used. |
34 | 46 | *
|
35 | 47 | * @author Michael Reiche
|
36 | 48 | */
|
37 | 49 | public class CouchbasePropertyValueConverterFactory implements PropertyValueConverterFactory {
|
38 | 50 |
|
39 |
| - CryptoManager cryptoManager; |
40 |
| - Map<Class<? extends PropertyValueConverter<?, ?, ?>>, PropertyValueConverter<?, ?, ?>> converterCache = new HashMap<>(); |
| 51 | + final CryptoManager cryptoManager; |
| 52 | + static protected final Map<Class<?>, Optional<PropertyValueConverter<?, ?, ?>>> converterCacheForType = new ConcurrentHashMap<>(); |
41 | 53 |
|
42 | 54 | public CouchbasePropertyValueConverterFactory(CryptoManager cryptoManager) {
|
43 | 55 | this.cryptoManager = cryptoManager;
|
44 | 56 | }
|
45 | 57 |
|
| 58 | + /** |
| 59 | + * @param property must not be {@literal null}. |
| 60 | + * @return |
| 61 | + * @param <DV> destination value |
| 62 | + * @param <SV> source value |
| 63 | + * @param <P> context |
| 64 | + */ |
46 | 65 | @Override
|
47 | 66 | public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
|
48 | 67 | PersistentProperty<?> property) {
|
| 68 | + |
49 | 69 | PropertyValueConverter<DV, SV, P> valueConverter = PropertyValueConverterFactory.super.getConverter(property);
|
50 | 70 | if (valueConverter != null) {
|
51 | 71 | return valueConverter;
|
52 | 72 | }
|
53 |
| - Encrypted encryptedAnn = property.findAnnotation(Encrypted.class); |
54 |
| - if (encryptedAnn != null) { |
55 |
| - Class cryptoConverterClass = CryptoConverter.class; |
56 |
| - return getConverter((Class<PropertyValueConverter<DV, SV, P>>) cryptoConverterClass); |
57 |
| - } else { |
| 73 | + |
| 74 | + // this will return the converter for the first annotation that requires a PropertyValueConverter like @Encrypted |
| 75 | + for (Annotation ann : property.getField().getAnnotations()) { |
| 76 | + Class<?> converterClass = converterFromFieldAnnotation(ann); |
| 77 | + if (converterClass != null) { |
| 78 | + return getConverter((Class<PropertyValueConverter<DV, SV, P>>) converterClass, property); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (property.getType().isEnum()) { // Enums have type-based converters for JsonValue/Creator. see OtherConverters. |
58 | 83 | return null;
|
59 | 84 | }
|
| 85 | + |
| 86 | + // Maybe the type of the property has annotations that indicate a converter (like a method with a @JsonValue) |
| 87 | + return (PropertyValueConverter<DV, SV, P>) converterCacheForType |
| 88 | + .computeIfAbsent(property.getType(), p -> Optional.ofNullable((maybeTypePropertyConverter(property)))) |
| 89 | + .orElse(null); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * lookup the converter class from the annotation. Analogous to getting the converter class from the value() attribute |
| 94 | + * of the @ValueProperty annotation |
| 95 | + * |
| 96 | + * @param ann the annotation |
| 97 | + * @return the class of the converter |
| 98 | + */ |
| 99 | + private Class<?> converterFromFieldAnnotation(Annotation ann) { |
| 100 | + if (ann instanceof Encrypted) { |
| 101 | + return CryptoConverter.class; |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> maybeTypePropertyConverter( |
| 107 | + PersistentProperty<?> property) { |
| 108 | + |
| 109 | + Class<?> type = property.getType(); |
| 110 | + |
| 111 | + // find the annotated method to determine if a converter is required, and cache it. |
| 112 | + Method jsonValueMethod = null; |
| 113 | + for (Method m : type.getDeclaredMethods()) { |
| 114 | + JsonValue jsonValueAnn = m.getAnnotation(JsonValue.class); |
| 115 | + if (jsonValueAnn != null && jsonValueAnn.value()) { |
| 116 | + jsonValueMethod = m; |
| 117 | + jsonValueMethod.setAccessible(true); |
| 118 | + JsonValueConverter.valueMethodCache.put(type, jsonValueMethod); |
| 119 | + Constructor<?> jsonCreatorMethod = null; |
| 120 | + for (Constructor<?> c : type.getConstructors()) { |
| 121 | + JsonCreator jsonCreatorAnn = c.getAnnotation(JsonCreator.class); |
| 122 | + if (jsonCreatorAnn != null && !jsonCreatorAnn.mode().equals(JsonCreator.Mode.DISABLED)) { |
| 123 | + jsonCreatorMethod = c; |
| 124 | + jsonCreatorMethod.setAccessible(true); |
| 125 | + JsonValueConverter.creatorMethodCache.put(type, jsonCreatorMethod); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + Class jsonValueConverterClass = JsonValueConverter.class; |
| 130 | + return getConverter((Class<PropertyValueConverter<DV, SV, P>>) jsonValueConverterClass, property); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return null; // we didn't find a property value converter to use |
60 | 135 | }
|
61 | 136 |
|
62 | 137 | @Override
|
63 | 138 | public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
|
64 | 139 | Class<? extends PropertyValueConverter<DV, SV, P>> converterType) {
|
| 140 | + return getConverter(converterType, null); |
| 141 | + } |
65 | 142 |
|
66 |
| - PropertyValueConverter<?, ?, ?> converter = converterCache.get(converterType); |
67 |
| - if (converter != null) { |
68 |
| - return (PropertyValueConverter<DV, SV, P>) converter; |
69 |
| - } |
| 143 | + /** |
| 144 | + * @param converterType |
| 145 | + * @param property |
| 146 | + * @return |
| 147 | + * @param <DV> |
| 148 | + * @param <SV> |
| 149 | + * @param <P> |
| 150 | + */ |
| 151 | + public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter( |
| 152 | + Class<? extends PropertyValueConverter<DV, SV, P>> converterType, PersistentProperty<?> property) { |
70 | 153 |
|
| 154 | + // CryptoConverter takes a cryptoManager argument |
71 | 155 | if (CryptoConverter.class.isAssignableFrom(converterType)) {
|
72 |
| - converter = new CryptoConverter(cryptoManager); |
73 |
| - } else { |
| 156 | + return (PropertyValueConverter<DV, SV, P>) new CryptoConverter(cryptoManager); |
| 157 | + } else if (property != null) { // try constructor that takes PersistentProperty |
74 | 158 | try {
|
75 |
| - Constructor constructor = converterType.getConstructor(); |
76 |
| - converter = (PropertyValueConverter<?, ?, ?>) constructor.newInstance(); |
77 |
| - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { |
78 |
| - throw new RuntimeException(e); |
79 |
| - } |
| 159 | + Constructor<?> constructor = converterType.getConstructor(PersistentProperty.class); |
| 160 | + return (PropertyValueConverter<DV, SV, P>) BeanUtils.instantiateClass(constructor, property); |
| 161 | + } catch (NoSuchMethodException e) {} |
80 | 162 | }
|
81 |
| - converterCache.put((Class<? extends PropertyValueConverter<DV, SV, P>>) converter.getClass(), converter); |
82 |
| - return (PropertyValueConverter<DV, SV, P>) converter; |
83 |
| - |
| 163 | + // there is no constructor that takes a property, fall-back to no-args constructor |
| 164 | + return BeanUtils.instantiateClass(converterType); |
84 | 165 | }
|
85 | 166 | }
|
0 commit comments