Search in sources :

Example 11 with BeanManager

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

the class ContainerCtrlTckTest method testParallelThreadExecution.

@Test
public void testParallelThreadExecution() throws Exception {
    final CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);
    cc.boot();
    cc.getContextControl().startContexts();
    final BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);
    final AtomicInteger numErrors = new AtomicInteger(0);
    final ContextControl contextControl = cc.getContextControl();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                contextControl.startContext(SessionScoped.class);
                contextControl.startContext(RequestScoped.class);
                Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
                Bean<?> bean = bm.resolve(beans);
                CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
                Assert.assertNotNull(carRepair);
                for (int i = 0; i < 100000; i++) {
                    // we need the threads doing something ;)
                    Assert.assertNotNull(carRepair.getCar());
                    Assert.assertNotNull(carRepair.getCar().getUser());
                    Assert.assertNull(carRepair.getCar().getUser().getName());
                }
                contextControl.stopContext(RequestScoped.class);
                contextControl.stopContext(SessionScoped.class);
            } catch (Throwable e) {
                log.log(Level.SEVERE, "An exception happened on a new worker thread", e);
                numErrors.incrementAndGet();
            }
        }
    };
    Thread[] threads = new Thread[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(runnable);
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
    Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get());
    cc.shutdown();
}
Also used : Bean(javax.enterprise.inject.spi.Bean) CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextControl(org.apache.deltaspike.cdise.api.ContextControl) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 12 with BeanManager

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

the class ContainerCtrlTckTest method testContainerBoot.

@Test
public void testContainerBoot() {
    CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);
    cc.boot();
    cc.getContextControl().startContexts();
    BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);
    Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
    Bean<?> bean = bm.resolve(beans);
    CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
    Assert.assertNotNull(carRepair);
    Assert.assertNotNull(carRepair.getCar());
    Assert.assertNotNull(carRepair.getCar().getUser());
    cc.shutdown();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 13 with BeanManager

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

the class BeanManagerProvider method getBeanManager.

/**
     * The active {@link BeanManager} for the current application (current {@link ClassLoader}). This method will throw
     * an {@link IllegalStateException} if the BeanManager cannot be found.
     *
     * @return the current BeanManager, never <code>null</code>
     *
     * @throws IllegalStateException if the BeanManager cannot be found
     */
public BeanManager getBeanManager() {
    BeanManagerInfo bmi = getBeanManagerInfo(ClassUtils.getClassLoader(null));
    if (!bmi.booted) {
        // and later run the WARs with their own child ClassLoaders.
        if (bmi.loadTimeBm == null) {
            BeanManagerInfo parentBmi = getParentBeanManagerInfo(ClassUtils.getClassLoader(null));
            if (parentBmi != null) {
                bmi.loadTimeBm = parentBmi.loadTimeBm;
            }
        }
    }
    BeanManager result = bmi.finalBm;
    if (result == null) {
        synchronized (bmi) {
            result = bmi.finalBm;
            if (result == null) {
                // first we look for a BeanManager from JNDI
                result = resolveBeanManagerViaJndi();
                // (esp. in case of EAR based applications)
                if (result == null) {
                    result = resolveBeanManagerViaStaticHelper();
                }
                if (result == null) {
                    // if none found, we take the one we got from the Extension loading
                    result = bmi.loadTimeBm;
                }
                if (result == null) {
                    throw new IllegalStateException("Unable to find BeanManager. " + "Please ensure that you configured the CDI implementation of your choice properly.");
                }
                // store the resolved BeanManager in the result cache until #cleanupFinalBeanManagers gets called
                // -> afterwards the next call of #getBeanManager will trigger the final lookup
                bmi.finalBm = result;
            }
        }
    }
    return result;
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 14 with BeanManager

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

the class BeanProvider method getContextualReferences.

/**
     * Get a list of Contextual References by type, regardless of the qualifier.
     *
     * Further details are available at {@link #getContextualReferences(Class, boolean)}.
     * <p>
     * <b>Attention:</b> please see the notes on manually resolving &#064;Dependent bean in
     * {@link #getContextualReference(Class, java.lang.annotation.Annotation...)}!</p>
     * <p>
     * <b>Attention:</b> This will also return instances of beans for which an Alternative exists! The &#064;Alternative
     * resolving is only done via {@link BeanManager#resolve(java.util.Set)} which we cannot use in this case!</p>
     *
     * @param type                      the type of the bean in question
     * @param optional                  if <code>true</code> it will return an empty list if no bean could be found or
     *                                  created. Otherwise it will throw an {@code IllegalStateException}
     * @param includeDefaultScopedBeans specifies if dependent scoped beans should be included in the result
     * @param <T>                       target type
     *
     * @return the resolved list of Contextual Reference or an empty-list if optional is true
     */
public static <T> List<T> getContextualReferences(Class<T> type, boolean optional, boolean includeDefaultScopedBeans) {
    BeanManager beanManager = getBeanManager();
    Set<Bean<T>> beans = getBeanDefinitions(type, optional, includeDefaultScopedBeans, beanManager);
    List<T> result = new ArrayList<T>(beans.size());
    for (Bean<?> bean : beans) {
        //noinspection unchecked
        result.add(getContextualReference(type, beanManager, bean));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 15 with BeanManager

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

the class InterceptorLookup method resolveInterceptors.

protected List<Interceptor<?>> resolveInterceptors(Object instance, Method method) {
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    Annotation[] interceptorBindings = extractInterceptorBindings(beanManager, instance, method);
    if (interceptorBindings.length > 0) {
        return beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, interceptorBindings);
    }
    return new ArrayList<Interceptor<?>>();
}
Also used : ArrayList(java.util.ArrayList) BeanManager(javax.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation)

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