001
014
015 package com.liferay.portal.spring.context;
016
017 import com.liferay.portal.bean.BeanLocatorImpl;
018 import com.liferay.portal.kernel.bean.BeanLocator;
019 import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
023
024 import java.lang.reflect.Method;
025
026 import javax.servlet.ServletContext;
027 import javax.servlet.ServletContextEvent;
028
029 import org.springframework.context.ApplicationContext;
030 import org.springframework.web.context.ContextLoader;
031 import org.springframework.web.context.ContextLoaderListener;
032 import org.springframework.web.context.WebApplicationContext;
033 import org.springframework.web.context.support.WebApplicationContextUtils;
034
035
040 public class PortletContextLoaderListener extends ContextLoaderListener {
041
042 public void contextDestroyed(ServletContextEvent event) {
043 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
044
045 ServletContext servletContext = event.getServletContext();
046
047 try {
048 Class<?> beanLocatorUtilClass = Class.forName(
049 "com.liferay.util.bean.PortletBeanLocatorUtil", true,
050 classLoader);
051
052 Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
053 "setBeanLocator", new Class[] {BeanLocator.class});
054
055 setBeanLocatorMethod.invoke(
056 beanLocatorUtilClass, new Object[] {null});
057
058 PortletBeanLocatorUtil.setBeanLocator(
059 servletContext.getServletContextName(), null);
060 }
061 catch (Exception e) {
062 if (_log.isWarnEnabled()) {
063 _log.warn(e, e);
064 }
065 }
066
067 super.contextDestroyed(event);
068 }
069
070 public void contextInitialized(ServletContextEvent event) {
071 super.contextInitialized(event);
072
073 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
074
075 ServletContext servletContext = event.getServletContext();
076
077 ApplicationContext applicationContext =
078 WebApplicationContextUtils.getWebApplicationContext(servletContext);
079
080 BeanLocator beanLocator = new BeanLocatorImpl(
081 classLoader, applicationContext);
082
083 try {
084 Class<?> beanLocatorUtilClass = Class.forName(
085 "com.liferay.util.bean.PortletBeanLocatorUtil", true,
086 classLoader);
087
088 Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
089 "setBeanLocator", new Class[] {BeanLocator.class});
090
091 setBeanLocatorMethod.invoke(
092 beanLocatorUtilClass, new Object[] {beanLocator});
093
094 PortletBeanLocatorUtil.setBeanLocator(
095 servletContext.getServletContextName(), beanLocator);
096 }
097 catch (Exception e) {
098 _log.error(e, e);
099 }
100
101 servletContext.removeAttribute(
102 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
103 }
104
105 protected ContextLoader createContextLoader() {
106 return new PortletContextLoader();
107 }
108
109 private static Log _log = LogFactoryUtil.getLog(
110 PortletContextLoaderListener.class);
111
112 }