001
014
015 package com.liferay.portal.spring.context;
016
017 import com.liferay.portal.kernel.exception.LoggedExceptionInInitializerError;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ReflectionUtil;
021
022 import java.lang.reflect.Field;
023 import java.lang.reflect.Method;
024
025 import java.util.HashSet;
026 import java.util.Map;
027 import java.util.Set;
028
029 import org.aspectj.weaver.tools.ShadowMatch;
030
031 import org.springframework.aop.ClassFilter;
032 import org.springframework.aop.Pointcut;
033 import org.springframework.aop.aspectj.AspectJExpressionPointcut;
034 import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
035 import org.springframework.beans.factory.BeanFactory;
036 import org.springframework.beans.factory.BeanFactoryAware;
037 import org.springframework.beans.factory.ListableBeanFactory;
038
039
042 public class PortletBeanFactoryCleaner implements BeanFactoryAware {
043
044 public static void readBeans() {
045 if (_beanFactory == null) {
046 if (_log.isWarnEnabled()) {
047 _log.warn("Bean factory is null");
048 }
049
050 return;
051 }
052
053 if (!(_beanFactory instanceof ListableBeanFactory)) {
054 return;
055 }
056
057 ListableBeanFactory listableBeanFactory =
058 (ListableBeanFactory)_beanFactory;
059
060 String[] names = listableBeanFactory.getBeanDefinitionNames();
061
062 for (String name : names) {
063 try {
064 _readBean(listableBeanFactory, name);
065 }
066 catch (Exception e) {
067 }
068 }
069 }
070
071 public void destroy() {
072 for (BeanFactoryAware beanFactoryAware : _beanFactoryAwares) {
073 try {
074 beanFactoryAware.setBeanFactory(null);
075 }
076 catch (Exception e) {
077 }
078 }
079
080 _beanFactoryAwares.clear();
081
082 for (AspectJExpressionPointcut aspectJExpressionPointcut :
083 _aspectJExpressionPointcuts) {
084
085 try {
086 Map<Method, ShadowMatch> shadowMatchCache =
087 (Map<Method, ShadowMatch>)_SHADOW_MATCH_CACHE_FIELD.get(
088 aspectJExpressionPointcut);
089
090 shadowMatchCache.clear();
091 }
092 catch (Exception e) {
093 }
094 }
095
096 _aspectJExpressionPointcuts.clear();
097 }
098
099 @Override
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 final Field _SHADOW_MATCH_CACHE_FIELD;
132
133 private static final Log _log = LogFactoryUtil.getLog(
134 PortletBeanFactoryCleaner.class);
135
136 private static final Set<AspectJExpressionPointcut>
137 _aspectJExpressionPointcuts = new HashSet<>();
138 private static BeanFactory _beanFactory;
139 private static final Set<BeanFactoryAware> _beanFactoryAwares =
140 new HashSet<>();
141
142 static {
143 try {
144 _SHADOW_MATCH_CACHE_FIELD = ReflectionUtil.getDeclaredField(
145 AspectJExpressionPointcut.class, "shadowMatchCache");
146 }
147 catch (Exception e) {
148 throw new LoggedExceptionInInitializerError(e);
149 }
150 }
151
152 }