Search in sources :

Example 6 with Context

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

the class GetContextUtilMethodsTest method getActiveContextsTest.

@Test
public void getActiveContextsTest() {
    try (WeldContainer container = new Weld().initialize()) {
        // TheLoneBean is just to have some bean in the archive
        container.select(TheLoneBean.class).get().ping();
        WeldManager manager = container.select(WeldManager.class).get();
        // there are 7 scopes by default in SE, only 3 have active contexts by default
        // it is dependent, singleton and application
        Collection<Context> activeContexts = manager.getActiveContexts();
        Assert.assertEquals(3, activeContexts.size());
        Set<Class<? extends Annotation>> scopes = activeContexts.stream().map(t -> t.getScope()).collect(Collectors.toSet());
        Assert.assertTrue(scopes.contains(Dependent.class));
        Assert.assertTrue(scopes.contains(Singleton.class));
        Assert.assertTrue(scopes.contains(ApplicationScoped.class));
    }
}
Also used : Context(jakarta.enterprise.context.spi.Context) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Dependent(jakarta.enterprise.context.Dependent) Arquillian(org.jboss.arquillian.junit.Arquillian) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) RunWith(org.junit.runner.RunWith) WeldManager(org.jboss.weld.manager.api.WeldManager) BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) TheLoneBean(org.jboss.weld.environment.se.test.weldManager.contextActive.TheLoneBean) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Utils(org.jboss.weld.test.util.Utils) Collection(java.util.Collection) Set(java.util.Set) Singleton(jakarta.inject.Singleton) Context(jakarta.enterprise.context.spi.Context) Test(org.junit.Test) Archive(org.jboss.shrinkwrap.api.Archive) ClassPath(org.jboss.arquillian.container.se.api.ClassPath) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) Deployment(org.jboss.arquillian.container.test.api.Deployment) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Assert(org.junit.Assert) RequestScoped(jakarta.enterprise.context.RequestScoped) Singleton(jakarta.inject.Singleton) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Annotation(java.lang.annotation.Annotation) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Example 7 with Context

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

Example 8 with Context

use of jakarta.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(jakarta.enterprise.context.spi.Context) AbstractConversationContext(org.jboss.weld.contexts.AbstractConversationContext) ContextNotActiveException(jakarta.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 9 with Context

use of jakarta.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 : AlterableContext(jakarta.enterprise.context.spi.AlterableContext) Context(jakarta.enterprise.context.spi.Context) InvocationContext(jakarta.interceptor.InvocationContext) AlterableContext(jakarta.enterprise.context.spi.AlterableContext)

Example 10 with Context

use of jakarta.enterprise.context.spi.Context in project mojarra by eclipse-ee4j.

the class CdiUtils method getBeanInstance.

/**
 * Returns concrete (non-proxied) bean instance of given class in current context.
 *
 * @param <T> the generic bean type
 * @param type the required bean type the instance must have
 * @param create whether to auto-create bean if not exist
 * @return a bean instance adhering to the required type
 */
public static <T> T getBeanInstance(Class<T> type, boolean create) {
    BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance());
    @SuppressWarnings("unchecked") Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(type));
    if (bean != null) {
        Context context = beanManager.getContext(bean.getScope());
        if (create) {
            return context.get(bean, beanManager.createCreationalContext(bean));
        } else {
            return context.get(bean);
        }
    } else {
        return null;
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) Context(jakarta.enterprise.context.spi.Context) CreationalContext(jakarta.enterprise.context.spi.CreationalContext) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Bean(jakarta.enterprise.inject.spi.Bean)

Aggregations

Context (jakarta.enterprise.context.spi.Context)11 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)4 EntityManager (jakarta.persistence.EntityManager)3 Annotation (java.lang.annotation.Annotation)3 ContextNotActiveException (jakarta.enterprise.context.ContextNotActiveException)2 Dependent (jakarta.enterprise.context.Dependent)2 AlterableContext (jakarta.enterprise.context.spi.AlterableContext)2 Bean (jakarta.enterprise.inject.spi.Bean)2 BeanManager (jakarta.enterprise.inject.spi.BeanManager)2 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)2 WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)2 ReferenceCountedContext (io.helidon.integrations.cdi.referencecountedcontext.ReferenceCountedContext)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 RequestScoped (jakarta.enterprise.context.RequestScoped)1 RequestContextController (jakarta.enterprise.context.control.RequestContextController)1 CreationException (jakarta.enterprise.inject.CreationException)1 AnnotatedType (jakarta.enterprise.inject.spi.AnnotatedType)1 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)1 InterceptionType (jakarta.enterprise.inject.spi.InterceptionType)1 ProcessInjectionPoint (jakarta.enterprise.inject.spi.ProcessInjectionPoint)1