1
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
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 }