View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2010 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
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   * Inject the entity manager factory in GenericDaos (or other daos) if needed.
37   *  It gets the entityManagerFactory from the spring context
38   *   by using the default name {@link ENTITY_MANAGER_FACTORY_BEAN_DEFAULT_NAME} or
39   *   via its setter method.
40   *
41   * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$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/jpa/dao/JpaEntityManagerFactoryInjectorBeanPostProcessor.java $
42   *
43   * @author Simon Stelling (SST)
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  	 * The default name for the property of the session factory.
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  	 * Initiates the real work.
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  	/** {@inheritDoc} */
71  	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
72  		return bean;
73  	}
74  
75  
76  	/**
77  	 * Try to init the sessionFactory of the bean.
78  	 * @param dao
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  				// ignore problems
90  				s_logger.info("problem when auto-setting entityManagerFactory", e);
91  
92  			}
93  		}
94  	}
95  
96  	/**
97  	 * Gets the session factory (from spring context if needed).
98  	 * @return
99  	 */
100 	public EntityManagerFactory getEntityManagerFactory() {
101 		if ((m_entityManagerFactory == null) && (m_applicationContext != null)) {
102 			// try to locate the session factory
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 	 * You can either set the session factory explicitly or
114 	 *  have the factory load the session factory implicitly (by name).
115 	 * @param factory
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 	/** {@inheritDoc} */
130 	public int getOrder() {
131 		return this.order;
132 	}
133 
134 	/** {@inheritDoc} */
135 	public void setApplicationContext(ApplicationContext applicationContext)
136 		throws BeansException {
137 		
138 		m_applicationContext = applicationContext;
139 	}
140 }