use of com.github.os72.protobuf.dynamic.DynamicSchema in project druid by druid-io.
the class FileBasedProtobufBytesDecoder method getDescriptor.
private Descriptors.Descriptor getDescriptor(String descriptorFilePath) {
InputStream fin;
fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath);
if (fin == null) {
URL url;
try {
url = new URL(descriptorFilePath);
} catch (MalformedURLException e) {
throw new ParseException(descriptorFilePath, e, "Descriptor not found in class path or malformed URL:" + descriptorFilePath);
}
try {
fin = url.openConnection().getInputStream();
} catch (IOException e) {
throw new ParseException(url.toString(), e, "Cannot read descriptor file: " + url);
}
}
DynamicSchema dynamicSchema;
try {
dynamicSchema = DynamicSchema.parseFrom(fin);
} catch (Descriptors.DescriptorValidationException e) {
throw new ParseException(null, e, "Invalid descriptor file: " + descriptorFilePath);
} catch (IOException e) {
throw new ParseException(null, e, "Cannot read descriptor file: " + descriptorFilePath);
}
Set<String> messageTypes = dynamicSchema.getMessageTypes();
if (messageTypes.size() == 0) {
throw new ParseException(null, "No message types found in the descriptor: " + descriptorFilePath);
}
String messageType = protoMessageType == null ? (String) messageTypes.toArray()[0] : protoMessageType;
Descriptors.Descriptor desc = dynamicSchema.getMessageDescriptor(messageType);
if (desc == null) {
throw new ParseException(null, StringUtils.format("Protobuf message type %s not found in the specified descriptor. Available messages types are %s", protoMessageType, messageTypes));
}
return desc;
}
Aggregations