001    /**
002     * Copyright (c) 2000-2010 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.portlet.PortletBagPool;
023    import com.liferay.portal.kernel.util.InstancePool;
024    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
025    import com.liferay.portal.util.InitUtil;
026    
027    import javax.servlet.ServletContextEvent;
028    
029    import org.springframework.beans.CachedIntrospectionResults;
030    import org.springframework.context.ApplicationContext;
031    import org.springframework.web.context.ContextLoader;
032    import org.springframework.web.context.ContextLoaderListener;
033    
034    /**
035     * @author Michael Young
036     * @author Shuyang Zhou
037     */
038    public class PortalContextLoaderListener extends ContextLoaderListener {
039    
040            public void contextInitialized(ServletContextEvent event) {
041                    InitUtil.init();
042    
043                    super.contextInitialized(event);
044    
045                    ApplicationContext applicationContext =
046                            ContextLoader.getCurrentWebApplicationContext();
047    
048                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
049    
050                    BeanLocator beanLocator = new BeanLocatorImpl(
051                            portalClassLoader, applicationContext);
052    
053                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
054    
055                    ClassLoader classLoader = portalClassLoader;
056    
057                    while (classLoader != null) {
058                            CachedIntrospectionResults.clearClassLoader(classLoader);
059    
060                            classLoader = classLoader.getParent();
061                    }
062            }
063    
064            public void contextDestroyed(ServletContextEvent event) {
065                    super.contextDestroyed(event);
066    
067                    HotDeployUtil.reset();
068                    InstancePool.reset();
069                    PortletBagPool.reset();
070    
071                    ThreadLocalCacheManager.destroy();
072            }
073    
074    }