/
HttpSessionContextImpl.java
115 lines (100 loc) · 2.93 KB
/
HttpSessionContextImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.jboss.weld.context.http;
import java.lang.annotation.Annotation;
import javax.enterprise.context.SessionScoped;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jboss.weld.Container;
import org.jboss.weld.context.AbstractBoundContext;
import org.jboss.weld.context.beanstore.NamingScheme;
import org.jboss.weld.context.beanstore.SimpleNamingScheme;
import org.jboss.weld.context.beanstore.http.EagerSessionBeanStore;
import org.jboss.weld.context.beanstore.http.LazySessionBeanStore;
public class HttpSessionContextImpl extends AbstractBoundContext<HttpServletRequest> implements HttpSessionContext
{
private static final String IDENTIFIER = HttpSessionContextImpl.class.getName();
private final NamingScheme namingScheme;
public HttpSessionContextImpl()
{
super(true);
this.namingScheme = new SimpleNamingScheme(HttpSessionContext.class.getName());
}
public boolean associate(HttpServletRequest request)
{
if (request.getAttribute(IDENTIFIER) == null)
{
// Don't reassociate
request.setAttribute(IDENTIFIER, IDENTIFIER);
setBeanStore(new LazySessionBeanStore(request, namingScheme));
return true;
}
else
{
return false;
}
}
public boolean dissociate(HttpServletRequest request)
{
if (request.getAttribute(IDENTIFIER) != null)
{
try
{
setBeanStore(null);
request.removeAttribute(IDENTIFIER);
return true;
}
finally
{
cleanup();
}
}
else
{
return false;
}
}
public boolean destroy(HttpSession session)
{
if (getBeanStore() == null)
{
try
{
HttpConversationContext conversationContext = getConversationContext();
setBeanStore(new EagerSessionBeanStore(namingScheme, session));
activate();
invalidate();
conversationContext.destroy(session);
deactivate();
setBeanStore(null);
return true;
}
finally
{
cleanup();
}
}
else
{
HttpConversationContext conversationContext = getConversationContext();
// We are in a request, invalidate it
invalidate();
if (conversationContext.isActive())
{
getConversationContext().invalidate();
}
else
{
// In a request, with no conversations, so destroy now
getConversationContext().destroy(session);
}
return false;
}
}
public Class<? extends Annotation> getScope()
{
return SessionScoped.class;
}
protected HttpConversationContext getConversationContext()
{
return Container.instance().deploymentManager().instance().select(HttpConversationContext.class).get();
}
}