Search in sources :

Example 46 with BeanManager

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

the class OpenEJBEnricher method resolve.

public static Object[] resolve(final AppContext appContext, final TestClass ignored, final Method method) {
    // suppose all is a CDI bean...
    final Object[] values = new Object[method.getParameterTypes().length];
    if (appContext == null) {
        return values;
    }
    final List<BeanManager> beanManagers = new ArrayList<>();
    final BeanManager bm = findBeanManager(appContext);
    if (bm != null) {
        // then add web bean manager first, TODO: selection of the webapp containing the test?
        for (final WebContext web : appContext.getWebContexts()) {
            final WebBeansContext webBeansContext = web.getWebBeansContext();
            if (webBeansContext == null) {
                continue;
            }
            final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
            if (bm != webAppBm) {
                beanManagers.add(webAppBm);
            }
        }
        beanManagers.add(bm);
    }
    if (beanManagers.isEmpty()) {
        return values;
    }
    final Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        Exception ex = null;
        for (final BeanManager beanManager : beanManagers) {
            try {
                values[i] = getParamInstance(beanManager, i, method);
                break;
            } catch (final Exception e) {
                ex = e;
            }
        }
        if (ex != null) {
            LOGGER.info(ex.getMessage());
        }
    }
    return values;
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) ArrayList(java.util.ArrayList) TestClass(org.jboss.arquillian.test.spi.TestClass) BeanManager(javax.enterprise.inject.spi.BeanManager) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 47 with BeanManager

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

the class OpenEJBLifecycle method initializeServletContext.

public static void initializeServletContext(final ServletContext servletContext, final WebBeansContext context) {
    if (context == null || !context.getBeanManagerImpl().isInUse()) {
        return;
    }
    final ELAdaptor elAdaptor = context.getService(ELAdaptor.class);
    final ELResolver resolver = elAdaptor.getOwbELResolver();
    // Application is configured as JSP
    if (context.getOpenWebBeansConfiguration().isJspApplication()) {
        logger.debug("Application is configured as JSP. Adding EL Resolver.");
        setJspELFactory(servletContext, resolver);
    }
    // Add BeanManager to the 'javax.enterprise.inject.spi.BeanManager' servlet context attribute
    servletContext.setAttribute(BeanManager.class.getName(), context.getBeanManagerImpl());
}
Also used : ELResolver(javax.el.ELResolver) BeanManager(javax.enterprise.inject.spi.BeanManager) ELAdaptor(org.apache.webbeans.spi.adaptor.ELAdaptor)

Example 48 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project jetty.project by eclipse.

the class WebSocketCdiListener method newInstance.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> ScopedInstance<T> newInstance(Class<T> clazz) {
    BeanManager bm = CDI.current().getBeanManager();
    ScopedInstance sbean = new ScopedInstance();
    Set<Bean<?>> beans = bm.getBeans(clazz, AnyLiteral.INSTANCE);
    if (beans.size() > 0) {
        sbean.bean = beans.iterator().next();
        sbean.creationalContext = bm.createCreationalContext(sbean.bean);
        sbean.instance = bm.getReference(sbean.bean, clazz, sbean.creationalContext);
        return sbean;
    } else {
        throw new RuntimeException(String.format("Can't find class %s", clazz));
    }
}
Also used : ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 49 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project javaee7-samples by javaee-samples.

the class GreetingTest method testJNDI.

@Test
public void testJNDI() throws Exception {
    // Third way to get BeanManager: name service
    BeanManager bm = InitialContext.doLookup("java:comp/BeanManager");
    test(bm);
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.junit.Test)

Example 50 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project javaee7-samples by javaee-samples.

the class GreetingTest method testCurrent.

@Test
public void testCurrent() throws Exception {
    // Second way to get BeanManager: current CDI container
    BeanManager bm = CDI.current().getBeanManager();
    test(bm);
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.junit.Test)

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