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