use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class WebContext method inject.
public Object inject(final Object o) throws OpenEJBException {
try {
final WebBeansContext webBeansContext = getWebBeansContext();
// Create bean instance
final Context initialContext = (Context) new InitialContext().lookup("java:");
final Context unwrap = InjectionProcessor.unwrap(initialContext);
final InjectionProcessor injectionProcessor = new InjectionProcessor(o, injections, unwrap);
final Object beanInstance = injectionProcessor.createInstance();
if (webBeansContext != null) {
final ConstructorInjectionBean<Object> beanDefinition = getConstructorInjectionBean(o.getClass(), webBeansContext);
final CreationalContext<Object> creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
final InjectionTargetBean<Object> bean = InjectionTargetBean.class.cast(beanDefinition);
bean.getInjectionTarget().inject(beanInstance, creationalContext);
if (shouldBeReleased(beanDefinition.getScope())) {
creationalContexts.put(beanInstance, creationalContext);
}
}
return beanInstance;
} catch (final NamingException | OpenEJBException e) {
throw new OpenEJBException(e);
}
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class CdiPlugin method findBeanContext.
private static BeanContext findBeanContext(final WebBeansContext ctx, final Class<?> clazz) {
final Map<Class<?>, BeanContext> beans = pluginBeans(ctx);
final BeanContext b = beans.get(clazz);
if (b != null) {
return b;
}
for (final BeanContext bc : beans.values()) {
if (bc.isLocalbean()) {
// see isSessionBean() impl
continue;
}
final CdiEjbBean<?> cdiEjbBean = bc.get(CdiEjbBean.class);
if (cdiEjbBean == null) {
continue;
}
for (final Class<?> itf : cdiEjbBean.getBusinessLocalInterfaces()) {
if (itf.equals(clazz)) {
return bc;
}
}
}
final WebBeansContext parentCtx = superContext(ctx);
if (parentCtx != null) {
return findBeanContext(parentCtx, clazz);
}
return null;
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class CustomELAdapter method getOwbELResolver.
@Override
public ELResolver getOwbELResolver() {
WebBeansContext old = null;
boolean exit = false;
try {
// just some safety around this but should be very very rare
WebBeansContext.currentInstance();
} catch (final IllegalStateException ise) {
old = ThreadSingletonServiceImpl.enter(appContext.getWebBeansContext());
exit = true;
}
try {
return new WebBeansELResolver();
} finally {
if (exit) {
ThreadSingletonServiceImpl.exit(old);
}
}
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class CdiPasswordCipher method decrypt.
@Override
public String decrypt(final char[] encryptedPassword) {
final String string = new String(encryptedPassword);
final BeanManagerImpl mgr;
try {
final WebBeansContext wbc = WebBeansContext.currentInstance();
mgr = wbc.getBeanManagerImpl();
if (!mgr.isInUse()) {
// would be cool to log a warning here but would pollute the logs with false positives
return "cipher:cdi:" + string;
}
} catch (final IllegalStateException ise) {
// no cdi
return "cipher:cdi:" + string;
}
final int split = string.indexOf(':');
final String delegate = string.substring(0, split);
final String pwdStr = string.substring(split + 1, string.length());
final char[] pwd = pwdStr.toCharArray();
try {
final Class<?> beanType = Thread.currentThread().getContextClassLoader().loadClass(delegate);
final Bean<?> bean = mgr.resolve(mgr.getBeans(beanType));
if (bean == null) {
throw new IllegalArgumentException("No bean for " + delegate);
}
final CreationalContext<?> cc = mgr.createCreationalContext(null);
try {
return PasswordCipher.class.cast(mgr.getReference(bean, PasswordCipher.class, cc)).decrypt(pwd);
} finally {
if (!mgr.isNormalScope(bean.getScope())) {
cc.release();
}
}
} catch (final ClassNotFoundException e) {
throw new IllegalArgumentException("Can't find " + delegate, e);
}
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class RequestScopedThreadContextListener method contextEntered.
@Override
public void contextEntered(final ThreadContext oldContext, final ThreadContext newContext) {
final BeanContext beanContext = newContext.getBeanContext();
final WebBeansContext webBeansContext = beanContext.getModuleContext().getAppContext().getWebBeansContext();
if (webBeansContext == null) {
return;
}
final ContextsService contextsService = webBeansContext.getContextsService();
final Context requestContext = CdiAppContextsService.class.cast(contextsService).getRequestContext(false);
if (requestContext == null) {
contextsService.startContext(RequestScoped.class, CdiAppContextsService.EJB_REQUEST_EVENT);
newContext.set(DestroyContext.class, new DestroyContext(contextsService, newContext));
}
}
Aggregations