1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.servlet;
21  
22  import java.io.Serializable;
23  
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.http.HttpSessionBindingEvent;
32  import javax.servlet.http.HttpSessionBindingListener;
33  
34  /**
35   * <a href="PortletSessionTracker.java.html"><b><i>View Source</i></b></a>
36   *
37   * <p>
38   * See http://support.liferay.com/browse/LEP-1466.
39   * </p>
40   *
41   * @author Rudy Hilado
42   *
43   */
44  public class PortletSessionTracker
45      implements HttpSessionBindingListener, Serializable {
46  
47      public static void add(HttpSession session) {
48          _instance._add(session);
49      }
50  
51      public static HttpSessionBindingListener getInstance() {
52          return _instance;
53      }
54  
55      public static void invalidate(String sessionId) {
56          _instance._invalidate(sessionId);
57      }
58  
59      public void valueBound(HttpSessionBindingEvent event) {
60      }
61  
62      public void valueUnbound(HttpSessionBindingEvent event) {
63          invalidate(event.getSession().getId());
64      }
65  
66      private PortletSessionTracker() {
67          _sessions = new HashMap<String, Set<HttpSession>>();
68      }
69  
70      private void _add(HttpSession session) {
71          String sessionId = session.getId();
72  
73          synchronized (_sessions) {
74              Set<HttpSession> portletSessions = _sessions.get(sessionId);
75  
76              if (portletSessions == null) {
77                  portletSessions = new HashSet<HttpSession>();
78  
79                  _sessions.put(sessionId, portletSessions);
80              }
81  
82              portletSessions.add(session);
83          }
84      }
85  
86      private void _invalidate(String sessionId) {
87          synchronized (_sessions) {
88              Set<HttpSession> portletSessions = _sessions.get(sessionId);
89  
90              if (portletSessions != null) {
91                  Iterator<HttpSession> itr = portletSessions.iterator();
92  
93                  while (itr.hasNext()) {
94                      HttpSession session = itr.next();
95  
96                      try {
97                          session.invalidate();
98                      }
99                      catch (Exception e) {
100                     }
101                 }
102             }
103 
104             _sessions.remove(sessionId);
105         }
106     }
107 
108     private static PortletSessionTracker _instance =
109         new PortletSessionTracker();
110 
111     private transient Map<String, Set<HttpSession>> _sessions;
112 
113 }