-
Notifications
You must be signed in to change notification settings - Fork 77
/
MapperConfiguratorBase.java
243 lines (212 loc) · 7.66 KB
/
MapperConfiguratorBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package tools.jackson.jaxrs.cfg;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import tools.jackson.databind.*;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.jaxrs.cfg.MapperConfiguratorBase;
/**
* Helper class used to encapsulate details of configuring an
* {@link ObjectMapper} instance to be used for data binding, as
* well as accessing it.
*/
public abstract class MapperConfiguratorBase<IMPL extends MapperConfiguratorBase<IMPL,MAPPER>,
MAPPER extends ObjectMapper
>
{
private final ReentrantLock _lock = new ReentrantLock();
/*
/**********************************************************************
/* Configuration, simple features
/**********************************************************************
*/
/**
* {@link DeserializationFeature}s to explicitly enable or disable
*/
protected EnumMap<MapperFeature, Boolean> _mapperFeatures;
/**
* {@link DeserializationFeature}s to explicitly enable or disable
*/
protected EnumMap<DeserializationFeature, Boolean> _deserFeatures;
/**
* {@link SerializationFeature}s to explicitly enable or disable
*/
protected EnumMap<SerializationFeature, Boolean> _serFeatures;
/*
/**********************************************************************
/* Configuration, other
/**********************************************************************
*/
/**
* {@code AnnotationIntrospector} to use as an override over default
* {@code JacksonAnnotationIntrospector}, if any.
*
* @since 3.0
*/
protected AnnotationIntrospector _instropectorOverride;
/*
/**********************************************************************
/* Lazily constructed Mapper instance(s)
/**********************************************************************
*/
/**
* Mapper provider was constructed with if any, or that was constructed
* due to a call to explicitly configure mapper.
* If defined (explicitly or implicitly) it will be used, instead
* of using provider-based lookup.
*/
protected MAPPER _mapper;
/**
* If no mapper was specified when constructed, and no configuration
* calls are made, a default mapper is constructed. The difference
* between default mapper and regular one is that default mapper
* is only used if no mapper is found via provider lookup.
*/
protected MAPPER _defaultMapper;
/*
/**********************************************************************
/* Life-cycle
/**********************************************************************
*/
public MapperConfiguratorBase(MAPPER mapper,
AnnotationIntrospector instropectorOverride)
{
_mapper = mapper;
_instropectorOverride = instropectorOverride;
}
public MAPPER getDefaultMapper() {
if (_defaultMapper == null) {
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = _mapperWithConfiguration(mapperBuilder());
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
/**
* Helper method that will ensure that there is a configurable non-default
* mapper (constructing an instance if one didn't yet exit), and return
* that mapper.
*/
protected MAPPER mapper()
{
if (_mapper == null) {
_lock.lock();
try {
if (_mapper == null) {
_mapper = _mapperWithConfiguration(mapperBuilder());
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
/*
/**********************************************************************
/* Abstract methods to implement
/**********************************************************************
*/
/**
* @since 3.0
*/
protected abstract MapperBuilder<?,?> mapperBuilder();
/*
/**********************************************************************
/* Configuration methods
/**********************************************************************
*/
/**
* Method that locates, configures and returns {@link ObjectMapper} to use
*/
public MAPPER getConfiguredMapper() {
// important: should NOT call mapper(); needs to return null
// if no instance has been passed or constructed
return _mapper;
}
public final void setMapper(MAPPER m) {
_mapper = m;
}
public final void setAnnotationIntrospector(AnnotationIntrospector aiOverride) {
_instropectorOverride = aiOverride;
}
public final void configure(DeserializationFeature f, boolean state) {
if (_deserFeatures == null) {
_deserFeatures = new EnumMap<>(DeserializationFeature.class);
}
_deserFeatures.put(f, state);
}
public final void configure(SerializationFeature f, boolean state) {
if (_serFeatures == null) {
_serFeatures = new EnumMap<>(SerializationFeature.class);
}
_serFeatures.put(f, state);
}
/*
/**********************************************************************
/* Helper methods for sub-classes
/**********************************************************************
*/
/**
* Helper method that will configure given builder using configured overrides.
*/
@SuppressWarnings("unchecked")
protected MAPPER _mapperWithConfiguration(MapperBuilder<?,?> mapperBuilder)
{
return (MAPPER) _builderWithConfiguration(mapperBuilder)
.build();
}
/**
* Overridable helper method that applies all configuration on given builder.
*/
protected MapperBuilder<?,?> _builderWithConfiguration(MapperBuilder<?,?> mapperBuilder)
{
// First, AnnotationIntrospector settings
if (_instropectorOverride != null) {
mapperBuilder = mapperBuilder.annotationIntrospector(_instropectorOverride);
}
// Features?
if (_mapperFeatures != null) {
for (Map.Entry<MapperFeature,Boolean> entry : _mapperFeatures.entrySet()) {
mapperBuilder = mapperBuilder.configure(entry.getKey(), entry.getValue());
}
}
if (_serFeatures != null) {
for (Map.Entry<SerializationFeature,Boolean> entry : _serFeatures.entrySet()) {
mapperBuilder = mapperBuilder.configure(entry.getKey(), entry.getValue());
}
}
if (_deserFeatures != null) {
for (Map.Entry<DeserializationFeature,Boolean> entry : _deserFeatures.entrySet()) {
mapperBuilder = mapperBuilder.configure(entry.getKey(), entry.getValue());
}
}
// anything else?
return mapperBuilder;
}
/*
protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotationsToUse)
{
// Let's ensure there are no dups there first, filter out nulls
ArrayList<AnnotationIntrospector> intr = new ArrayList<AnnotationIntrospector>();
for (Annotations a : annotationsToUse) {
if (a != null) {
intr.add(_resolveIntrospector(a));
}
}
int count = intr.size();
if (count == 0) {
return AnnotationIntrospector.nopInstance();
}
AnnotationIntrospector curr = intr.get(0);
for (int i = 1, len = intr.size(); i < len; ++i) {
curr = AnnotationIntrospector.pair(curr, intr.get(i));
}
return curr;
}
*/
}