in library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/avro/AvroSchemaResolver.java [111:159]
private AvroSchema computeIfAbsent(Exchange exchange) {
if (this.schema != null) {
return this.schema;
}
AvroSchema answer = exchange.getProperty(SchemaHelper.CONTENT_SCHEMA, AvroSchema.class);
if (answer == null && exchange.getProperties().containsKey(SchemaHelper.SCHEMA)) {
String schemaJson = exchange.getProperty(SchemaHelper.SCHEMA, String.class);
Schema raw = new Schema.Parser().setValidate(validate).parse(schemaJson);
answer = new AvroSchema(raw);
}
if (answer == null) {
String contentClass = SchemaHelper.resolveContentClass(exchange, this.contentClass);
if (contentClass != null) {
answer = this.schemes.computeIfAbsent(contentClass, t -> {
Resource res = PluginHelper.getResourceLoader(exchange.getContext())
.resolveResource("classpath:schemas/" + SchemaType.AVRO.type() + "/" + t + "." + SchemaType.AVRO.type());
try {
if (res.exists()) {
try (InputStream is = res.getInputStream()) {
if (is != null) {
return Avro.MAPPER.schemaFrom(is);
}
}
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to load Avro schema for type: " + t + ", resource: " + res.getLocation(), e);
}
try {
return Avro.MAPPER.schemaFor(Class.forName(contentClass));
} catch (JsonMappingException | ClassNotFoundException e) {
throw new RuntimeException(
"Unable to compute Avro schema for type: " + t, e);
}
});
}
}
if (answer != null) {
this.schema = answer;
}
return answer;
}