View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.krad.app.persistence.jpa;
17  
18  import org.kuali.rice.core.api.config.property.Config;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.core.framework.persistence.jpa.DevHibernateJpaVendorAdapter;
21  import org.springframework.orm.jpa.JpaVendorAdapter;
22  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
23  import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
24  import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
25  
26  import javax.sql.DataSource;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class RiceLocalContainerEntityManagerFactoryBean extends LocalContainerEntityManagerFactoryBean {
34  	
35  	public RiceLocalContainerEntityManagerFactoryBean() {
36  		throw new RuntimeException(getClass().getName() + " can not be constructed without a datasource");
37  	}
38  
39  	public RiceLocalContainerEntityManagerFactoryBean(DataSource datasource) {
40  		this("", datasource);
41  	}
42  	
43  	public RiceLocalContainerEntityManagerFactoryBean(String prefix, DataSource datasource) {
44  		if (prefix.equals("")) {			
45  			prefix = "rice";
46  		}
47  		prefix += ".jpa.";
48  		
49  		Config config = ConfigContext.getCurrentContextConfig();
50  	
51  		setPersistenceUnitManager(preparePersistentUnitManager(config, prefix, datasource));
52  		setPersistenceXmlLocation(determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml"));
53  		setDataSource(datasource);
54  		setPersistenceUnitName(determineConfigProperty(config, prefix, "PersistenceUnitName", "rice"));
55  		setJpaDialect(new org.springframework.orm.jpa.vendor.HibernateJpaDialect());
56  		setJpaPropertyMap(prepareJpaProperties(config, prefix));
57  		setJpaVendorAdapter(prepareJpaVendorAdapter(config, prefix));
58  		
59  		RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor();
60  		postProcessor.setJtaDataSource(datasource);
61  		setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor });
62  	}
63  
64  
65  	private PersistenceUnitManager preparePersistentUnitManager(Config config, String prefix, DataSource datasource) {
66  		DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
67  		persistenceUnitManager.setDefaultDataSource(datasource);
68  		persistenceUnitManager.setPersistenceXmlLocations(new String[] {determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml")});
69  		persistenceUnitManager.setDefaultPersistenceUnitRootLocation(determineConfigProperty(config, prefix, "PersistenceUnitRootLocation", "classpath:"));
70  		RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor();
71  		postProcessor.setJtaDataSource(datasource);
72  		persistenceUnitManager.setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor });
73  		persistenceUnitManager.afterPropertiesSet();
74  		return persistenceUnitManager;
75  	}
76  
77  	private JpaVendorAdapter prepareJpaVendorAdapter(Config config, String prefix) {
78  		DevHibernateJpaVendorAdapter jpaVendorAdapter = new DevHibernateJpaVendorAdapter();
79  		jpaVendorAdapter.setDatabasePlatform(determineConfigProperty(config, prefix, "DatabasePlatform", "org.hibernate.dialect.MySQLDialect"));
80  		jpaVendorAdapter.setGenerateDdl(new Boolean(determineConfigProperty(config, prefix, "GenerateDdl", "false")));
81  		jpaVendorAdapter.setSerializationFilename(determineConfigProperty(config, prefix, "SerializationFilename", "/tmp/Ejb3Configuration.out"));
82  		jpaVendorAdapter.setUseSerialization(new Boolean(determineConfigProperty(config, prefix, "UseSerialization", "true")));
83  		try {
84  			jpaVendorAdapter.afterPropertiesSet();
85  		} catch (Exception e) {}
86  		return jpaVendorAdapter;
87  	}
88  
89  	private Map<String, String> prepareJpaProperties(Config config, String prefix) {
90  		Map<String, String> jpaProperties = new HashMap<String, String>();
91  		
92  		// Load in all user specified "JPAProperties" prefixed properties		
93  		jpaProperties.putAll(config.getPropertiesWithPrefix(prefix + "JpaProperties.", true));
94  		
95  		// Load in the defaults for a Hibernate JPA Setup. Since the JPA spec states that these properties will be ignored by
96  		// vendors that do not understand them, we can add all of the necessary defaults per supported JPA vendor here.
97  		jpaProperties.put("hibernate.show_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.show_sql", "false"));
98          jpaProperties.put("hibernate.format_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.format_sql", "false"));
99          jpaProperties.put("hibernate.use_sql_comments", determineConfigProperty(config, prefix, "JpaProperties.hibernate.use_sql_comments", "false"));
100         // Default now JTOM rather than Atomikos. Atomikos can be used by setting (KULRICE-1909)
101         //   JpaProperties.hibernate.transaction.manager_lookup_class=org.kuali.rice.core.jta.AtomikosTransactionManagerLookup
102         // in a configuration file for a JPA persistence unit.
103         jpaProperties.put("hibernate.transaction.manager_lookup_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JOTMTransactionManagerLookup"));
104         //jpaProperties.put("hibernate.transaction.manager_lookup_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.manager_lookup_class", "org.kuali.rice.core.jta.AtomikosTransactionManagerLookup"));
105         jpaProperties.put("hibernate.current_session_context_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.current_session_context_class", "org.hibernate.context.JTASessionContext"));
106         jpaProperties.put("hibernate.connection.release_mode", determineConfigProperty(config, prefix, "JpaProperties.hibernate.connection.release_mode", "auto"));
107         jpaProperties.put("hibernate.transaction.flush_before_completion", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.flush_before_completion", "true"));
108         jpaProperties.put("hibernate.bytecode.use_reflection_optimizer", determineConfigProperty(config, prefix, "JpaProperties.hibernate.bytecode.use_reflection_optimizer", "false"));
109         jpaProperties.put("hibernate.transaction.auto_close_session", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.auto_close_session", "false"));
110         jpaProperties.put("hibernate.hbm2ddl.auto", determineConfigProperty(config, prefix, "JpaProperties.hibernate.hbm2ddl.auto", ""));
111         
112         // TODO: Add more vendor specific defaults...
113         
114         return jpaProperties;
115 	}
116 
117 	private String determineConfigProperty(Config config, String prefix, String key, String defaultValue) {
118 		String value = config.getProperty(prefix + key);
119 		// fallback on the defaults (non-module based properties)
120 		if (value == null) {
121 			value = config.getProperty("rice.jpa." + key);
122 		}
123 		// fallback on the default value passed in if still no value found for key
124 		return value == null ? defaultValue : value;
125 	}
126 	
127 }