001    /**
002     * Copyright (c) 2000-2013 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.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("Bean factory 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            @Override
099            public void setBeanFactory(BeanFactory beanFactory) {
100                    _beanFactory = beanFactory;
101            }
102    
103            private static void _readBean(
104                            ListableBeanFactory listableBeanFactory, String name)
105                    throws Exception {
106    
107                    Object bean = listableBeanFactory.getBean(name);
108    
109                    if (bean instanceof AspectJPointcutAdvisor) {
110                            AspectJPointcutAdvisor aspectJPointcutAdvisor =
111                                    (AspectJPointcutAdvisor)bean;
112    
113                            Pointcut pointcut = aspectJPointcutAdvisor.getPointcut();
114    
115                            ClassFilter classFilter = pointcut.getClassFilter();
116    
117                            if (classFilter instanceof AspectJExpressionPointcut) {
118                                    AspectJExpressionPointcut aspectJExpressionPointcut =
119                                            (AspectJExpressionPointcut)classFilter;
120    
121                                    _beanFactoryAwares.add(aspectJExpressionPointcut);
122                                    _aspectJExpressionPointcuts.add(aspectJExpressionPointcut);
123                            }
124                    }
125                    else if (bean instanceof BeanFactoryAware) {
126                            _beanFactoryAwares.add((BeanFactoryAware)bean);
127                    }
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(
131                    PortletBeanFactoryCleaner.class);
132    
133            private static Set<AspectJExpressionPointcut> _aspectJExpressionPointcuts =
134                    new HashSet<AspectJExpressionPointcut>();
135            private static BeanFactory _beanFactory;
136            private static Set<BeanFactoryAware> _beanFactoryAwares =
137                    new HashSet<BeanFactoryAware>();
138            private static Field _shadowMatchCacheField;
139    
140            static {
141                    try {
142                            _shadowMatchCacheField = ReflectionUtil.getDeclaredField(
143                                    AspectJExpressionPointcut.class, "shadowMatchCache");
144                    }
145                    catch (Exception e) {
146                            _log.error(e, e);
147                    }
148            }
149    
150    }