1   /**
2    * Copyright (c) 2000-2009 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.portlet.PortletClassLoaderUtil;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.kernel.util.PortalInitable;
30  import com.liferay.portal.kernel.util.PortalInitableUtil;
31  
32  import javax.servlet.ServletContextEvent;
33  import javax.servlet.ServletContextListener;
34  
35  /**
36   * <a href="PortalClassLoaderServletContextListener.java.html"><b><i>View Source
37   * </i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Sandeep Soni
41   *
42   */
43  public abstract class PortalClassLoaderServletContextListener
44      implements PortalInitable, ServletContextListener {
45  
46      public PortalClassLoaderServletContextListener() {
47      }
48  
49      public void contextDestroyed(ServletContextEvent event) {
50          PortletClassLoaderUtil.setClassLoader(_portletClassLoader);
51  
52          Thread currentThread = Thread.currentThread();
53  
54          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
55  
56          try {
57              currentThread.setContextClassLoader(
58                  PortalClassLoaderUtil.getClassLoader());
59  
60              _servletContextListener.contextDestroyed(event);
61          }
62          finally {
63              currentThread.setContextClassLoader(contextClassLoader);
64          }
65      }
66  
67      public void contextInitialized(ServletContextEvent event) {
68          _event = event;
69  
70          Thread currentThread = Thread.currentThread();
71  
72          _portletClassLoader = currentThread.getContextClassLoader();
73  
74          PortalInitableUtil.init(this);
75      }
76  
77      public void portalInit() {
78          PortletClassLoaderUtil.setClassLoader(_portletClassLoader);
79  
80          Thread currentThread = Thread.currentThread();
81  
82          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
83  
84          try {
85              currentThread.setContextClassLoader(
86                  PortalClassLoaderUtil.getClassLoader());
87  
88              _servletContextListener = getInstance();
89  
90              _servletContextListener.contextInitialized(_event);
91          }
92          catch (Exception e) {
93              _log.error(e, e);
94          }
95          finally {
96              currentThread.setContextClassLoader(contextClassLoader);
97          }
98      }
99  
100     protected abstract ServletContextListener getInstance() throws Exception;
101 
102     private static Log _log =
103         LogFactoryUtil.getLog(PortalClassLoaderServletContextListener.class);
104 
105     private ServletContextEvent _event;
106     private ServletContextListener _servletContextListener;
107     private ClassLoader _portletClassLoader;
108 
109 }