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.HttpSessionActivationListener;
024    import javax.servlet.http.HttpSessionAttributeListener;
025    import javax.servlet.http.HttpSessionBindingEvent;
026    import javax.servlet.http.HttpSessionBindingListener;
027    import javax.servlet.http.HttpSessionEvent;
028    import javax.servlet.http.HttpSessionListener;
029    
030    /**
031     * <p>
032     * See http://issues.liferay.com/browse/LEP-2299.
033     * </p>
034     *
035     * @author Olaf Fricke
036     * @author Brian Wing Shun Chan
037     * @author Raymond Augé
038     */
039    public class PortletSessionListenerManager
040            implements HttpSessionActivationListener, HttpSessionAttributeListener,
041                               HttpSessionBindingListener, HttpSessionListener {
042    
043            public static void addHttpSessionActivationListener(
044                    HttpSessionActivationListener httpSessionActivationListener) {
045    
046                    _httpSessionActivationListeners.add(httpSessionActivationListener);
047            }
048    
049            public static void addHttpSessionAttributeListener(
050                    HttpSessionAttributeListener httpSessionAttributeListener) {
051    
052                    _httpSessionAttributeListeners.add(httpSessionAttributeListener);
053            }
054    
055            public static void addHttpSessionBindingListener(
056                    HttpSessionBindingListener httpSessionBindingListener) {
057    
058                    _httpSessionBindingListeners.add(httpSessionBindingListener);
059            }
060    
061            public static void addHttpSessionListener(
062                    HttpSessionListener httpSessionListener) {
063    
064                    _httpSessionListeners.add(httpSessionListener);
065            }
066    
067            public static void removeHttpSessionActivationListener(
068                    HttpSessionActivationListener httpSessionActivationListener) {
069    
070                    _httpSessionActivationListeners.remove(httpSessionActivationListener);
071            }
072    
073            public static void removeHttpSessionAttributeListener(
074                    HttpSessionAttributeListener httpSessionAttributeListener) {
075    
076                    _httpSessionAttributeListeners.remove(httpSessionAttributeListener);
077            }
078    
079            public static void removeHttpSessionBindingListener(
080                    HttpSessionBindingListener httpSessionBindingListener) {
081    
082                    _httpSessionBindingListeners.remove(httpSessionBindingListener);
083            }
084    
085            public static void removeHttpSessionListener(
086                    HttpSessionListener httpSessionListener) {
087    
088                    _httpSessionListeners.remove(httpSessionListener);
089            }
090    
091            public void attributeAdded(
092                    HttpSessionBindingEvent httpSessionBindingEvent) {
093    
094                    httpSessionBindingEvent = getHttpSessionBindingEvent(
095                            httpSessionBindingEvent);
096    
097                    for (HttpSessionAttributeListener httpSessionAttributeListener :
098                                    _httpSessionAttributeListeners) {
099    
100                            httpSessionAttributeListener.attributeAdded(
101                                    httpSessionBindingEvent);
102                    }
103            }
104    
105            public void attributeRemoved(
106                    HttpSessionBindingEvent httpSessionBindingEvent) {
107    
108                    httpSessionBindingEvent = getHttpSessionBindingEvent(
109                            httpSessionBindingEvent);
110    
111                    for (HttpSessionAttributeListener httpSessionAttributeListener :
112                                    _httpSessionAttributeListeners) {
113    
114                            httpSessionAttributeListener.attributeRemoved(
115                                    httpSessionBindingEvent);
116                    }
117            }
118    
119            public void attributeReplaced(
120                    HttpSessionBindingEvent httpSessionBindingEvent) {
121    
122                    httpSessionBindingEvent = getHttpSessionBindingEvent(
123                            httpSessionBindingEvent);
124    
125                    for (HttpSessionAttributeListener httpSessionAttributeListener :
126                                    _httpSessionAttributeListeners) {
127    
128                            httpSessionAttributeListener.attributeReplaced(
129                                    httpSessionBindingEvent);
130                    }
131            }
132    
133            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
134                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
135    
136                    Thread currentThread = Thread.currentThread();
137    
138                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
139    
140                    try {
141                            for (HttpSessionListener httpSessionListener :
142                                            _httpSessionListeners) {
143    
144                                    Class<?> clazz = httpSessionListener.getClass();
145    
146                                    ClassLoader classLoader = clazz.getClassLoader();
147    
148                                    currentThread.setContextClassLoader(classLoader);
149    
150                                    httpSessionListener.sessionCreated(httpSessionEvent);
151                            }
152                    }
153                    finally {
154                            currentThread.setContextClassLoader(contextClassLoader);
155                    }
156            }
157    
158            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
159                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
160    
161                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
162                            httpSessionListener.sessionDestroyed(httpSessionEvent);
163                    }
164            }
165    
166            public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
167                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
168    
169                    for (HttpSessionActivationListener httpSessionActivationListener :
170                                    _httpSessionActivationListeners) {
171    
172                            httpSessionActivationListener.sessionDidActivate(httpSessionEvent);
173                    }
174            }
175    
176            public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
177                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
178    
179                    for (HttpSessionActivationListener httpSessionActivationListener :
180                                    _httpSessionActivationListeners) {
181    
182                            httpSessionActivationListener.sessionWillPassivate(
183                                    httpSessionEvent);
184                    }
185            }
186    
187            public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
188                    httpSessionBindingEvent = getHttpSessionBindingEvent(
189                            httpSessionBindingEvent);
190    
191                    for (HttpSessionBindingListener httpSessionBindingListener :
192                                    _httpSessionBindingListeners) {
193    
194                            httpSessionBindingListener.valueBound(httpSessionBindingEvent);
195                    }
196            }
197    
198            public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
199                    httpSessionBindingEvent = getHttpSessionBindingEvent(
200                            httpSessionBindingEvent);
201    
202                    for (HttpSessionBindingListener httpSessionBindingListener :
203                                    _httpSessionBindingListeners) {
204    
205                            httpSessionBindingListener.valueUnbound(httpSessionBindingEvent);
206                    }
207            }
208    
209            protected HttpSessionBindingEvent getHttpSessionBindingEvent(
210                    HttpSessionBindingEvent httpSessionBindingEvent) {
211    
212                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
213                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
214                                    new CompoundSessionIdHttpSession(
215                                            httpSessionBindingEvent.getSession());
216    
217                            httpSessionBindingEvent = new HttpSessionBindingEvent(
218                                    compoundSessionIdHttpSession, httpSessionBindingEvent.getName(),
219                                    httpSessionBindingEvent.getValue());
220                    }
221    
222                    return httpSessionBindingEvent;
223            }
224    
225            protected HttpSessionEvent getHttpSessionEvent(
226                    HttpSessionEvent httpSessionEvent) {
227    
228                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
229                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
230                                    new CompoundSessionIdHttpSession(httpSessionEvent.getSession());
231    
232                            httpSessionEvent = new HttpSessionEvent(
233                                    compoundSessionIdHttpSession);
234                    }
235    
236                    return httpSessionEvent;
237            }
238    
239            private static List<HttpSessionActivationListener>
240                    _httpSessionActivationListeners =
241                            new ArrayList<HttpSessionActivationListener>();
242            private static List<HttpSessionAttributeListener>
243                    _httpSessionAttributeListeners =
244                            new ArrayList<HttpSessionAttributeListener>();
245            private static List<HttpSessionBindingListener>
246                    _httpSessionBindingListeners =
247                            new ArrayList<HttpSessionBindingListener>();
248            private static List<HttpSessionListener> _httpSessionListeners =
249                    new ArrayList<HttpSessionListener>();
250    
251    }