1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server.session;
20
21 import java.lang.ref.WeakReference;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Random;
30 import java.util.Set;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34
35 import org.eclipse.jetty.server.SessionIdManager;
36
37
38
39
40
41 public class HashSessionIdManager extends AbstractSessionIdManager
42 {
43 private final Map<String, Set<WeakReference<HttpSession>>> _sessions = new HashMap<String, Set<WeakReference<HttpSession>>>();
44
45
46 public HashSessionIdManager()
47 {
48 }
49
50
51 public HashSessionIdManager(Random random)
52 {
53 super(random);
54 }
55
56
57
58
59
60 public Collection<String> getSessions()
61 {
62 return Collections.unmodifiableCollection(_sessions.keySet());
63 }
64
65
66
67
68
69 public Collection<HttpSession> getSession(String id)
70 {
71 ArrayList<HttpSession> sessions = new ArrayList<HttpSession>();
72 Set<WeakReference<HttpSession>> refs =_sessions.get(id);
73 if (refs!=null)
74 {
75 for (WeakReference<HttpSession> ref: refs)
76 {
77 HttpSession session = ref.get();
78 if (session!=null)
79 sessions.add(session);
80 }
81 }
82 return sessions;
83 }
84
85
86 @Override
87 protected void doStart() throws Exception
88 {
89 super.doStart();
90 }
91
92
93 @Override
94 protected void doStop() throws Exception
95 {
96 _sessions.clear();
97 super.doStop();
98 }
99
100
101
102
103
104 @Override
105 public boolean idInUse(String id)
106 {
107 synchronized (this)
108 {
109 return _sessions.containsKey(id);
110 }
111 }
112
113
114
115
116
117 @Override
118 public void addSession(HttpSession session)
119 {
120 String id = getClusterId(session.getId());
121 WeakReference<HttpSession> ref = new WeakReference<HttpSession>(session);
122
123 synchronized (this)
124 {
125 Set<WeakReference<HttpSession>> sessions = _sessions.get(id);
126 if (sessions==null)
127 {
128 sessions=new HashSet<WeakReference<HttpSession>>();
129 _sessions.put(id,sessions);
130 }
131 sessions.add(ref);
132 }
133 }
134
135
136
137
138
139 @Override
140 public void removeSession(HttpSession session)
141 {
142 String id = getClusterId(session.getId());
143
144 synchronized (this)
145 {
146 Collection<WeakReference<HttpSession>> sessions = _sessions.get(id);
147 if (sessions!=null)
148 {
149 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
150 {
151 WeakReference<HttpSession> ref = iter.next();
152 HttpSession s=ref.get();
153 if (s==null)
154 {
155 iter.remove();
156 continue;
157 }
158 if (s==session)
159 {
160 iter.remove();
161 break;
162 }
163 }
164 if (sessions.isEmpty())
165 _sessions.remove(id);
166 }
167 }
168 }
169
170
171
172
173
174 @Override
175 public void invalidateAll(String id)
176 {
177 Collection<WeakReference<HttpSession>> sessions;
178 synchronized (this)
179 {
180 sessions = _sessions.remove(id);
181 }
182
183 if (sessions!=null)
184 {
185 for (WeakReference<HttpSession> ref: sessions)
186 {
187 AbstractSession session=(AbstractSession)ref.get();
188 if (session!=null && session.isValid())
189 session.invalidate();
190 }
191 sessions.clear();
192 }
193 }
194
195
196
197 @Override
198 public void renewSessionId (String oldClusterId, String oldNodeId, HttpServletRequest request)
199 {
200
201 String newClusterId = newSessionId(request.hashCode());
202
203
204 synchronized (this)
205 {
206 Set<WeakReference<HttpSession>> sessions = _sessions.remove(oldClusterId);
207 if (sessions!=null)
208 {
209 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
210 {
211 WeakReference<HttpSession> ref = iter.next();
212 HttpSession s = ref.get();
213 if (s == null)
214 {
215 continue;
216 }
217 else
218 {
219 if (s instanceof AbstractSession)
220 {
221 AbstractSession abstractSession = (AbstractSession)s;
222 abstractSession.getSessionManager().renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request));
223 }
224 }
225 }
226 _sessions.put(newClusterId, sessions);
227 }
228 }
229 }
230
231 }