use of org.jboss.weld.contexts.WeldCreationalContext in project core by weld.
the class BeanManagerImpl method getInjectableReference.
/**
* Get a reference, registering the injection point used.
*
* @param injectionPoint the injection point to register
* @param resolvedBean the bean to get a reference to
* @param creationalContext the creationalContext
* @return the injectable reference
*/
public Object getInjectableReference(InjectionPoint injectionPoint, Bean<?> resolvedBean, CreationalContext<?> creationalContext) {
Preconditions.checkArgumentNotNull(resolvedBean, "resolvedBean");
Preconditions.checkArgumentNotNull(creationalContext, CREATIONAL_CONTEXT);
boolean registerInjectionPoint = isRegisterableInjectionPoint(injectionPoint);
boolean delegateInjectionPoint = injectionPoint != null && injectionPoint.isDelegate();
final ThreadLocalStackReference<InjectionPoint> stack = currentInjectionPoint.pushConditionally(injectionPoint, registerInjectionPoint);
try {
Type requestedType = null;
if (injectionPoint != null) {
requestedType = injectionPoint.getType();
}
if (clientProxyOptimization && injectionPoint != null && injectionPoint.getBean() != null) {
// For certain combinations of scopes, the container is permitted to optimize an injectable reference lookup
// This should also partially solve circular @PostConstruct invocation
CreationalContextImpl<?> weldCreationalContext = null;
Bean<?> bean = injectionPoint.getBean();
// Do not optimize for self injection
if (!bean.equals(resolvedBean)) {
if (creationalContext instanceof CreationalContextImpl) {
weldCreationalContext = (CreationalContextImpl<?>) creationalContext;
}
if (weldCreationalContext != null && Dependent.class.equals(bean.getScope()) && isNormalScope(resolvedBean.getScope())) {
bean = findNormalScopedDependant(weldCreationalContext);
}
if (InjectionPoints.isInjectableReferenceLookupOptimizationAllowed(bean, resolvedBean)) {
if (weldCreationalContext != null) {
final Object incompleteInstance = weldCreationalContext.getIncompleteInstance(resolvedBean);
if (incompleteInstance != null) {
return incompleteInstance;
}
}
Context context = internalGetContext(resolvedBean.getScope());
if (context != null) {
@SuppressWarnings({ "unchecked", "rawtypes" }) final Object existinInstance = context.get(Reflections.<Contextual>cast(resolvedBean));
if (existinInstance != null) {
return existinInstance;
}
}
}
}
}
return getReference(resolvedBean, requestedType, creationalContext, delegateInjectionPoint);
} finally {
stack.pop();
}
}
use of org.jboss.weld.contexts.WeldCreationalContext in project Payara by payara.
the class JCDIServiceImpl method _createJCDIInjectionContext.
// instance could be null. If null, create a new one
@SuppressWarnings("unchecked")
private <T> JCDIInjectionContext<T> _createJCDIInjectionContext(EjbDescriptor ejb, T instance, Map<Class<?>, Object> ejbInfo) {
BaseContainer baseContainer = null;
EJBContextImpl ejbContext = null;
JCDIInjectionContextImpl<T> jcdiCtx = null;
CreationalContext<T> creationalContext = null;
if (ejbInfo != null) {
baseContainer = (BaseContainer) ejbInfo.get(BaseContainer.class);
ejbContext = (EJBContextImpl) ejbInfo.get(EJBContextImpl.class);
}
BundleDescriptor topLevelBundleDesc = (BundleDescriptor) ejb.getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
// First get BeanDeploymentArchive for this ejb
BeanDeploymentArchive bda = getBDAForBeanClass(topLevelBundleDesc, ejb.getEjbClassName());
WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(ejb.getEjbBundleDescriptor().getApplication());
WeldManager weldManager = bootstrap.getManager(bda);
// when calling _createJCDIInjectionContext
if (weldManager == null) {
logger.severe("The reference for weldManager is not available, this is an un-sync state of the container");
return null;
}
org.jboss.weld.ejb.spi.EjbDescriptor<T> ejbDesc = weldManager.getEjbDescriptor(ejb.getName());
// get or create the ejb's creational context
if (null != ejbInfo) {
jcdiCtx = (JCDIInjectionContextImpl<T>) ejbInfo.get(JCDIService.JCDIInjectionContext.class);
}
if (null != jcdiCtx) {
creationalContext = jcdiCtx.getCreationalContext();
}
if (null != jcdiCtx && creationalContext == null) {
// The creational context may have been created by interceptors because they are created first
// (see createInterceptorInstance below.)
// And we only want to create the ejb's creational context once or we will have a memory
// leak there too.
Bean<T> bean = weldManager.getBean(ejbDesc);
creationalContext = weldManager.createCreationalContext(bean);
jcdiCtx.setCreationalContext(creationalContext);
}
// Create the injection target
InjectionTarget<T> it = null;
if (ejbDesc.isMessageDriven()) {
// message driven beans are non-contextual and therefore createInjectionTarget is not appropriate
it = createMdbInjectionTarget(weldManager, ejbDesc);
} else {
it = weldManager.createInjectionTarget(ejbDesc);
}
if (null != jcdiCtx) {
jcdiCtx.setInjectionTarget(it);
}
// JJS: 7/20/17 We must perform the around_construct interception because Weld does not know about
// interceptors defined by descriptors.
WeldCreationalContext<T> weldCreationalContext = (WeldCreationalContext<T>) creationalContext;
weldCreationalContext.setConstructorInterceptionSuppressed(true);
JCDIAroundConstructCallback<T> aroundConstructCallback = new JCDIAroundConstructCallback<>(baseContainer, ejbContext);
weldCreationalContext.registerAroundConstructCallback(aroundConstructCallback);
if (null != jcdiCtx) {
jcdiCtx.setJCDIAroundConstructCallback(aroundConstructCallback);
}
T beanInstance = instance;
if (null != jcdiCtx) {
jcdiCtx.setInstance(beanInstance);
}
return jcdiCtx;
// Injection is not performed yet. Separate injectEJBInstance() call is required.
}
Aggregations