1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }