Search in sources :

Example 6 with AfterBeanDiscovery

use of jakarta.enterprise.inject.spi.AfterBeanDiscovery in project helidon by oracle.

the class ConfigCdiExtension method registerConfigProducer.

/**
 * Register a config producer bean for each {@link org.eclipse.microprofile.config.inject.ConfigProperty} injection.
 *
 * @param abd event from CDI container
 */
private void registerConfigProducer(@Observes AfterBeanDiscovery abd) {
    // we also must support injection of Config itself
    abd.addBean().addTransitiveTypeClosure(org.eclipse.microprofile.config.Config.class).beanClass(org.eclipse.microprofile.config.Config.class).scope(ApplicationScoped.class).createWith(creationalContext -> new SerializableConfig());
    abd.addBean().addTransitiveTypeClosure(io.helidon.config.Config.class).beanClass(io.helidon.config.Config.class).scope(ApplicationScoped.class).createWith(creationalContext -> {
        Config config = ConfigProvider.getConfig();
        if (config instanceof io.helidon.config.Config) {
            return config;
        } else {
            return MpConfig.toHelidonConfig(config);
        }
    });
    Set<Type> types = ips.stream().map(InjectionPoint::getType).map(it -> {
        if (it instanceof Class) {
            Class<?> clazz = (Class<?>) it;
            if (clazz.isPrimitive()) {
                return REPLACED_TYPES.getOrDefault(clazz, clazz);
            }
        }
        return it;
    }).collect(Collectors.toSet());
    types.forEach(type -> {
        abd.addBean().addType(type).scope(Dependent.class).addQualifier(CONFIG_PROPERTY_LITERAL).produceWith(it -> produce(it.select(InjectionPoint.class).get()));
    });
    configBeans.values().forEach(beanDescriptor -> abd.addBean().addType(beanDescriptor.type()).addTransitiveTypeClosure(beanDescriptor.type()).qualifiers(ConfigProperties.Literal.NO_PREFIX).scope(Dependent.class).produceWith(it -> beanDescriptor.produce(it.select(InjectionPoint.class).get(), ConfigProvider.getConfig())));
}
Also used : Dependent(jakarta.enterprise.context.Dependent) ProcessObserverMethod(jakarta.enterprise.inject.spi.ProcessObserverMethod) MpConfig(io.helidon.config.mp.MpConfig) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AfterDeploymentValidation(jakarta.enterprise.inject.spi.AfterDeploymentValidation) ConfigException(io.helidon.config.ConfigException) Bean(jakarta.enterprise.inject.spi.Bean) ProcessBean(jakarta.enterprise.inject.spi.ProcessBean) Matcher(java.util.regex.Matcher) Converter(org.eclipse.microprofile.config.spi.Converter) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) Map(java.util.Map) Method(java.lang.reflect.Method) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Member(java.lang.reflect.Member) Set(java.util.Set) Config(org.eclipse.microprofile.config.Config) Logger(java.util.logging.Logger) ConfigValue(org.eclipse.microprofile.config.ConfigValue) Collectors(java.util.stream.Collectors) List(java.util.List) Type(java.lang.reflect.Type) WithAnnotations(jakarta.enterprise.inject.spi.WithAnnotations) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) NativeImageHelper(io.helidon.common.NativeImageHelper) Observes(jakarta.enterprise.event.Observes) OptionalDouble(java.util.OptionalDouble) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) OptionalLong(java.util.OptionalLong) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) AnnotatedMethod(jakarta.enterprise.inject.spi.AnnotatedMethod) Extension(jakarta.enterprise.inject.spi.Extension) ProcessAnnotatedType(jakarta.enterprise.inject.spi.ProcessAnnotatedType) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) LinkedHashSet(java.util.LinkedHashSet) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) CreationalContext(jakarta.enterprise.context.spi.CreationalContext) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Annotated(jakarta.enterprise.inject.spi.Annotated) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) MpConfig(io.helidon.config.mp.MpConfig) Config(org.eclipse.microprofile.config.Config) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Type(java.lang.reflect.Type) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) ProcessAnnotatedType(jakarta.enterprise.inject.spi.ProcessAnnotatedType)

Example 7 with AfterBeanDiscovery

use of jakarta.enterprise.inject.spi.AfterBeanDiscovery in project helidon by oracle.

the class GrpcClientCdiExtension method afterBean.

/**
 * Process the previously captured {@link GrpcProxy} injection points.
 * <p>
 * For each {@link GrpcProxy} injection point we create a producer bean
 * for the required type.
 *
 * @param event the {@link AfterBeanDiscovery} event
 * @param beanManager the CDI bean manager
 */
