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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
package nginx.unit;
import java.io.Serializable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* @author Andrey Kazankov
*/
public class Session implements HttpSession, Serializable
{
private final Map<String, Object> attributes = new HashMap<>();
private final long creation_time = new Date().getTime();
private long last_access_time = creation_time;
private long access_time = creation_time;
private int max_inactive_interval;
private String id;
private final Context context;
private boolean is_new = true;
private final HttpSessionAttributeListener attr_listener;
public Session(Context context, String id,
HttpSessionAttributeListener al, int max_inactive_interval)
{
this.id = id;
this.context = context;
attr_listener = al;
this.max_inactive_interval = max_inactive_interval;
}
public void setId(String id)
{
this.id = id;
}
@Override
public long getCreationTime()
{
return creation_time;
}
@Override
public String getId()
{
return id;
}
@Override
public long getLastAccessedTime()
{
return last_access_time;
}
@Override
public ServletContext getServletContext()
{
return context;
}
@Override
public void setMaxInactiveInterval(int i)
{
max_inactive_interval = i;
}
@Override
public int getMaxInactiveInterval()
{
return max_inactive_interval;
}
@Deprecated
@Override
public javax.servlet.http.HttpSessionContext getSessionContext()
{
return null;
}
@Override
public Object getAttribute(String s)
{
synchronized (attributes) {
return attributes.get(s);
}
}
@Deprecated
@Override
public Object getValue(String s)
{
return getAttribute(s);
}
@Override
public Enumeration<String> getAttributeNames()
{
synchronized (attributes) {
return Collections.enumeration(attributes.keySet());
}
}
@Deprecated
@Override
public String[] getValueNames()
{
synchronized (attributes) {
return attributes.keySet().toArray(new String[attributes.keySet().size()]);
}
}
@Override
public void setAttribute(String s, Object o)
{
Object old;
if (o != null && o instanceof HttpSessionBindingListener) {
HttpSessionBindingListener l = (HttpSessionBindingListener) o;
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s);
l.valueBound(e);
}
synchronized (attributes) {
if (o != null) {
old = attributes.put(s, o);
} else {
old = attributes.remove(s);
}
}
if (old != null && old instanceof HttpSessionBindingListener) {
HttpSessionBindingListener l = (HttpSessionBindingListener) old;
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s);
l.valueUnbound(e);
}
if (attr_listener == null) {
return;
}
if (o == null) {
if (old != null) {
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s, old);
attr_listener.attributeRemoved(e);
}
return;
}
if (old != null) {
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s, old);
attr_listener.attributeReplaced(e);
} else {
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s, o);
attr_listener.attributeAdded(e);
}
}
@Deprecated
@Override
public void putValue(String s, Object o)
{
setAttribute(s,o);
}
@Override
public void removeAttribute(String s)
{
Object o;
synchronized (attributes) {
o = attributes.remove(s);
}
if (o != null && o instanceof HttpSessionBindingListener) {
HttpSessionBindingListener l = (HttpSessionBindingListener) o;
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s);
l.valueUnbound(e);
}
if (attr_listener == null || o == null) {
return;
}
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, s, o);
attr_listener.attributeRemoved(e);
}
@Deprecated
@Override
public void removeValue(String s)
{
removeAttribute(s);
}
@Override
public void invalidate()
{
context.invalidateSession(this);
unboundAttributes();
}
private void unboundAttributes()
{
for (Map.Entry<String, Object> a : attributes.entrySet()) {
Object o = a.getValue();
if (o != null && o instanceof HttpSessionBindingListener) {
HttpSessionBindingListener l = (HttpSessionBindingListener) o;
HttpSessionBindingEvent e = new HttpSessionBindingEvent(this, a.getKey());
l.valueUnbound(e);
}
}
attributes.clear();
}
@Override
public boolean isNew()
{
return is_new;
}
public void accessed() {
synchronized (this) {
is_new = false;
last_access_time = access_time;
access_time = new Date().getTime();
}
}
public boolean checkTimeOut()
{
return (max_inactive_interval > 0) &&
(access_time - last_access_time > max_inactive_interval * 1000);
}
}
|