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