use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class CDILoginModule method initialize.
@Override
public void initialize(final Subject subject, final CallbackHandler callbackHandler, final Map<String, ?> sharedState, final Map<String, ?> options) {
final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
if (!bm.isInUse()) {
throw new OpenEJBRuntimeException("CDI not activated");
}
String delegate = String.valueOf(options.get("delegate"));
if ("null".equals(delegate)) {
final String app = findAppName(webBeansContext);
delegate = String.valueOf(options.get(app));
if ("null".equals(delegate)) {
throw new OpenEJBRuntimeException("Please specify a delegate class");
}
}
final Class<?> clazz;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(delegate);
} catch (final ClassNotFoundException e) {
throw new OpenEJBRuntimeException(e.getMessage(), e);
}
cc = bm.createCreationalContext(null);
final String cdiName = String.valueOf(options.get("cdiName"));
if ("true".equals(String.valueOf(options.get("loginModuleAsCdiBean")))) {
final Set<Bean<?>> beans;
if ("null".equals(cdiName)) {
beans = bm.getBeans(clazz);
} else {
beans = bm.getBeans(cdiName);
}
loginModule = LoginModule.class.cast(bm.getReference(bm.resolve(beans), clazz, cc));
} else {
try {
loginModule = LoginModule.class.cast(clazz.newInstance());
OWBInjector.inject(bm, loginModule, cc);
} catch (final Exception e) {
throw new OpenEJBRuntimeException("Can't inject into delegate class " + loginModule, e);
}
}
loginModule.initialize(subject, callbackHandler, sharedState, options);
}
use of org.apache.webbeans.container.BeanManagerImpl 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;
}
Aggregations