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