Search in sources :

Example 16 with BeanManager

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

the class OpenEJbContainerControlConfigurationTest method basicInjection.

@Test
public // useless because of tcks but nice to have when working on this specific container
void basicInjection() {
    final CdiContainer container = CdiContainerLoader.getCdiContainer();
    container.boot();
    try {
        final BeanManager beanManager = container.getBeanManager();
        assertEquals("foo", Foo.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(Foo.class)), Foo.class, null)).name());
    } finally {
        container.shutdown();
    }
}
Also used : Foo(org.apache.deltaspike.cdise.openejb.bean.Foo) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 17 with BeanManager

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

the class OpenWebBeansContainerControl method getContextControl.

@Override
public synchronized ContextControl getContextControl() {
    if (ctxCtrl == null) {
        BeanManager beanManager = getBeanManager();
        if (beanManager == null) {
            LOG.warning("If the CDI-container was started by the environment, you can't use this helper." + "Instead you can resolve ContextControl manually " + "(e.g. via BeanProvider.getContextualReference(ContextControl.class) ). " + "If the container wasn't started already, you have to use CdiContainer#boot before.");
            return null;
        }
        Set<Bean<?>> beans = beanManager.getBeans(ContextControl.class);
        ctxCtrlBean = (Bean<ContextControl>) beanManager.resolve(beans);
        ctxCtrlCreationalContext = getBeanManager().createCreationalContext(ctxCtrlBean);
        ctxCtrl = (ContextControl) getBeanManager().getReference(ctxCtrlBean, ContextControl.class, ctxCtrlCreationalContext);
    }
    return ctxCtrl;
}
Also used : ContextControl(org.apache.deltaspike.cdise.api.ContextControl) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 18 with BeanManager

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

the class DynamicMBeanWrapper method instance.

private synchronized Object instance() {
    final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classloader);
    try {
        if (instance != null) {
            return instance;
        }
        if (normalScope) {
            instance = BeanProvider.getContextualReference(clazz, qualifiers);
        } else {
            final BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
            final Set<Bean<?>> beans = bm.getBeans(clazz, qualifiers);
            if (beans == null || beans.isEmpty()) {
                throw new IllegalStateException("Could not find beans for Type=" + clazz + " and qualifiers:" + Arrays.toString(qualifiers));
            }
            final Bean<?> resolvedBean = bm.resolve(beans);
            final CreationalContext<?> creationalContext = bm.createCreationalContext(resolvedBean);
            instance = bm.getReference(resolvedBean, clazz, creationalContext);
            creationalContext.release();
        }
        return instance;
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
    }
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) DynamicMBean(javax.management.DynamicMBean) Bean(javax.enterprise.inject.spi.Bean) MBean(org.apache.deltaspike.core.api.jmx.MBean)

Example 19 with BeanManager

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

the class CdiTestRunner method createTest.

@Override
protected Object createTest() throws Exception {
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    Class<?> type = getTestClass().getJavaClass();
    Set<Bean<?>> beans = beanManager.getBeans(type);
    Object result;
    if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty()) {
        result = super.createTest();
        //fallback to simple injection
        BeanProvider.injectFields(result);
    } else {
        Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans);
        CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean);
        result = beanManager.getReference(bean, type, creationalContext);
    }
    return result;
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 20 with BeanManager

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

the class BeanManagerProviderTest method testBeanManagerProvider.

@Test
public void testBeanManagerProvider() throws Exception {
    BeanManagerProvider bmp = BeanManagerProvider.getInstance();
    Assert.assertNotNull(bmp);
    BeanManager bm = bmp.getBeanManager();
    Assert.assertNotNull(bm);
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) BeanManagerProvider(org.apache.deltaspike.core.api.provider.BeanManagerProvider) Test(org.junit.Test)

Aggregations

BeanManager (javax.enterprise.inject.spi.BeanManager)61 Bean (javax.enterprise.inject.spi.Bean)27 Test (org.junit.Test)12 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)6 ArrayList (java.util.ArrayList)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 InitialContext (javax.naming.InitialContext)4 NamingException (javax.naming.NamingException)4 ValidatorFactory (javax.validation.ValidatorFactory)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 ServiceName (org.jboss.msc.service.ServiceName)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)3 TransactionManager (javax.transaction.TransactionManager)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 BeanService (org.apache.aries.cdi.test.interfaces.BeanService)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 MalformedURLException (java.net.MalformedURLException)2