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