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());
}
}
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);
}
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;
}
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);
}
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;
}
}
Aggregations