use of org.jboss.weld.context.bound.BoundConversationContext in project core by weld.
the class WeldStartup method createContexts.
protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) {
List<ContextHolder<? extends Context>> contexts = new ArrayList<ContextHolder<? extends Context>>();
BeanIdentifierIndex beanIdentifierIndex = services.get(BeanIdentifierIndex.class);
/*
* Register a full set of bound and unbound contexts. Although we may not use all of
* these (e.g. if we are running in a servlet environment) they may be
* useful for an application.
*/
Set<Annotation> boundQualifires = ImmutableSet.<Annotation>builder().addAll(Bindings.DEFAULT_QUALIFIERS).add(BoundLiteral.INSTANCE).build();
Set<Annotation> unboundQualifiers = ImmutableSet.<Annotation>builder().addAll(Bindings.DEFAULT_QUALIFIERS).add(UnboundLiteral.INSTANCE).build();
contexts.add(new ContextHolder<ApplicationContext>(new ApplicationContextImpl(contextId), ApplicationContext.class, unboundQualifiers));
contexts.add(new ContextHolder<SingletonContext>(new SingletonContextImpl(contextId), SingletonContext.class, unboundQualifiers));
contexts.add(new ContextHolder<BoundSessionContext>(new BoundSessionContextImpl(contextId, beanIdentifierIndex), BoundSessionContext.class, boundQualifires));
contexts.add(new ContextHolder<BoundConversationContext>(new BoundConversationContextImpl(contextId, services), BoundConversationContext.class, boundQualifires));
contexts.add(new ContextHolder<BoundRequestContext>(new BoundRequestContextImpl(contextId), BoundRequestContext.class, boundQualifires));
contexts.add(new ContextHolder<RequestContext>(new RequestContextImpl(contextId), RequestContext.class, unboundQualifiers));
contexts.add(new ContextHolder<DependentContext>(new DependentContextImpl(services.get(ContextualStore.class)), DependentContext.class, unboundQualifiers));
services.get(WeldModules.class).postContextRegistration(contextId, services, contexts);
/*
* Register the contexts with the bean manager and add the beans to the
* deployment manager so that they are easily accessible (contexts are app
* scoped)
*/
for (ContextHolder<? extends Context> context : contexts) {
deploymentManager.addContext(context.getContext());
deploymentManager.addBean(ContextBean.of(context, deploymentManager));
}
return contexts;
}
use of org.jboss.weld.context.bound.BoundConversationContext in project wildfly by wildfly.
the class WeldContextSetup method setup.
@SuppressWarnings("unchecked")
public void setup(Map<String, Object> properties) {
try {
final BeanManager manager = (BeanManager) new InitialContext().lookup(STANDARD_BEAN_MANAGER_JNDI_NAME);
if (manager != null && Container.available()) {
final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
sessionContext.associate(sessionContexts.get());
sessionContext.activate();
final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
ctx = manager.createCreationalContext(requestContextBean);
final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
requestContext.associate(requestContexts.get());
requestContext.activate();
final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
ctx = manager.createCreationalContext(conversationContextBean);
final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
BoundRequest request = new MutableBoundRequest(requestContexts.get(), sessionContexts.get());
boundRequests.set(request);
conversationContext.associate(request);
conversationContext.activate();
}
} catch (NamingException e) {
WeldLogger.ROOT_LOGGER.failedToSetupWeldContexts(e);
}
}
use of org.jboss.weld.context.bound.BoundConversationContext in project core by weld.
the class NaiveClusterTest method testConversationReplication.
@Test(description = "A simple test to check conversation replication")
public void testConversationReplication() throws Exception {
TestContainer container1 = bootstrapContainer(1, Collections.singletonList(Baz.class));
BeanManagerImpl beanManager1 = getBeanManager(container1);
TestContainer container2 = bootstrapContainer(2, Collections.singletonList(Baz.class));
BeanManagerImpl beanManager2 = getBeanManager(container2);
use(1);
// Set up the conversation context
BoundRequest request1 = new BoundRequestImpl(container1.getSessionStore());
BoundConversationContext conversationContext1 = Utils.getReference(beanManager1, BoundConversationContext.class);
conversationContext1.associate(request1);
conversationContext1.activate();
// Set a value into Baz1
Baz baz1 = Utils.getReference(beanManager1, Baz.class);
baz1.setName("pete");
// Begin the conversation
Conversation conversation1 = Utils.getReference(beanManager1, Conversation.class);
conversation1.begin();
// refetch the test bean and check it has the right value
baz1 = Utils.getReference(beanManager1, Baz.class);
assert baz1.getName().equals("pete");
// Simulate ending the request (from the POV of the conversation only!)
assert !conversation1.isTransient();
String cid = conversation1.getId();
conversationContext1.invalidate();
conversationContext1.deactivate();
conversationContext1.dissociate(request1);
// and start another, propagating the conversation
request1 = new BoundRequestImpl(container1.getSessionStore());
conversationContext1.associate(request1);
conversationContext1.activate(cid);
// refetch the test bean and check it has the right value
baz1 = Utils.getReference(beanManager1, Baz.class);
assert baz1.getName().equals("pete");
assert !conversation1.isTransient();
replicateSession(1, container1, 2, container2);
use(2);
// Set up the conversation context
BoundRequest request2 = new BoundRequestImpl(container2.getSessionStore());
BoundConversationContext conversationContext2 = Utils.getReference(beanManager2, BoundConversationContext.class);
conversationContext2.associate(request2);
conversationContext2.activate(cid);
Baz baz2 = Utils.getReference(beanManager2, Baz.class);
assert baz2.getName().equals("pete");
Conversation conversation2 = Utils.getReference(beanManager2, Conversation.class);
assert !conversation2.isTransient();
use(2);
container2.stopContainer();
use(1);
container1.stopContainer();
}
use of org.jboss.weld.context.bound.BoundConversationContext in project wildfly by wildfly.
the class WeldContextSetup method teardown.
@SuppressWarnings("unchecked")
public void teardown(Map<String, Object> properties) {
try {
final BeanManager manager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
if (manager != null && Container.available()) {
final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
sessionContext.invalidate();
sessionContext.deactivate();
sessionContext.dissociate(sessionContexts.get());
final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
ctx = manager.createCreationalContext(requestContextBean);
final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
requestContext.invalidate();
requestContext.deactivate();
requestContext.dissociate(requestContexts.get());
final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
ctx = manager.createCreationalContext(conversationContextBean);
final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
conversationContext.invalidate();
conversationContext.deactivate();
conversationContext.dissociate(boundRequests.get());
}
} catch (NamingException e) {
WeldLogger.ROOT_LOGGER.failedToTearDownWeldContexts(e);
} finally {
sessionContexts.remove();
requestContexts.remove();
boundRequests.remove();
}
}
Aggregations