Search in sources :

Example 16 with Context

use of javax.enterprise.context.spi.Context 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 17 with Context

use of javax.enterprise.context.spi.Context in project core by weld.

the class JsonObjects method inspectContext.

private static JsonArrayBuilder inspectContext(Class<? extends Annotation> scope, BeanManagerImpl beanManager, Probe probe) {
    Context context;
    try {
        context = beanManager.getContext(scope);
    } catch (ContextNotActiveException e) {
        return null;
    }
    JsonArrayBuilder builder = Json.arrayBuilder();
    for (Bean<?> bean : probe.getBeans()) {
        if (bean.getScope().equals(scope)) {
            Object contextualInstance = context.get(bean);
            if (contextualInstance != null) {
                JsonObjectBuilder instanceBuilder = createSimpleBeanJson(bean, probe);
                instanceBuilder.add(OBJECT_TO_STRING, contextualInstance.getClass().getName() + "@" + Integer.toHexString(contextualInstance.hashCode()));
                instanceBuilder.add(AS_STRING, Strings.abbreviate(contextualInstance.toString(), CONTEXTUAL_INSTANCE_TO_STRING_LIMIT));
                builder.add(instanceBuilder);
            }
        }
    }
    return builder;
}
Also used : Context(javax.enterprise.context.spi.Context) AbstractConversationContext(org.jboss.weld.contexts.AbstractConversationContext) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder)

Example 18 with Context

use of javax.enterprise.context.spi.Context in project core by weld.

the class SuicidalInterceptor method destroyInstance.

private void destroyInstance() throws Exception {
    Context context = manager.getContext(bean.getScope());
    if (!(context instanceof AlterableContext)) {
        throw new IllegalStateException("Context does not support removal of instances");
    }
    AlterableContext alterableContext = AlterableContext.class.cast(context);
    alterableContext.destroy(bean);
}
Also used : Context(javax.enterprise.context.spi.Context) InvocationContext(javax.interceptor.InvocationContext) AlterableContext(javax.enterprise.context.spi.AlterableContext) AlterableContext(javax.enterprise.context.spi.AlterableContext)

Example 19 with Context

use of javax.enterprise.context.spi.Context in project AngularBeans by bessemHmidi.

the class BeanLocator method lookup.

public Object lookup(String beanName, String sessionID) {
    NGSessionScopeContext.setCurrentContext(sessionID);
    Set<Bean<?>> beans = beanManager.getBeans(beanName);
    Class beanClass = CommonUtils.beanNamesHolder.get(beanName);
    if (beans.isEmpty()) {
        beans = beanManager.getBeans(beanClass, new // 
        AnnotationLiteral<Any>() {
        });
    }
    Bean bean = beanManager.resolve(beans);
    Class scopeAnnotationClass = bean.getScope();
    Context context;
    if (scopeAnnotationClass.equals(RequestScoped.class)) {
        context = beanManager.getContext(scopeAnnotationClass);
        if (context == null)
            return bean.create(beanManager.createCreationalContext(bean));
    } else {
        if (scopeAnnotationClass.equals(NGSessionScopeContext.class)) {
            context = NGSessionScopeContext.getINSTANCE();
        } else {
            context = beanManager.getContext(scopeAnnotationClass);
        }
    }
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    Object reference = context.get(bean, creationalContext);
    return reference;
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) CreationalContext(javax.enterprise.context.spi.CreationalContext) AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) Bean(javax.enterprise.inject.spi.Bean)

Example 20 with Context

use of javax.enterprise.context.spi.Context in project kernel by exoplatform.

the class MX4JComponentAdapter method create.

private T create(ContextManager manager, boolean retryAllowed) {
    Class<? extends Annotation> scope = getScope(true, false);
    if (scope.equals(Unknown.class) || scope.equals(Singleton.class) || scope.equals(Dependent.class) || scope.equals(ApplicationScoped.class)) {
        return create();
    }
    final Context ctx = manager.getContext(scope);
    if (ctx == null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("The scope {} is unknown, thus we will create the component {} out of a scope context.", scope.getName(), getComponentImplementation().getName());
        }
        if (!retryAllowed)
            throw new IllegalArgumentException("The scope and the default scope of the class " + getComponentImplementation().getName() + " are unknown");
        try {
            Class<? extends Annotation> defaultScope = ContainerUtil.getScope(getComponentImplementation(), true);
            setScope(scope, defaultScope);
            return create(manager, false);
        } catch (DefinitionException e) {
            throw new IllegalArgumentException("The scope of the class " + getComponentImplementation().getName() + " is unknown and we cannot get a clear default scope: " + e.getMessage());
        }
    }
    NormalScope normalScope = scope.getAnnotation(NormalScope.class);
    if (normalScope != null) {
        // A proxy is expected
        if (normalScope.passivating() && !Serializable.class.isAssignableFrom(getComponentImplementation())) {
            throw new IllegalArgumentException("As the scope " + scope.getName() + " is a passivating scope, we expect only serializable objects and " + getComponentImplementation().getName() + " is not serializable.");
        }
        try {
            lock.lock();
            if (proxy != null)
                return proxy;
            T result = ContainerUtil.createProxy(getComponentImplementation(), new Provider<T>() {

                public T get() {
                    return createInstance(ctx);
                }
            });
            return proxy = result;
        } finally {
            lock.unlock();
        }
    }
    return createInstance(ctx);
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) NormalScope(javax.enterprise.context.NormalScope) Singleton(javax.inject.Singleton) DefinitionException(org.exoplatform.container.context.DefinitionException) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Aggregations

Context (javax.enterprise.context.spi.Context)20 CreationalContext (javax.enterprise.context.spi.CreationalContext)8 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 Test (org.junit.Test)4 Annotation (java.lang.annotation.Annotation)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)3 WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Dependent (javax.enterprise.context.Dependent)2 RequestScoped (javax.enterprise.context.RequestScoped)2 AlterableContext (javax.enterprise.context.spi.AlterableContext)2 InvocationContext (javax.interceptor.InvocationContext)2 BeanContext (org.apache.openejb.BeanContext)2 ContextsService (org.apache.webbeans.spi.ContextsService)2 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)2 JoynrJeeMessageScoped (io.joynr.jeeintegration.api.JoynrJeeMessageScoped)1 JoynrJeeMessageContext (io.joynr.jeeintegration.context.JoynrJeeMessageContext)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1