001    /**
002     * Copyright (c) 2000-2013 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.spring.util.SpringFactoryUtil;
018    import com.liferay.portal.spring.aop.ChainableMethodAdviceInjectorCollector;
019    
020    import org.springframework.beans.factory.BeanIsAbstractException;
021    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
022    import org.springframework.beans.factory.config.BeanPostProcessor;
023    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PortletBeanFactoryPostProcessor
029            implements BeanFactoryPostProcessor {
030    
031            @Override
032            public void postProcessBeanFactory(
033                    ConfigurableListableBeanFactory configurableListableBeanFactory) {
034    
035                    ChainableMethodAdviceInjectorCollector.collect(
036                            configurableListableBeanFactory);
037    
038                    configurableListableBeanFactory.setBeanClassLoader(
039                            PortletApplicationContext.getBeanClassLoader());
040    
041                    String[] names =
042                            configurableListableBeanFactory.getBeanDefinitionNames();
043    
044                    for (String name : names) {
045                            if (!name.contains(SpringFactoryUtil.class.getName())) {
046                                    continue;
047                            }
048    
049                            try {
050                                    Object bean = configurableListableBeanFactory.getBean(name);
051    
052                                    if (bean instanceof BeanPostProcessor) {
053                                            configurableListableBeanFactory.addBeanPostProcessor(
054                                                    (BeanPostProcessor)bean);
055                                    }
056                            }
057                            catch (BeanIsAbstractException biae) {
058                                    continue;
059                            }
060                    }
061            }
062    
063    }