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