/
ExtractProtobufPathsBuilder.java
342 lines (302 loc) · 13 KB
/
ExtractProtobufPathsBuilder.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
/*
* Copyright 2013 Cloudera Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kitesdk.morphline.protobuf;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.kitesdk.morphline.api.Command;
import org.kitesdk.morphline.api.CommandBuilder;
import org.kitesdk.morphline.api.MorphlineCompilationException;
import org.kitesdk.morphline.api.MorphlineContext;
import org.kitesdk.morphline.api.MorphlineRuntimeException;
import org.kitesdk.morphline.api.Record;
import org.kitesdk.morphline.base.AbstractCommand;
import org.kitesdk.morphline.base.Configs;
import org.kitesdk.morphline.base.Fields;
import org.kitesdk.morphline.base.Validator;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.typesafe.config.Config;
/**
* Command that uses zero or more protocol buffer path expressions to extract
* values from a protocol buffer object.
*
* The protocol buffer input object is expected to be contained in the
* {@link Fields#ATTACHMENT_BODY}
*
* Each expression consists of a record output field name (on the left side of
* the colon ':') as well as zero or more path steps (on the right hand side),
* each path step separated by a '/' slash. Protocol buffer arrays are traversed
* with the '[]' notation.
*
* The result of a path expression is a list of objects, each of which is added
* to the given record output field.
*
* ExtractMethod option says what will results path to object. <li>toByteArray -
* pass protocol buffer bytes to next command <li>toString - pass toString value
* to next command <li>none - pass protocol buffer object to next command
*/
public final class ExtractProtobufPathsBuilder implements CommandBuilder {
@Override
public Command build(Config config, Command parent, Command child, MorphlineContext context) {
return new ExtractProtobufPaths(this, config, parent, child, context);
}
@Override
public Collection<String> getNames() {
return Collections.singletonList("extractProtobufPaths");
}
// /////////////////////////////////////////////////////////////////////////////
// Nested classes:
// /////////////////////////////////////////////////////////////////////////////
private static final class ExtractProtobufPaths extends AbstractCommand {
private static final String OBJECT_EXTRACT_METHOD = "objectExtractMethod";
private static final String ENUM_EXTRACT_METHOD = "enumExtractMethod";
private static final String LIST = "List";
private static final String GET = "get";
private static final String HAS = "has";
private static final String ARRAY_TOKEN = "[]";
private static final Set<Class<?>> WRAPPERS;
static {
Set<Class<?>> ret = new HashSet<Class<?>>();
ret.add(Boolean.class);
ret.add(Character.class);
ret.add(Byte.class);
ret.add(Short.class);
ret.add(Integer.class);
ret.add(Long.class);
ret.add(Float.class);
ret.add(Double.class);
ret.add(String.class);
ret.add(byte[].class);
WRAPPERS = Collections.<Class<?>> unmodifiableSet(ret);
}
private final ObjectExtractMethods objectExtractMethod;
private final EnumExtractMethods enumExtractMethod;
private final Map<String, Collection<String>> stepMap;
private final Map<Class<?>, Method> objectExtractMethods = new HashMap<Class<?>, Method>();
private final Map<Class<?>, Method> enumExtractMethods = new HashMap<Class<?>, Method>();
private final Map<Class<?>, Map<String, Method>> propertyGetters = new HashMap<Class<?>, Map<String, Method>>();
private final Map<Class<?>, Map<String, Method>> propertyCheckers = new HashMap<Class<?>, Map<String, Method>>();
public ExtractProtobufPaths(CommandBuilder builder, Config config, Command parent, Command child,
MorphlineContext context) {
super(builder, config, parent, child, context);
ListMultimap<String, String> stepMultiMap = ArrayListMultimap.create();
this.objectExtractMethod = new Validator<ObjectExtractMethods>().validateEnum(config,
getConfigs().getString(config, OBJECT_EXTRACT_METHOD, ObjectExtractMethods.toByteArray.name()),
ObjectExtractMethods.class);
this.enumExtractMethod = new Validator<EnumExtractMethods>()
.validateEnum(config, getConfigs().getString(config, ENUM_EXTRACT_METHOD, EnumExtractMethods.name.name()),
EnumExtractMethods.class);
Config paths = getConfigs().getConfig(config, "paths");
for (Map.Entry<String, Object> entry : new Configs().getEntrySet(paths)) {
String fieldName = entry.getKey();
String path = entry.getValue().toString().trim();
if (path.contains("//")) {
throw new MorphlineCompilationException("No support for descendant axis available yet", config);
}
if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
path = path.trim();
for (String step : path.split("/")) {
step = step.trim();
if (step.length() > ARRAY_TOKEN.length() && step.endsWith(ARRAY_TOKEN)) {
step = step.substring(0, step.length() - ARRAY_TOKEN.length());
stepMultiMap.put(fieldName, normalize(step));
stepMultiMap.put(fieldName, ARRAY_TOKEN);
} else {
stepMultiMap.put(fieldName, normalize(step));
}
}
}
this.stepMap = stepMultiMap.asMap();
LOG.debug("stepMap: {}", stepMap);
validateArguments();
}
@Override
protected boolean doProcess(Record inputRecord) {
Object datum = inputRecord.getFirstValue(Fields.ATTACHMENT_BODY);
Preconditions.checkNotNull(datum);
Record outputRecord = inputRecord.copy();
for (Map.Entry<String, Collection<String>> entry : stepMap.entrySet()) {
String fieldName = entry.getKey();
List<String> steps = (List<String>) entry.getValue();
try {
extractPath(datum, fieldName, steps, outputRecord, 0);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return false;
}
}
// pass record to next command in chain:
return getChild().process(outputRecord);
}
private void extractPath(Object datum, String fieldName, List<String> steps, Record record, int level)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
if (level >= steps.size()) {
return;
}
boolean isLeaf = (level + 1 == steps.size());
String step = steps.get(level);
if (ARRAY_TOKEN == step) {
if (!List.class.isAssignableFrom(datum.getClass())) {
throw new MorphlineRuntimeException("Datum " + datum + " is not a list. Steps: " + steps + " Level: " + level);
}
if (isLeaf) {
resolve(datum, record, fieldName);
} else {
Iterator<?> iter = ((List<?>) datum).iterator();
while (iter.hasNext()) {
extractPath(iter.next(), fieldName, steps, record, level + 1);
}
}
} else if (hasProperty(datum, step)) {
Object value = readProperty(datum, step);
if (value != null) {
if (isLeaf) {
resolve(value, record, fieldName);
} else {
extractPath(value, fieldName, steps, record, level + 1);
}
}
}
}
private Object extractValue(Object datum, Class<?> clazz) throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
boolean isEnum = clazz.isEnum();
if (isEnum && enumExtractMethod == EnumExtractMethods.none) {
return datum;
} else if (objectExtractMethod == ObjectExtractMethods.none) {
return datum;
}
Map<Class<?>, Method> extractMethods;
String methodName;
if (isEnum) {
extractMethods = enumExtractMethods;
methodName = enumExtractMethod.name();
} else {
extractMethods = objectExtractMethods;
methodName = objectExtractMethod.name();
}
Method extractMethod = extractMethods.get(clazz);
if (extractMethod == null) {
extractMethod = clazz.getMethod(methodName);
extractMethods.put(clazz, extractMethod);
}
return extractMethod.invoke(datum);
}
private void findGetAndCheckMethods(String propertyName, Class<?> clazz) {
Map<String, Method> getters = propertyGetters.get(clazz);
Map<String, Method> checkers = propertyCheckers.get(clazz);
if (getters == null) {
getters = new HashMap<String, Method>();
checkers = new HashMap<String, Method>();
propertyGetters.put(clazz, getters);
propertyCheckers.put(clazz, checkers);
}
if (!getters.containsKey(propertyName)) {
String uppercaseProperty = new StringBuilder(propertyName.substring(0, 1).toUpperCase(Locale.ROOT)).append(
propertyName.substring(1)).toString();
String checkerName = new StringBuilder(HAS).append(uppercaseProperty).toString();
try {
checkers.put(propertyName, clazz.getMethod(checkerName));
} catch (Exception e) {
checkers.put(propertyName, null);
}
StringBuilder getterName = new StringBuilder(GET).append(uppercaseProperty);
try {
getters.put(propertyName, clazz.getMethod(getterName.toString()));
} catch (NoSuchMethodException e) {
getterName.append(LIST);
try {
getters.put(propertyName, clazz.getMethod(getterName.toString()));
} catch (Exception e1) {
throw new MorphlineRuntimeException("Property '" + propertyName + "' does not exist in class '"
+ clazz.getName() + "'.");
}
}
}
}
private Method getCheckMethod(String propertyName, Class<?> clazz) {
if (!propertyCheckers.containsKey(clazz) || !propertyCheckers.get(clazz).containsKey(propertyName)) {
findGetAndCheckMethods(propertyName, clazz);
}
return propertyCheckers.get(clazz).get(propertyName);
}
private Method getReadMethod(String propertyName, Class<?> clazz) {
if (!propertyGetters.containsKey(clazz) || !propertyGetters.get(clazz).containsKey(propertyName)) {
findGetAndCheckMethods(propertyName, clazz);
}
return propertyGetters.get(clazz).get(propertyName);
}
private boolean hasProperty(Object datum, String propertyName) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Method checker = getCheckMethod(propertyName, datum.getClass());
return checker == null || Boolean.TRUE.equals(checker.invoke(datum));
}
private boolean isCommonType(Class<?> clazz) {
return clazz.isPrimitive() || isWrapper(clazz);
}
private boolean isWrapper(Class<?> clazz) {
return WRAPPERS.contains(clazz);
}
private String normalize(String step) { // for faster subsequent query
// performance
return ARRAY_TOKEN.equals(step) ? ARRAY_TOKEN : step;
}
private Object readProperty(Object datum, String propertyName) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Method getter = getReadMethod(propertyName, datum.getClass());
return getter.invoke(datum);
}
private void resolve(Object datum, Record record, String fieldName) throws NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (datum == null) {
return;
}
Class<?> clazz = datum.getClass();
if (isCommonType(clazz)) {
record.put(fieldName, datum);
} else if (List.class.isAssignableFrom(clazz)) {
for (Object o : (List<?>) datum) {
resolve(o, record, fieldName);
}
} else {
Object extracted = extractValue(datum, clazz);
record.put(fieldName, extracted);
}
}
private static enum EnumExtractMethods {
name, toString, none
}
private static enum ObjectExtractMethods {
toByteArray, toString, none
}
}
}