1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import java.io.Serializable;
18  
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.servlet.http.HttpSession;
26  import javax.servlet.http.HttpSessionBindingEvent;
27  import javax.servlet.http.HttpSessionBindingListener;
28  
29  /**
30   * <a href="PortletSessionTracker.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * See http://issues.liferay.com/browse/LEP-1466.
34   * </p>
35   *
36   * @author Rudy Hilado
37   */
38  public class PortletSessionTracker
39      implements HttpSessionBindingListener, Serializable {
40  
41      public static void add(HttpSession session) {
42          _instance._add(session);
43      }
44  
45      public static HttpSessionBindingListener getInstance() {
46          return _instance;
47      }
48  
49      public static void invalidate(String sessionId) {
50          _instance._invalidate(sessionId);
51      }
52  
53      public void valueBound(HttpSessionBindingEvent event) {
54      }
55  
56      public void valueUnbound(HttpSessionBindingEvent event) {
57          invalidate(event.getSession().getId());
58      }
59  
60      private PortletSessionTracker() {
61          _sessions = new HashMap<String, Set<HttpSession>>();
62      }
63  
64      private void _add(HttpSession session) {
65          String sessionId = session.getId();
66  
67          synchronized (_sessions) {
68              Set<HttpSession> portletSessions = _sessions.get(sessionId);
69  
70              if (portletSessions == null) {
71                  portletSessions = new HashSet<HttpSession>();
72  
73                  _sessions.put(sessionId, portletSessions);
74              }
75  
76              portletSessions.add(session);
77          }
78      }
79  
80      private void _invalidate(String sessionId) {
81          Set<HttpSession> sessionsToInvalidate = new HashSet<HttpSession>();
82  
83          synchronized (_sessions) {
84              Set<HttpSession> portletSessions = _sessions.get(sessionId);
85  
86              if (portletSessions != null) {
87                  Iterator<HttpSession> itr = portletSessions.iterator();
88  
89                  while (itr.hasNext()) {
90                      sessionsToInvalidate.add(itr.next());
91                  }
92              }
93  
94              _sessions.remove(sessionId);
95          }
96  
97          Iterator<HttpSession> itr = sessionsToInvalidate.iterator();
98  
99          while (itr.hasNext()) {
100             HttpSession session = itr.next();
101 
102             try {
103                 session.invalidate();
104             }
105             catch (Exception e) {
106             }
107         }
108     }
109 
110     private static PortletSessionTracker _instance =
111         new PortletSessionTracker();
112 
113     private transient Map<String, Set<HttpSession>> _sessions;
114 
115 }