Search in sources :

Example 1 with InjectionProcessor

use of org.apache.openejb.InjectionProcessor in project tomee by apache.

the class ApplicationComposers method enrich.

private void enrich(final Object inputTestInstance, final BeanContext context) throws org.apache.openejb.OpenEJBException {
    if (context == null) {
        return;
    }
    final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    try {
        final InjectionProcessor processor = new InjectionProcessor(inputTestInstance, context.getInjections(), context.getJndiContext());
        processor.createInstance();
        Throwable error = null;
        try {
            if (appContext.getBeanManager() != null) {
                OWBInjector.inject(appContext.getBeanManager(), inputTestInstance, null);
            }
        } catch (final Throwable t) {
            error = t;
        }
        for (final WebContext web : appContext.getWebContexts()) {
            if (web.getWebBeansContext() == null) {
                continue;
            }
            try {
                OWBInjector.inject(web.getWebBeansContext().getBeanManagerImpl(), inputTestInstance, null);
                // hourra, we enriched correctly the test then cleanup error state and quit
                error = null;
                break;
            } catch (final Throwable t) {
                if (error == null) {
                    error = t;
                }
            // else keep original one
            }
        }
        if (error != null) {
            error.printStackTrace();
        }
    } finally {
        ThreadContext.exit(oldContext);
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) ThreadContext(org.apache.openejb.core.ThreadContext) InjectionProcessor(org.apache.openejb.InjectionProcessor)

Example 2 with InjectionProcessor

use of org.apache.openejb.InjectionProcessor 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)

Example 3 with InjectionProcessor

use of org.apache.openejb.InjectionProcessor 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 4 with InjectionProcessor

use of org.apache.openejb.InjectionProcessor in project tomee by apache.

the class HandlerResolverImpl method buildHandlers.

private List<Handler> buildHandlers(final PortInfo portInfo, final HandlerChainData handlerChain) {
    if (!matchServiceName(portInfo, handlerChain.getServiceNamePattern()) || !matchPortName(portInfo, handlerChain.getPortNamePattern()) || !matchBinding(portInfo, handlerChain.getProtocolBindings())) {
        return Collections.emptyList();
    }
    final List<Handler> handlers = new ArrayList<Handler>(handlerChain.getHandlers().size());
    for (final HandlerData handler : handlerChain.getHandlers()) {
        final WebBeansContext webBeansContext = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE);
        if (webBeansContext != null) {
            // cdi
            final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
            if (bm.isInUse()) {
                try {
                    final Set<Bean<?>> beans = bm.getBeans(handler.getHandlerClass());
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean != null) {
                        // proxy so faster to do it
                        final boolean normalScoped = bm.isNormalScope(bean.getScope());
                        final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
                        final Handler instance = Handler.class.cast(bm.getReference(bean, bean.getBeanClass(), creationalContext));
                        // hack for destroyHandlers()
                        handlers.add(instance);
                        handlerInstances.add(new InjectionProcessor<Handler>(instance, Collections.<Injection>emptySet(), null) {

                            @Override
                            public void preDestroy() {
                                if (!normalScoped) {
                                    creationalContext.release();
                                }
                            }
                        });
                        continue;
                    }
                } catch (final InjectionException ie) {
                    LOGGER.info(ie.getMessage(), ie);
                }
            }
        }
        try {
            // old way
            final Class<? extends Handler> handlerClass = handler.getHandlerClass().asSubclass(Handler.class);
            final InjectionProcessor<Handler> processor = new InjectionProcessor<Handler>(handlerClass, injections, handler.getPostConstruct(), handler.getPreDestroy(), unwrap(context));
            processor.createInstance();
            processor.postConstruct();
            final Handler handlerInstance = processor.getInstance();
            handlers.add(handlerInstance);
            handlerInstances.add(processor);
        } catch (final Exception e) {
            throw new WebServiceException("Failed to instantiate handler", e);
        }
    }
    return handlers;
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) Injection(org.apache.openejb.Injection) InjectionProcessor(org.apache.openejb.InjectionProcessor) InjectionException(javax.enterprise.inject.InjectionException) WebServiceException(javax.xml.ws.WebServiceException) Bean(javax.enterprise.inject.spi.Bean) InjectionException(javax.enterprise.inject.InjectionException) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl)

Example 5 with InjectionProcessor

use of org.apache.openejb.InjectionProcessor in project tomee by apache.

the class OpenEJBEnricher method enrich.

public static void enrich(final Object testInstance, final AppContext appCtx) {
    // don't rely on arquillian since this enrichment should absolutely be done before the following ones
    new MockitoEnricher().enrich(testInstance);
    AppContext ctx = appCtx;
    if (ctx == null) {
        ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
        if (ctx == null) {
            return;
        }
    }
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
    final WebBeansContext appWBC = ctx.getWebBeansContext();
    final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
    boolean ok = false;
    for (final WebContext web : ctx.getWebContexts()) {
        final WebBeansContext webBeansContext = web.getWebBeansContext();
        if (webBeansContext == null) {
            continue;
        }
        final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
        if (webBeansContext != appWBC && webAppBm.isInUse()) {
            try {
                doInject(testInstance, context, webAppBm);
                ok = true;
                break;
            } catch (final Exception e) {
            // no-op, try next
            }
        }
    }
    if (bm != null && bm.isInUse() && !ok) {
        try {
            doInject(testInstance, context, bm);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
            if (RuntimeException.class.isInstance(e)) {
                throw RuntimeException.class.cast(e);
            }
            throw new OpenEJBRuntimeException(e);
        }
    }
    if (context != null) {
        final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
        final ThreadContext oldContext = ThreadContext.enter(callContext);
        try {
            final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
            processor.createInstance();
        } catch (final OpenEJBException e) {
        // ignored
        } finally {
            ThreadContext.exit(oldContext);
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) InjectionProcessor(org.apache.openejb.InjectionProcessor) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) MockitoEnricher(org.apache.openejb.arquillian.common.mockito.MockitoEnricher)

Aggregations

InjectionProcessor (org.apache.openejb.InjectionProcessor)5 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 AppContext (org.apache.openejb.AppContext)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 CreationalContext (javax.enterprise.context.spi.CreationalContext)2 Context (javax.naming.Context)2 InitialContext (javax.naming.InitialContext)2 ServletContext (javax.servlet.ServletContext)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 WebContext (org.apache.openejb.core.WebContext)2 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)2 ArrayList (java.util.ArrayList)1 InjectionException (javax.enterprise.inject.InjectionException)1 Bean (javax.enterprise.inject.spi.Bean)1 NamingException (javax.naming.NamingException)1 WebServiceException (javax.xml.ws.WebServiceException)1 Handler (javax.xml.ws.handler.Handler)1 LogicalHandler (javax.xml.ws.handler.LogicalHandler)1 BeanContext (org.apache.openejb.BeanContext)1 Injection (org.apache.openejb.Injection)1