001    /**
002     * Copyright (c) 2000-2013 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.cache.ehcache.ClearEhcacheThreadUtil;
019    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
022    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
023    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
024    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
025    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
026    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
027    import com.liferay.portal.kernel.deploy.DeployManagerUtil;
028    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.portlet.PortletBagPool;
032    import com.liferay.portal.kernel.process.ClassPathUtil;
033    import com.liferay.portal.kernel.servlet.DirectServletRegistryUtil;
034    import com.liferay.portal.kernel.servlet.ServletContextPool;
035    import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
036    import com.liferay.portal.kernel.util.CharBufferPool;
037    import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
038    import com.liferay.portal.kernel.util.ClearTimerThreadUtil;
039    import com.liferay.portal.kernel.util.InstancePool;
040    import com.liferay.portal.kernel.util.MethodCache;
041    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
042    import com.liferay.portal.kernel.util.ReferenceRegistry;
043    import com.liferay.portal.kernel.util.ReflectionUtil;
044    import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
045    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
046    import com.liferay.portal.security.permission.PermissionCacheUtil;
047    import com.liferay.portal.servlet.filters.cache.CacheUtil;
048    import com.liferay.portal.spring.bean.BeanReferenceRefreshUtil;
049    import com.liferay.portal.util.ClassLoaderUtil;
050    import com.liferay.portal.util.InitUtil;
051    import com.liferay.portal.util.PropsValues;
052    import com.liferay.portal.util.WebAppPool;
053    import com.liferay.portlet.PortletContextBagPool;
054    import com.liferay.portlet.wiki.util.WikiCacheUtil;
055    
056    import java.beans.PropertyDescriptor;
057    
058    import java.lang.reflect.Field;
059    
060    import java.util.Map;
061    
062    import javax.servlet.ServletContext;
063    import javax.servlet.ServletContextEvent;
064    
065    import org.springframework.beans.CachedIntrospectionResults;
066    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
067    import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
068    import org.springframework.context.ApplicationContext;
069    import org.springframework.web.context.ContextLoader;
070    import org.springframework.web.context.ContextLoaderListener;
071    
072    /**
073     * @author Michael Young
074     * @author Shuyang Zhou
075     * @author Raymond Augé
076     */
077    public class PortalContextLoaderListener extends ContextLoaderListener {
078    
079            @Override
080            public void contextDestroyed(ServletContextEvent servletContextEvent) {
081                    PortalContextLoaderLifecycleThreadLocal.setDestroying(true);
082    
083                    ThreadLocalCacheManager.destroy();
084    
085                    try {
086                            ClearThreadLocalUtil.clearThreadLocal();
087                    }
088                    catch (Exception e) {
089                            _log.error(e, e);
090                    }
091    
092                    try {
093                            ClearTimerThreadUtil.clearTimerThread();
094                    }
095                    catch (Exception e) {
096                            _log.error(e, e);
097                    }
098    
099                    try {
100                            ClearEhcacheThreadUtil.clearEhcacheReplicationThread();
101                    }
102                    catch (Exception e) {
103                            _log.error(e, e);
104                    }
105    
106                    try {
107                            DirectServletRegistryUtil.clearServlets();
108                    }
109                    catch (Exception e) {
110                            _log.error(e, e);
111                    }
112    
113                    try {
114                            HotDeployUtil.reset();
115                    }
116                    catch (Exception e) {
117                            _log.error(e, e);
118                    }
119    
120                    if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
121                            try {
122                                    ModuleFrameworkUtilAdapter.stopRuntime();
123                            }
124                            catch (Exception e) {
125                                    _log.error(e, e);
126                            }
127                    }
128    
129                    try {
130                            super.contextDestroyed(servletContextEvent);
131    
132                            if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
133                                    try {
134                                            ModuleFrameworkUtilAdapter.stopFramework();
135                                    }
136                                    catch (Exception e) {
137                                            _log.error(e, e);
138                                    }
139                            }
140                    }
141                    finally {
142                            PortalContextLoaderLifecycleThreadLocal.setDestroying(false);
143                    }
144            }
145    
146            @Override
147            public void contextInitialized(ServletContextEvent servletContextEvent) {
148                    DBFactoryUtil.reset();
149                    DeployManagerUtil.reset();
150                    InstancePool.reset();
151                    MethodCache.reset();
152                    PortalBeanLocatorUtil.reset();
153                    PortalLifecycleUtil.reset();
154                    PortletBagPool.reset();
155    
156                    ReferenceRegistry.releaseReferences();
157    
158                    InitUtil.init();
159    
160                    ServletContext servletContext = servletContextEvent.getServletContext();
161    
162                    ClassPathUtil.initializeClassPaths(servletContext);
163    
164                    CacheRegistryUtil.clear();
165                    CharBufferPool.cleanUp();
166                    PortletContextBagPool.clear();
167                    WebAppPool.clear();
168    
169                    if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
170                            try {
171                                    ModuleFrameworkUtilAdapter.startFramework();
172                            }
173                            catch (Exception e) {
174                                    _log.error(e, e);
175                            }
176                    }
177    
178                    PortalContextLoaderLifecycleThreadLocal.setInitializing(true);
179    
180                    try {
181                            super.contextInitialized(servletContextEvent);
182                    }
183                    finally {
184                            PortalContextLoaderLifecycleThreadLocal.setInitializing(false);
185                    }
186    
187                    ApplicationContext applicationContext =
188                            ContextLoader.getCurrentWebApplicationContext();
189    
190                    try {
191                            BeanReferenceRefreshUtil.refresh(applicationContext);
192                    }
193                    catch (Exception e) {
194                            _log.error(e, e);
195                    }
196    
197                    FinderCacheUtil.clearCache();
198                    FinderCacheUtil.clearLocalCache();
199                    EntityCacheUtil.clearCache();
200                    EntityCacheUtil.clearLocalCache();
201                    PermissionCacheUtil.clearCache();
202                    PermissionCacheUtil.clearLocalCache();
203                    TemplateResourceLoaderUtil.clearCache();
204                    WikiCacheUtil.clearCache(0);
205    
206                    ServletContextPool.clear();
207    
208                    CacheUtil.clearCache();
209                    MultiVMPoolUtil.clear();
210                    SingleVMPoolUtil.clear();
211                    WebCachePoolUtil.clear();
212    
213                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
214    
215                    BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
216                            portalClassLoader, applicationContext);
217    
218                    PortalBeanLocatorUtil.setBeanLocator(beanLocatorImpl);
219    
220                    ClassLoader classLoader = portalClassLoader;
221    
222                    while (classLoader != null) {
223                            CachedIntrospectionResults.clearClassLoader(classLoader);
224    
225                            classLoader = classLoader.getParent();
226                    }
227    
228                    AutowireCapableBeanFactory autowireCapableBeanFactory =
229                            applicationContext.getAutowireCapableBeanFactory();
230    
231                    clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
232    
233                    if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
234                            try {
235                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
236                                    ModuleFrameworkUtilAdapter.registerContext(servletContext);
237    
238                                    ModuleFrameworkUtilAdapter.startRuntime();
239                            }
240                            catch (Exception e) {
241                                    _log.error(e, e);
242                            }
243                    }
244            }
245    
246            protected void clearFilteredPropertyDescriptorsCache(
247                    AutowireCapableBeanFactory autowireCapableBeanFactory) {
248    
249                    try {
250                            Map<Class<?>, PropertyDescriptor[]>
251                                    filteredPropertyDescriptorsCache =
252                                            (Map<Class<?>, PropertyDescriptor[]>)
253                                                    _filteredPropertyDescriptorsCacheField.get(
254                                                            autowireCapableBeanFactory);
255    
256                            filteredPropertyDescriptorsCache.clear();
257                    }
258                    catch (Exception e) {
259                            _log.error(e, e);
260                    }
261            }
262    
263            private static Log _log = LogFactoryUtil.getLog(
264                    PortalContextLoaderListener.class);
265    
266            private static Field _filteredPropertyDescriptorsCacheField;
267    
268            static {
269                    try {
270                            _filteredPropertyDescriptorsCacheField =
271                                    ReflectionUtil.getDeclaredField(
272                                            AbstractAutowireCapableBeanFactory.class,
273                                            "filteredPropertyDescriptorsCache");
274                    }
275                    catch (Exception e) {
276                            _log.error(e, e);
277                    }
278            }
279    
280    }