1
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
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 }