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 import com.liferay.portal.kernel.util.MethodCache;
024
025 import java.lang.reflect.Method;
026
027 import javax.servlet.ServletContext;
028 import javax.servlet.ServletContextEvent;
029
030 import org.springframework.context.ApplicationContext;
031 import org.springframework.web.context.ConfigurableWebApplicationContext;
032 import org.springframework.web.context.ContextLoaderListener;
033 import org.springframework.web.context.WebApplicationContext;
034 import org.springframework.web.context.support.WebApplicationContextUtils;
035
036
040 public class PortletContextLoaderListener extends ContextLoaderListener {
041
042 @Override
043 public void contextDestroyed(ServletContextEvent servletContextEvent) {
044 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
045
046 ServletContext servletContext = servletContextEvent.getServletContext();
047
048 try {
049 Class<?> beanLocatorUtilClass = Class.forName(
050 "com.liferay.util.bean.PortletBeanLocatorUtil", true,
051 classLoader);
052
053 Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
054 "setBeanLocator", new Class[] {BeanLocator.class});
055
056 setBeanLocatorMethod.invoke(
057 beanLocatorUtilClass, new Object[] {null});
058
059 PortletBeanLocatorUtil.setBeanLocator(
060 servletContext.getServletContextName(), null);
061 }
062 catch (Exception e) {
063 if (_log.isWarnEnabled()) {
064 _log.warn(e, e);
065 }
066 }
067
068 super.contextDestroyed(servletContextEvent);
069 }
070
071 @Override
072 public void contextInitialized(ServletContextEvent servletContextEvent) {
073 MethodCache.reset();
074
075 ServletContext servletContext = servletContextEvent.getServletContext();
076
077 Object previousApplicationContext = servletContext.getAttribute(
078 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
079
080 servletContext.removeAttribute(
081 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
082
083 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
084
085 super.contextInitialized(servletContextEvent);
086
087 PortletBeanFactoryCleaner.readBeans();
088
089 ApplicationContext applicationContext =
090 WebApplicationContextUtils.getWebApplicationContext(servletContext);
091
092 BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
093 classLoader, applicationContext);
094
095 beanLocatorImpl.setPACLServletContextName(
096 servletContext.getServletContextName());
097
098 try {
099 Class<?> beanLocatorUtilClass = Class.forName(
100 "com.liferay.util.bean.PortletBeanLocatorUtil", true,
101 classLoader);
102
103 Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
104 "setBeanLocator", new Class[] {BeanLocator.class});
105
106 setBeanLocatorMethod.invoke(
107 beanLocatorUtilClass, new Object[] {beanLocatorImpl});
108
109 PortletBeanLocatorUtil.setBeanLocator(
110 servletContext.getServletContextName(), beanLocatorImpl);
111 }
112 catch (Exception e) {
113 _log.error(e, e);
114 }
115
116 if (previousApplicationContext == null) {
117 servletContext.removeAttribute(
118 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
119 }
120 else {
121 servletContext.setAttribute(
122 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
123 previousApplicationContext);
124 }
125 }
126
127 @Override
128 protected void customizeContext(
129 ServletContext servletContext,
130 ConfigurableWebApplicationContext configurableWebApplicationContext) {
131
132 String configLocation = servletContext.getInitParameter(
133 _PORTAL_CONFIG_LOCATION_PARAM);
134
135 configurableWebApplicationContext.setConfigLocation(configLocation);
136
137 configurableWebApplicationContext.addBeanFactoryPostProcessor(
138 new PortletBeanFactoryPostProcessor());
139 }
140
141 @Override
142 protected Class<?> determineContextClass(ServletContext servletContext) {
143 return PortletApplicationContext.class;
144 }
145
146 @Override
147 protected ApplicationContext loadParentContext(
148 ServletContext servletContext) {
149
150 return null;
151 }
152
153 private static final String _PORTAL_CONFIG_LOCATION_PARAM =
154 "portalContextConfigLocation";
155
156 private static Log _log = LogFactoryUtil.getLog(
157 PortletContextLoaderListener.class);
158
159 }