View Javadoc

1   /*
2    * Copyright 2002-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Inject the session factory in GenericDaos (or other daos) if needed.
36   *  It gets the sessionFactory from the spring context
37   *   by using the default name {@link SESSION_FACTORY_BEAN_DEFAULT_NAME} or
38   *   via its setter method.
39   *
40   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/hibernate/dao/HibernateSessionFactoryInjectorBeanPostProcessor.java $
41   *
42   * @author Philipp Oser (POS)
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  	 * The default name for the property of the session factory.
51  	 */
52  	public static final String SESSION_FACTORY_BEAN_DEFAULT_NAME = "sessionFactory";
53  	
54  	private int order = Ordered.LOWEST_PRECEDENCE;
55  		
56  
57  	/**
58  	 * Initiates the real work.
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  	/** {@inheritDoc} */
70  	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
71  		return bean;
72  	}
73  
74  
75  	/**
76  	 * Try to init the sessionFactory of the bean.
77  	 * @param dao
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  				// ignore problems
91  				s_logger.info("problem when auto-setting sessionFactory", e);
92  
93  			}
94  		}
95  	}
96  
97  	
98  	
99  	/**
100 	 * Gets the session factory (from spring context if needed).
101 	 * @return
102 	 */
103 	public SessionFactory getSessionFactory() {
104 		if ((m_sessionFactory == null) && (m_applicationContext != null)) {
105 			// try to locate the session factory
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 	 * You can either set the session factory explicitly or
117 	 *  have the factory load the session factory implicitly (by name).
118 	 * @param factory
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 	/** {@inheritDoc} */
133 	public int getOrder() {
134 		return this.order;
135 	}
136 
137 	/** {@inheritDoc} */
138 	public void setApplicationContext(ApplicationContext applicationContext)
139 		throws BeansException {
140 		
141 		m_applicationContext = applicationContext;
142 	}
143 }