1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class PortalClassLoaderServletContextListener
43      implements PortalInitable, ServletContextListener {
44  
45      public PortalClassLoaderServletContextListener() {
46      }
47  
48      public void contextDestroyed(ServletContextEvent event) {
49          PortletClassLoaderUtil.setClassLoader(_portletClassLoader);
50  
51          Thread currentThread = Thread.currentThread();
52  
53          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
54  
55          try {
56              currentThread.setContextClassLoader(
57                  PortalClassLoaderUtil.getClassLoader());
58  
59              _servletContextListener.contextDestroyed(event);
60          }
61          finally {
62              currentThread.setContextClassLoader(contextClassLoader);
63          }
64      }
65  
66      public void contextInitialized(ServletContextEvent event) {
67          _event = event;
68  
69          Thread currentThread = Thread.currentThread();
70  
71          _portletClassLoader = currentThread.getContextClassLoader();
72  
73          PortalInitableUtil.init(this);
74      }
75  
76      public void portalInit() {
77          PortletClassLoaderUtil.setClassLoader(_portletClassLoader);
78  
79          Thread currentThread = Thread.currentThread();
80  
81          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
82  
83          try {
84              currentThread.setContextClassLoader(
85                  PortalClassLoaderUtil.getClassLoader());
86  
87              _servletContextListener = getInstance();
88  
89              _servletContextListener.contextInitialized(_event);
90          }
91          catch (Exception e) {
92              _log.error(e, e);
93          }
94          finally {
95              currentThread.setContextClassLoader(contextClassLoader);
96          }
97      }
98  
99      protected abstract ServletContextListener getInstance() throws Exception;
100 
101     private static Log _log =
102         LogFactoryUtil.getLog(PortalClassLoaderServletContextListener.class);
103 
104     private ServletContextEvent _event;
105     private ServletContextListener _servletContextListener;
106     private ClassLoader _portletClassLoader;
107 
108 }