001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }