1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.spring.context;
16  
17  import com.liferay.portal.bean.BeanLocatorImpl;
18  import com.liferay.portal.kernel.bean.BeanLocator;
19  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
22  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.portlet.PortletBagPool;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.MethodCache;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.kernel.util.ReferenceRegistry;
30  import com.liferay.portal.kernel.util.ReflectionUtil;
31  import com.liferay.portal.util.InitUtil;
32  
33  import java.beans.PropertyDescriptor;
34  
35  import java.lang.reflect.Field;
36  
37  import java.util.Map;
38  
39  import javax.servlet.ServletContextEvent;
40  
41  import org.springframework.beans.CachedIntrospectionResults;
42  import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
43  import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
44  import org.springframework.context.ApplicationContext;
45  import org.springframework.web.context.ContextLoader;
46  import org.springframework.web.context.ContextLoaderListener;
47  
48  /**
49   * <a href="PortalContextLoaderListener.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Michael Young
52   * @author Shuyang Zhou
53   */
54  public class PortalContextLoaderListener extends ContextLoaderListener {
55  
56      public void contextInitialized(ServletContextEvent event) {
57          CacheRegistry.reset();
58          HotDeployUtil.reset();
59          InstancePool.reset();
60          MethodCache.reset();
61          PortletBagPool.reset();
62  
63          ReferenceRegistry.releaseReferences();
64  
65          InitUtil.init();
66  
67          super.contextInitialized(event);
68  
69          ApplicationContext applicationContext =
70              ContextLoader.getCurrentWebApplicationContext();
71  
72          ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
73  
74          BeanLocator beanLocator = new BeanLocatorImpl(
75              portalClassLoader, applicationContext);
76  
77          PortalBeanLocatorUtil.setBeanLocator(beanLocator);
78  
79          ClassLoader classLoader = portalClassLoader;
80  
81          while (classLoader != null) {
82              CachedIntrospectionResults.clearClassLoader(classLoader);
83  
84              classLoader = classLoader.getParent();
85          }
86  
87          AutowireCapableBeanFactory autowireCapableBeanFactory =
88              applicationContext.getAutowireCapableBeanFactory();
89  
90          clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
91      }
92  
93      public void contextDestroyed(ServletContextEvent event) {
94          super.contextDestroyed(event);
95  
96          ThreadLocalCacheManager.destroy();
97      }
98  
99      protected void clearFilteredPropertyDescriptorsCache(
100         AutowireCapableBeanFactory autowireCapableBeanFactory) {
101 
102         try {
103             Map<Class<?>, PropertyDescriptor[]>
104                 filteredPropertyDescriptorsCache =
105                     (Map<Class<?>, PropertyDescriptor[]>)
106                         _filteredPropertyDescriptorsCacheField.get(
107                             autowireCapableBeanFactory);
108 
109             filteredPropertyDescriptorsCache.clear();
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113         }
114     }
115 
116     private static Field _filteredPropertyDescriptorsCacheField;
117 
118     private static Log _log = LogFactoryUtil.getLog(
119         PortalContextLoaderListener.class);
120 
121     static {
122         try {
123             _filteredPropertyDescriptorsCacheField =
124                 ReflectionUtil.getDeclaredField(
125                     AbstractAutowireCapableBeanFactory.class,
126                     "filteredPropertyDescriptorsCache");
127         }
128         catch (Exception e) {
129             _log.error(e, e);
130         }
131     }
132 
133 }