1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.persistence.jpa.dao;
18
19 import java.lang.reflect.Method;
20
21 import javax.persistence.EntityManagerFactory;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.factory.config.BeanPostProcessor;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.ApplicationContextAware;
29 import org.springframework.core.Ordered;
30 import org.springframework.core.PriorityOrdered;
31
32 import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
33 import ch.elca.el4j.services.persistence.jpa.dao.extentstrategies.ExtentFetcher;
34 import ch.elca.el4j.util.codingsupport.Reject;
35
36
37
38
39
40
41
42
43
44
45 public class JpaExtentFetcherInjectorBeanPostProcessor
46 implements BeanPostProcessor, PriorityOrdered, ApplicationContextAware {
47
48
49
50
51 public static final String EXTENT_FETCHER_BEAN_DEFAULT_NAME = "extentFetcher";
52
53
54
55
56 private static final Logger s_logger = LoggerFactory.getLogger(JpaExtentFetcherInjectorBeanPostProcessor.class);
57
58
59
60
61 protected ExtentFetcher extentFetcher;
62
63
64
65
66 private int order = Ordered.LOWEST_PRECEDENCE;
67
68
69
70
71 private ApplicationContext m_applicationContext;
72
73
74
75
76
77
78
79
80 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
81 s_logger.debug("Treating bean with name:" + beanName);
82 if (GenericJpaDao.class.isAssignableFrom(bean.getClass())) {
83 s_logger.debug("init dao with name:" + beanName);
84 initDao((GenericJpaDao<?, ?>) bean);
85 }
86 return bean;
87 }
88
89
90 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
91 return bean;
92 }
93
94
95
96
97
98
99 protected void initDao(GenericJpaDao<?, ?> dao) {
100 if (getExtentFetcher() != null) {
101 try {
102 Method setter = dao.getClass().getMethod(
103 "setExtentFetcher", ExtentFetcher.class);
104 setter.invoke(dao, getExtentFetcher());
105 s_logger.debug("value set in dao");
106
107 } catch (Exception e) {
108
109 s_logger.error("problem when auto-setting extentFetcher", e);
110
111 }
112 } else {
113 s_logger.error("no extentFetcher available -- cannot use extent-based fetch methods");
114 }
115 }
116
117
118
119
120
121 public ExtentFetcher getExtentFetcher() {
122 if ((extentFetcher == null) && (m_applicationContext != null)) {
123
124 if (m_applicationContext.containsBean(EXTENT_FETCHER_BEAN_DEFAULT_NAME)) {
125 extentFetcher = (ExtentFetcher)
126 m_applicationContext.getBean(EXTENT_FETCHER_BEAN_DEFAULT_NAME);
127 } else {
128 s_logger.error("failed to obtain extentFetcher from application context "
129 + "-- cannot use Extent-based fetching");
130 }
131 }
132 return extentFetcher;
133 }
134
135
136
137
138
139
140 public void setExtentFetcher(ExtentFetcher extentFetcher) {
141 this.extentFetcher = extentFetcher;
142 }
143
144
145 public void setOrder(int order) {
146 this.order = order;
147 }
148
149
150 public int getOrder() {
151 return this.order;
152 }
153
154
155 public void setApplicationContext(ApplicationContext applicationContext)
156 throws BeansException {
157
158 m_applicationContext = applicationContext;
159 }
160 }