1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
28  import com.liferay.portal.kernel.util.PortalInitable;
29  import com.liferay.portal.kernel.util.PortalInitableUtil;
30  
31  import javax.servlet.ServletContextEvent;
32  import javax.servlet.ServletContextListener;
33  
34  /**
35   * <a href="PortalClassLoaderServletContextListener.java.html"><b><i>View Source
36   * </i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public abstract class PortalClassLoaderServletContextListener
42      implements PortalInitable, ServletContextListener {
43  
44      public PortalClassLoaderServletContextListener() {
45      }
46  
47      public void portalInit() {
48          ClassLoader contextClassLoader =
49              Thread.currentThread().getContextClassLoader();
50  
51          try {
52              Thread.currentThread().setContextClassLoader(
53                  PortalClassLoaderUtil.getClassLoader());
54  
55              _servletContextListener = getInstance();
56  
57              _servletContextListener.contextInitialized(_event);
58          }
59          catch (Exception e) {
60              _log.error(e, e);
61          }
62          finally {
63              Thread.currentThread().setContextClassLoader(
64                  contextClassLoader);
65          }
66      }
67  
68      public void contextInitialized(ServletContextEvent event) {
69          _event = event;
70  
71          PortalInitableUtil.init(this);
72      }
73  
74      public void contextDestroyed(ServletContextEvent event) {
75          ClassLoader contextClassLoader =
76              Thread.currentThread().getContextClassLoader();
77  
78          try {
79              Thread.currentThread().setContextClassLoader(
80                  PortalClassLoaderUtil.getClassLoader());
81  
82              _servletContextListener.contextDestroyed(event);
83          }
84          finally {
85              Thread.currentThread().setContextClassLoader(contextClassLoader);
86          }
87      }
88  
89      protected abstract ServletContextListener getInstance() throws Exception;
90  
91      private static Log _log =
92          LogFactoryUtil.getLog(PortalClassLoaderServletContextListener.class);
93  
94      private ServletContextListener _servletContextListener;
95      private ServletContextEvent _event;
96  
97  }