Search in sources :

Example 41 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.

the class TransactionScopedCDIUtil method fireEvent.

public static void fireEvent(String eventType) {
    BeanManager beanManager = null;
    try {
        beanManager = CDI.current().getBeanManager();
    } catch (Exception e) {
        log("Can't get instance of BeanManager to process TransactionScoped CDI Event!");
    }
    if (beanManager != null) {
        // TransactionScopedCDIEventHelperImpl AnnotatedType is created in Extension
        Set<Bean<?>> availableBeans = beanManager.getBeans(TransactionScopedCDIEventHelperImpl.class);
        if (null != availableBeans && !availableBeans.isEmpty()) {
            Bean<?> bean = beanManager.resolve(availableBeans);
            TransactionScopedCDIEventHelper eventHelper = (TransactionScopedCDIEventHelper) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(null));
            if (eventType.equalsIgnoreCase(INITIALIZED_EVENT)) {
                eventHelper.fireInitializedEvent(new TransactionScopedCDIEventPayload());
            } else {
                eventHelper.fireDestroyedEvent(new TransactionScopedCDIEventPayload());
            }
        }
    } else {
        log("Can't get instance of BeanManager to process TransactionScoped CDI Event!");
    }
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 42 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.

the class TransactionalInterceptorBase method getTransactionalAnnotation.

private Transactional getTransactionalAnnotation(InvocationContext invocationContext) {
    Optional<Transactional> optionalTransactional;
    // Try the Weld bindings first. This gives us the *exact* binding which caused this interceptor being called
    @SuppressWarnings("unchecked") Set<Annotation> bindings = (Set<Annotation>) invocationContext.getContextData().get("org.jboss.weld.interceptor.bindings");
    if (bindings != null) {
        optionalTransactional = bindings.stream().filter(annotation -> annotation.annotationType().equals(Transactional.class)).findAny().map(annotation -> Transactional.class.cast(annotation));
        if (optionalTransactional.isPresent()) {
            return optionalTransactional.get();
        }
    }
    BeanManager beanManager = CDI.current().getBeanManager();
    // Failing the Weld binding, check the method first
    optionalTransactional = getAnnotation(beanManager, invocationContext.getMethod(), Transactional.class);
    if (optionalTransactional.isPresent()) {
        return optionalTransactional.get();
    }
    // If nothing found on the method, check the the bean class
    optionalTransactional = getAnnotation(beanManager, invocationContext.getTarget().getClass(), Transactional.class);
    if (optionalTransactional.isPresent()) {
        return optionalTransactional.get();
    }
    // find it signals a critical error.
    throw new IllegalStateException("@Transactional not found on " + invocationContext.getTarget().getClass());
}
Also used : InvocationManager(org.glassfish.api.invocation.InvocationManager) InvocationContext(javax.interceptor.InvocationContext) Globals(org.glassfish.internal.api.Globals) LoggerInfo(org.glassfish.logging.annotation.LoggerInfo) NamingException(javax.naming.NamingException) SEVERE(java.util.logging.Level.SEVERE) Level(java.util.logging.Level) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) PreDestroy(javax.annotation.PreDestroy) NameNotFoundException(javax.naming.NameNotFoundException) FINE(java.util.logging.Level.FINE) Transaction(javax.transaction.Transaction) LogMessageInfo(org.glassfish.logging.annotation.LogMessageInfo) InitialContext(javax.naming.InitialContext) Transactional(javax.transaction.Transactional) WARNING(java.util.logging.Level.WARNING) Set(java.util.Set) CDI(javax.enterprise.inject.spi.CDI) Logger(java.util.logging.Logger) TransactionScopedCDIUtil.getAnnotation(org.glassfish.cdi.transaction.TransactionScopedCDIUtil.getAnnotation) Serializable(java.io.Serializable) SystemException(javax.transaction.SystemException) LogMessagesResourceBundle(org.glassfish.logging.annotation.LogMessagesResourceBundle) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TransactionOperationsManager(com.sun.enterprise.transaction.spi.TransactionOperationsManager) TransactionManager(javax.transaction.TransactionManager) BeanManager(javax.enterprise.inject.spi.BeanManager) Set(java.util.Set) BeanManager(javax.enterprise.inject.spi.BeanManager) TransactionScopedCDIUtil.getAnnotation(org.glassfish.cdi.transaction.TransactionScopedCDIUtil.getAnnotation) Annotation(java.lang.annotation.Annotation) Transactional(javax.transaction.Transactional)

Example 43 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.

the class ValidationNamingProxy method obtainBeanManager.

/**
 * Obtain the BeanManagerNamingProxy from hk2, so the BeanManager can be looked up
 *
 * @throws NamingException
 */
private synchronized BeanManager obtainBeanManager() throws NamingException {
    BeanManager beanManager = null;
    // Use invocation context to find applicable BeanDeploymentArchive.
    ComponentInvocation inv = invocationManager.getCurrentInvocation();
    if (inv != null) {
        JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId());
        if (componentEnv != null) {
            BundleDescriptor bundle = null;
            if (componentEnv instanceof EjbDescriptor) {
                bundle = (BundleDescriptor) ((EjbDescriptor) componentEnv).getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
            } else if (componentEnv instanceof WebBundleDescriptor) {
                bundle = (BundleDescriptor) componentEnv;
            }
            if (bundle != null) {
                BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
                if (bda != null) {
                    WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(bundle.getApplication());
                    beanManager = bootstrap.getManager(bda);
                }
            }
        }
    }
    return beanManager;
}
Also used : WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) JndiNameEnvironment(com.sun.enterprise.deployment.JndiNameEnvironment) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) BeanManager(javax.enterprise.inject.spi.BeanManager) EjbDescriptor(com.sun.enterprise.deployment.EjbDescriptor)

Example 44 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.

the class ValidationNamingProxy method handle.

@Override
public Object handle(String name) throws NamingException {
    // delegate to the java:comp/BeanManager handler to obtain the appropriate BeanManager
    BeanManager beanManager = obtainBeanManager();
    if (beanManager == null) {
        // There is no bean manager available, return and let BeanValidatorNamingProxy handle lookup..
        return null;
    }
    if (VALIDATOR_FACTORY_CONTEXT.equals(name)) {
        try {
            ValidatorFactory validatorFactory = (ValidatorFactory) getAndCreateBean(beanManager, ValidatorFactory.class);
            if (validatorFactory != null) {
                return validatorFactory;
            } else {
                throw new NamingException("Error retrieving " + name);
            }
        } catch (Throwable t) {
            NamingException ne = new NamingException("Error retrieving " + name);
            ne.initCause(t);
            throw ne;
        }
    } else if (VALIDATOR_CONTEXT.equals(name)) {
        try {
            Validator validator = (Validator) getAndCreateBean(beanManager, Validator.class);
            if (validator != null) {
                return validator;
            } else {
                throw new NamingException("Error retrieving " + name);
            }
        } catch (Throwable t) {
            NamingException ne = new NamingException("Error retrieving " + name);
            ne.initCause(t);
            throw ne;
        }
    } else {
        throw new NamingException("wrong handler for " + name);
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) NamingException(javax.naming.NamingException) BeanManager(javax.enterprise.inject.spi.BeanManager) Validator(javax.validation.Validator)

Example 45 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project tomee by apache.

the class HashServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    final BeanManager bm = HashCdiExtension.BMS.get(tccl);
    // todo: do some lookup
    resp.getWriter().write(Boolean.toString(bm != null));
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager)

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