2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.session;
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;
29 import java.util.Random;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
35 /* ------------------------------------------------------------ */
37 * HashSessionIdManager. An in-memory implementation of the session ID manager.
39 public class HashSessionIdManager extends AbstractSessionIdManager
41 private final Map<String, Set<WeakReference<HttpSession>>> _sessions = new HashMap<String, Set<WeakReference<HttpSession>>>();
43 /* ------------------------------------------------------------ */
44 public HashSessionIdManager()
48 /* ------------------------------------------------------------ */
49 public HashSessionIdManager(Random random)
54 /* ------------------------------------------------------------ */
56 * @return Collection of String session IDs
58 public Collection<String> getSessions()
60 return Collections.unmodifiableCollection(_sessions.keySet());
63 /* ------------------------------------------------------------ */
65 * @return Collection of Sessions for the passed session ID
67 public Collection<HttpSession> getSession(String id)
69 ArrayList<HttpSession> sessions = new ArrayList<HttpSession>();
70 Set<WeakReference<HttpSession>> refs =_sessions.get(id);
73 for (WeakReference<HttpSession> ref: refs)
75 HttpSession session = ref.get();
77 sessions.add(session);
83 /* ------------------------------------------------------------ */
85 protected void doStart() throws Exception
90 /* ------------------------------------------------------------ */
92 protected void doStop() throws Exception
98 /* ------------------------------------------------------------ */
100 * @see SessionIdManager#idInUse(String)
103 public boolean idInUse(String id)
107 return _sessions.containsKey(id);
111 /* ------------------------------------------------------------ */
113 * @see SessionIdManager#addSession(HttpSession)
116 public void addSession(HttpSession session)
118 String id = getClusterId(session.getId());
119 WeakReference<HttpSession> ref = new WeakReference<HttpSession>(session);
123 Set<WeakReference<HttpSession>> sessions = _sessions.get(id);
126 sessions=new HashSet<WeakReference<HttpSession>>();
127 _sessions.put(id,sessions);
133 /* ------------------------------------------------------------ */
135 * @see SessionIdManager#removeSession(HttpSession)
138 public void removeSession(HttpSession session)
140 String id = getClusterId(session.getId());
144 Collection<WeakReference<HttpSession>> sessions = _sessions.get(id);
147 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
149 WeakReference<HttpSession> ref = iter.next();
150 HttpSession s=ref.get();
162 if (sessions.isEmpty())
163 _sessions.remove(id);
168 /* ------------------------------------------------------------ */
170 * @see SessionIdManager#invalidateAll(String)
173 public void invalidateAll(String id)
175 Collection<WeakReference<HttpSession>> sessions;
178 sessions = _sessions.remove(id);
183 for (WeakReference<HttpSession> ref: sessions)
185 AbstractSession session=(AbstractSession)ref.get();
186 if (session!=null && session.isValid())
187 session.invalidate();
194 /* ------------------------------------------------------------ */
196 public void renewSessionId (String oldClusterId, String oldNodeId, HttpServletRequest request)
199 String newClusterId = newSessionId(request.hashCode());
204 Set<WeakReference<HttpSession>> sessions = _sessions.remove(oldClusterId); //get the list of sessions with same id from other contexts
207 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
209 WeakReference<HttpSession> ref = iter.next();
210 HttpSession s = ref.get();
217 if (s instanceof AbstractSession)
219 AbstractSession abstractSession = (AbstractSession)s;
220 abstractSession.getSessionManager().renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request));
224 _sessions.put(newClusterId, sessions);