Search in sources :

Example 1 with WeldCreationalContext

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();
    }
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) Context(javax.enterprise.context.spi.Context) SlimAnnotatedType(org.jboss.weld.annotated.slim.SlimAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) EnhancedAnnotatedType(org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType) AnnotatedTypeValidator.validateAnnotatedType(org.jboss.weld.annotated.AnnotatedTypeValidator.validateAnnotatedType) InterceptionType(javax.enterprise.inject.spi.InterceptionType) CurrentInjectionPoint(org.jboss.weld.injection.CurrentInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) EmptyInjectionPoint(org.jboss.weld.injection.EmptyInjectionPoint) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) CreationalContextImpl(org.jboss.weld.contexts.CreationalContextImpl)

Example 2 with WeldCreationalContext

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.
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) EJBContextImpl(com.sun.ejb.containers.EJBContextImpl) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) javax.enterprise.inject.spi(javax.enterprise.inject.spi) WeldManager(org.jboss.weld.manager.api.WeldManager) BaseContainer(com.sun.ejb.containers.BaseContainer) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)

Aggregations

WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)2 BaseContainer (com.sun.ejb.containers.BaseContainer)1 EJBContextImpl (com.sun.ejb.containers.EJBContextImpl)1 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)1 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)1 Type (java.lang.reflect.Type)1 Context (javax.enterprise.context.spi.Context)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 javax.enterprise.inject.spi (javax.enterprise.inject.spi)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 InterceptionType (javax.enterprise.inject.spi.InterceptionType)1 ProcessInjectionPoint (javax.enterprise.inject.spi.ProcessInjectionPoint)1 AnnotatedTypeValidator.validateAnnotatedType (org.jboss.weld.annotated.AnnotatedTypeValidator.validateAnnotatedType)1 EnhancedAnnotatedType (org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType)1 SlimAnnotatedType (org.jboss.weld.annotated.slim.SlimAnnotatedType)1 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)1 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)1 CreationalContextImpl (org.jboss.weld.contexts.CreationalContextImpl)1 CurrentInjectionPoint (org.jboss.weld.injection.CurrentInjectionPoint)1