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.bean;
016    
017    import com.liferay.portal.cluster.ClusterableAdvice;
018    import com.liferay.portal.kernel.bean.BeanLocatorException;
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.bean.IdentifiableBean;
021    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    
025    import java.io.PrintWriter;
026    import java.io.StringWriter;
027    
028    import java.lang.reflect.Field;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    import org.springframework.beans.BeansException;
034    import org.springframework.beans.factory.BeanCreationException;
035    import org.springframework.beans.factory.BeanFactory;
036    import org.springframework.beans.factory.BeanFactoryAware;
037    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
038    import org.springframework.beans.factory.config.BeanPostProcessor;
039    import org.springframework.util.ReflectionUtils;
040    
041    /**
042     * @author Michael Young
043     * @author Shuyang Zhou
044     */
045    public class BeanReferenceAnnotationBeanPostProcessor
046            implements BeanFactoryAware, BeanPostProcessor {
047    
048            public BeanReferenceAnnotationBeanPostProcessor() {
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("Creating instance " + this.hashCode());
051                    }
052            }
053    
054            public void destroy() {
055                    _beans.clear();
056            }
057    
058            public Object postProcessAfterInitialization(Object bean, String beanName)
059                    throws BeansException {
060    
061                    return bean;
062            }
063    
064            public Object postProcessBeforeInitialization(Object bean, String beanName)
065                    throws BeansException {
066    
067                    if (bean instanceof IdentifiableBean) {
068                            IdentifiableBean identifiableBean = (IdentifiableBean)bean;
069    
070                            identifiableBean.setBeanIdentifier(beanName);
071                    }
072                    else if (beanName.endsWith("Service")) {
073                            if (_log.isWarnEnabled()) {
074                                    _log.warn(
075                                            beanName + " should implement " +
076                                                    IdentifiableBean.class.getName() +
077                                                            " for " + ClusterableAdvice.class.getName());
078                            }
079                    }
080    
081                    _autoInject(bean, beanName, bean.getClass());
082    
083                    return bean;
084            }
085    
086            public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
087                    _beanFactory = beanFactory;
088            }
089    
090            private void _autoInject(
091                    Object targetBean, String targetBeanName, Class<?> beanClass) {
092    
093                    if ((beanClass == null) || beanClass.isInterface()) {
094                            return;
095                    }
096    
097                    String className = beanClass.getName();
098    
099                    if (className.equals(_JAVA_LANG_OBJECT) ||
100                            className.startsWith(_ORG_SPRINGFRAMEWORK)) {
101    
102                            return;
103                    }
104    
105                    Field[] fields = beanClass.getDeclaredFields();
106    
107                    for (Field field : fields) {
108                            BeanReference beanReference = field.getAnnotation(
109                                    BeanReference.class);
110    
111                            String referencedBeanName = null;
112                            Class<?> referencedBeanType = null;
113    
114                            if (beanReference != null) {
115                                    referencedBeanName = beanReference.name();
116                                    referencedBeanType = beanReference.type();
117                            }
118                            else {
119                                    continue;
120                            }
121    
122                            if (!Object.class.equals(referencedBeanType)) {
123                                    referencedBeanName = referencedBeanType.getName();
124                            }
125    
126                            Object referencedBean = _beans.get(referencedBeanName);
127    
128                            if (referencedBean == null) {
129                                    try {
130                                            referencedBean = _beanFactory.getBean(referencedBeanName);
131                                    }
132                                    catch (NoSuchBeanDefinitionException nsbde) {
133                                            try {
134                                                    referencedBean = PortalBeanLocatorUtil.locate(
135                                                            referencedBeanName);
136                                            }
137                                            catch (BeanLocatorException ble) {
138                                                    StringWriter stringWriter = new StringWriter();
139    
140                                                    PrintWriter printWriter = new PrintWriter(stringWriter);
141    
142                                                    printWriter.print("BeanFactory could not find bean: ");
143    
144                                                    nsbde.printStackTrace(printWriter);
145    
146                                                    printWriter.print(
147                                                            " and PortalBeanLocator failed with: ");
148                                                    printWriter.append(ble.getMessage());
149    
150                                                    printWriter.close();
151    
152                                                    throw new BeanLocatorException(
153                                                            stringWriter.toString(), ble);
154                                            }
155                                    }
156    
157                                    _beans.put(referencedBeanName, referencedBean);
158                            }
159    
160                            ReflectionUtils.makeAccessible(field);
161    
162                            BeanReferenceRefreshUtil.registerRefreshPoint(
163                                    targetBean, field, referencedBeanName);
164    
165                            try {
166                                    field.set(targetBean, referencedBean);
167                            }
168                            catch (Throwable t) {
169                                    throw new BeanCreationException(
170                                            targetBeanName, "Could not inject BeanReference fields", t);
171                            }
172                    }
173    
174                    _autoInject(targetBean, targetBeanName, beanClass.getSuperclass());
175            }
176    
177            private static final String _JAVA_LANG_OBJECT = "java.lang.Object";
178    
179            private static final String _ORG_SPRINGFRAMEWORK = "org.springframework";
180    
181            private static Log _log = LogFactoryUtil.getLog(
182                    BeanReferenceAnnotationBeanPostProcessor.class);
183    
184            private BeanFactory _beanFactory;
185            private Map<String, Object> _beans = new HashMap<String, Object>();
186    
187    }