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.servlet;
16  
17  import com.liferay.portal.kernel.servlet.ServletVersionDetector;
18  import com.liferay.portal.kernel.util.ConcurrentHashSet;
19  import com.liferay.portal.util.PropsValues;
20  
21  import java.util.Set;
22  
23  import javax.servlet.http.HttpSession;
24  import javax.servlet.http.HttpSessionAttributeListener;
25  import javax.servlet.http.HttpSessionBindingEvent;
26  import javax.servlet.http.HttpSessionEvent;
27  import javax.servlet.http.HttpSessionListener;
28  
29  /**
30   * <a href="SharedSessionAttributeListener.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * <p>
34   * Listener used to help manage shared session attributes into a cache. This
35   * cache is more thread safe than the HttpSession and leads to fewer problems
36   * with shared session attributes being modified out of sequence.
37   * </p>
38   *
39   * @author Michael C. Han
40   */
41  public class SharedSessionAttributeListener
42      implements HttpSessionAttributeListener, HttpSessionListener {
43  
44      public SharedSessionAttributeListener() {
45          if (ServletVersionDetector.is2_5()) {
46              return;
47          }
48  
49          _sessionIds = new ConcurrentHashSet<String>();
50      }
51  
52      public void attributeAdded(HttpSessionBindingEvent event) {
53          if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
54              return;
55          }
56  
57          HttpSession session = event.getSession();
58  
59          if (!_sessionIds.contains(session.getId())) {
60              return;
61          }
62  
63          SharedSessionAttributeCache cache =
64              SharedSessionAttributeCache.getInstance(session);
65  
66          String name = event.getName();
67  
68          for (String sharedName : PropsValues.SHARED_SESSION_ATTRIBUTES) {
69              if (name.startsWith(sharedName)) {
70                  cache.setAttribute(name, event.getValue());
71  
72                  return;
73              }
74          }
75      }
76  
77      public void attributeRemoved(HttpSessionBindingEvent event) {
78          if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
79              return;
80          }
81  
82          HttpSession session = event.getSession();
83  
84          if (!_sessionIds.contains(session.getId())) {
85              return;
86          }
87  
88          SharedSessionAttributeCache cache =
89              SharedSessionAttributeCache.getInstance(session);
90  
91          cache.removeAttribute(event.getName());
92      }
93  
94      public void attributeReplaced(HttpSessionBindingEvent event) {
95          if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
96              return;
97          }
98  
99          HttpSession session = event.getSession();
100 
101         if (!_sessionIds.contains(session.getId())) {
102             return;
103         }
104 
105         SharedSessionAttributeCache cache =
106             SharedSessionAttributeCache.getInstance(session);
107 
108         if (cache.contains(event.getName())) {
109             cache.setAttribute(event.getName(), event.getValue());
110         }
111     }
112 
113     public void sessionCreated(HttpSessionEvent event) {
114         if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
115             return;
116         }
117 
118         HttpSession session = event.getSession();
119 
120         SharedSessionAttributeCache.getInstance(session);
121 
122         _sessionIds.add(session.getId());
123     }
124 
125     public void sessionDestroyed(HttpSessionEvent event) {
126         if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
127             return;
128         }
129 
130         HttpSession session = event.getSession();
131 
132         _sessionIds.remove(session.getId());
133     }
134 
135     private Set<String> _sessionIds;
136 
137 }