1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class BeanReferenceAnnotationBeanPostProcessor
48      implements BeanFactoryAware, InstantiationAwareBeanPostProcessor,
49          MergedBeanDefinitionPostProcessor {
50  
51      public Object postProcessAfterInitialization(Object bean, String beanName)
52          throws BeansException {
53  
54          return bean;
55      }
56  
57      public boolean postProcessAfterInstantiation(Object bean, String beanName)
58          throws BeansException {
59  
60          InjectionMetadata injectionMetadata = findResourceMetadata(
61              bean.getClass());
62  
63          try {
64              injectionMetadata.injectFields(bean, beanName);
65          }
66          catch (Throwable t) {
67              throw new BeanCreationException(
68                  beanName, "Injection of BeanReference fields failed", t);
69          }
70  
71          return true;
72      }
73  
74      public Object postProcessBeforeInitialization(Object bean, String beanName)
75          throws BeansException {
76  
77          return bean;
78      }
79  
80      public Object postProcessBeforeInstantiation(
81              Class beanClass, String beanName)
82          throws BeansException {
83  
84          return null;
85      }
86  
87      public void postProcessMergedBeanDefinition(
88              RootBeanDefinition beanDefinition, Class beanType,
89              String beanName) {
90  
91          if (beanType == null) {
92              return;
93          }
94  
95          InjectionMetadata injectionMetadata = findResourceMetadata(beanType);
96  
97          injectionMetadata.checkConfigMembers(beanDefinition);
98      }
99  
100     public PropertyValues postProcessPropertyValues(
101             PropertyValues propertyValues,
102             PropertyDescriptor[] propertyDescriptors, Object bean,
103             String beanName)
104         throws BeansException {
105 
106         InjectionMetadata metadata = findResourceMetadata(bean.getClass());
107 
108         try {
109             metadata.injectMethods(bean, beanName, propertyValues);
110         }
111         catch (Throwable t) {
112             throw new BeanCreationException(
113                 beanName, "Injection of BeanReference methods failed", t);
114         }
115 
116         return propertyValues;
117     }
118 
119     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
120         _beanFactory = beanFactory;
121     }
122 
123     protected InjectionMetadata findResourceMetadata(Class clazz) {
124         InjectionMetadata injectionMetadata = _injectionMetadataMap.get(clazz);
125 
126         if (injectionMetadata != null) {
127             return injectionMetadata;
128         }
129 
130         synchronized (_injectionMetadataMap) {
131             injectionMetadata = _injectionMetadataMap.get(clazz);
132 
133             if (injectionMetadata == null) {
134                 injectionMetadata = new InjectionMetadata(clazz);
135 
136                 BeanReferenceCallback callback = new BeanReferenceCallback(
137                     _beanFactory, injectionMetadata, clazz);
138 
139                 ReflectionUtils.doWithFields(clazz, callback);
140                 ReflectionUtils.doWithMethods(clazz, callback);
141 
142                 _injectionMetadataMap.put(clazz, injectionMetadata);
143             }
144         }
145 
146         return injectionMetadata;
147     }
148 
149     private BeanFactory _beanFactory;
150 
151     private Map<Class<?>, InjectionMetadata> _injectionMetadataMap =
152         new ConcurrentHashMap<Class<?>, InjectionMetadata>();
153 
154 }