1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.servlet;
24  
25  import com.liferay.portal.kernel.util.ConcurrentHashSet;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.util.Set;
29  
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.http.HttpSessionAttributeListener;
32  import javax.servlet.http.HttpSessionBindingEvent;
33  import javax.servlet.http.HttpSessionEvent;
34  import javax.servlet.http.HttpSessionListener;
35  
36  /**
37   * <a href="SharedSessionAttributeListener.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * <p>
41   * Listener used to help manage shared session attributes into a cache. This
42   * cache is more thread safe than the HttpSession and leads to fewer problems
43   * with shared session attributes being modified out of sequence.
44   * </p>
45   *
46   * @author Michael C. Han
47   *
48   */
49  public class SharedSessionAttributeListener
50      implements HttpSessionAttributeListener, HttpSessionListener {
51  
52      public void attributeAdded(HttpSessionBindingEvent event) {
53          if (PropsValues.SESSION_DISABLED) {
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) {
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) {
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) {
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) {
127             return;
128         }
129 
130         HttpSession session = event.getSession();
131 
132         _sessionIds.remove(session.getId());
133     }
134 
135     private Set<String> _sessionIds = new ConcurrentHashSet<String>();
136 
137 }