1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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  import javax.servlet.http.HttpSessionEvent;
29  import javax.servlet.http.HttpSessionListener;
30  
31  /**
32   * <a href="PortletSessionTracker.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * See http://issues.liferay.com/browse/LEP-1466.
36   * </p>
37   *
38   * @author Rudy Hilado
39   */
40  public class PortletSessionTracker
41      implements HttpSessionListener, HttpSessionBindingListener, Serializable {
42  
43      public static void add(HttpSession session) {
44          _instance._add(session);
45      }
46  
47      public static void invalidate(HttpSession session) {
48          _instance._invalidate(session.getId());
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 sessionCreated(HttpSessionEvent httpSessionEvent) {
60      }
61  
62      public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
63          _invalidate(httpSessionEvent.getSession().getId());
64      }
65  
66      public void valueBound(HttpSessionBindingEvent event) {
67      }
68  
69      public void valueUnbound(HttpSessionBindingEvent event) {
70          invalidate(event.getSession().getId());
71      }
72  
73      private PortletSessionTracker() {
74          _sessions = new HashMap<String, Set<HttpSession>>();
75  
76          PortletSessionListenerManager.addListener(this);
77      }
78  
79      private void _add(HttpSession session) {
80          String sessionId = session.getId();
81  
82          synchronized (_sessions) {
83              Set<HttpSession> portletSessions = _sessions.get(sessionId);
84  
85              if (portletSessions == null) {
86                  portletSessions = new HashSet<HttpSession>();
87  
88                  _sessions.put(sessionId, portletSessions);
89              }
90  
91              portletSessions.add(session);
92          }
93      }
94  
95      private void _invalidate(String sessionId) {
96          Set<HttpSession> sessionsToInvalidate = null;
97  
98          synchronized (_sessions) {
99              Set<HttpSession> portletSessions = _sessions.get(sessionId);
100 
101             if (portletSessions != null) {
102                 sessionsToInvalidate = new HashSet<HttpSession>(
103                     portletSessions);
104             }
105 
106             _sessions.remove(sessionId);
107         }
108 
109         if (sessionsToInvalidate != null) {
110             Iterator<HttpSession> itr = sessionsToInvalidate.iterator();
111 
112             while (itr.hasNext()) {
113                 HttpSession session = itr.next();
114 
115                 try {
116                     session.invalidate();
117                 }
118                 catch (Exception e) {
119                 }
120             }
121         }
122     }
123 
124     private static PortletSessionTracker _instance =
125         new PortletSessionTracker();
126 
127     private transient Map<String, Set<HttpSession>> _sessions;
128 
129 }