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.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ReflectionUtil;
20  
21  import java.lang.reflect.Field;
22  import java.lang.reflect.Method;
23  
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.aspectj.weaver.tools.ShadowMatch;
29  
30  import org.springframework.aop.ClassFilter;
31  import org.springframework.aop.Pointcut;
32  import org.springframework.aop.aspectj.AspectJExpressionPointcut;
33  import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
34  import org.springframework.beans.factory.BeanFactory;
35  import org.springframework.beans.factory.BeanFactoryAware;
36  import org.springframework.beans.factory.ListableBeanFactory;
37  
38  /**
39   * <a href="PortletBeanFactoryCleaner.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Shuyang Zhou
42   */
43  public class PortletBeanFactoryCleaner implements BeanFactoryAware {
44  
45      public static void readBeans() {
46          if (_beanFactory == null) {
47              if (_log.isWarnEnabled()) {
48                  _log.warn("BeanFactory is null");
49              }
50  
51              return;
52          }
53  
54          if (!(_beanFactory instanceof ListableBeanFactory)) {
55              return;
56          }
57  
58          ListableBeanFactory listableBeanFactory =
59              (ListableBeanFactory)_beanFactory;
60  
61          String[] names = listableBeanFactory.getBeanDefinitionNames();
62  
63          for (String name : names) {
64              try {
65                  _readBean(listableBeanFactory, name);
66              }
67              catch (Exception e) {
68              }
69          }
70      }
71  
72      public void destroy() {
73          for (BeanFactoryAware beanFactoryAware : _beanFactoryAwares) {
74              try {
75                  beanFactoryAware.setBeanFactory(null);
76              }
77              catch (Exception e) {
78              }
79          }
80  
81          _beanFactoryAwares.clear();
82  
83          for (AspectJExpressionPointcut aspectJExpressionPointcut :
84                  _aspectJExpressionPointcuts) {
85  
86              try {
87                  Map<Method, ShadowMatch> shadowMapCache =
88                      (Map<Method, ShadowMatch>)_shadowMapCacheField.get(
89                          aspectJExpressionPointcut);
90  
91                  shadowMapCache.clear();
92              }
93              catch (Exception e) {
94              }
95          }
96  
97          _aspectJExpressionPointcuts.clear();
98      }
99  
100     public void setBeanFactory(BeanFactory beanFactory) {
101         _beanFactory = beanFactory;
102     }
103 
104     private static void _readBean(
105             ListableBeanFactory listableBeanFactory, String name)
106         throws Exception {
107 
108         Object bean = listableBeanFactory.getBean(name);
109 
110         if (bean instanceof AspectJPointcutAdvisor) {
111             AspectJPointcutAdvisor aspectJPointcutAdvisor =
112                 (AspectJPointcutAdvisor)bean;
113 
114             Pointcut pointcut = aspectJPointcutAdvisor.getPointcut();
115 
116             ClassFilter classFilter = pointcut.getClassFilter();
117 
118             if (classFilter instanceof AspectJExpressionPointcut) {
119                 AspectJExpressionPointcut aspectJExpressionPointcut =
120                     (AspectJExpressionPointcut)classFilter;
121 
122                 _beanFactoryAwares.add(aspectJExpressionPointcut);
123                 _aspectJExpressionPointcuts.add(aspectJExpressionPointcut);
124             }
125         }
126         else if (bean instanceof BeanFactoryAware) {
127             _beanFactoryAwares.add((BeanFactoryAware)bean);
128         }
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(
132         PortletBeanFactoryCleaner.class);
133 
134     private static Set<AspectJExpressionPointcut> _aspectJExpressionPointcuts =
135         new HashSet<AspectJExpressionPointcut>();
136     private static BeanFactory _beanFactory;
137     private static Set<BeanFactoryAware> _beanFactoryAwares =
138         new HashSet<BeanFactoryAware>();
139     private static Field _shadowMapCacheField;
140 
141     static {
142         try {
143             _shadowMapCacheField = ReflectionUtil.getDeclaredField(
144                 AspectJExpressionPointcut.class, "shadowMapCache");
145         }
146         catch (Exception e) {
147             throw new ExceptionInInitializerError(e);
148         }
149     }
150 
151 }