001    /**
002     * Copyright (c) 2000-2012 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.lang.PortalSecurityManagerThreadLocal;
047    import com.liferay.portal.security.permission.PermissionCacheUtil;
048    import com.liferay.portal.servlet.filters.cache.CacheUtil;
049    import com.liferay.portal.spring.bean.BeanReferenceRefreshUtil;
050    import com.liferay.portal.util.ClassLoaderUtil;
051    import com.liferay.portal.util.InitUtil;
052    import com.liferay.portal.util.PropsValues;
053    import com.liferay.portal.util.WebAppPool;
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                    if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
122                            try {
123                                    ModuleFrameworkUtilAdapter.stopRuntime();
124                            }
125                            catch (Exception e) {
126                                    _log.error(e, e);
127                            }
128                    }
129    
130                    try {
131                            super.contextDestroyed(servletContextEvent);
132    
133                            if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
134                                    try {
135                                            ModuleFrameworkUtilAdapter.stopFramework();
136                                    }
137                                    catch (Exception e) {
138                                            _log.error(e, e);
139                                    }
140                            }
141                    }
142                    finally {
143                            PortalContextLoaderLifecycleThreadLocal.setDestroying(false);
144                    }
145            }
146    
147            @Override
148            public void contextInitialized(ServletContextEvent servletContextEvent) {
149                    PortalSecurityManagerThreadLocal.setEnabled(false);
150    
151                    DBFactoryUtil.reset();
152                    DeployManagerUtil.reset();
153                    InstancePool.reset();
154                    MethodCache.reset();
155                    PortalBeanLocatorUtil.reset();
156                    PortalLifecycleUtil.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                                    ModuleFrameworkUtilAdapter.startFramework();
175                            }
176                            catch (Exception e) {
177                                    _log.error(e, e);
178                            }
179                    }
180    
181                    PortalContextLoaderLifecycleThreadLocal.setInitializing(true);
182    
183                    try {
184                            super.contextInitialized(servletContextEvent);
185                    }
186                    finally {
187                            PortalContextLoaderLifecycleThreadLocal.setInitializing(false);
188    
189                            PortalSecurityManagerThreadLocal.setEnabled(true);
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                    PermissionCacheUtil.clearCache();
207                    PermissionCacheUtil.clearLocalCache();
208                    TemplateResourceLoaderUtil.clearCache();
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                    beanLocatorImpl.setPACLWrapPersistence(true);
224    
225                    PortalBeanLocatorUtil.setBeanLocator(beanLocatorImpl);
226    
227                    ClassLoader classLoader = portalClassLoader;
228    
229                    while (classLoader != null) {
230                            CachedIntrospectionResults.clearClassLoader(classLoader);
231    
232                            classLoader = classLoader.getParent();
233                    }
234    
235                    AutowireCapableBeanFactory autowireCapableBeanFactory =
236                            applicationContext.getAutowireCapableBeanFactory();
237    
238                    clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
239    
240                    if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
241                            try {
242                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
243                                    ModuleFrameworkUtilAdapter.registerContext(servletContext);
244    
245                                    ModuleFrameworkUtilAdapter.startRuntime();
246                            }
247                            catch (Exception e) {
248                                    _log.error(e, e);
249                            }
250                    }
251            }
252    
253            protected void clearFilteredPropertyDescriptorsCache(
254                    AutowireCapableBeanFactory autowireCapableBeanFactory) {
255    
256                    try {
257                            Map<Class<?>, PropertyDescriptor[]>
258                                    filteredPropertyDescriptorsCache =
259                                            (Map<Class<?>, PropertyDescriptor[]>)
260                                                    _filteredPropertyDescriptorsCacheField.get(
261                                                            autowireCapableBeanFactory);
262    
263                            filteredPropertyDescriptorsCache.clear();
264                    }
265                    catch (Exception e) {
266                            _log.error(e, e);
267                    }
268            }
269    
270            private static Log _log = LogFactoryUtil.getLog(
271                    PortalContextLoaderListener.class);
272    
273            private static Field _filteredPropertyDescriptorsCacheField;
274    
275            static {
276                    try {
277                            _filteredPropertyDescriptorsCacheField =
278                                    ReflectionUtil.getDeclaredField(
279                                            AbstractAutowireCapableBeanFactory.class,
280                                            "filteredPropertyDescriptorsCache");
281                    }
282                    catch (Exception e) {
283                            _log.error(e, e);
284                    }
285            }
286    
287    }