1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server.session;
15
16 import java.lang.ref.WeakReference;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Random;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentLinkedQueue;
28 import java.util.concurrent.ConcurrentMap;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpSession;
32
33 import org.eclipse.jetty.server.SessionIdManager;
34 import org.eclipse.jetty.server.session.AbstractSessionManager.Session;
35 import org.eclipse.jetty.util.MultiMap;
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
61
62
63 public String getNodeId(String clusterId,HttpServletRequest request)
64 {
65
66 String worker=request==null?null:(String)request.getAttribute("org.eclipse.jetty.ajp.JVMRoute");
67 if (worker!=null)
68 return clusterId+'.'+worker;
69
70 if (_workerName!=null)
71 return clusterId+'.'+_workerName;
72
73 return clusterId;
74 }
75
76
77
78
79
80
81
82 public String getClusterId(String nodeId)
83 {
84 int dot=nodeId.lastIndexOf('.');
85 return (dot>0)?nodeId.substring(0,dot):nodeId;
86 }
87
88
89 @Override
90 protected void doStart() throws Exception
91 {
92 super.doStart();
93 }
94
95
96 @Override
97 protected void doStop() throws Exception
98 {
99 _sessions.clear();
100 super.doStop();
101 }
102
103
104
105
106
107 public boolean idInUse(String id)
108 {
109 synchronized (this)
110 {
111 return _sessions.containsKey(id);
112 }
113 }
114
115
116
117
118
119 public void addSession(HttpSession session)
120 {
121 String id = getClusterId(session.getId());
122 WeakReference<HttpSession> ref = new WeakReference<HttpSession>(session);
123
124 synchronized (this)
125 {
126 Set<WeakReference<HttpSession>> sessions = _sessions.get(id);
127 if (sessions==null)
128 {
129 sessions=new HashSet<WeakReference<HttpSession>>();
130 _sessions.put(id,sessions);
131 }
132 sessions.add(ref);
133 }
134 }
135
136
137
138
139
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 public void invalidateAll(String id)
175 {
176 Collection<WeakReference<HttpSession>> sessions;
177 synchronized (this)
178 {
179 sessions = _sessions.remove(id);
180 }
181
182 if (sessions!=null)
183 {
184 for (WeakReference<HttpSession> ref: sessions)
185 {
186 Session session=(Session)ref.get();
187 if (session!=null && session.isValid())
188 session.invalidate();
189 }
190 sessions.clear();
191 }
192 }
193
194 }