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