ppalaga commented on a change in pull request #1239: URL: https://github.com/apache/camel-quarkus/pull/1239#discussion_r426925410
########## File path: extensions/avro/runtime/src/main/java/org/apache/camel/quarkus/component/avro/AvroSchemaRegistry.java ########## @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.quarkus.component.avro; + +import java.util.HashMap; + +import org.apache.avro.Schema; + +public class AvroSchemaRegistry extends HashMap<String, Schema> { Review comment: I was about to say that as long as we need just Map.get() and Map.put(), we should prefer aggregation to inheritance. But in the end, can't we fully avoid the AvroSchemaRegistry class in this specific case and use a stock HashMap instead? ########## File path: extensions/avro/deployment/src/main/java/org/apache/camel/quarkus/component/avro/deployment/AvroProcessor.java ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.quarkus.component.avro.deployment; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; +import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem; +import io.quarkus.arc.deployment.BeanContainerBuildItem; +import io.quarkus.arc.processor.AnnotationsTransformer; +import io.quarkus.deployment.annotations.BuildProducer; +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.annotations.ExecutionTime; +import io.quarkus.deployment.annotations.Record; +import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.deployment.builditem.ObjectSubstitutionBuildItem; +import io.quarkus.deployment.builditem.ObjectSubstitutionBuildItem.Holder; +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import org.apache.avro.Schema; +import org.apache.avro.SchemaParseException; +import org.apache.avro.generic.GenericContainer; +import org.apache.camel.quarkus.component.avro.AvroDataFormatProducer; +import org.apache.camel.quarkus.component.avro.AvroRecorder; +import org.apache.camel.quarkus.component.avro.AvroSchemaRegistry; +import org.apache.camel.quarkus.component.avro.AvroSchemaRegistrySubstitution; +import org.apache.camel.quarkus.component.avro.BuildTimeAvroDataFormat; +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.DotName; +import org.jboss.jandex.FieldInfo; +import org.jboss.jandex.IndexView; +import org.jboss.logging.Logger; + +class AvroProcessor { + + private static final Logger LOG = Logger.getLogger(AvroProcessor.class); + private static final String FEATURE = "camel-avro"; + private static DotName BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION = DotName + .createSimple(BuildTimeAvroDataFormat.class.getName()); + + @BuildStep + FeatureBuildItem feature() { + return new FeatureBuildItem(FEATURE); + } + + @BuildStep + List<ReflectiveClassBuildItem> reflectiveClasses() { + List<ReflectiveClassBuildItem> items = new ArrayList<ReflectiveClassBuildItem>(); + items.add(new ReflectiveClassBuildItem(false, false, GenericContainer.class)); + return items; + } + + @BuildStep + void additionalBeanClasses(BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer) { + additionalBeanProducer.produce(new AdditionalBeanBuildItem(AvroDataFormatProducer.class)); + } + + @BuildStep + AnnotationsTransformerBuildItem markFieldsAnnotatedWithBuildTimeAvroDataFormatAsInjectable() { + return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() { + + public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) { + return kind == org.jboss.jandex.AnnotationTarget.Kind.FIELD; + } + + @Override + public void transform(TransformationContext ctx) { + FieldInfo fieldInfo = ctx.getTarget().asField(); + if (fieldInfo.annotation(BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION) != null) { + ctx.transform().add(Inject.class).done(); + } + } + }); + } + + @BuildStep + void overrideAvroSchemaRegistrySerialization(BuildProducer<ObjectSubstitutionBuildItem> substitutions) { + Holder<AvroSchemaRegistry, byte[]> holder = new Holder(AvroSchemaRegistry.class, byte[].class, + AvroSchemaRegistrySubstitution.class); + substitutions.produce(new ObjectSubstitutionBuildItem(holder)); + } + + @Record(ExecutionTime.STATIC_INIT) + @BuildStep + void recordAvroSchemaRegistryInitialization(BeanArchiveIndexBuildItem beanArchiveIndex, + BeanContainerBuildItem beanContainer, AvroRecorder avroRecorder) { + AvroSchemaRegistry avroSchemaRegistry = new AvroSchemaRegistry(); + IndexView index = beanArchiveIndex.getIndex(); + for (AnnotationInstance annotation : index.getAnnotations(BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION)) { + String schemaResourceName = annotation.value().asString(); + FieldInfo fieldInfo = annotation.target().asField(); + String injectedFieldId = fieldInfo.declaringClass().name() + "." + fieldInfo.name(); + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaResourceName)) { + Schema avroSchema = new Schema.Parser().parse(is); + avroSchemaRegistry.put(injectedFieldId, avroSchema); + LOG.debug("Parsed the avro schema at build time from resource named " + schemaResourceName); + } catch (SchemaParseException | IOException ex) { + final String message = "An issue occured while parsing schema resource on field " + injectedFieldId; + throw new BuildTimeAvroSchemaParseException(message, ex); Review comment: Couldn't we throw a stock RuntimeException here and avoid having the BuildTimeAvroSchemaParseException class? ########## File path: extensions/avro/runtime/src/main/java/org/apache/camel/quarkus/component/avro/AvroSchemaRegistrySubstitution.java ########## @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.quarkus.component.avro; + +import io.quarkus.runtime.ObjectSubstitution; +import org.apache.commons.lang3.SerializationUtils; + +/** + * {@code AvroSchemaRegistrySubstitution} offers a way to bypass Quarkus + * default serialization. This workaround has been introduced as Quarkus default + * serialization used to failed with {@code org.apache.avro.Schema}. + */ +public class AvroSchemaRegistrySubstitution implements ObjectSubstitution<AvroSchemaRegistry, byte[]> { + + @Override + public byte[] serialize(AvroSchemaRegistry registry) { + return SerializationUtils.serialize(registry); Review comment: SerializationUtils.(de)serialize() seem to be the only methods used from commons-lang3. Inlining these methods (which should go quite easily) would allow us to eliminate the commons-lang3 dependency and thus reduce number of classes GraalVM has to analyze and eo ipso speed up the native image builds. ########## File path: extensions/avro/deployment/src/main/java/org/apache/camel/quarkus/component/avro/deployment/AvroProcessor.java ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.quarkus.component.avro.deployment; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; +import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem; +import io.quarkus.arc.deployment.BeanContainerBuildItem; +import io.quarkus.arc.processor.AnnotationsTransformer; +import io.quarkus.deployment.annotations.BuildProducer; +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.annotations.ExecutionTime; +import io.quarkus.deployment.annotations.Record; +import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.deployment.builditem.ObjectSubstitutionBuildItem; +import io.quarkus.deployment.builditem.ObjectSubstitutionBuildItem.Holder; +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import org.apache.avro.Schema; +import org.apache.avro.SchemaParseException; +import org.apache.avro.generic.GenericContainer; +import org.apache.camel.quarkus.component.avro.AvroDataFormatProducer; +import org.apache.camel.quarkus.component.avro.AvroRecorder; +import org.apache.camel.quarkus.component.avro.AvroSchemaRegistry; +import org.apache.camel.quarkus.component.avro.AvroSchemaRegistrySubstitution; +import org.apache.camel.quarkus.component.avro.BuildTimeAvroDataFormat; +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.DotName; +import org.jboss.jandex.FieldInfo; +import org.jboss.jandex.IndexView; +import org.jboss.logging.Logger; + +class AvroProcessor { + + private static final Logger LOG = Logger.getLogger(AvroProcessor.class); + private static final String FEATURE = "camel-avro"; + private static DotName BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION = DotName + .createSimple(BuildTimeAvroDataFormat.class.getName()); + + @BuildStep + FeatureBuildItem feature() { + return new FeatureBuildItem(FEATURE); + } + + @BuildStep + List<ReflectiveClassBuildItem> reflectiveClasses() { + List<ReflectiveClassBuildItem> items = new ArrayList<ReflectiveClassBuildItem>(); + items.add(new ReflectiveClassBuildItem(false, false, GenericContainer.class)); + return items; + } + + @BuildStep + void additionalBeanClasses(BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer) { + additionalBeanProducer.produce(new AdditionalBeanBuildItem(AvroDataFormatProducer.class)); + } + + @BuildStep + AnnotationsTransformerBuildItem markFieldsAnnotatedWithBuildTimeAvroDataFormatAsInjectable() { + return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() { + + public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) { + return kind == org.jboss.jandex.AnnotationTarget.Kind.FIELD; + } + + @Override + public void transform(TransformationContext ctx) { + FieldInfo fieldInfo = ctx.getTarget().asField(); + if (fieldInfo.annotation(BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION) != null) { + ctx.transform().add(Inject.class).done(); + } + } + }); + } + + @BuildStep + void overrideAvroSchemaRegistrySerialization(BuildProducer<ObjectSubstitutionBuildItem> substitutions) { + Holder<AvroSchemaRegistry, byte[]> holder = new Holder(AvroSchemaRegistry.class, byte[].class, + AvroSchemaRegistrySubstitution.class); + substitutions.produce(new ObjectSubstitutionBuildItem(holder)); + } + + @Record(ExecutionTime.STATIC_INIT) + @BuildStep + void recordAvroSchemaRegistryInitialization(BeanArchiveIndexBuildItem beanArchiveIndex, + BeanContainerBuildItem beanContainer, AvroRecorder avroRecorder) { + AvroSchemaRegistry avroSchemaRegistry = new AvroSchemaRegistry(); + IndexView index = beanArchiveIndex.getIndex(); + for (AnnotationInstance annotation : index.getAnnotations(BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION)) { + String schemaResourceName = annotation.value().asString(); + FieldInfo fieldInfo = annotation.target().asField(); + String injectedFieldId = fieldInfo.declaringClass().name() + "." + fieldInfo.name(); + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaResourceName)) { + Schema avroSchema = new Schema.Parser().parse(is); + avroSchemaRegistry.put(injectedFieldId, avroSchema); + LOG.debug("Parsed the avro schema at build time from resource named " + schemaResourceName); + } catch (SchemaParseException | IOException ex) { + final String message = "An issue occured while parsing schema resource on field " + injectedFieldId; + throw new BuildTimeAvroSchemaParseException(message, ex); + } + } + avroRecorder.recordDataFormatProducerRegistryInitialization(beanContainer.getValue(), avroSchemaRegistry); Review comment: I wonder whether it would not be safer to not to rely on HashMap serialization and instead create the HashMap in the recorder and pass the key-schema pairs one by one there. We'd thus still depend on Schema serialization but the amount of possible breakage would be a bit smaller. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org