001    /**
002     * Copyright (c) 2000-2010 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 java.util.ArrayList;
018    import java.util.Iterator;
019    import java.util.List;
020    
021    import javax.servlet.http.HttpSessionEvent;
022    import javax.servlet.http.HttpSessionListener;
023    
024    /**
025     * <p>
026     * See http://issues.liferay.com/browse/LEP-2299.
027     * </p>
028     *
029     * @author Olaf Fricke
030     * @author Brian Wing Shun Chan
031     */
032    public class PortletSessionListenerManager implements HttpSessionListener {
033    
034            public static void addListener(HttpSessionListener listener) {
035                    _listeners.add(listener);
036            }
037    
038            public static void removeListener(HttpSessionListener listener) {
039                    _listeners.remove(listener);
040            }
041    
042            public void sessionCreated(HttpSessionEvent event) {
043                    Thread currentThread = Thread.currentThread();
044    
045                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
046    
047                    try {
048                            Iterator<HttpSessionListener> itr = _listeners.iterator();
049    
050                            while (itr.hasNext()) {
051                                    HttpSessionListener listener = itr.next();
052    
053                                    ClassLoader listenerClassLoader =
054                                            listener.getClass().getClassLoader();
055    
056                                    currentThread.setContextClassLoader(listenerClassLoader);
057    
058                                    listener.sessionCreated(event);
059                            }
060                    }
061                    finally {
062                            currentThread.setContextClassLoader(contextClassLoader);
063                    }
064            }
065    
066            public void sessionDestroyed(HttpSessionEvent event) {
067                    Iterator<HttpSessionListener> itr = _listeners.iterator();
068    
069                    while (itr.hasNext()) {
070                            HttpSessionListener listener = itr.next();
071    
072                            listener.sessionDestroyed(event);
073                    }
074            }
075    
076            private static List<HttpSessionListener> _listeners =
077                    new ArrayList<HttpSessionListener>();
078    
079    }