001    /**
002     * Copyright (c) 2000-present 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.spring.util.SpringFactoryUtil;
018    import com.liferay.portal.spring.aop.ChainableMethodAdviceInjectorCollector;
019    
020    import java.util.Map;
021    
022    import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
023    import org.springframework.beans.factory.BeanFactoryAware;
024    import org.springframework.beans.factory.BeanIsAbstractException;
025    import org.springframework.beans.factory.ListableBeanFactory;
026    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
027    import org.springframework.beans.factory.config.BeanPostProcessor;
028    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PortletBeanFactoryPostProcessor
034            implements BeanFactoryPostProcessor {
035    
036            @Override
037            public void postProcessBeanFactory(
038                    ConfigurableListableBeanFactory configurableListableBeanFactory) {
039    
040                    ChainableMethodAdviceInjectorCollector.collect(
041                            configurableListableBeanFactory);
042    
043                    ClassLoader classLoader = getClassLoader();
044    
045                    configurableListableBeanFactory.setBeanClassLoader(classLoader);
046    
047                    ListableBeanFactory parentListableBeanFactory =
048                            (ListableBeanFactory)
049                                    configurableListableBeanFactory.getParentBeanFactory();
050    
051                    if (parentListableBeanFactory != null) {
052                            Map<String, BeanPostProcessor> beanPostProcessors =
053                                    parentListableBeanFactory.getBeansOfType(
054                                            BeanPostProcessor.class, true, false);
055    
056                            for (BeanPostProcessor beanPostProcessor :
057                                            beanPostProcessors.values()) {
058    
059                                    if (beanPostProcessor instanceof BeanFactoryAware) {
060                                            BeanFactoryAware beanFactoryAware =
061                                                    (BeanFactoryAware)beanPostProcessor;
062    
063                                            beanFactoryAware.setBeanFactory(
064                                                    configurableListableBeanFactory);
065                                    }
066    
067                                    if (beanPostProcessor instanceof
068                                                    AbstractAutoProxyCreator) {
069    
070                                            AbstractAutoProxyCreator abstractAutoProxyCreator =
071                                                    (AbstractAutoProxyCreator)beanPostProcessor;
072    
073                                            abstractAutoProxyCreator.setProxyClassLoader(classLoader);
074                                    }
075    
076                                    configurableListableBeanFactory.addBeanPostProcessor(
077                                            beanPostProcessor);
078                            }
079                    }
080    
081                    String[] names =
082                            configurableListableBeanFactory.getBeanDefinitionNames();
083    
084                    for (String name : names) {
085                            if (!name.contains(SpringFactoryUtil.class.getName())) {
086                                    continue;
087                            }
088    
089                            try {
090                                    Object bean = configurableListableBeanFactory.getBean(name);
091    
092                                    if (bean instanceof BeanPostProcessor) {
093                                            configurableListableBeanFactory.addBeanPostProcessor(
094                                                    (BeanPostProcessor)bean);
095                                    }
096                            }
097                            catch (BeanIsAbstractException biae) {
098                                    continue;
099                            }
100                    }
101            }
102    
103            protected ClassLoader getClassLoader() {
104                    return PortletApplicationContext.getBeanClassLoader();
105            }
106    
107    }