001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.context;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ReflectionUtil;
020    
021    import java.lang.reflect.Field;
022    import java.lang.reflect.Method;
023    
024    import java.util.HashSet;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import org.aspectj.weaver.tools.ShadowMatch;
029    
030    import org.springframework.aop.ClassFilter;
031    import org.springframework.aop.Pointcut;
032    import org.springframework.aop.aspectj.AspectJExpressionPointcut;
033    import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
034    import org.springframework.beans.factory.BeanFactory;
035    import org.springframework.beans.factory.BeanFactoryAware;
036    import org.springframework.beans.factory.ListableBeanFactory;
037    
038    /**
039     * @author Shuyang Zhou
040     */
041    public class PortletBeanFactoryCleaner implements BeanFactoryAware {
042    
043            public static void readBeans() {
044                    if (_beanFactory == null) {
045                            if (_log.isWarnEnabled()) {
046                                    _log.warn("BeanFactory is null");
047                            }
048    
049                            return;
050                    }
051    
052                    if (!(_beanFactory instanceof ListableBeanFactory)) {
053                            return;
054                    }
055    
056                    ListableBeanFactory listableBeanFactory =
057                            (ListableBeanFactory)_beanFactory;
058    
059                    String[] names = listableBeanFactory.getBeanDefinitionNames();
060    
061                    for (String name : names) {
062                            try {
063                                    _readBean(listableBeanFactory, name);
064                            }
065                            catch (Exception e) {
066                            }
067                    }
068            }
069    
070            public void destroy() {
071                    for (BeanFactoryAware beanFactoryAware : _beanFactoryAwares) {
072                            try {
073                                    beanFactoryAware.setBeanFactory(null);
074                            }
075                            catch (Exception e) {
076                            }
077                    }
078    
079                    _beanFactoryAwares.clear();
080    
081                    for (AspectJExpressionPointcut aspectJExpressionPointcut :
082                                    _aspectJExpressionPointcuts) {
083    
084                            try {
085                                    Map<Method, ShadowMatch> shadowMatchCache =
086                                            (Map<Method, ShadowMatch>)_shadowMatchCacheField.get(
087                                                    aspectJExpressionPointcut);
088    
089                                    shadowMatchCache.clear();
090                            }
091                            catch (Exception e) {
092                            }
093                    }
094    
095                    _aspectJExpressionPointcuts.clear();
096            }
097    
098            public void setBeanFactory(BeanFactory beanFactory) {
099                    _beanFactory = beanFactory;
100            }
101    
102            private static void _readBean(
103                            ListableBeanFactory listableBeanFactory, String name)
104                    throws Exception {
105    
106                    Object bean = listableBeanFactory.getBean(name);
107    
108                    if (bean instanceof AspectJPointcutAdvisor) {
109                            AspectJPointcutAdvisor aspectJPointcutAdvisor =
110                                    (AspectJPointcutAdvisor)bean;
111    
112                            Pointcut pointcut = aspectJPointcutAdvisor.getPointcut();
113    
114                            ClassFilter classFilter = pointcut.getClassFilter();
115    
116                            if (classFilter instanceof AspectJExpressionPointcut) {
117                                    AspectJExpressionPointcut aspectJExpressionPointcut =
118                                            (AspectJExpressionPointcut)classFilter;
119    
120                                    _beanFactoryAwares.add(aspectJExpressionPointcut);
121                                    _aspectJExpressionPointcuts.add(aspectJExpressionPointcut);
122                            }
123                    }
124                    else if (bean instanceof BeanFactoryAware) {
125                            _beanFactoryAwares.add((BeanFactoryAware)bean);
126                    }
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(
130                    PortletBeanFactoryCleaner.class);
131    
132            private static Set<AspectJExpressionPointcut> _aspectJExpressionPointcuts =
133                    new HashSet<AspectJExpressionPointcut>();
134            private static BeanFactory _beanFactory;
135            private static Set<BeanFactoryAware> _beanFactoryAwares =
136                    new HashSet<BeanFactoryAware>();
137            private static Field _shadowMatchCacheField;
138    
139            static {
140                    try {
141                            _shadowMatchCacheField = ReflectionUtil.getDeclaredField(
142                                    AspectJExpressionPointcut.class, "shadowMatchCache");
143                    }
144                    catch (Exception e) {
145                            _log.error(e, e);
146                    }
147            }
148    
149    }