001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.configuration.ConfigurationFactoryUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
024    import com.liferay.portal.kernel.util.MethodCache;
025    import com.liferay.portal.spring.bean.BeanReferenceRefreshUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.lang.reflect.Method;
029    
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletContextEvent;
032    
033    import org.springframework.context.ApplicationContext;
034    import org.springframework.context.ConfigurableApplicationContext;
035    import org.springframework.context.support.ClassPathXmlApplicationContext;
036    import org.springframework.web.context.ConfigurableWebApplicationContext;
037    import org.springframework.web.context.ContextLoaderListener;
038    import org.springframework.web.context.WebApplicationContext;
039    import org.springframework.web.context.support.WebApplicationContextUtils;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @see    PortletApplicationContext
044     */
045    public class PortletContextLoaderListener extends ContextLoaderListener {
046    
047            @Override
048            public void contextDestroyed(ServletContextEvent servletContextEvent) {
049                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
050    
051                    ServletContext servletContext = servletContextEvent.getServletContext();
052    
053                    try {
054                            Class<?> beanLocatorUtilClass = Class.forName(
055                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
056                                    classLoader);
057    
058                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
059                                    "setBeanLocator", new Class[] {BeanLocator.class});
060    
061                            setBeanLocatorMethod.invoke(
062                                    beanLocatorUtilClass, new Object[] {null});
063    
064                            PortletBeanLocatorUtil.setBeanLocator(
065                                    servletContext.getServletContextName(), null);
066                    }
067                    catch (Exception e) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn(e, e);
070                            }
071                    }
072    
073                    super.contextDestroyed(servletContextEvent);
074    
075                    Object parentApplicationContext = servletContext.getAttribute(
076                            _PARENT_APPLICATION_CONTEXT_KEY);
077    
078                    if (parentApplicationContext instanceof
079                                    ConfigurableApplicationContext) {
080    
081                            servletContext.removeAttribute(_PARENT_APPLICATION_CONTEXT_KEY);
082    
083                            ConfigurableApplicationContext configurableApplicationContext =
084                                    (ConfigurableApplicationContext)parentApplicationContext;
085    
086                            configurableApplicationContext.close();
087                    }
088            }
089    
090            @Override
091            public void contextInitialized(ServletContextEvent servletContextEvent) {
092                    MethodCache.reset();
093    
094                    ServletContext servletContext = servletContextEvent.getServletContext();
095    
096                    Object previousApplicationContext = servletContext.getAttribute(
097                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
098    
099                    servletContext.removeAttribute(
100                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
101    
102                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
103    
104                    super.contextInitialized(servletContextEvent);
105    
106                    PortletBeanFactoryCleaner.readBeans();
107    
108                    ApplicationContext applicationContext =
109                            WebApplicationContextUtils.getWebApplicationContext(servletContext);
110    
111                    try {
112                            BeanReferenceRefreshUtil.refresh(
113                                    applicationContext.getAutowireCapableBeanFactory());
114                    }
115                    catch (Exception e) {
116                            _log.error(e, e);
117                    }
118    
119                    BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
120                            classLoader, applicationContext);
121    
122                    beanLocatorImpl.setPACLServletContextName(
123                            servletContext.getServletContextName());
124    
125                    try {
126                            Class<?> beanLocatorUtilClass = Class.forName(
127                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
128                                    classLoader);
129    
130                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
131                                    "setBeanLocator", new Class[] {BeanLocator.class});
132    
133                            setBeanLocatorMethod.invoke(
134                                    beanLocatorUtilClass, new Object[] {beanLocatorImpl});
135    
136                            PortletBeanLocatorUtil.setBeanLocator(
137                                    servletContext.getServletContextName(), beanLocatorImpl);
138                    }
139                    catch (Exception e) {
140                            _log.error(e, e);
141                    }
142    
143                    if (previousApplicationContext == null) {
144                            servletContext.removeAttribute(
145                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
146                    }
147                    else {
148                            servletContext.setAttribute(
149                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
150                                    previousApplicationContext);
151                    }
152            }
153    
154            @Override
155            protected void customizeContext(
156                    ServletContext servletContext,
157                    ConfigurableWebApplicationContext configurableWebApplicationContext) {
158    
159                    String configLocation = servletContext.getInitParameter(
160                            _PORTAL_CONFIG_LOCATION_PARAM);
161    
162                    configurableWebApplicationContext.setConfigLocation(configLocation);
163    
164                    configurableWebApplicationContext.addBeanFactoryPostProcessor(
165                            new PortletBeanFactoryPostProcessor());
166            }
167    
168            @Override
169            protected Class<?> determineContextClass(ServletContext servletContext) {
170                    return PortletApplicationContext.class;
171            }
172    
173            @Override
174            protected ApplicationContext loadParentContext(
175                    ServletContext servletContext) {
176    
177                    try {
178                            ConfigurationFactoryUtil.getConfiguration(
179                                    PortletClassLoaderUtil.getClassLoader(), "service");
180                    }
181                    catch (Exception e) {
182                            return null;
183                    }
184    
185                    ApplicationContext applicationContext =
186                            new ClassPathXmlApplicationContext(
187                                    PropsValues.SPRING_PORTLET_CONFIGS, true);
188    
189                    servletContext.setAttribute(
190                            _PARENT_APPLICATION_CONTEXT_KEY, applicationContext);
191    
192                    return applicationContext;
193            }
194    
195            private static final String _PARENT_APPLICATION_CONTEXT_KEY =
196                    PortletContextLoaderListener.class.getName() +
197                            "#parentApplicationContext";
198    
199            private static final String _PORTAL_CONFIG_LOCATION_PARAM =
200                    "portalContextConfigLocation";
201    
202            private static final Log _log = LogFactoryUtil.getLog(
203                    PortletContextLoaderListener.class);
204    
205    }