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.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    }