/
AvroFactory.java
477 lines (408 loc) · 15 KB
/
AvroFactory.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package com.fasterxml.jackson.dataformat.avro;
import java.io.*;
import java.net.URL;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.format.InputAccessor;
import com.fasterxml.jackson.core.format.MatchStrength;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.dataformat.avro.deser.*;
/**
* Default {@link JsonFactory} implementation for encoding/decoding Avro
* content, uses native Jackson encoder/decoder.
*
* @see com.fasterxml.jackson.dataformat.avro.apacheimpl.ApacheAvroFactory
*/
public class AvroFactory extends JsonFactory
{
private static final long serialVersionUID = 1L;
public final static String FORMAT_NAME_AVRO = "avro";
/**
* Bitfield (set of flags) of all parser features that are enabled
* by default.
*/
final static int DEFAULT_AVRO_PARSER_FEATURE_FLAGS = AvroParser.Feature.collectDefaults();
/**
* Bitfield (set of flags) of all generator features that are enabled
* by default.
*/
final static int DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS = AvroGenerator.Feature.collectDefaults();
/*
/**********************************************************
/* Configuration
/**********************************************************
*/
protected int _avroParserFeatures;
protected int _avroGeneratorFeatures;
/**
* Flag that is set if Apache Avro lib's decoder is to be used for decoding;
* `false` to use Jackson native Avro decoder.
*
* @since 2.9
*/
protected boolean _useApacheLibDecoder;
/*
/**********************************************************
/* Factory construction, configuration
/**********************************************************
*/
/**
* Default constructor used to create factory instances.
* Creation of a factory instance is a light-weight operation,
* but it is still a good idea to reuse limited number of
* factory instances (and quite often just a single instance):
* factories are used as context for storing some reused
* processing objects (such as symbol tables parsers use)
* and this reuse only works within context of a single
* factory instance.
*/
public AvroFactory() {
// 09-Jan-2017, tatu: We must actually create and pass builder to be able to change
// one of JsonGenerator.Features (See builder for details)
super(new AvroFactoryBuilder(), false);
_avroParserFeatures = DEFAULT_AVRO_PARSER_FEATURE_FLAGS;
_avroGeneratorFeatures = DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS;
_useApacheLibDecoder = false;
}
public AvroFactory(ObjectCodec oc)
{
super(oc);
_avroParserFeatures = DEFAULT_AVRO_PARSER_FEATURE_FLAGS;
_avroGeneratorFeatures = DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS;
_useApacheLibDecoder = false;
/* 04-Mar-2013, tatu: Content auto-closing is unfortunately a feature
* that works poorly with Avro error reporting, and generally
* manages to replace actual failure with a bogus one when
* missing "END_OBJECT"s (etc) are called. So let's default
* it to disabled, unlike for most JsonFactory sub-types.
*/
disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
}
protected AvroFactory(AvroFactory src, ObjectCodec oc)
{
super(src, oc);
_avroParserFeatures = src._avroParserFeatures;
_avroGeneratorFeatures = src._avroGeneratorFeatures;
_useApacheLibDecoder = src._useApacheLibDecoder;
}
/**
* Constructors used by {@link AvroFactoryBuilder} for instantiation.
*
* @since 2.9
*/
protected AvroFactory(AvroFactoryBuilder b)
{
super(b, false);
_avroParserFeatures = b.formatParserFeaturesMask();
_avroGeneratorFeatures = b.formatGeneratorFeaturesMask();
_useApacheLibDecoder = b.useApacheLibDecoder();
}
@Override
public AvroFactoryBuilder rebuild() {
return new AvroFactoryBuilder(this);
}
/**
* Main factory method to use for constructing a builder for creating
* {@link AvroFactory} instances with different configuration.
* Builder is initialized to defaults and this is equivalent to calling
* {@link #builderWithNativeDecoder}.
*/
public static AvroFactoryBuilder builder() {
return new AvroFactoryBuilder();
}
/**
* Main factory method to use for constructing a builder for creating
* {@link AvroFactory} instances with different configuration,
* initialized to use Apache Avro library codec for decoding content
* (instead of Jackson native decoder).
*/
public static AvroFactoryBuilder builderWithApacheDecoder() {
return new AvroFactoryBuilder(true);
}
/**
* Main factory method to use for constructing a builder for creating
* {@link AvroFactory} instances with different configuration,
* initialized to use Jackson antive codec for decoding content
* (instead of Apache Avro library decoder).
*/
public static AvroFactoryBuilder builderWithNativeDecoder() {
return new AvroFactoryBuilder(false);
}
@Override
public AvroFactory copy()
{
_checkInvalidCopy(AvroFactory.class);
return new AvroFactory(this, null);
}
/*
/**********************************************************
/* Capability introspection
/**********************************************************
*/
// Yes, Avro is strictly positional based on schema
@Override
public boolean requiresPropertyOrdering() {
return true;
}
@Override // since 2.10 (should have been earlier)
public boolean canHandleBinaryNatively() {
return true;
}
/*
/**********************************************************
/* Serializable overrides
/**********************************************************
*/
/**
* Method that we need to override to actually make restoration go
* through constructors etc.
* Also: must be overridden by sub-classes as well.
*/
@Override
protected Object readResolve() {
return new AvroFactory(this, _objectCodec);
}
/*
/**********************************************************
/* Versioned
/**********************************************************
*/
@Override
public Version version() {
return PackageVersion.VERSION;
}
/*
/**********************************************************
/* Format detection functionality
/**********************************************************
*/
@Override
public String getFormatName() {
return FORMAT_NAME_AVRO;
}
@Override
public boolean canUseSchema(FormatSchema schema) {
return (schema instanceof AvroSchema);
}
/**
* Sub-classes need to override this method
*/
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
// TODO, if possible... probably isn't?
return MatchStrength.INCONCLUSIVE;
}
/*
/**********************************************************
/* Configuration, parser settings
/**********************************************************
*/
/**
* Method for enabling or disabling specified parser feature
* (check {@link AvroParser.Feature} for list of features)
*/
public final AvroFactory configure(AvroParser.Feature f, boolean state)
{
if (state) {
enable(f);
} else {
disable(f);
}
return this;
}
/**
* Method for enabling specified parser feature
* (check {@link AvroParser.Feature} for list of features)
*/
public AvroFactory enable(AvroParser.Feature f) {
_avroParserFeatures |= f.getMask();
return this;
}
/**
* Method for disabling specified parser features
* (check {@link AvroParser.Feature} for list of features)
*/
public AvroFactory disable(AvroParser.Feature f) {
_avroParserFeatures &= ~f.getMask();
return this;
}
/**
* Checked whether specified parser feature is enabled.
*/
public final boolean isEnabled(AvroParser.Feature f) {
return (_avroParserFeatures & f.getMask()) != 0;
}
@Override
public int getFormatParserFeatures() {
return _avroParserFeatures;
}
/*
/**********************************************************
/* Configuration, generator settings
/**********************************************************
*/
/**
* Method for enabling or disabling specified generator feature
* (check {@link AvroGenerator.Feature} for list of features)
*/
public final AvroFactory configure(AvroGenerator.Feature f, boolean state) {
if (state) {
enable(f);
} else {
disable(f);
}
return this;
}
/**
* Method for enabling specified generator features
* (check {@link AvroGenerator.Feature} for list of features)
*/
public AvroFactory enable(AvroGenerator.Feature f) {
_avroGeneratorFeatures |= f.getMask();
return this;
}
/**
* Method for disabling specified generator feature
* (check {@link AvroGenerator.Feature} for list of features)
*/
public AvroFactory disable(AvroGenerator.Feature f) {
_avroGeneratorFeatures &= ~f.getMask();
return this;
}
/**
* Check whether specified generator feature is enabled.
*/
public final boolean isEnabled(AvroGenerator.Feature f) {
return (_avroGeneratorFeatures & f.getMask()) != 0;
}
@Override
public int getFormatGeneratorFeatures() {
return _avroGeneratorFeatures;
}
/*
/**********************************************************
/* Overridden parser factory methods
/**********************************************************
*/
@SuppressWarnings("resource")
@Override
public AvroParser createParser(File f) throws IOException {
final IOContext ctxt = _createContext(f, true);
return _createParser(_decorate(new FileInputStream(f), ctxt), ctxt);
}
@Override
public AvroParser createParser(URL url) throws IOException {
final IOContext ctxt = _createContext(url, true);
return _createParser(_decorate(_optimizedStreamFromURL(url), ctxt), ctxt);
}
@Override
public AvroParser createParser(InputStream in) throws IOException {
final IOContext ctxt = _createContext(in, false);
return _createParser(_decorate(in, ctxt), ctxt);
}
//public JsonParser createParser(Reader r)
@Override
public AvroParser createParser(byte[] data) throws IOException {
return createParser(data, 0, data.length);
}
@SuppressWarnings("resource")
@Override
public AvroParser createParser(byte[] data, int offset, int len) throws IOException {
IOContext ctxt = _createContext(data, true);
if (_inputDecorator != null) {
InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
if (in != null) {
return _createParser(in, ctxt);
}
}
return _createParser(data, offset, len, ctxt);
}
/*
/**********************************************************
/* Overridden generator factory methods
/**********************************************************
*/
/**
*<p>
* note: co-variant return type
*/
@Override
public AvroGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException {
return createGenerator(out);
}
/**
* Since Avro format always uses UTF-8 internally, no encoding need
* to be passed to this method.
*/
@Override
public AvroGenerator createGenerator(OutputStream out) throws IOException
{
// false -> we won't manage the stream unless explicitly directed to
IOContext ctxt = _createContext(out, false);
return _createGenerator(_decorate(out, ctxt), ctxt);
}
/*
/******************************************************
/* Overridden internal factory methods
/******************************************************
*/
//protected IOContext _createContext(Object srcRef, boolean resourceManaged)
/**
* Overridable factory method that actually instantiates desired
* parser.
*/
@Override
protected AvroParser _createParser(InputStream in, IOContext ctxt) throws IOException {
// !!! 21-Apr-2017, tatu: make configurable
return new JacksonAvroParserImpl(ctxt, _parserFeatures, _avroParserFeatures,
// return new ApacheAvroParserImpl(ctxt, _parserFeatures, _avroParserFeatures,
_objectCodec, in);
}
@Override
protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
return _nonByteSource();
}
@Override
protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
boolean recyclable) throws IOException {
return _nonByteSource();
}
@Override
protected AvroParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
// !!! 21-Apr-2017, tatu: make configurable
return new JacksonAvroParserImpl(ctxt, _parserFeatures, _avroParserFeatures,
// return new ApacheAvroParserImpl(ctxt, _parserFeatures, _avroParserFeatures,
_objectCodec, data, offset, len);
}
/**
* Overridable factory method that actually instantiates desired
* generator.
*/
@Override
protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException {
return _nonByteTarget();
}
//public BufferRecycler _getBufferRecycler()
@Override
protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException {
return _nonByteTarget();
}
/*
/**********************************************************
/* Internal methods
/**********************************************************
*/
protected AvroGenerator _createGenerator(OutputStream out, IOContext ctxt) throws IOException
{
int feats = _avroGeneratorFeatures;
AvroGenerator gen = new AvroGenerator(ctxt, _generatorFeatures, feats,
_objectCodec, out);
return gen;
}
protected <T> T _nonByteSource() throws IOException {
throw new UnsupportedOperationException("Can not create parser for character-based (not byte-based) source");
}
protected <T> T _nonByteTarget() throws IOException {
throw new UnsupportedOperationException("Can not create generator for character-based (not byte-based) target");
}
}