Search in sources :

Example 56 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project wildfly by wildfly.

the class WeldContextSetup method teardown.

@SuppressWarnings("unchecked")
public void teardown(Map<String, Object> properties) {
    try {
        final BeanManager manager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
        if (manager != null && Container.available()) {
            final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
            CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
            final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
            sessionContext.invalidate();
            sessionContext.deactivate();
            sessionContext.dissociate(sessionContexts.get());
            final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(requestContextBean);
            final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
            requestContext.invalidate();
            requestContext.deactivate();
            requestContext.dissociate(requestContexts.get());
            final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(conversationContextBean);
            final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
            conversationContext.invalidate();
            conversationContext.deactivate();
            conversationContext.dissociate(boundRequests.get());
        }
    } catch (NamingException e) {
        WeldLogger.ROOT_LOGGER.failedToTearDownWeldContexts(e);
    } finally {
        sessionContexts.remove();
        requestContexts.remove();
        boundRequests.remove();
    }
}
Also used : BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) NamingException(javax.naming.NamingException) BeanManager(javax.enterprise.inject.spi.BeanManager) InitialContext(javax.naming.InitialContext) Bean(javax.enterprise.inject.spi.Bean)

Example 57 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project wildfly by wildfly.

the class WeldClassIntrospector method createInstance.

@Override
public ManagedReference createInstance(final Object instance) {
    final BeanManager beanManager = this.beanManager.getValue();
    final InjectionTarget injectionTarget = getInjectionTarget(instance.getClass());
    final CreationalContext context = beanManager.createCreationalContext(null);
    injectionTarget.inject(instance, context);
    injectionTarget.postConstruct(instance);
    return new WeldManagedReference(injectionTarget, context, instance);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 58 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project wildfly by wildfly.

the class CdiBeanValidationFactoryProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final DeploymentUnit topLevelDeployment = deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
    final ServiceName weldStartService = topLevelDeployment.getServiceName().append(ServiceNames.WELD_START_SERVICE_NAME);
    if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
        return;
    }
    if (!deploymentUnit.hasAttachment(BeanValidationAttachments.VALIDATOR_FACTORY)) {
        return;
    }
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    final ServiceName serviceName = deploymentUnit.getServiceName().append(CdiValidatorFactoryService.SERVICE_NAME);
    final CdiValidatorFactoryService cdiValidatorFactoryService = new CdiValidatorFactoryService(deploymentUnit);
    serviceTarget.addService(serviceName, cdiValidatorFactoryService).addDependency(ServiceNames.beanManagerServiceName(deploymentUnit), new CastingInjector<BeanManager>(cdiValidatorFactoryService.getBeanManagerInjector(), BeanManager.class)).addDependency(weldStartService).install();
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) ServiceTarget(org.jboss.msc.service.ServiceTarget) CdiValidatorFactoryService(org.jboss.as.weld.CdiValidatorFactoryService) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 59 with BeanManager

use of javax.enterprise.inject.spi.BeanManager 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 60 with BeanManager

use of javax.enterprise.inject.spi.BeanManager 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)

Aggregations

BeanManager (javax.enterprise.inject.spi.BeanManager)83 Bean (javax.enterprise.inject.spi.Bean)34 Test (org.junit.Test)16 NamingException (javax.naming.NamingException)7 Annotation (java.lang.annotation.Annotation)6 ArrayList (java.util.ArrayList)6 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)6 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)6 CreationalContext (javax.enterprise.context.spi.CreationalContext)5 InitialContext (javax.naming.InitialContext)5 ValidatorFactory (javax.validation.ValidatorFactory)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TransactionManager (javax.transaction.TransactionManager)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 Validator (javax.validation.Validator)3