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          PortletBeanFactoryCleaner.readBeans();
75  
76          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
77  
78          ServletContext servletContext = event.getServletContext();
79  
80          ApplicationContext applicationContext =
81              WebApplicationContextUtils.getWebApplicationContext(servletContext);
82  
83          BeanLocator beanLocator = new BeanLocatorImpl(
84              classLoader, applicationContext);
85  
86          try {
87              Class<?> beanLocatorUtilClass = Class.forName(
88                  "com.liferay.util.bean.PortletBeanLocatorUtil", true,
89                  classLoader);
90  
91              Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
92                  "setBeanLocator", new Class[] {BeanLocator.class});
93  
94              setBeanLocatorMethod.invoke(
95                  beanLocatorUtilClass, new Object[] {beanLocator});
96  
97              PortletBeanLocatorUtil.setBeanLocator(
98                  servletContext.getServletContextName(), beanLocator);
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102         }
103 
104         servletContext.removeAttribute(
105             WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
106     }
107 
108     protected ContextLoader createContextLoader() {
109         return new PortletContextLoader();
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(
113         PortletContextLoaderListener.class);
114 
115 }