001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PortalBeanLocatorUtil;
020    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
021    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.portlet.PortletBagPool;
025    import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
026    import com.liferay.portal.kernel.util.ClearTimerThreadUtil;
027    import com.liferay.portal.kernel.util.InstancePool;
028    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
029    import com.liferay.portal.kernel.util.ReflectionUtil;
030    import com.liferay.portal.util.InitUtil;
031    
032    import java.beans.PropertyDescriptor;
033    
034    import java.lang.reflect.Field;
035    
036    import java.util.Map;
037    
038    import javax.servlet.ServletContextEvent;
039    
040    import org.springframework.beans.CachedIntrospectionResults;
041    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
042    import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
043    import org.springframework.context.ApplicationContext;
044    import org.springframework.web.context.ContextLoader;
045    import org.springframework.web.context.ContextLoaderListener;
046    
047    /**
048     * @author Michael Young
049     * @author Shuyang Zhou
050     */
051    public class PortalContextLoaderListener extends ContextLoaderListener {
052    
053            public void contextInitialized(ServletContextEvent event) {
054                    InitUtil.init();
055    
056                    super.contextInitialized(event);
057    
058                    ApplicationContext applicationContext =
059                            ContextLoader.getCurrentWebApplicationContext();
060    
061                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
062    
063                    BeanLocator beanLocator = new BeanLocatorImpl(
064                            portalClassLoader, applicationContext);
065    
066                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
067    
068                    ClassLoader classLoader = portalClassLoader;
069    
070                    while (classLoader != null) {
071                            CachedIntrospectionResults.clearClassLoader(classLoader);
072    
073                            classLoader = classLoader.getParent();
074                    }
075    
076                    AutowireCapableBeanFactory autowireCapableBeanFactory =
077                            applicationContext.getAutowireCapableBeanFactory();
078    
079                    clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
080            }
081    
082            public void contextDestroyed(ServletContextEvent event) {
083                    super.contextDestroyed(event);
084    
085                    HotDeployUtil.reset();
086                    InstancePool.reset();
087                    PortletBagPool.reset();
088    
089                    ThreadLocalCacheManager.destroy();
090    
091                    try {
092                            ClearThreadLocalUtil.clearThreadLocal();
093                    }
094                    catch (Exception e) {
095                            _log.error(e, e);
096                    }
097    
098                    try {
099                            ClearTimerThreadUtil.clearTimerThread();
100                    }
101                    catch(Exception e) {
102                            _log.error(e, e);
103                    }
104            }
105    
106            protected void clearFilteredPropertyDescriptorsCache(
107                    AutowireCapableBeanFactory autowireCapableBeanFactory) {
108    
109                    try {
110                            Map<Class<?>, PropertyDescriptor[]>
111                                    filteredPropertyDescriptorsCache =
112                                            (Map<Class<?>, PropertyDescriptor[]>)
113                                                    _filteredPropertyDescriptorsCacheField.get(
114                                                            autowireCapableBeanFactory);
115    
116                            filteredPropertyDescriptorsCache.clear();
117                    }
118                    catch (Exception e) {
119                            _log.error(e, e);
120                    }
121            }
122    
123            private static Field _filteredPropertyDescriptorsCacheField;
124    
125            private static Log _log = LogFactoryUtil.getLog(
126                    PortalContextLoaderListener.class);
127    
128            static {
129                    try {
130                            _filteredPropertyDescriptorsCacheField =
131                                    ReflectionUtil.getDeclaredField(
132                                            AbstractAutowireCapableBeanFactory.class,
133                                            "filteredPropertyDescriptorsCache");
134                    }
135                    catch (Exception e) {
136                            _log.error(e, e);
137                    }
138            }
139    
140    }