1
14
15 package com.liferay.portal.servlet;
16
17 import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
18 import com.liferay.portal.kernel.servlet.ServletVersionDetector;
19 import com.liferay.portal.kernel.util.ArrayUtil;
20 import com.liferay.portal.util.PropsValues;
21
22 import java.util.Set;
23
24 import javax.servlet.http.HttpSession;
25 import javax.servlet.http.HttpSessionAttributeListener;
26 import javax.servlet.http.HttpSessionBindingEvent;
27 import javax.servlet.http.HttpSessionEvent;
28 import javax.servlet.http.HttpSessionListener;
29
30
42 public class SharedSessionAttributeListener
43 implements HttpSessionAttributeListener, HttpSessionListener {
44
45 public SharedSessionAttributeListener() {
46 if (ServletVersionDetector.is2_5()) {
47 return;
48 }
49
50 _sessionIds = new ConcurrentHashSet<String>();
51 }
52
53 public void attributeAdded(HttpSessionBindingEvent event) {
54 if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
55 return;
56 }
57
58 HttpSession session = event.getSession();
59
60 if (!_sessionIds.contains(session.getId())) {
61 return;
62 }
63
64 SharedSessionAttributeCache sharedSessionAttributeCache =
65 SharedSessionAttributeCache.getInstance(session);
66
67 String name = event.getName();
68
69 if (ArrayUtil.contains(
70 PropsValues.SHARED_SESSION_ATTRIBUTES_EXCLUDES, name)) {
71
72 return;
73 }
74
75 for (String sharedName : PropsValues.SHARED_SESSION_ATTRIBUTES) {
76 if (!name.startsWith(sharedName)) {
77 continue;
78 }
79
80 sharedSessionAttributeCache.setAttribute(name, event.getValue());
81
82 return;
83 }
84 }
85
86 public void attributeRemoved(HttpSessionBindingEvent event) {
87 if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
88 return;
89 }
90
91 HttpSession session = event.getSession();
92
93 if (!_sessionIds.contains(session.getId())) {
94 return;
95 }
96
97 SharedSessionAttributeCache sharedSessionAttributeCache =
98 SharedSessionAttributeCache.getInstance(session);
99
100 sharedSessionAttributeCache.removeAttribute(event.getName());
101 }
102
103 public void attributeReplaced(HttpSessionBindingEvent event) {
104 if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
105 return;
106 }
107
108 HttpSession session = event.getSession();
109
110 if (!_sessionIds.contains(session.getId())) {
111 return;
112 }
113
114 SharedSessionAttributeCache sharedSessionAttributeCache =
115 SharedSessionAttributeCache.getInstance(session);
116
117 if (sharedSessionAttributeCache.contains(event.getName())) {
118 Object value = session.getAttribute(event.getName());
119
120 sharedSessionAttributeCache.setAttribute(event.getName(), value);
121 }
122 }
123
124 public void sessionCreated(HttpSessionEvent event) {
125 if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
126 return;
127 }
128
129 HttpSession session = event.getSession();
130
131 SharedSessionAttributeCache.getInstance(session);
132
133 _sessionIds.add(session.getId());
134 }
135
136 public void sessionDestroyed(HttpSessionEvent event) {
137 if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
138 return;
139 }
140
141 HttpSession session = event.getSession();
142
143 _sessionIds.remove(session.getId());
144 }
145
146 private Set<String> _sessionIds;
147
148 }