X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Fserver%2Fsession%2FHashSessionIdManager.java;fp=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Fserver%2Fsession%2FHashSessionIdManager.java;h=a17bc06f7683b02cab295cf30fba470eb9b2da48;hb=73ef54a38e3930a1a789cdc6b5fa23cdd4c9d086;hp=0000000000000000000000000000000000000000;hpb=515007c7c1351045420669d65b59c08fa46850f2;p=gigi.git diff --git a/lib/jetty/org/eclipse/jetty/server/session/HashSessionIdManager.java b/lib/jetty/org/eclipse/jetty/server/session/HashSessionIdManager.java new file mode 100644 index 00000000..a17bc06f --- /dev/null +++ b/lib/jetty/org/eclipse/jetty/server/session/HashSessionIdManager.java @@ -0,0 +1,231 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server.session; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.eclipse.jetty.server.SessionIdManager; + +/* ------------------------------------------------------------ */ +/** + * HashSessionIdManager. An in-memory implementation of the session ID manager. + */ +public class HashSessionIdManager extends AbstractSessionIdManager +{ + private final Map>> _sessions = new HashMap>>(); + + /* ------------------------------------------------------------ */ + public HashSessionIdManager() + { + } + + /* ------------------------------------------------------------ */ + public HashSessionIdManager(Random random) + { + super(random); + } + + /* ------------------------------------------------------------ */ + /** + * @return Collection of String session IDs + */ + public Collection getSessions() + { + return Collections.unmodifiableCollection(_sessions.keySet()); + } + + /* ------------------------------------------------------------ */ + /** + * @return Collection of Sessions for the passed session ID + */ + public Collection getSession(String id) + { + ArrayList sessions = new ArrayList(); + Set> refs =_sessions.get(id); + if (refs!=null) + { + for (WeakReference ref: refs) + { + HttpSession session = ref.get(); + if (session!=null) + sessions.add(session); + } + } + return sessions; + } + + /* ------------------------------------------------------------ */ + @Override + protected void doStart() throws Exception + { + super.doStart(); + } + + /* ------------------------------------------------------------ */ + @Override + protected void doStop() throws Exception + { + _sessions.clear(); + super.doStop(); + } + + /* ------------------------------------------------------------ */ + /** + * @see SessionIdManager#idInUse(String) + */ + @Override + public boolean idInUse(String id) + { + synchronized (this) + { + return _sessions.containsKey(id); + } + } + + /* ------------------------------------------------------------ */ + /** + * @see SessionIdManager#addSession(HttpSession) + */ + @Override + public void addSession(HttpSession session) + { + String id = getClusterId(session.getId()); + WeakReference ref = new WeakReference(session); + + synchronized (this) + { + Set> sessions = _sessions.get(id); + if (sessions==null) + { + sessions=new HashSet>(); + _sessions.put(id,sessions); + } + sessions.add(ref); + } + } + + /* ------------------------------------------------------------ */ + /** + * @see SessionIdManager#removeSession(HttpSession) + */ + @Override + public void removeSession(HttpSession session) + { + String id = getClusterId(session.getId()); + + synchronized (this) + { + Collection> sessions = _sessions.get(id); + if (sessions!=null) + { + for (Iterator> iter = sessions.iterator(); iter.hasNext();) + { + WeakReference ref = iter.next(); + HttpSession s=ref.get(); + if (s==null) + { + iter.remove(); + continue; + } + if (s==session) + { + iter.remove(); + break; + } + } + if (sessions.isEmpty()) + _sessions.remove(id); + } + } + } + + /* ------------------------------------------------------------ */ + /** + * @see SessionIdManager#invalidateAll(String) + */ + @Override + public void invalidateAll(String id) + { + Collection> sessions; + synchronized (this) + { + sessions = _sessions.remove(id); + } + + if (sessions!=null) + { + for (WeakReference ref: sessions) + { + AbstractSession session=(AbstractSession)ref.get(); + if (session!=null && session.isValid()) + session.invalidate(); + } + sessions.clear(); + } + } + + + /* ------------------------------------------------------------ */ + @Override + public void renewSessionId (String oldClusterId, String oldNodeId, HttpServletRequest request) + { + //generate a new id + String newClusterId = newSessionId(request.hashCode()); + + + synchronized (this) + { + Set> sessions = _sessions.remove(oldClusterId); //get the list of sessions with same id from other contexts + if (sessions!=null) + { + for (Iterator> iter = sessions.iterator(); iter.hasNext();) + { + WeakReference ref = iter.next(); + HttpSession s = ref.get(); + if (s == null) + { + continue; + } + else + { + if (s instanceof AbstractSession) + { + AbstractSession abstractSession = (AbstractSession)s; + abstractSession.getSessionManager().renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request)); + } + } + } + _sessions.put(newClusterId, sessions); + } + } + } + +}