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