public void afterBean(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
    AnnotatedType<GrpcProxyProducer> producerType = beanManager.createAnnotatedType(GrpcProxyProducer.class);
    AnnotatedMethod<? super GrpcProxyProducer> producerMethod = producerType.getMethods().stream().filter(m -> m.isAnnotationPresent(GrpcProxy.class)).filter(m -> m.isAnnotationPresent(GrpcChannel.class)).findFirst().get();
    AnnotatedMethod<? super GrpcProxyProducer> inProcessMethod = producerType.getMethods().stream().filter(m -> m.isAnnotationPresent(GrpcProxy.class)).filter(m -> m.isAnnotationPresent(InProcessGrpcChannel.class)).findFirst().get();
    for (Type type : proxyTypes) {
        addProducerBean(event, beanManager, producerMethod, type);
    }
    for (Type type : inProcessProxyTypes) {
        addProducerBean(event, beanManager, inProcessMethod, type);
    }
}
Also used : InProcessGrpcChannel(io.helidon.microprofile.grpc.core.InProcessGrpcChannel) BeanAttributes(jakarta.enterprise.inject.spi.BeanAttributes) Observes(jakarta.enterprise.event.Observes) Set(java.util.Set) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) HashSet(java.util.HashSet) ProducerFactory(jakarta.enterprise.inject.spi.ProducerFactory) AnnotatedMethod(jakarta.enterprise.inject.spi.AnnotatedMethod) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Extension(jakarta.enterprise.inject.spi.Extension) Type(java.lang.reflect.Type) Annotated(jakarta.enterprise.inject.spi.Annotated) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Type(java.lang.reflect.Type) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) InProcessGrpcChannel(io.helidon.microprofile.grpc.core.InProcessGrpcChannel) InProcessGrpcChannel(io.helidon.microprofile.grpc.core.InProcessGrpcChannel)

Example 8 with AfterBeanDiscovery

use of jakarta.enterprise.inject.spi.AfterBeanDiscovery in project helidon by oracle.

the class CacheExtension method addBeans.

/*
     * create EmbeddedStorageManager beans
     */
private void addBeans(@Observes final AfterBeanDiscovery event, final BeanManager beanManager) {
    if (event != null && beanManager != null) {
        if (!this.cacheBeans.isEmpty()) {
            for (final Descriptor entry : this.cacheBeans) {
                assert entry != null;
                // create Microstream Cache bean
                final Set<Annotation> qualifiers = entry.getAnnotations();
                assert qualifiers != null;
                assert !qualifiers.isEmpty();
                ParameterizedType types = entry.getTypes();
                GenericType<?> keyType = GenericType.create(types.getActualTypeArguments()[0]);
                GenericType<?> valueType = GenericType.create(types.getActualTypeArguments()[1]);
                String name = getName(qualifiers);
                event.<Cache<?, ?>>addBean().qualifiers(qualifiers).scope(ApplicationScoped.class).addTransitiveTypeClosure(Cache.class).addTypes(types).createWith(cc -> {
                    return CacheBuilder.create(name, getConfigNode(qualifiers), keyType.rawType(), valueType.rawType());
                }).destroyWith((cache, context) -> cache.close());
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Cache(one.microstream.cache.types.Cache) Observes(jakarta.enterprise.event.Observes) Config(io.helidon.config.Config) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) CacheBuilder(io.helidon.integrations.microstream.cache.CacheBuilder) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Set(java.util.Set) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) GenericType(io.helidon.common.GenericType) HashSet(java.util.HashSet) Objects(java.util.Objects) ParameterizedType(java.lang.reflect.ParameterizedType) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Extension(jakarta.enterprise.inject.spi.Extension) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Priority(jakarta.annotation.Priority) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation) Cache(one.microstream.cache.types.Cache)

Aggregations

AfterBeanDiscovery (jakarta.enterprise.inject.spi.AfterBeanDiscovery)8 Extension (jakarta.enterprise.inject.spi.Extension)6 Observes (jakarta.enterprise.event.Observes)5 ProcessInjectionPoint (jakarta.enterprise.inject.spi.ProcessInjectionPoint)5 Set (java.util.Set)5 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)4 ProcessAnnotatedType (jakarta.enterprise.inject.spi.ProcessAnnotatedType)4 Annotation (java.lang.annotation.Annotation)4 HashSet (java.util.HashSet)4 Dependent (jakarta.enterprise.context.Dependent)3 AfterDeploymentValidation (jakarta.enterprise.inject.spi.AfterDeploymentValidation)3 BeanManager (jakarta.enterprise.inject.spi.BeanManager)3 BeforeBeanDiscovery (jakarta.enterprise.inject.spi.BeforeBeanDiscovery)3 Priority (jakarta.annotation.Priority)2 RequestScoped (jakarta.enterprise.context.RequestScoped)2 AfterTypeDiscovery (jakarta.enterprise.inject.spi.AfterTypeDiscovery)2 Annotated (jakarta.enterprise.inject.spi.Annotated)2 AnnotatedMethod (jakarta.enterprise.inject.spi.AnnotatedMethod)2 AnnotatedType (jakarta.enterprise.inject.spi.AnnotatedType)2 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)2