JiriOndrusek commented on code in PR #9018: URL: https://github.com/apache/camel-quarkus/pull/9018#discussion_r3805315777
########## extensions/langchain4j-ingest/deployment/src/main/java/org/apache/camel/quarkus/component/langchain4j/ingest/deployment/Langchain4jIngestProcessor.java: ########## @@ -0,0 +1,224 @@ +/* + * 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.langchain4j.ingest.deployment; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; +import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem; +import io.quarkus.arc.deployment.ValidationPhaseBuildItem.ValidationErrorBuildItem; +import io.quarkus.deployment.annotations.BuildProducer; +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.annotations.Consume; +import io.quarkus.deployment.annotations.ExecutionTime; +import io.quarkus.deployment.annotations.Record; +import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem; +import io.quarkus.deployment.builditem.CombinedIndexBuildItem; +import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import io.quarkus.runtime.configuration.ConfigurationException; +import jakarta.inject.Singleton; +import org.apache.camel.quarkus.component.langchain4j.ingest.Ingest; +import org.apache.camel.quarkus.component.langchain4j.ingest.IngestBuildTimeConfig; +import org.apache.camel.quarkus.component.langchain4j.ingest.IngestBuilderPipelines; +import org.apache.camel.quarkus.component.langchain4j.ingest.IngestPipeline; +import org.apache.camel.quarkus.component.langchain4j.ingest.IngestRoutes; +import org.apache.camel.quarkus.component.langchain4j.ingest.Langchain4jIngestRecorder; +import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem; +import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem; +import org.apache.camel.quarkus.core.deployment.spi.CamelServiceBuildItem; +import org.apache.camel.quarkus.core.deployment.util.CamelSupport; +import org.apache.camel.quarkus.core.deployment.util.PathFilter; +import org.apache.camel.util.URISupport; +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationTarget; +import org.jboss.jandex.DotName; +import org.jboss.jandex.MethodInfo; + +class Langchain4jIngestProcessor { + + private static final String FEATURE = "camel-langchain4j-ingest"; + + @BuildStep + FeatureBuildItem feature() { + return new FeatureBuildItem(FEATURE); + } + + @BuildStep + AdditionalBeanBuildItem beans() { + return AdditionalBeanBuildItem.builder() + .addBeanClasses(IngestRoutes.class) + .setUnremovable() + .build(); + } + + /** + * Discovers {@code @Ingest} builder methods: validated here (return type, no parameters, + * unique names, no collision with configuration-declared pipelines), invoked reflectively once + * at startup. + */ + @BuildStep + @Record(ExecutionTime.STATIC_INIT) + void discoverBuilderPipelines( + CombinedIndexBuildItem combinedIndex, + IngestBuildTimeConfig config, + Langchain4jIngestRecorder recorder, + BuildProducer<AdditionalBeanBuildItem> beans, + BuildProducer<ReflectiveClassBuildItem> reflectiveClasses, + BuildProducer<SyntheticBeanBuildItem> syntheticBeans) { + + DotName ingestAnnotation = DotName.createSimple(Ingest.class.getName()); + DotName pipelineType = DotName.createSimple(IngestPipeline.class.getName()); + + List<String> flatEntries = new ArrayList<>(); + Set<String> names = new HashSet<>(); + Set<String> beanClasses = new HashSet<>(); + + for (AnnotationInstance annotation : combinedIndex.getIndex().getAnnotations(ingestAnnotation)) { + if (annotation.target().kind() != AnnotationTarget.Kind.METHOD) { + continue; + } + MethodInfo method = annotation.target().asMethod(); + String name = annotation.value().asString(); + String location = method.declaringClass().name() + "#" + method.name(); + + if (name.isBlank()) { + throw new ConfigurationException("@Ingest on " + location + " has a blank pipeline name"); + } + if (!method.returnType().name().equals(pipelineType)) { + throw new ConfigurationException("@Ingest method " + location + " must return " + + IngestPipeline.class.getSimpleName()); + } + if (!method.parameters().isEmpty()) { + throw new ConfigurationException("@Ingest method " + location + " must take no parameters"); + } + // the method is invoked on a CDI bean instance, which a static method would bypass + // and a private one would run against the client proxy, seeing null injected fields + if (Modifier.isPrivate(method.flags()) || Modifier.isStatic(method.flags())) { + throw new ConfigurationException("@Ingest method " + location + " must not be private or static"); Review Comment: Done — final methods and final declaring classes are rejected at build time (new test), and @Dependent beans are destroyed after the invocation via Instance.getHandle(). -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
