1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.spring.context;
16  
17  import com.liferay.portal.bean.BeanLocatorImpl;
18  import com.liferay.portal.kernel.bean.BeanLocator;
19  import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
23  
24  import java.lang.reflect.Method;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.ServletContextEvent;
28  
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.web.context.ContextLoader;
31  import org.springframework.web.context.ContextLoaderListener;
32  import org.springframework.web.context.WebApplicationContext;
33  import org.springframework.web.context.support.WebApplicationContextUtils;
34  
35  /**
36   * <a href="PortletContextLoaderListener.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   * @see    PortletApplicationContext
41   * @see    PortletContextLoader
42   */
43  public class PortletContextLoaderListener extends ContextLoaderListener {
44  
45      public void contextDestroyed(ServletContextEvent event) {
46          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
47  
48          ServletContext servletContext = event.getServletContext();
49  
50          try {
51              Class<?> beanLocatorUtilClass = Class.forName(
52                  "com.liferay.util.bean.PortletBeanLocatorUtil", true,
53                  classLoader);
54  
55              Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
56                  "setBeanLocator", new Class[] {BeanLocator.class});
57  
58              setBeanLocatorMethod.invoke(
59                  beanLocatorUtilClass, new Object[] {null});
60  
61              PortletBeanLocatorUtil.setBeanLocator(
62                  servletContext.getServletContextName(), null);
63          }
64          catch (Exception e) {
65              _log.error(e, e);
66          }
67  
68          super.contextDestroyed(event);
69      }
70  
71      public void contextInitialized(ServletContextEvent event) {
72          super.contextInitialized(event);
73  
74          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
75  
76          ServletContext servletContext = event.getServletContext();
77  
78          ApplicationContext applicationContext =
79              WebApplicationContextUtils.getWebApplicationContext(servletContext);
80  
81          BeanLocator beanLocator = new BeanLocatorImpl(
82              classLoader, applicationContext);
83  
84          try {
85              Class<?> beanLocatorUtilClass = Class.forName(
86                  "com.liferay.util.bean.PortletBeanLocatorUtil", true,
87                  classLoader);
88  
89              Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
90                  "setBeanLocator", new Class[] {BeanLocator.class});
91  
92              setBeanLocatorMethod.invoke(
93                  beanLocatorUtilClass, new Object[] {beanLocator});
94  
95              PortletBeanLocatorUtil.setBeanLocator(
96                  servletContext.getServletContextName(), beanLocator);
97          }
98          catch (Exception e) {
99              _log.error(e, e);
100         }
101 
102         servletContext.removeAttribute(
103             WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
104     }
105 
106     protected ContextLoader createContextLoader() {
107         return new PortletContextLoader();
108     }
109 
110     private static Log _log = LogFactoryUtil.getLog(
111         PortletContextLoaderListener.class);
112 
113 }