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();
}
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();
}
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;
}
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 @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 @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;
}
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<?>>();
}
Aggregations