Search in sources :

Example 1 with WebBeansContext

use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.

the class EmbeddedTomEEContainer method stopCdiContexts.

private void stopCdiContexts(final String name) {
    try {
        final HttpSession session = SESSIONS.remove(name);
        if (session != null) {
            final WebBeansContext wbc = container.getAppContexts(container.getInfo(name).appId).getWebBeansContext();
            if (wbc != null && wbc.getBeanManagerImpl().isInUse()) {
                wbc.getContextsService().startContext(RequestScoped.class, null);
                wbc.getContextsService().startContext(SessionScoped.class, session);
                wbc.getContextsService().startContext(ConversationScoped.class, null);
            }
        }
    } catch (final Exception e) {
    // no-op
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) MockHttpSession(org.apache.webbeans.web.lifecycle.test.MockHttpSession) HttpSession(javax.servlet.http.HttpSession) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException)

Example 2 with WebBeansContext

use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.

the class WebappBeanManagerTest method containerEventsShouldntGoUp.

@Test
public void containerEventsShouldntGoUp() {
    final WebappWebBeansContext ctx = new WebappWebBeansContext(Collections.<Class<?>, Object>emptyMap(), new Properties(), new WebBeansContext());
    final WebappBeanManager wbm = new WebappBeanManager(ctx) {

        @Override
        public BeanManagerImpl getParentBm() {
            throw new IllegalStateException("shouldn't be called");
        }
    };
    wbm.fireEvent(new GProcessProducer(null, null), true);
    wbm.fireEvent(new GProcessProducerField(null, null, null), true);
    wbm.fireEvent(new GProcessProducerMethod(null, null, null), true);
    wbm.fireEvent(new GProcessInjectionTarget(null, null), true);
    wbm.fireEvent(new GProcessBean(null, null), true);
    wbm.fireEvent(new GProcessAnnotatedType(null), true);
    wbm.fireEvent(new GProcessSessionBean(null, null, null, null), true);
    wbm.fireEvent(new AfterBeanDiscoveryImpl(ctx), true);
    wbm.fireEvent(new AfterDeploymentValidationImpl(wbm), true);
    wbm.fireEvent(new BeforeBeanDiscoveryImpl(ctx), true);
    wbm.fireEvent(new BeforeShutdownImpl(), true);
}
Also used : GProcessBean(org.apache.webbeans.portable.events.generics.GProcessBean) BeforeShutdownImpl(org.apache.webbeans.portable.events.discovery.BeforeShutdownImpl) AfterBeanDiscoveryImpl(org.apache.webbeans.portable.events.discovery.AfterBeanDiscoveryImpl) GProcessAnnotatedType(org.apache.webbeans.portable.events.generics.GProcessAnnotatedType) Properties(java.util.Properties) BeforeBeanDiscoveryImpl(org.apache.webbeans.portable.events.discovery.BeforeBeanDiscoveryImpl) AfterDeploymentValidationImpl(org.apache.webbeans.portable.events.discovery.AfterDeploymentValidationImpl) GProcessInjectionTarget(org.apache.webbeans.portable.events.generics.GProcessInjectionTarget) GProcessProducer(org.apache.webbeans.portable.events.generics.GProcessProducer) WebBeansContext(org.apache.webbeans.config.WebBeansContext) GProcessProducerMethod(org.apache.webbeans.portable.events.generics.GProcessProducerMethod) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) GProcessProducerField(org.apache.webbeans.portable.events.generics.GProcessProducerField) Test(org.junit.Test)

Example 3 with WebBeansContext

use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.

the class WebbeansContextInEmbeddedModeTest method checkWebbeansContext.

@Test
public void checkWebbeansContext() {
    final WebBeansContext ctx1 = WebBeansContext.currentInstance();
    final List<AppContext> appCtxs = SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts();
    assertEquals(1, appCtxs.size());
    final WebBeansContext ctx2 = appCtxs.iterator().next().getWebBeansContext();
    assertSame(ctx1, ctx2);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebBeansContext(org.apache.webbeans.config.WebBeansContext) AppContext(org.apache.openejb.AppContext) Test(org.junit.Test)

Example 4 with WebBeansContext

use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.

the class SingleApplicationComposerRunner method composerInject.

private static void composerInject(final Object target) throws IllegalAccessException {
    WebBeansContext wbc = null;
    try {
        wbc = WebBeansContext.currentInstance();
    } catch (final IllegalStateException ise) {
    // no-op
    }
    if (wbc != null) {
        OWBInjector.inject(wbc.getBeanManagerImpl(), target, null);
    }
    final Object app = APP.get();
    final Class<?> aClass = target.getClass();
    for (final Field f : aClass.getDeclaredFields()) {
        if (f.isAnnotationPresent(RandomPort.class)) {
            for (final Field field : app.getClass().getDeclaredFields()) {
                if (field.getType() == f.getType()) {
                    if (!field.isAccessible()) {
                        field.setAccessible(true);
                    }
                    if (!f.isAccessible()) {
                        f.setAccessible(true);
                    }
                    final Object value = field.get(app);
                    f.set(target, value);
                    break;
                }
            }
        } else if (f.isAnnotationPresent(Application.class)) {
            if (!f.isAccessible()) {
                f.setAccessible(true);
            }
            f.set(target, app);
        }
    }
    final Class<?> superclass = aClass.getSuperclass();
    if (superclass != Object.class) {
        composerInject(superclass);
    }
}
Also used : Field(java.lang.reflect.Field) WebBeansContext(org.apache.webbeans.config.WebBeansContext)

Example 5 with WebBeansContext

use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.

the class WebContext method newWeakableInstance.

public <T> Instance newWeakableInstance(final Class<T> beanClass) throws OpenEJBException {
    final WebBeansContext webBeansContext = getWebBeansContext();
    final ConstructorInjectionBean<Object> beanDefinition = getConstructorInjectionBean(beanClass, webBeansContext);
    CreationalContext<Object> creationalContext;
    final Object o;
    if (webBeansContext == null) {
        creationalContext = null;
        try {
            o = beanClass.newInstance();
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new OpenEJBException(e);
        }
    } else {
        creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
        o = beanDefinition.create(creationalContext);
    }
    // Create bean instance
    final Context unwrap = InjectionProcessor.unwrap(getInitialContext());
    final InjectionProcessor injectionProcessor = new InjectionProcessor(o, injections, unwrap);
    final Object beanInstance;
    try {
        beanInstance = injectionProcessor.createInstance();
        if (webBeansContext != null) {
            final InjectionTargetBean<Object> bean = InjectionTargetBean.class.cast(beanDefinition);
            bean.getInjectionTarget().inject(beanInstance, creationalContext);
            if (shouldBeReleased(bean.getScope())) {
                creationalContexts.put(beanInstance, creationalContext);
            }
        }
    } catch (final OpenEJBException oejbe) {
        if (creationalContext != null) {
            creationalContext.release();
        }
        throw oejbe;
    }
    return new Instance(beanInstance, creationalContext);
}
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) InjectionProcessor(org.apache.openejb.InjectionProcessor)

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