Search in sources :

Example 1 with CamelRuntimeTaskBuildItem

use of org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem in project camel-quarkus by apache.

the class InjectionPointsProcessor method injectedComponents.

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void injectedComponents(CombinedIndexBuildItem index, InjectionPointsRecorder recorder, BeanRegistrationPhaseBuildItem beanRegistrationPhase, BuildProducer<SyntheticBeanBuildItem> syntheticBeans, BuildProducer<CamelRuntimeTaskBuildItem> runtimeTasks, BuildProducer<BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem> beanConfigurator) {
    final Collection<ClassInfo> components = index.getIndex().getAllKnownImplementors(INTERFACE_NAME_COMPONENT);
    final Set<String> created = new HashSet<>();
    for (InjectionPointInfo injectionPoint : beanRegistrationPhase.getContext().get(BuildExtension.Key.INJECTION_POINTS)) {
        if (injectionPoint.getTarget().kind() == AnnotationTarget.Kind.FIELD) {
            FieldInfo target = injectionPoint.getTarget().asField();
            if (!created.add(target.type().name().toString())) {
                continue;
            }
            if (components.stream().anyMatch(ci -> Objects.equals(ci.name(), target.type().name()))) {
                final AnnotationInstance named = target.annotation(ANNOTATION_NAME_NAMED);
                final String componentName = named == null ? target.name() : named.value().asString();
                final Supplier<?> creator = recorder.componentSupplier(componentName, target.type().toString());
                LOGGER.debugf("Creating synthetic component bean [name=%s, type=%s]", componentName, target.type().name());
                syntheticBeans.produce(syntheticBean(target.type().name(), creator));
            }
        }
        if (injectionPoint.getTarget().kind() == AnnotationTarget.Kind.METHOD) {
            final MethodInfo target = injectionPoint.getTarget().asMethod();
            final List<Type> types = target.parameters();
            for (int i = 0; i < types.size(); i++) {
                Type type = types.get(0);
                if (!created.add(type.name().toString())) {
                    continue;
                }
                if (components.stream().anyMatch(ci -> Objects.equals(ci.name(), type.name()))) {
                    final AnnotationInstance named = target.annotation(ANNOTATION_NAME_NAMED);
                    final String componentName = named == null ? target.parameterName(i) : named.value().asString();
                    final Supplier<?> creator = recorder.componentSupplier(componentName, type.toString());
                    LOGGER.debugf("Creating synthetic component bean [name=%s, type=%s]", componentName, type.name());
                    syntheticBeans.produce(syntheticBean(type.name(), creator));
                }
            }
        }
    }
    // Ensure the task is executed before the runtime is assembled
    runtimeTasks.produce(new CamelRuntimeTaskBuildItem("components-injection"));
    // Methods using BeanRegistrationPhaseBuildItem should return/produce a BeanConfiguratorBuildItem
    // otherwise the build step may be processed at the wrong time.
    // 
    // See BeanRegistrationPhaseBuildItem javadoc
    beanConfigurator.produce(new BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem());
}
Also used : Endpoint(org.apache.camel.Endpoint) BeanRegistrationPhaseBuildItem(io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem) Type(org.jboss.jandex.Type) InjectionPointInfo(io.quarkus.arc.processor.InjectionPointInfo) CamelRuntimeTaskBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem) MethodInfo(org.jboss.jandex.MethodInfo) FieldInfo(org.jboss.jandex.FieldInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo) HashSet(java.util.HashSet) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BeanRegistrationPhaseBuildItem (io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem)1 InjectionPointInfo (io.quarkus.arc.processor.InjectionPointInfo)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 Record (io.quarkus.deployment.annotations.Record)1 HashSet (java.util.HashSet)1 Endpoint (org.apache.camel.Endpoint)1 CamelRuntimeTaskBuildItem (org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 FieldInfo (org.jboss.jandex.FieldInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1 Type (org.jboss.jandex.Type)1