]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/session/HashSessionIdManager.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / session / HashSessionIdManager.java
1 //
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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
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  * HashSessionIdManager. An in-memory implementation of the session ID manager.
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      * @return Collection of String session IDs
59      */
60     public Collection<String> getSessions()
61     {
62         return Collections.unmodifiableCollection(_sessions.keySet());
63     }
64
65     /* ------------------------------------------------------------ */
66     /**
67      * @return Collection of Sessions for the passed session ID
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      * @see SessionIdManager#idInUse(String)
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      * @see SessionIdManager#addSession(HttpSession)
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      * @see SessionIdManager#removeSession(HttpSession)
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      * @see SessionIdManager#invalidateAll(String)
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         //generate a new id
201         String newClusterId = newSessionId(request.hashCode());
202
203
204         synchronized (this)
205         {
206             Set<WeakReference<HttpSession>> sessions = _sessions.remove(oldClusterId); //get the list of sessions with same id from other contexts
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 }