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