1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.spring.annotation;
16  
17  import com.liferay.portal.kernel.annotation.BeanReference;
18  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
19  
20  import java.lang.reflect.Field;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.springframework.beans.BeansException;
26  import org.springframework.beans.factory.BeanCreationException;
27  import org.springframework.beans.factory.BeanFactory;
28  import org.springframework.beans.factory.BeanFactoryAware;
29  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
30  import org.springframework.beans.factory.config.BeanPostProcessor;
31  import org.springframework.util.ReflectionUtils;
32  
33  /**
34   * <a href="BeanReferenceAnnotationBeanPostProcessor.java.html"><b><i>View
35   * Source</i></b></a>
36   *
37   * @author Michael Young
38   * @author Shuyang Zhou
39   */
40  public class BeanReferenceAnnotationBeanPostProcessor
41      implements BeanFactoryAware, BeanPostProcessor {
42  
43      public Object postProcessAfterInitialization(Object bean, String beanName)
44          throws BeansException {
45  
46          return bean;
47      }
48  
49      public Object postProcessBeforeInitialization(Object bean, String beanName)
50          throws BeansException {
51  
52          _autoInject(bean, beanName, bean.getClass());
53  
54          return bean;
55      }
56  
57      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
58          _beanFactory = beanFactory;
59      }
60  
61      private void _autoInject(
62          Object targetBean, String targetBeanName, Class<?> beanClass) {
63  
64          if ((beanClass == null) || beanClass.isInterface()) {
65              return;
66          }
67  
68          String className = beanClass.getName();
69  
70          if (className.equals(_JAVA_LANG_OBJECT) ||
71              className.startsWith(_ORG_SPRINGFRAMEWORK)) {
72  
73              return;
74          }
75  
76          Field[] fields = beanClass.getDeclaredFields();
77  
78          for (Field field : fields) {
79              BeanReference beanReference = field.getAnnotation(
80                  BeanReference.class);
81  
82              if (beanReference == null) {
83                  continue;
84              }
85  
86              String referencedBeanName = beanReference.name();
87  
88              Object referencedBean = _beans.get(referencedBeanName);
89  
90              if (referencedBean == null) {
91                  try {
92                      referencedBean = _beanFactory.getBean(referencedBeanName);
93                  }
94                  catch (NoSuchBeanDefinitionException nsbde) {
95                      referencedBean = PortalBeanLocatorUtil.locate(
96                          referencedBeanName);
97                  }
98  
99                  _beans.put(referencedBeanName, referencedBean);
100             }
101 
102             ReflectionUtils.makeAccessible(field);
103 
104             try {
105                 field.set(targetBean, referencedBean);
106             }
107             catch (Throwable t) {
108                 throw new BeanCreationException(
109                     targetBeanName, "Could not inject BeanReference fields",
110                     t);
111             }
112         }
113 
114         _autoInject(targetBean, targetBeanName, beanClass.getSuperclass());
115 
116         return;
117     }
118 
119     private static String _JAVA_LANG_OBJECT = "java.lang.Object";
120 
121     private static String _ORG_SPRINGFRAMEWORK = "org.springframework";
122 
123     private BeanFactory _beanFactory;
124     private Map<String, Object> _beans = new HashMap<String, Object>();
125 
126 }