Java Code Examples for javax.enterprise.inject.spi.AfterBeanDiscovery#addBean()

The following examples show how to use javax.enterprise.inject.spi.AfterBeanDiscovery#addBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigExtension.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
protected void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
    Set<Class<?>> customTypes = new HashSet<>();
    for (InjectionPoint ip : injectionPoints) {
        Type requiredType = ip.getType();
        if (requiredType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) requiredType;
            // TODO We should probably handle all parameterized types correctly
            if (type.getRawType().equals(Provider.class) || type.getRawType().equals(Instance.class)) {
                // These injection points are satisfied by the built-in Instance bean 
                Type typeArgument = type.getActualTypeArguments()[0];
                if (typeArgument instanceof Class && !isClassHandledByConfigProducer(typeArgument)) {
                    customTypes.add((Class<?>) typeArgument);
                }
            }
        } else if (requiredType instanceof Class
                && !isClassHandledByConfigProducer(requiredType)) {
            // type is not produced by ConfigProducer
            customTypes.add((Class<?>) requiredType);
        }
    }

    for (Class<?> customType : customTypes) {
        abd.addBean(new ConfigInjectionBean(bm, customType));
    }
}
 
Example 2
Source File: ExtraJCacheExtension.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
{
    if (!ACTIVATED)
    {
        return;
    }

    if (cacheManagerFound && cacheProviderFound) {
        return;
    }

    cachingProvider = Caching.getCachingProvider();
    if (!cacheManagerFound)
    {
        cacheManager = cachingProvider.getCacheManager(
                cachingProvider.getDefaultURI(),
                cachingProvider.getDefaultClassLoader(),
                new Properties());
        afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
    }
    if (!cacheProviderFound)
    {
        afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
    }
}
 
Example 3
Source File: ConfigurationExtension.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public void addDynamicBeans(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager bm)
{
    if (dynamicProducer != null && !dynamicConfigTypes.isEmpty())
    {
        afterBeanDiscovery.addBean(new DynamicBean(dynamicProducer, dynamicConfigTypes));
    }
    for (final Class<?> proxyType : dynamicConfigurationBeanClasses)
    {
        afterBeanDiscovery.addBean(new BeanBuilder(null)
                .types(proxyType, Object.class)
                .qualifiers(new DefaultLiteral(), new AnyLiteral())
                .beanLifecycle(new ProxyConfigurationLifecycle(proxyType))
                .scope(ApplicationScoped.class)
                .passivationCapable(true)
                .id("DeltaSpikeConfiguration#" + proxyType.getName())
                .beanClass(proxyType)
                .create());
    }
}
 
Example 4
Source File: ConverterAndValidatorProxyExtension.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public <X> void createBeans(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
{
    if (!this.isActivated)
    {
        return;
    }

    for (Class<?> originalClass : this.classesToProxy)
    {
        Bean bean = createBean(originalClass, beanManager);

        if (bean != null)
        {
            afterBeanDiscovery.addBean(bean);
        }
    }

    this.classesToProxy.clear();
}
 
Example 5
Source File: JtaExtension.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
void addContextAndBeans(@Observes final AfterBeanDiscovery afterBeanDiscovery, final BeanManager bm) {
    context = new TransactionContext();
    afterBeanDiscovery.addContext(context);

    if (!hasManager && !hasRegistry) {
        try {
            final GeronimoTransactionManager mgr = new GeronimoTransactionManager();
            afterBeanDiscovery.addBean(new JtaBean(mgr));
        } catch (final XAException e) {
            throw new IllegalStateException(e);
        }
        hasManager = true;
        hasRegistry = true;
    }

    afterBeanDiscovery.addBean(new JtaConfigBean(config));
}
 
Example 6
Source File: ExtraJCacheExtension.java    From jcache-cdi with Apache License 2.0 6 votes vote down vote up
public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
{
    if (!ACTIVATED)
    {
        return;
    }

    if (cacheManagerFound && cacheProviderFound) {
        return;
    }

    cachingProvider = Caching.getCachingProvider();
    if (!cacheManagerFound)
    {
        cacheManager = cachingProvider.getCacheManager(
                cachingProvider.getDefaultURI(),
                cachingProvider.getDefaultClassLoader(),
                new Properties());
        afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
    }
    if (!cacheProviderFound)
    {
        afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
    }
}
 
Example 7
Source File: WeldCDIExtension.java    From weld-junit with Apache License 2.0 5 votes vote down vote up
void afterBeandiscovery(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
    if (scopesToActivate != null) {
        for (Class<? extends Annotation> scope : scopesToActivate) {
            ContextImpl ctx = new ContextImpl(scope, beanManager);
            contexts.add(ctx);
            event.addContext(ctx);
        }
    }
    if (beans != null) {
        for (Bean<?> bean : beans) {
            event.addBean(bean);
        }
    }
}
 
Example 8
Source File: ConfigViewProducingExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    try (AutoCloseable handle = Performance.time("ConfigViewProducingExtension.afterBeanDiscovery")) {
        CommonBean<ConfigView> configViewBean = CommonBeanBuilder.newBuilder(ConfigView.class)
                .beanClass(ConfigViewProducingExtension.class)
                .scope(Singleton.class)
                .addQualifier(Default.Literal.INSTANCE)
                .createSupplier(() -> configView)
                .addType(ConfigView.class)
                .addType(Object.class).build();
        abd.addBean(configViewBean);
    }
}
 
