1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.persistence.hibernate.dao;
18
19 import java.beans.PropertyDescriptor;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.hibernate.SessionFactory;
24 import org.springframework.beans.BeansException;
25 import org.springframework.beans.factory.config.BeanPostProcessor;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.ApplicationContextAware;
28 import org.springframework.core.Ordered;
29 import org.springframework.core.PriorityOrdered;
30
31 import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
32 import ch.elca.el4j.util.codingsupport.Reject;
33
34
35
36
37
38
39
40
41
42
43
44 public class HibernateSessionFactoryInjectorBeanPostProcessor
45 implements BeanPostProcessor, PriorityOrdered, ApplicationContextAware {
46
47 private static final Logger s_logger= LoggerFactory.getLogger(HibernateSessionFactoryInjectorBeanPostProcessor.class);
48
49
50
51
52 public static final String SESSION_FACTORY_BEAN_DEFAULT_NAME = "sessionFactory";
53
54 private int order = Ordered.LOWEST_PRECEDENCE;
55
56
57
58
59
60 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
61 s_logger.debug("Treating bean with name:" + beanName);
62 if (GenericDao.class.isAssignableFrom(bean.getClass())) {
63 s_logger.debug("init dao with name:" + beanName);
64 initDao((GenericDao<?>) bean);
65 }
66 return bean;
67 }
68
69
70 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
71 return bean;
72 }
73
74
75
76
77
78
79 protected void initDao(GenericDao<?> dao) {
80 if (getSessionFactory() != null) {
81 try {
82 PropertyDescriptor pd = new PropertyDescriptor(SESSION_FACTORY_BEAN_DEFAULT_NAME,
83 dao.getClass());
84 Object value = pd.getReadMethod().invoke(dao);
85 if (value == null) {
86 pd.getWriteMethod().invoke(dao, m_sessionFactory);
87 }
88 s_logger.debug("value set in dao set");
89 } catch (Exception e) {
90
91 s_logger.info("problem when auto-setting sessionFactory", e);
92
93 }
94 }
95 }
96
97
98
99
100
101
102
103 public SessionFactory getSessionFactory() {
104 if ((m_sessionFactory == null) && (m_applicationContext != null)) {
105
106 if (m_applicationContext.containsBean(SESSION_FACTORY_BEAN_DEFAULT_NAME)) {
107 m_sessionFactory = (SessionFactory)
108 m_applicationContext.getBean(SESSION_FACTORY_BEAN_DEFAULT_NAME);
109 Reject.ifNull(m_sessionFactory, "session factory must not be null!");
110 }
111 }
112 return m_sessionFactory;
113 }
114
115
116
117
118
119
120 public void setSessionFactory(SessionFactory factory) {
121 m_sessionFactory = factory;
122 }
123
124 protected SessionFactory m_sessionFactory;
125
126 private ApplicationContext m_applicationContext;
127
128 public void setOrder(int order) {
129 this.order = order;
130 }
131
132
133 public int getOrder() {
134 return this.order;
135 }
136
137
138 public void setApplicationContext(ApplicationContext applicationContext)
139 throws BeansException {
140
141 m_applicationContext = applicationContext;
142 }
143 }