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.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            @Override
049            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
050                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
051    
052                    Thread currentThread = Thread.currentThread();
053    
054                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
055    
056                    try {
057                            for (HttpSessionListener httpSessionListener :
058                                            _httpSessionListeners) {
059    
060                                    Class<?> clazz = httpSessionListener.getClass();
061    
062                                    ClassLoader classLoader = clazz.getClassLoader();
063    
064                                    currentThread.setContextClassLoader(classLoader);
065    
066                                    httpSessionListener.sessionCreated(httpSessionEvent);
067                            }
068                    }
069                    finally {
070                            currentThread.setContextClassLoader(contextClassLoader);
071                    }
072            }
073    
074            @Override
075            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
076                    httpSessionEvent = getHttpSessionEvent(httpSessionEvent);
077    
078                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
079                            httpSessionListener.sessionDestroyed(httpSessionEvent);
080                    }
081            }
082    
083            protected HttpSessionEvent getHttpSessionEvent(
084                    HttpSessionEvent httpSessionEvent) {
085    
086                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
087                            CompoundSessionIdHttpSession compoundSessionIdHttpSession =
088                                    new CompoundSessionIdHttpSession(httpSessionEvent.getSession());
089    
090                            httpSessionEvent = new HttpSessionEvent(
091                                    compoundSessionIdHttpSession);
092                    }
093    
094                    return httpSessionEvent;
095            }
096    
097            private static List<HttpSessionListener> _httpSessionListeners =
098                    new ArrayList<HttpSessionListener>();
099    
100    }