1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.servlet;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.http.HttpSessionEvent;
27  import javax.servlet.http.HttpSessionListener;
28  
29  /**
30   * <a href="PortletSessionListenerManager.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * <p>
34   * See http://support.liferay.com/browse/LEP-2299.
35   * </p>
36   *
37   * @author Olaf Fricke
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PortletSessionListenerManager implements HttpSessionListener {
42  
43      public static void addListener(HttpSessionListener listener) {
44          _listeners.add(listener);
45      }
46  
47      public static void removeListener(HttpSessionListener listener) {
48          _listeners.remove(listener);
49      }
50  
51      public void sessionCreated(HttpSessionEvent event) {
52          Thread currentThread = Thread.currentThread();
53  
54          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
55  
56          try {
57              Iterator<HttpSessionListener> itr = _listeners.iterator();
58  
59              while (itr.hasNext()) {
60                  HttpSessionListener listener = itr.next();
61  
62                  ClassLoader listenerClassLoader =
63                      listener.getClass().getClassLoader();
64  
65                  currentThread.setContextClassLoader(listenerClassLoader);
66  
67                  listener.sessionCreated(event);
68              }
69          }
70          finally {
71              currentThread.setContextClassLoader(contextClassLoader);
72          }
73      }
74  
75      public void sessionDestroyed(HttpSessionEvent event) {
76          Iterator<HttpSessionListener> itr = _listeners.iterator();
77  
78          while (itr.hasNext()) {
79              HttpSessionListener listener = itr.next();
80  
81              listener.sessionDestroyed(event);
82          }
83      }
84  
85      private static List<HttpSessionListener> _listeners =
86          new ArrayList<HttpSessionListener>();
87  
88  }