Example 9
Source File: XMLConfigProducingExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    try (AutoCloseable handle = Performance.time("XMLConfigProducingExtension.afterBeanDiscovery")) {
        CommonBean<URL> urlBean = CommonBeanBuilder.newBuilder(URL.class)
                .beanClass(XMLConfigProducingExtension.class)
                .scope(Dependent.class)
                .addQualifier(XMLConfig.Literal.INSTANCE)
                .createSupplier(this::getXMLConfig)
                .addType(URL.class)
                .addType(Object.class).build();
        abd.addBean(urlBean);
    }
}
 
Example 10
Source File: OutboundSocketBindingExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    try (AutoCloseable handle = Performance.time("OutboundSocketBindingExtension.afterBeanDiscovery")) {
        for (OutboundSocketBindingRequest each : this.bindings) {

            Supplier<Customizer> customizerSupplier = () -> (Customizer) () -> {
                Set<Bean<?>> groups = beanManager.getBeans(SocketBindingGroup.class, Any.Literal.INSTANCE);

                groups.stream()
                        .map((Bean<?> e) -> {
                            CreationalContext<?> ctx = beanManager.createCreationalContext(e);
                            return (SocketBindingGroup) beanManager.getReference(e, SocketBindingGroup.class, ctx);
                        })
                        .filter(group -> group.name().equals(each.socketBindingGroup()))
                        .findFirst()
                        .ifPresent((group) -> group.outboundSocketBinding(each.outboundSocketBinding()));
            };
            CommonBean<Customizer> customizerBean = CommonBeanBuilder.newBuilder(Customizer.class)
                    .beanClass(OutboundSocketBindingExtension.class)
                    .scope(Singleton.class)
                    .addQualifier(Pre.Literal.INSTANCE)
                    .addQualifier(Any.Literal.INSTANCE)
                    .createSupplier(customizerSupplier)
                    .addType(Customizer.class)
                    .addType(Object.class).build();
            abd.addBean(customizerBean);
        }
    }
}
 
Example 11
Source File: ConfigurableExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unused"})
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) throws Exception {
    try (AutoCloseable handle = Performance.time("ConfigurationExtension.afterBeanDiscovery")) {
        CommonBean<ConfigurableManager> configurableManagerBean = CommonBeanBuilder.newBuilder(ConfigurableManager.class)
                .beanClass(ConfigurableManager.class)
                .scope(Singleton.class)
                .addQualifier(Default.Literal.INSTANCE)
                .createSupplier(() -> configurableManager)
                .addType(ConfigurableManager.class)
                .addType(Object.class).build();
        abd.addBean(configurableManagerBean);
    }
}
 
