Search in sources :

Example 1 with WeldAlterableContext

use of org.jboss.weld.context.WeldAlterableContext in project core by weld.

the class GetContextUtilMethodsTest method getActiveWeldAlterableContextsTest.

@Test
public void getActiveWeldAlterableContextsTest() {
    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();
        RequestContextController controller = manager.instance().select(RequestContextController.class).get();
        controller.activate();
        Collection<WeldAlterableContext> activeContexts = manager.getActiveWeldAlterableContexts();
        // there are 7 scopes by default in SE, only 3 have active contexts by default
        // it is dependent, singleton and application -> none of these implements WeldAlterableContext
        // therefore we activated request scope and assume on that one
        controller.deactivate();
        Assert.assertEquals(1, activeContexts.size());
        Assert.assertEquals(RequestScoped.class, activeContexts.iterator().next().getScope());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Example 2 with WeldAlterableContext

use of org.jboss.weld.context.WeldAlterableContext in project core by weld.

the class ContextPropagationSEService method propagateContextsAndSubmitTask.

public static <T> Future<T> propagateContextsAndSubmitTask(Callable<T> task) {
    // gather all the contexts we want to propagate and the instances in them
    Map<Class<? extends Annotation>, Collection<ContextualInstance<?>>> scopeToContextualInstances = new HashMap<>();
    for (WeldAlterableContext context : WeldContainer.current().select(WeldManager.class).get().getActiveWeldAlterableContexts()) {
        scopeToContextualInstances.put(context.getScope(), context.getAllContextualInstances());
    }
    // We create a task wrapper which will make sure we have contexts propagated
    Callable<T> wrappedTask = new Callable<T>() {

        @Override
        public T call() throws Exception {
            WeldContainer container = WeldContainer.current();
            WeldManager weldManager = container.select(WeldManager.class).get();
            BoundRequestContext requestContext = weldManager.instance().select(BoundRequestContext.class, BoundLiteral.INSTANCE).get();
            // we will be using bound context, prepare backing map
            Map<String, Object> requestMap = new HashMap<>();
            // activate request context
            requestContext.associate(requestMap);
            requestContext.activate();
            // propagate req. context
            if (scopeToContextualInstances.get(requestContext.getScope()) != null) {
                requestContext.clearAndSet(scopeToContextualInstances.get(requestContext.getScope()));
            }
            // now execute the actual original task
            T result = task.call();
            // cleanup, context deactivation
            // requestContext.invalidate(); is deliberately left out so that pre destroy callbacks are not invoked
            requestContext.deactivate();
            // all done, return
            return result;
        }
    };
    return executor.submit(wrappedTask);
}
Also used : HashMap(java.util.HashMap) WeldContainer(org.jboss.weld.environment.se.WeldContainer) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Callable(java.util.concurrent.Callable) WeldManager(org.jboss.weld.manager.api.WeldManager) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) Collection(java.util.Collection)

Example 3 with WeldAlterableContext

use of org.jboss.weld.context.WeldAlterableContext in project core by weld.

the class ContextPropagationService method wrapAndRunOnTheSameThread.

