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.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.util.InitUtil;
030    
031    import javax.servlet.ServletContextEvent;
032    
033    import org.springframework.beans.CachedIntrospectionResults;
034    import org.springframework.context.ApplicationContext;
035    import org.springframework.web.context.ContextLoader;
036    import org.springframework.web.context.ContextLoaderListener;
037    
038    /**
039     * @author Michael Young
040     * @author Shuyang Zhou
041     */
042    public class PortalContextLoaderListener extends ContextLoaderListener {
043    
044            public void contextInitialized(ServletContextEvent event) {
045                    InitUtil.init();
046    
047                    super.contextInitialized(event);
048    
049                    ApplicationContext applicationContext =
050                            ContextLoader.getCurrentWebApplicationContext();
051    
052                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
053    
054                    BeanLocator beanLocator = new BeanLocatorImpl(
055                            portalClassLoader, applicationContext);
056    
057                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
058    
059                    ClassLoader classLoader = portalClassLoader;
060    
061                    while (classLoader != null) {
062                            CachedIntrospectionResults.clearClassLoader(classLoader);
063    
064                            classLoader = classLoader.getParent();
065                    }
066            }
067    
068            public void contextDestroyed(ServletContextEvent event) {
069                    super.contextDestroyed(event);
070    
071                    HotDeployUtil.reset();
072                    InstancePool.reset();
073                    PortletBagPool.reset();
074    
075                    ThreadLocalCacheManager.destroy();
076    
077                    try {
078                            ClearThreadLocalUtil.clearThreadLocal();
079                    }
080                    catch (Exception e) {
081                            _log.error(e, e);
082                    }
083    
084                    try {
085                            ClearTimerThreadUtil.clearTimerThread();
086                    }
087                    catch(Exception e) {
088                            _log.error(e, e);
089                    }
090            }
091    
092            private Log _log = LogFactoryUtil.getLog(PortalContextLoaderListener.class);
093    
094    }