Search in sources :

Example 6 with WebBeansContext

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);
    }
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) AppContext(org.apache.openejb.AppContext) ServletContext(javax.servlet.ServletContext) OpenEJBException(org.apache.openejb.OpenEJBException) WebBeansContext(org.apache.webbeans.config.WebBeansContext) NamingException(javax.naming.NamingException) InjectionProcessor(org.apache.openejb.InjectionProcessor) InitialContext(javax.naming.InitialContext)

Example 7 with WebBeansContext

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;
}
Also used : BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext)

Example 8 with WebBeansContext

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);
        }
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) WebBeansELResolver(org.apache.webbeans.el22.WebBeansELResolver)

Example 9 with WebBeansContext

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);
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl)

Example 10 with WebBeansContext

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));
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.enterprise.context.spi.Context) ThreadContext(org.apache.openejb.core.ThreadContext) BeanContext(org.apache.openejb.BeanContext) ContextsService(org.apache.webbeans.spi.ContextsService) WebBeansContext(org.apache.webbeans.config.WebBeansContext)

Aggregations

WebBeansContext (org.apache.webbeans.config.WebBeansContext)39 AppContext (org.apache.openejb.AppContext)11 BeanContext (org.apache.openejb.BeanContext)10 WebContext (org.apache.openejb.core.WebContext)9 Context (javax.naming.Context)8 OpenEJBException (org.apache.openejb.OpenEJBException)8 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Properties (java.util.Properties)6 NamingException (javax.naming.NamingException)6 ServletContext (javax.servlet.ServletContext)6 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)6 InitialContext (javax.naming.InitialContext)5 ContextsService (org.apache.webbeans.spi.ContextsService)5 MalformedURLException (java.net.MalformedURLException)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 InjectionProcessor (org.apache.openejb.InjectionProcessor)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3