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