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.util.codingsupport.Reject;
34
35
36
37
38
39
40
41
42
43
44
45 public class JpaEntityManagerFactoryInjectorBeanPostProcessor
46 implements BeanPostProcessor, PriorityOrdered, ApplicationContextAware {
47
48 private static final Logger s_logger= LoggerFactory.getLogger(JpaEntityManagerFactoryInjectorBeanPostProcessor.class);
49
50
51
52
53 public static final String ENTITY_MANAGER_FACTORY_BEAN_DEFAULT_NAME = "entityManagerFactory";
54
55 private int order = Ordered.LOWEST_PRECEDENCE;
56
57
58
59
60
61 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
62 s_logger.debug("Treating bean with name:" + beanName);
63 if (ConvenienceGenericJpaDao.class.isAssignableFrom(bean.getClass())) {
64 s_logger.debug("init dao with name:" + beanName);
65 initDao((ConvenienceGenericJpaDao<?, ?>) bean);
66 }
67 return bean;
68 }
69
70
71 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
72 return bean;
73 }
74
75
76
77
78
79
80 protected void initDao(ConvenienceGenericJpaDao<?, ?> dao) {
81 if (getEntityManagerFactory() != null) {
82 try {
83 Method setter = dao.getClass().getMethod(
84 "setEntityManagerFactory", EntityManagerFactory.class);
85 setter.invoke(dao, getEntityManagerFactory());
86 s_logger.debug("value set in dao");
87
88 } catch (Exception e) {
89
90 s_logger.info("problem when auto-setting entityManagerFactory", e);
91
92 }
93 }
94 }
95
96
97
98
99
100 public EntityManagerFactory getEntityManagerFactory() {
101 if ((m_entityManagerFactory == null) && (m_applicationContext != null)) {
102
103 if (m_applicationContext.containsBean(ENTITY_MANAGER_FACTORY_BEAN_DEFAULT_NAME)) {
104 m_entityManagerFactory = (EntityManagerFactory)
105 m_applicationContext.getBean(ENTITY_MANAGER_FACTORY_BEAN_DEFAULT_NAME);
106 Reject.ifNull(m_entityManagerFactory, "session factory must not be null!");
107 }
108 }
109 return m_entityManagerFactory;
110 }
111
112
113
114
115
116
117 public void setEntityManagerFactory(EntityManagerFactory factory) {
118 m_entityManagerFactory = factory;
119 }
120
121 protected EntityManagerFactory m_entityManagerFactory;
122
123 private ApplicationContext m_applicationContext;
124
125 public void setOrder(int order) {
126 this.order = order;
127 }
128
129
130 public int getOrder() {
131 return this.order;
132 }
133
134
135 public void setApplicationContext(ApplicationContext applicationContext)
136 throws BeansException {
137
138 m_applicationContext = applicationContext;
139 }
140 }