1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.annotation;
24  
25  import java.beans.PropertyDescriptor;
26  
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.springframework.beans.BeansException;
31  import org.springframework.beans.PropertyValues;
32  import org.springframework.beans.factory.BeanCreationException;
33  import org.springframework.beans.factory.BeanFactory;
34  import org.springframework.beans.factory.BeanFactoryAware;
35  import org.springframework.beans.factory.annotation.InjectionMetadata;
36  import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
37  import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
38  import org.springframework.beans.factory.support.RootBeanDefinition;
39  import org.springframework.util.ReflectionUtils;
40  
41  /**
42   * <a href="BeanReferenceAnnotationBeanPostProcessor.java.html"><b><i>View
43   * Source</i></b></a>
44   *
45   * @author Michael Young
46   *
47   */
48  public class BeanReferenceAnnotationBeanPostProcessor
49      implements BeanFactoryAware, InstantiationAwareBeanPostProcessor,
50          MergedBeanDefinitionPostProcessor {
51  
52      public Object postProcessAfterInitialization(Object bean, String beanName)
53          throws BeansException {
54  
55          return bean;
56      }
57  
58      public boolean postProcessAfterInstantiation(Object bean, String beanName)
59          throws BeansException {
60  
61          InjectionMetadata injectionMetadata = findResourceMetadata(
62              bean.getClass());
63  
64          try {
65              injectionMetadata.injectFields(bean, beanName);
66          }
67          catch (Throwable t) {
68              throw new BeanCreationException(
69                  beanName, "Injection of BeanReference fields failed", t);
70          }
71  
72          return true;
73      }
74  
75      public Object postProcessBeforeInitialization(Object bean, String beanName)
76          throws BeansException {
77  
78          return bean;
79      }
80  
81      public Object postProcessBeforeInstantiation(
82              Class beanClass, String beanName)
83          throws BeansException {
84  
85          return null;
86      }
87  
88      public void postProcessMergedBeanDefinition(
89              RootBeanDefinition beanDefinition, Class beanType,
90              String beanName) {
91  
92          if (beanType == null) {
93              return;
94          }
95  
96          InjectionMetadata injectionMetadata = findResourceMetadata(beanType);
97  
98          injectionMetadata.checkConfigMembers(beanDefinition);
99      }
100 
101     public PropertyValues postProcessPropertyValues(
102             PropertyValues propertyValues,
103             PropertyDescriptor[] propertyDescriptors, Object bean,
104             String beanName)
105         throws BeansException {
106 
107         InjectionMetadata metadata = findResourceMetadata(bean.getClass());
108 
109         try {
110             metadata.injectMethods(bean, beanName, propertyValues);
111         }
112         catch (Throwable t) {
113             throw new BeanCreationException(
114                 beanName, "Injection of BeanReference methods failed", t);
115         }
116 
117         return propertyValues;
118     }
119 
120     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
121         _beanFactory = beanFactory;
122     }
123 
124     protected InjectionMetadata findResourceMetadata(Class clazz) {
125         InjectionMetadata injectionMetadata = _injectionMetadataMap.get(clazz);
126 
127         if (injectionMetadata != null) {
128             return injectionMetadata;
129         }
130 
131         synchronized (_injectionMetadataMap) {
132             injectionMetadata = _injectionMetadataMap.get(clazz);
133 
134             if (injectionMetadata == null) {
135                 injectionMetadata = new InjectionMetadata(clazz);
136 
137                 BeanReferenceCallback callback = new BeanReferenceCallback(
138                     _beanFactory, injectionMetadata, clazz);
139 
140                 ReflectionUtils.doWithFields(clazz, callback);
141                 ReflectionUtils.doWithMethods(clazz, callback);
142 
143                 _injectionMetadataMap.put(clazz, injectionMetadata);
144             }
145         }
146 
147         return injectionMetadata;
148     }
149 
150     private BeanFactory _beanFactory;
151 
152     private Map<Class<?>, InjectionMetadata> _injectionMetadataMap =
153         new ConcurrentHashMap<Class<?>, InjectionMetadata>();
154 
155 }