001    /**
002     * Copyright (c) 2000-2012 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.HttpSessionEvent;
024    import javax.servlet.http.HttpSessionListener;
025    
026    /**
027     * <p>
028     * See http://issues.liferay.com/browse/LEP-2299.
029     * </p>
030     *
031     * @author Olaf Fricke
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletSessionListenerManager implements HttpSessionListener {
035    
036            public static void addHttpSessionListener(
037                    HttpSessionListener httpSessionListener) {
038    
039                    _httpSessionListeners.add(httpSessionListener);
040            }
041    
042            public static void removeHttpSessionListener(
043                    HttpSessionListener httpSessionListener) {
044    
045                    _httpSessionListeners.remove(httpSessionListener);
046            }
047    
048            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
049                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
050    
051                    Thread currentThread = Thread.currentThread();
052    
053                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
054    
055                    try {
056                            for (HttpSessionListener httpSessionListener :
057                                            _httpSessionListeners) {
058    
059                                    Class<?> clazz = httpSessionListener.getClass();
060    
061                                    ClassLoader classLoader = clazz.getClassLoader();
062    
063                                    currentThread.setContextClassLoader(classLoader);
064    
065                                    httpSessionListener.sessionCreated(httpSessionEvent);
066                            }
067                    }
068                    finally {
069                            currentThread.setContextClassLoader(contextClassLoader);
070                    }
071            }
072    
073            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
074                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
075    
076                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
077                            httpSessionListener.sessionDestroyed(httpSessionEvent);
078                    }
079            }
080    
081            protected HttpSessionEvent getHttpSessionEvent(
082                    HttpSessionEvent httpSessionEvent) {
083    
084                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
085                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
086                                    new CompoundSessionIdHttpSession(httpSessionEvent.getSession());
087    
088                            httpSessionEvent = new HttpSessionEvent(
089                                    compoundSessionIdHttpSession);
090                    }
091    
092                    return httpSessionEvent;
093            }
094    
095            private static List<HttpSessionListener> _httpSessionListeners =
096                    new ArrayList<HttpSessionListener>();
097    
098    }