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  public class SharedSessionAttributeListener
49      implements HttpSessionAttributeListener, HttpSessionListener {
50  
51      public void attributeAdded(HttpSessionBindingEvent event) {
52          if (PropsValues.SESSION_DISABLED) {
53              return;
54          }
55  
56          HttpSession session = event.getSession();
57  
58          if (!_sessionIds.contains(session.getId())) {
59              return;
60          }
61  
62          SharedSessionAttributeCache cache =
63              SharedSessionAttributeCache.getInstance(session);
64  
65          String name = event.getName();
66  
67          for (String sharedName : PropsValues.SHARED_SESSION_ATTRIBUTES) {
68              if (name.startsWith(sharedName)) {
69                  cache.setAttribute(name, event.getValue());
70  
71                  return;
72              }
73          }
74      }
75  
76      public void attributeRemoved(HttpSessionBindingEvent event) {
77          if (PropsValues.SESSION_DISABLED) {
78              return;
79          }
80  
81          HttpSession session = event.getSession();
82  
83          if (!_sessionIds.contains(session.getId())) {
84              return;
85          }
86  
87          SharedSessionAttributeCache cache =
88              SharedSessionAttributeCache.getInstance(session);
89  
90          cache.removeAttribute(event.getName());
91      }
92  
93      public void attributeReplaced(HttpSessionBindingEvent event) {
94          if (PropsValues.SESSION_DISABLED) {
95              return;
96          }
97  
98          HttpSession session = event.getSession();
99  
100         if (!_sessionIds.contains(session.getId())) {
101             return;
102         }
103 
104         SharedSessionAttributeCache cache =
105             SharedSessionAttributeCache.getInstance(session);
106 
107         if (cache.contains(event.getName())) {
108             cache.setAttribute(event.getName(), event.getValue());
109         }
110     }
111 
112     public void sessionCreated(HttpSessionEvent event) {
113         if (PropsValues.SESSION_DISABLED) {
114             return;
115         }
116 
117         HttpSession session = event.getSession();
118 
119         SharedSessionAttributeCache.getInstance(session);
120 
121         _sessionIds.add(session.getId());
122     }
123 
124     public void sessionDestroyed(HttpSessionEvent event) {
125         if (PropsValues.SESSION_DISABLED) {
126             return;
127         }
128 
129         HttpSession session = event.getSession();
130 
131         _sessionIds.remove(session.getId());
132     }
133 
134     private Set<String> _sessionIds = new ConcurrentHashSet<String>();
135 
136 }