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.annotation;
016    
017    import com.liferay.portal.kernel.annotation.BeanReference;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    
020    import java.lang.reflect.Field;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.springframework.beans.BeansException;
026    import org.springframework.beans.factory.BeanCreationException;
027    import org.springframework.beans.factory.BeanFactory;
028    import org.springframework.beans.factory.BeanFactoryAware;
029    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
030    import org.springframework.beans.factory.config.BeanPostProcessor;
031    import org.springframework.util.ReflectionUtils;
032    
033    /**
034     * @author Michael Young
035     * @author Shuyang Zhou
036     */
037    public class BeanReferenceAnnotationBeanPostProcessor
038            implements BeanFactoryAware, BeanPostProcessor {
039    
040            public void destroy() {
041                    _beans.clear();
042            }
043    
044            public Object postProcessAfterInitialization(Object bean, String beanName)
045                    throws BeansException {
046    
047                    return bean;
048            }
049    
050            public Object postProcessBeforeInitialization(Object bean, String beanName)
051                    throws BeansException {
052    
053                    _autoInject(bean, beanName, bean.getClass());
054    
055                    return bean;
056            }
057    
058            public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
059                    _beanFactory = beanFactory;
060            }
061    
062            private void _autoInject(
063                    Object targetBean, String targetBeanName, Class<?> beanClass) {
064    
065                    if ((beanClass == null) || beanClass.isInterface()) {
066                            return;
067                    }
068    
069                    String className = beanClass.getName();
070    
071                    if (className.equals(_JAVA_LANG_OBJECT) ||
072                            className.startsWith(_ORG_SPRINGFRAMEWORK)) {
073    
074                            return;
075                    }
076    
077                    Field[] fields = beanClass.getDeclaredFields();
078    
079                    for (Field field : fields) {
080                            BeanReference beanReference = field.getAnnotation(
081                                    BeanReference.class);
082    
083                            if (beanReference == null) {
084                                    continue;
085                            }
086    
087                            String referencedBeanName = beanReference.name();
088    
089                            Class<?> referencedBeanType = beanReference.type();
090    
091                            if (!Object.class.equals(referencedBeanType)) {
092                                    referencedBeanName = referencedBeanType.getName();
093                            }
094    
095                            Object referencedBean = _beans.get(referencedBeanName);
096    
097                            if (referencedBean == null) {
098                                    try {
099                                            referencedBean = _beanFactory.getBean(referencedBeanName);
100                                    }
101                                    catch (NoSuchBeanDefinitionException nsbde) {
102                                            referencedBean = PortalBeanLocatorUtil.locate(
103                                                    referencedBeanName);
104                                    }
105    
106                                    _beans.put(referencedBeanName, referencedBean);
107                            }
108    
109                            ReflectionUtils.makeAccessible(field);
110    
111                            try {
112                                    field.set(targetBean, referencedBean);
113                            }
114                            catch (Throwable t) {
115                                    throw new BeanCreationException(
116                                            targetBeanName, "Could not inject BeanReference fields",
117                                            t);
118                            }
119                    }
120    
121                    _autoInject(targetBean, targetBeanName, beanClass.getSuperclass());
122    
123                    return;
124            }
125    
126            private static String _JAVA_LANG_OBJECT = "java.lang.Object";
127    
128            private static String _ORG_SPRINGFRAMEWORK = "org.springframework";
129    
130            private BeanFactory _beanFactory;
131            private Map<String, Object> _beans = new HashMap<String, Object>();
132    
133    }