Search in sources :

Example 51 with Bean

use of javax.enterprise.inject.spi.Bean in project camel by apache.

the class CdiCamelExtension method afterDeploymentValidation.

private void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager manager) {
    // Send event for Camel CDI configuration
    manager.fireEvent(configuration);
    configuration.unmodifiable();
    Collection<CamelContext> contexts = new ArrayList<>();
    for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
        contexts.add(getReference(manager, CamelContext.class, context));
    }
    // Add type converters to Camel contexts
    CdiTypeConverterLoader loader = new CdiTypeConverterLoader();
    for (Class<?> converter : converters) {
        for (CamelContext context : contexts) {
            loader.loadConverterMethods(context.getTypeConverterRegistry(), converter);
        }
    }
    // Add routes to Camel contexts
    if (configuration.autoConfigureRoutes()) {
        boolean deploymentException = false;
        Set<Bean<?>> routes = new HashSet<>(manager.getBeans(RoutesBuilder.class, ANY));
        routes.addAll(manager.getBeans(RouteContainer.class, ANY));
        for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
            for (Bean<?> route : routes) {
                Set<Annotation> qualifiers = new HashSet<>(context.getQualifiers());
                qualifiers.retainAll(route.getQualifiers());
                if (qualifiers.size() > 1) {
                    deploymentException |= !addRouteToContext(route, context, manager, adv);
                }
            }
        }
        // Let's return to avoid starting misconfigured contexts
        if (deploymentException) {
            return;
        }
    }
    // Trigger eager beans instantiation (calling toString is necessary to force
    // the initialization of normal-scoped beans).
    // FIXME: This does not work with OpenWebBeans for bean whose bean type is an
    // interface as the Object methods does not get forwarded to the bean instances!
    eagerBeans.forEach(type -> getReferencesByType(manager, type.getJavaClass(), ANY).toString());
    manager.getBeans(Object.class, ANY, STARTUP).forEach(bean -> getReference(manager, bean.getBeanClass(), bean).toString());
    // Start Camel contexts
    for (CamelContext context : contexts) {
        if (ServiceStatus.Started.equals(context.getStatus())) {
            continue;
        }
        logger.info("Camel CDI is starting Camel context [{}]", context.getName());
        try {
            context.start();
        } catch (Exception exception) {
            adv.addDeploymentProblem(exception);
        }
    }
    // Clean-up
    Stream.of(converters, camelBeans, eagerBeans, cdiBeans).forEach(Set::clear);
    Stream.of(producerBeans, producerQualifiers).forEach(Map::clear);
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) RouteContainer(org.apache.camel.model.RouteContainer) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) CdiSpiHelper.hasAnnotation(org.apache.camel.cdi.CdiSpiHelper.hasAnnotation) InjectionException(javax.enterprise.inject.InjectionException) ProcessBean(javax.enterprise.inject.spi.ProcessBean) Bean(javax.enterprise.inject.spi.Bean) EventObject(java.util.EventObject) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collections.newSetFromMap(java.util.Collections.newSetFromMap) RoutesBuilder(org.apache.camel.RoutesBuilder) HashSet(java.util.HashSet)

Example 52 with Bean

use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.

the class AuditEntityListener method update.

@PreUpdate
public void update(Object entity) {
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    Set<Bean<?>> beans = beanManager.getBeans(PreUpdateAuditListener.class);
    for (Bean<?> bean : beans) {
        PreUpdateAuditListener result = (PreUpdateAuditListener) beanManager.getReference(bean, PreUpdateAuditListener.class, beanManager.createCreationalContext(bean));
        result.preUpdate(entity);
    }
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) PreUpdate(javax.persistence.PreUpdate)

Example 53 with Bean

use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.

the class EntityManagerRefLookup method initGlobalEntityManager.

private synchronized void initGlobalEntityManager() {
    // switch into paranoia mode
    if (this.globalEntityManagerInitialized == null) {
        this.globalEntityManagerInitialized = true;
        BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
        Bean<?> bean = beanManager.resolve(beans);
        if (bean == null) {
            throw new IllegalStateException("Could not find EntityManager with default qualifier.");
        }
        globalEntityManagerIsNormalScope = beanManager.isNormalScope(bean.getScope());
        if (globalEntityManagerIsNormalScope) {
            globalEntityManager = (EntityManager) beanManager.getReference(bean, EntityManager.class, beanManager.createCreationalContext(bean));
        }
    }
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 54 with Bean

use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.

the class PrincipalProvider method resolvePrincipal.

private Object resolvePrincipal(Object entity, Property<Object> property) {
    CurrentUser principal = AnnotationInstanceProvider.of(CurrentUser.class);
    Class<?> propertyClass = property.getJavaClass();
    Set<Bean<?>> beans = manager.getBeans(propertyClass, principal);
    if (!beans.isEmpty() && beans.size() == 1) {
        Bean<?> bean = beans.iterator().next();
        Object result = manager.getReference(bean, propertyClass, manager.createCreationalContext(bean));
        return result;
    }
    throw new IllegalArgumentException("Principal " + (beans.isEmpty() ? "not found" : "not unique") + " for " + propertyName(entity, property));
}
Also used : CurrentUser(org.apache.deltaspike.data.api.audit.CurrentUser) Bean(javax.enterprise.inject.spi.Bean)

Example 55 with Bean

use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.

the class RequestServlet method getRequestScopedBean.

private RequestScopedBean getRequestScopedBean() {
    BeanManager beanManager = CdiContainerLoader.getCdiContainer().getBeanManager();
    if (beanManager == null) {
        return null;
    }
    Set<Bean<?>> beans = beanManager.getBeans(RequestScopedBean.class);
    Bean<RequestScopedBean> reqScpdBean = (Bean<RequestScopedBean>) beanManager.resolve(beans);
    CreationalContext<RequestScopedBean> reqScpdCC = beanManager.createCreationalContext(reqScpdBean);
    RequestScopedBean instance = (RequestScopedBean) beanManager.getReference(reqScpdBean, RequestScopedBean.class, reqScpdCC);
    return instance;
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)74 BeanManager (javax.enterprise.inject.spi.BeanManager)33 Annotation (java.lang.annotation.Annotation)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)8 CreationalContext (javax.enterprise.context.spi.CreationalContext)7 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)7 Test (org.junit.Test)7 Set (java.util.Set)5 ProcessBean (javax.enterprise.inject.spi.ProcessBean)5 ScopedInstance (org.eclipse.jetty.cdi.core.ScopedInstance)5 IOException (java.io.IOException)4 Type (java.lang.reflect.Type)4 Map (java.util.Map)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)3 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)3 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)3