1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import javax.servlet.http.HttpSessionEvent;
22  import javax.servlet.http.HttpSessionListener;
23  
24  /**
25   * <a href="PortletSessionListenerManager.java.html"><b><i>View Source</i></b>
26   * </a>
27   *
28   * <p>
29   * See http://issues.liferay.com/browse/LEP-2299.
30   * </p>
31   *
32   * @author Olaf Fricke
33   * @author Brian Wing Shun Chan
34   */
35  public class PortletSessionListenerManager implements HttpSessionListener {
36  
37      public static void addListener(HttpSessionListener listener) {
38          _listeners.add(listener);
39      }
40  
41      public static void removeListener(HttpSessionListener listener) {
42          _listeners.remove(listener);
43      }
44  
45      public void sessionCreated(HttpSessionEvent event) {
46          Thread currentThread = Thread.currentThread();
47  
48          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
49  
50          try {
51              Iterator<HttpSessionListener> itr = _listeners.iterator();
52  
53              while (itr.hasNext()) {
54                  HttpSessionListener listener = itr.next();
55  
56                  ClassLoader listenerClassLoader =
57                      listener.getClass().getClassLoader();
58  
59                  currentThread.setContextClassLoader(listenerClassLoader);
60  
61                  listener.sessionCreated(event);
62              }
63          }
64          finally {
65              currentThread.setContextClassLoader(contextClassLoader);
66          }
67      }
68  
69      public void sessionDestroyed(HttpSessionEvent event) {
70          Iterator<HttpSessionListener> itr = _listeners.iterator();
71  
72          while (itr.hasNext()) {
73              HttpSessionListener listener = itr.next();
74  
75              listener.sessionDestroyed(event);
76          }
77      }
78  
79      private static List<HttpSessionListener> _listeners =
80          new ArrayList<HttpSessionListener>();
81  
82  }