Example 12
Source File: InterfaceExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {

    List<SimpleKey> configuredInterfaces = this.configView.simpleSubkeys(ROOT);

    for (SimpleKey interfaceName : configuredInterfaces) {

        Set<Bean<?>> ifaces = beanManager.getBeans(Interface.class, Any.Literal.INSTANCE);

        if (ifaces
                .stream()
                .noneMatch(e -> e.getQualifiers()
                        .stream()
                        .anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(interfaceName + "-interface")))) {

            Interface iface = new Interface(interfaceName.name(), "0.0.0.0");
            applyConfiguration(iface);
            CommonBean<Interface> interfaceBean = CommonBeanBuilder.newBuilder(Interface.class)
                        .beanClass(InterfaceExtension.class)
                        .scope(ApplicationScoped.class)
                        .addQualifier(Any.Literal.INSTANCE)
                        .addQualifier(NamedLiteral.of(interfaceName.name() + "-interface"))
                        .createSupplier(() -> iface)
                        .addType(Interface.class)
                        .addType(Object.class)
                        .build();

            abd.addBean(interfaceBean);
        }
    }
}
 
Example 13
Source File: PartialBeanBindingExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public <X> void createBeans(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
{
    if (!this.isActivated)
    {
        return;
    }

    if (this.definitionError != null)
    {
        afterBeanDiscovery.addDefinitionError(this.definitionError);
        return;
    }

    for (Map.Entry<Class<? extends Annotation>, PartialBeanDescriptor> entry : this.descriptors.entrySet())
    {
        PartialBeanDescriptor descriptor = entry.getValue();
        if (descriptor.getClasses() != null)
        {
            for (Class partialBeanClass : descriptor.getClasses())
            {
                Bean partialBean = createPartialBean(partialBeanClass, descriptor, afterBeanDiscovery, beanManager);
                if (partialBean != null)
                {
                    afterBeanDiscovery.addBean(partialBean);

                    List<Bean> partialProducerBeans =
                        createPartialProducersDefinedIn(partialBean, afterBeanDiscovery, beanManager);

                    for (Bean partialProducerBean : partialProducerBeans)
                    {
                        afterBeanDiscovery.addBean(partialProducerBean);
                    }
                }
            }
        }
    }

    this.descriptors.clear();
}
 
Example 14
Source File: HealthCheckExtension.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
private void defaultHealthCheckRegistry(@Observes AfterBeanDiscovery abd, BeanManager manager) {
    if (manager.getBeans(HealthCheckRegistry.class).isEmpty())
        abd.addBean(new SyntheticBean<HealthCheckRegistry>(manager, HealthCheckRegistry.class, "health-check-registry", "Default Health Check Registry Bean"));
}
 
Example 15
Source File: GraphQlClientExtension.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
public void createProxies(@Observes AfterBeanDiscovery afterBeanDiscovery) {
    for (Class<?> api : apis) {
        afterBeanDiscovery.addBean(new GraphQlClientBean<>(api));
    }
}
 
Example 16
Source File: FlywayExtension.java    From hammock with Apache License 2.0 4 votes vote down vote up
public void addFlywayBean(@Observes AfterBeanDiscovery afterBeanDiscovery) {
   if(!foundFlywayBean) {
      LOG.info("Installing default Flyway bean");
      afterBeanDiscovery.addBean(new FlywayBean());
   }
}
 
Example 17
Source File: MetricsExtension.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
private void defaultMetricRegistry(@Observes AfterBeanDiscovery abd, BeanManager manager) {
    if (manager.getBeans(MetricRegistry.class).isEmpty())
        abd.addBean(new SyntheticBean<MetricRegistry>(manager, MetricRegistry.class, "metric-registry", "Default Metric Registry Bean"));
}
 
Example 18
Source File: InjectConfigViewExtension.java    From thorntail with Apache License 2.0 4 votes vote down vote up
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) {
    abd.addBean(new ConfigViewBean(configView));
}
 
Example 19
Source File: InjectJoseExtension.java    From thorntail with Apache License 2.0 4 votes vote down vote up
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) {
    abd.addBean(new JoseBean(jose));
}
 
Example 20
Source File: AxonCdiExtension.java    From cdi with Apache License 2.0 4 votes vote down vote up
private <T> void addIfNotConfigured(Class<T> componentType, Producer<T> componentProducer,
        Supplier<T> componentSupplier, AfterBeanDiscovery afterBeanDiscovery) {
    if (componentProducer == null) {
        afterBeanDiscovery.addBean(new BeanWrapper<>(componentType, componentSupplier));
    }
}