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);
}
}
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);
}
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);
}
}
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;
}
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);
}
}
}
Aggregations