1 // ========================================================================
2 // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.server.session;
15
16 import java.security.NoSuchAlgorithmException;
17 import java.security.SecureRandom;
18 import java.util.Random;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpSession;
22
23 import org.eclipse.jetty.server.SessionIdManager;
24 import org.eclipse.jetty.server.session.AbstractSessionManager.Session;
25 import org.eclipse.jetty.util.MultiMap;
26 import org.eclipse.jetty.util.component.AbstractLifeCycle;
27 import org.eclipse.jetty.util.log.Log;
28
29 /* ------------------------------------------------------------ */
30 /**
31 * HashSessionIdManager. An in-memory implementation of the session ID manager.
32 */
33 public class HashSessionIdManager extends AbstractSessionIdManager
34 {
35 MultiMap<String> _sessions;
36
37 /* ------------------------------------------------------------ */
38 public HashSessionIdManager()
39 {
40 }
41
42 /* ------------------------------------------------------------ */
43 public HashSessionIdManager(Random random)
44 {
45 super(random);
46 }
47
48 /* ------------------------------------------------------------ */
49 /** Get the session ID with any worker ID.
50 *
51 * @param clusterId
52 * @param request
53 * @return sessionId plus any worker ID.
54 */
55 public String getNodeId(String clusterId,HttpServletRequest request)
56 {
57 String worker=request==null?null:(String)request.getAttribute("org.eclipse.http.ajp.JVMRoute");
58 if (worker!=null)
59 return clusterId+'.'+worker;
60
61 if (_workerName!=null)
62 return clusterId+'.'+_workerName;
63
64 return clusterId;
65 }
66
67 /* ------------------------------------------------------------ */
68 /** Get the session ID without any worker ID.
69 *
70 * @param nodeId the node id
71 * @return sessionId without any worker ID.
72 */
73 public String getClusterId(String nodeId)
74 {
75 int dot=nodeId.lastIndexOf('.');
76 return (dot>0)?nodeId.substring(0,dot):nodeId;
77 }
78
79 /* ------------------------------------------------------------ */
80 @Override
81 protected void doStart() throws Exception
82 {
83 _sessions=new MultiMap<String>(true);
84 super.doStart();
85 }
86
87 /* ------------------------------------------------------------ */
88 @Override
89 protected void doStop() throws Exception
90 {
91 if (_sessions!=null)
92 _sessions.clear(); // Maybe invalidate?
93 _sessions=null;
94 super.doStop();
95 }
96
97 /* ------------------------------------------------------------ */
98 /**
99 * @see SessionIdManager#idInUse(String)
100 */
101 public boolean idInUse(String id)
102 {
103 return _sessions.containsKey(id);
104 }
105
106 /* ------------------------------------------------------------ */
107 /**
108 * @see SessionIdManager#addSession(HttpSession)
109 */
110 public void addSession(HttpSession session)
111 {
112 _sessions.add(getClusterId(session.getId()),session);
113 }
114
115 /* ------------------------------------------------------------ */
116 /**
117 * @see SessionIdManager#removeSession(HttpSession)
118 */
119 public void removeSession(HttpSession session)
120 {
121 _sessions.removeValue(getClusterId(session.getId()),session);
122 }
123
124 /* ------------------------------------------------------------ */
125 /**
126 * @see SessionIdManager#invalidateAll(String)
127 */
128 public void invalidateAll(String id)
129 {
130 // Do not use iterators as this method tends to be called recursively
131 // by the invalidate calls.
132 while (_sessions.containsKey(id))
133 {
134 Session session=(Session)_sessions.getValue(id,0);
135
136 if (session.isValid())
137 {
138 session.invalidate();
139 }
140
141 _sessions.removeValue(id,session);
142 }
143 }
144
145 }