public void destroyRequest(@Observes After event, WeldManager manager) { BoundRequestContext requestContext = manager.instance().select(BoundRequestContext.class).get(); CDIRequestMap map = requestMap.get(); if (map != null) { try { requestContext.invalidate(); requestContext.deactivate(); } finally { requestContext.dissociate(map); map.clear(); } } }
@Override public void requestDestroyed(final ServletRequestEvent evt) { final ServletRequest req = evt.getServletRequest(); @SuppressWarnings("unchecked") final Map<String, Object> instanceMap = (Map<String, Object>) req.getAttribute(CDI_INSTANCE_MAP); final BoundRequestContext ctx = (BoundRequestContext) req.getAttribute(CDI_CONTEXT); ctx.invalidate(); ctx.deactivate(); ctx.dissociate(instanceMap); req.removeAttribute(CDI_CONTEXT); req.removeAttribute(CDI_INSTANCE_MAP); }
@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.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.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.deactivate(); conversationContext.dissociate(boundRequests.get()); } } catch (NamingException e) { log.error("Failed to tear down Weld contexts", e); } finally { sessionContexts.remove(); requestContexts.remove(); boundRequests.remove(); } }