public static <T> Integer wrapAndRunOnTheSameThread(Callable<T> task) {
    // gather all the contexts we want to propagate and the instances in them
    Map<Class<? extends Annotation>, Collection<ContextualInstance<?>>> scopeToContextualInstances = new HashMap<>();
    for (WeldAlterableContext context : CDI.current().select(WeldManager.class).get().getActiveWeldAlterableContexts()) {
        scopeToContextualInstances.put(context.getScope(), context.getAllContextualInstances());
    }
    // We create a task wrapper which will make sure we have contexts propagated
    Callable<T> wrappedTask = new Callable<T>() {

        @Override
        public T call() throws Exception {
            WeldManager weldManager = CDI.current().select(WeldManager.class).get();
            // verify we have req context active already, in arq. this will be BoundRequestContext
            Collection<WeldAlterableContext> activeContexts = weldManager.getActiveWeldAlterableContexts();
            WeldAlterableContext requestContext = null;
            for (WeldAlterableContext activeContext : activeContexts) {
                if (activeContext.getScope().equals(RequestScoped.class)) {
                    requestContext = activeContext;
                }
            }
            if (requestContext == null) {
                throw new IllegalStateException("RequestContext is expected to be active on current thread.");
            }
            // clear up the context
            requestContext.clearAndSet(Collections.emptySet());
            // now execute the actual original task, bean should be recreated, return result
            return task.call();
        }
    };
    Integer result = -1;
    try {
        result = (Integer) wrappedTask.call();
    } catch (Exception e) {
        throw new IllegalStateException("An exception occurred when executing the task on current thread: " + e);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Callable(java.util.concurrent.Callable) WeldManager(org.jboss.weld.manager.api.WeldManager) Collection(java.util.Collection)

Example 4 with WeldAlterableContext

use of org.jboss.weld.context.WeldAlterableContext in project core by weld.

the class ContextPropagationService method propagateContextsAndSubmitTask.

public static <T> Future<T> propagateContextsAndSubmitTask(Callable<T> task) {
    // gather all the contexts we want to propagate and the instances in them
    Map<Class<? extends Annotation>, Collection<ContextualInstance<?>>> scopeToContextualInstances = new HashMap<>();
    for (WeldAlterableContext context : CDI.current().select(WeldManager.class).get().getActiveWeldAlterableContexts()) {
        scopeToContextualInstances.put(context.getScope(), context.getAllContextualInstances());
    }
    // We create a task wrapper which will make sure we have contexts propagated
    Callable<T> wrappedTask = new Callable<T>() {

        @Override
        public T call() throws Exception {
            WeldManager weldManager = CDI.current().select(WeldManager.class).get();
            BoundRequestContext requestContext = weldManager.instance().select(BoundRequestContext.class, BoundLiteral.INSTANCE).get();
            BoundSessionContext sessionContext = weldManager.instance().select(BoundSessionContext.class, BoundLiteral.INSTANCE).get();
            BoundConversationContext conversationContext = weldManager.instance().select(BoundConversationContext.class, BoundLiteral.INSTANCE).get();
            // we will be using bound contexts, prepare backing structures for contexts
            Map<String, Object> sessionMap = new HashMap<>();
            Map<String, Object> requestMap = new HashMap<>();
            BoundRequest boundRequest = new MutableBoundRequest(requestMap, sessionMap);
            // activate contexts
            requestContext.associate(requestMap);
            requestContext.activate();
            sessionContext.associate(sessionMap);
            sessionContext.activate();
            conversationContext.associate(boundRequest);
            conversationContext.activate();
            // propagate all contexts that have some bean in them
            if (scopeToContextualInstances.get(requestContext.getScope()) != null) {
                requestContext.clearAndSet(scopeToContextualInstances.get(requestContext.getScope()));
            }
            if (scopeToContextualInstances.get(sessionContext.getScope()) != null) {
                sessionContext.clearAndSet(scopeToContextualInstances.get(sessionContext.getScope()));
            }
            if (scopeToContextualInstances.get(conversationContext.getScope()) != null) {
                conversationContext.clearAndSet(scopeToContextualInstances.get(conversationContext.getScope()));
            }
            // now execute the actual original task
            T result = task.call();
            // cleanup, context deactivation
            // context.invalidate() is deliberately left out so as to avoid calling all pre destroy/disposer callbacks
            // propagators might choose to invoke them but it could lead to these methods being invoked multiple times
            // while the bean is still 'alive' in yet another thread into which it was propagated
            requestContext.deactivate();
            conversationContext.deactivate();
            sessionContext.deactivate();
            // all done, return
            return result;
        }
    };
    return executor.submit(wrappedTask);
}
Also used : MutableBoundRequest(org.jboss.weld.context.bound.MutableBoundRequest) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) HashMap(java.util.HashMap) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Callable(java.util.concurrent.Callable) WeldManager(org.jboss.weld.manager.api.WeldManager) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) Collection(java.util.Collection) BoundRequest(org.jboss.weld.context.bound.BoundRequest) MutableBoundRequest(org.jboss.weld.context.bound.MutableBoundRequest)

Example 5 with WeldAlterableContext

use of org.jboss.weld.context.WeldAlterableContext in project helidon by oracle.

the class RequestScopeHelper method saveScope.

/**
 * Store request context information from the current thread. State
 * related to Jersey and CDI to handle {@code @Context} and {@code @Inject}
 * injections.
 */
void saveScope() {
    if (state == State.STORED) {
        throw new IllegalStateException("Request scope state already stored");
    }
    // Collect instances for request scope only
    weldManager = CDI.current().select(WeldManager.class).get();
    if (weldManager != null) {
        for (WeldAlterableContext context : weldManager.getActiveWeldAlterableContexts()) {
            if (context.getScope() == RequestScoped.class) {
                requestScopeInstances = context.getAllContextualInstances();
            }
        }
    }
    // Jersey scope
    // thread local
    injectionManager = WeldRequestScope.actualInjectorManager.get();
    try {
        requestScope = CDI.current().select(RequestScope.class).get();
        requestContext = requestScope.referenceCurrent();
    } catch (Exception e) {
    // Ignored, Jersey request scope not active
    } finally {
        state = State.STORED;
    }
}
Also used : WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext)

Aggregations

WeldAlterableContext (org.jboss.weld.context.WeldAlterableContext)5 WeldManager (org.jboss.weld.manager.api.WeldManager)4 Annotation (java.lang.annotation.Annotation)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Callable (java.util.concurrent.Callable)3 BoundRequestContext (org.jboss.weld.context.bound.BoundRequestContext)2 WeldContainer (org.jboss.weld.environment.se.WeldContainer)2 RequestContextController (jakarta.enterprise.context.control.RequestContextController)1 BoundConversationContext (org.jboss.weld.context.bound.BoundConversationContext)1 BoundRequest (org.jboss.weld.context.bound.BoundRequest)1 BoundSessionContext (org.jboss.weld.context.bound.BoundSessionContext)1 MutableBoundRequest (org.jboss.weld.context.bound.MutableBoundRequest)1 Weld (org.jboss.weld.environment.se.Weld)1 Test (org.junit.Test)1