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