001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdHttpSession;
018    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdSplitterUtil;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import javax.servlet.http.HttpSession;
024    import javax.servlet.http.HttpSessionActivationListener;
025    import javax.servlet.http.HttpSessionAttributeListener;
026    import javax.servlet.http.HttpSessionBindingEvent;
027    import javax.servlet.http.HttpSessionBindingListener;
028    import javax.servlet.http.HttpSessionEvent;
029    import javax.servlet.http.HttpSessionListener;
030    
031    /**
032     * <p>
033     * See http://issues.liferay.com/browse/LEP-2299.
034     * </p>
035     *
036     * @author Olaf Fricke
037     * @author Brian Wing Shun Chan
038     * @author Raymond Aug??
039     */
040    public class PortletSessionListenerManager
041            implements HttpSessionActivationListener, HttpSessionAttributeListener,
042                               HttpSessionBindingListener, HttpSessionListener {
043    
044            public static void addHttpSessionActivationListener(
045                    HttpSessionActivationListener httpSessionActivationListener) {
046    
047                    _httpSessionActivationListeners.add(httpSessionActivationListener);
048            }
049    
050            public static void addHttpSessionAttributeListener(
051                    HttpSessionAttributeListener httpSessionAttributeListener) {
052    
053                    _httpSessionAttributeListeners.add(httpSessionAttributeListener);
054            }
055    
056            public static void addHttpSessionBindingListener(
057                    HttpSessionBindingListener httpSessionBindingListener) {
058    
059                    _httpSessionBindingListeners.add(httpSessionBindingListener);
060            }
061    
062            public static void addHttpSessionListener(
063                    HttpSessionListener httpSessionListener) {
064    
065                    _httpSessionListeners.add(httpSessionListener);
066            }
067    
068            public static void removeHttpSessionActivationListener(
069                    HttpSessionActivationListener httpSessionActivationListener) {
070    
071                    _httpSessionActivationListeners.remove(httpSessionActivationListener);
072            }
073    
074            public static void removeHttpSessionAttributeListener(
075                    HttpSessionAttributeListener httpSessionAttributeListener) {
076    
077                    _httpSessionAttributeListeners.remove(httpSessionAttributeListener);
078            }
079    
080            public static void removeHttpSessionBindingListener(
081                    HttpSessionBindingListener httpSessionBindingListener) {
082    
083                    _httpSessionBindingListeners.remove(httpSessionBindingListener);
084            }
085    
086            public static void removeHttpSessionListener(
087                    HttpSessionListener httpSessionListener) {
088    
089                    _httpSessionListeners.remove(httpSessionListener);
090            }
091    
092            @Override
093            public void attributeAdded(
094                    HttpSessionBindingEvent httpSessionBindingEvent) {
095    
096                    if (_httpSessionAttributeListeners.isEmpty()) {
097                            return;
098                    }
099    
100                    httpSessionBindingEvent = getHttpSessionBindingEvent(
101                            httpSessionBindingEvent);
102    
103                    for (HttpSessionAttributeListener httpSessionAttributeListener :
104                                    _httpSessionAttributeListeners) {
105    
106                            httpSessionAttributeListener.attributeAdded(
107                                    httpSessionBindingEvent);
108                    }
109            }
110    
111            @Override
112            public void attributeRemoved(
113                    HttpSessionBindingEvent httpSessionBindingEvent) {
114    
115                    if (_httpSessionAttributeListeners.isEmpty()) {
116                            return;
117                    }
118    
119                    httpSessionBindingEvent = getHttpSessionBindingEvent(
120                            httpSessionBindingEvent);
121    
122                    for (HttpSessionAttributeListener httpSessionAttributeListener :
123                                    _httpSessionAttributeListeners) {
124    
125                            httpSessionAttributeListener.attributeRemoved(
126                                    httpSessionBindingEvent);
127                    }
128            }
129    
130            @Override
131            public void attributeReplaced(
132                    HttpSessionBindingEvent httpSessionBindingEvent) {
133    
134                    if (_httpSessionAttributeListeners.isEmpty()) {
135                            return;
136                    }
137    
138                    httpSessionBindingEvent = getHttpSessionBindingEvent(
139                            httpSessionBindingEvent);
140    
141                    for (HttpSessionAttributeListener httpSessionAttributeListener :
142                                    _httpSessionAttributeListeners) {
143    
144                            httpSessionAttributeListener.attributeReplaced(
145                                    httpSessionBindingEvent);
146                    }
147            }
148    
149            @Override
150            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
151                    if (_httpSessionListeners.isEmpty()) {
152                            return;
153                    }
154    
155                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
156    
157                    Thread currentThread = Thread.currentThread();
158    
159                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
160    
161                    try {
162                            for (HttpSessionListener httpSessionListener :
163                                            _httpSessionListeners) {
164    
165                                    Class<?> clazz = httpSessionListener.getClass();
166    
167                                    ClassLoader classLoader = clazz.getClassLoader();
168    
169                                    currentThread.setContextClassLoader(classLoader);
170    
171                                    httpSessionListener.sessionCreated(httpSessionEvent);
172                            }
173                    }
174                    finally {
175                            currentThread.setContextClassLoader(contextClassLoader);
176                    }
177            }
178    
179            @Override
180            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
181                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
182    
183                    HttpSession session = httpSessionEvent.getSession();
184    
185                    PortletSessionTracker.invalidate(session.getId());
186    
187                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
188                            httpSessionListener.sessionDestroyed(httpSessionEvent);
189                    }
190            }
191    
192            @Override
193            public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
194                    if (_httpSessionActivationListeners.isEmpty()) {
195                            return;
196                    }
197    
198                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
199    
200                    for (HttpSessionActivationListener httpSessionActivationListener :
201                                    _httpSessionActivationListeners) {
202    
203                            httpSessionActivationListener.sessionDidActivate(httpSessionEvent);
204                    }
205            }
206    
207            @Override
208            public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
209                    if (_httpSessionActivationListeners.isEmpty()) {
210                            return;
211                    }
212    
213                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
214    
215                    for (HttpSessionActivationListener httpSessionActivationListener :
216                                    _httpSessionActivationListeners) {
217    
218                            httpSessionActivationListener.sessionWillPassivate(
219                                    httpSessionEvent);
220                    }
221            }
222    
223            @Override
224            public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
225                    if (_httpSessionBindingListeners.isEmpty()) {
226                            return;
227                    }
228    
229                    httpSessionBindingEvent = getHttpSessionBindingEvent(
230                            httpSessionBindingEvent);
231    
232                    for (HttpSessionBindingListener httpSessionBindingListener :
233                                    _httpSessionBindingListeners) {
234    
235                            httpSessionBindingListener.valueBound(httpSessionBindingEvent);
236                    }
237            }
238    
239            @Override
240            public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
241                    if (_httpSessionBindingListeners.isEmpty()) {
242                            return;
243                    }
244    
245                    httpSessionBindingEvent = getHttpSessionBindingEvent(
246                            httpSessionBindingEvent);
247    
248                    for (HttpSessionBindingListener httpSessionBindingListener :
249                                    _httpSessionBindingListeners) {
250    
251                            httpSessionBindingListener.valueUnbound(httpSessionBindingEvent);
252                    }
253            }
254    
255            protected HttpSessionBindingEvent getHttpSessionBindingEvent(
256                    HttpSessionBindingEvent httpSessionBindingEvent) {
257    
258                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
259                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
260                                    new CompoundSessionIdHttpSession(
261                                            httpSessionBindingEvent.getSession());
262    
263                            httpSessionBindingEvent = new HttpSessionBindingEvent(
264                                    compoundSessionIdHttpSession, httpSessionBindingEvent.getName(),
265                                    httpSessionBindingEvent.getValue());
266                    }
267    
268                    return httpSessionBindingEvent;
269            }
270    
271            protected HttpSessionEvent getHttpSessionEvent(
272                    HttpSessionEvent httpSessionEvent) {
273    
274                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
275                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
276                                    new CompoundSessionIdHttpSession(httpSessionEvent.getSession());
277    
278                            httpSessionEvent = new HttpSessionEvent(
279                                    compoundSessionIdHttpSession);
280                    }
281    
282                    return httpSessionEvent;
283            }
284    
285            private static List<HttpSessionActivationListener>
286                    _httpSessionActivationListeners =
287                            new ArrayList<HttpSessionActivationListener>();
288            private static List<HttpSessionAttributeListener>
289                    _httpSessionAttributeListeners =
290                            new ArrayList<HttpSessionAttributeListener>();
291            private static List<HttpSessionBindingListener>
292                    _httpSessionBindingListeners =
293                            new ArrayList<HttpSessionBindingListener>();
294            private static List<HttpSessionListener> _httpSessionListeners =
295                    new ArrayList<HttpSessionListener>();
296    
297    }