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