2 // ========================================================================
3 // Copyright (c) 1995-2014 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 import org.eclipse.jetty.server.SessionIdManager;
37 /* ------------------------------------------------------------ */
39 * HashSessionIdManager. An in-memory implementation of the session ID manager.
41 public class HashSessionIdManager extends AbstractSessionIdManager
43 private final Map<String, Set<WeakReference<HttpSession>>> _sessions = new HashMap<String, Set<WeakReference<HttpSession>>>();
45 /* ------------------------------------------------------------ */
46 public HashSessionIdManager()
50 /* ------------------------------------------------------------ */
51 public HashSessionIdManager(Random random)
56 /* ------------------------------------------------------------ */
58 * @return Collection of String session IDs
60 public Collection<String> getSessions()
62 return Collections.unmodifiableCollection(_sessions.keySet());
65 /* ------------------------------------------------------------ */
67 * @return Collection of Sessions for the passed session ID
69 public Collection<HttpSession> getSession(String id)
71 ArrayList<HttpSession> sessions = new ArrayList<HttpSession>();
72 Set<WeakReference<HttpSession>> refs =_sessions.get(id);
75 for (WeakReference<HttpSession> ref: refs)
77 HttpSession session = ref.get();
79 sessions.add(session);
85 /* ------------------------------------------------------------ */
87 protected void doStart() throws Exception
92 /* ------------------------------------------------------------ */
94 protected void doStop() throws Exception
100 /* ------------------------------------------------------------ */
102 * @see SessionIdManager#idInUse(String)
105 public boolean idInUse(String id)
109 return _sessions.containsKey(id);
113 /* ------------------------------------------------------------ */
115 * @see SessionIdManager#addSession(HttpSession)
118 public void addSession(HttpSession session)
120 String id = getClusterId(session.getId());
121 WeakReference<HttpSession> ref = new WeakReference<HttpSession>(session);
125 Set<WeakReference<HttpSession>> sessions = _sessions.get(id);
128 sessions=new HashSet<WeakReference<HttpSession>>();
129 _sessions.put(id,sessions);
135 /* ------------------------------------------------------------ */
137 * @see SessionIdManager#removeSession(HttpSession)
140 public void removeSession(HttpSession session)
142 String id = getClusterId(session.getId());
146 Collection<WeakReference<HttpSession>> sessions = _sessions.get(id);
149 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
151 WeakReference<HttpSession> ref = iter.next();
152 HttpSession s=ref.get();
164 if (sessions.isEmpty())
165 _sessions.remove(id);
170 /* ------------------------------------------------------------ */
172 * @see SessionIdManager#invalidateAll(String)
175 public void invalidateAll(String id)
177 Collection<WeakReference<HttpSession>> sessions;
180 sessions = _sessions.remove(id);
185 for (WeakReference<HttpSession> ref: sessions)
187 AbstractSession session=(AbstractSession)ref.get();
188 if (session!=null && session.isValid())
189 session.invalidate();
196 /* ------------------------------------------------------------ */
198 public void renewSessionId (String oldClusterId, String oldNodeId, HttpServletRequest request)
201 String newClusterId = newSessionId(request.hashCode());
206 Set<WeakReference<HttpSession>> sessions = _sessions.remove(oldClusterId); //get the list of sessions with same id from other contexts
209 for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
211 WeakReference<HttpSession> ref = iter.next();
212 HttpSession s = ref.get();
219 if (s instanceof AbstractSession)
221 AbstractSession abstractSession = (AbstractSession)s;
222 abstractSession.getSessionManager().renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request));
226 _sessions.put(newClusterId, sessions);