1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.jpa.spring; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.sql.DataSource; |
22 | |
|
23 | |
import org.kuali.rice.core.config.Config; |
24 | |
import org.kuali.rice.core.config.ConfigContext; |
25 | |
import org.springframework.orm.jpa.JpaVendorAdapter; |
26 | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
27 | |
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; |
28 | |
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class RiceLocalContainerEntityManagerFactoryBean extends LocalContainerEntityManagerFactoryBean { |
34 | |
|
35 | 0 | public RiceLocalContainerEntityManagerFactoryBean() { |
36 | 0 | throw new RuntimeException(getClass().getName() + " can not be constructed without a datasource"); |
37 | |
} |
38 | |
|
39 | |
public RiceLocalContainerEntityManagerFactoryBean(DataSource datasource) { |
40 | 0 | this("", datasource); |
41 | 0 | } |
42 | |
|
43 | 0 | public RiceLocalContainerEntityManagerFactoryBean(String prefix, DataSource datasource) { |
44 | 0 | if (prefix.equals("")) { |
45 | 0 | prefix = "rice"; |
46 | |
} |
47 | 0 | prefix += ".jpa."; |
48 | |
|
49 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
50 | |
|
51 | 0 | setPersistenceUnitManager(preparePersistentUnitManager(config, prefix, datasource)); |
52 | 0 | setPersistenceXmlLocation(determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml")); |
53 | 0 | setDataSource(datasource); |
54 | 0 | setPersistenceUnitName(determineConfigProperty(config, prefix, "PersistenceUnitName", "rice")); |
55 | 0 | setJpaDialect(new org.springframework.orm.jpa.vendor.HibernateJpaDialect()); |
56 | 0 | setJpaPropertyMap(prepareJpaProperties(config, prefix)); |
57 | 0 | setJpaVendorAdapter(prepareJpaVendorAdapter(config, prefix)); |
58 | |
|
59 | 0 | RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor(); |
60 | 0 | postProcessor.setJtaDataSource(datasource); |
61 | 0 | setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor }); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
private PersistenceUnitManager preparePersistentUnitManager(Config config, String prefix, DataSource datasource) { |
66 | 0 | DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); |
67 | 0 | persistenceUnitManager.setDefaultDataSource(datasource); |
68 | 0 | persistenceUnitManager.setPersistenceXmlLocations(new String[] {determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml")}); |
69 | 0 | persistenceUnitManager.setDefaultPersistenceUnitRootLocation(determineConfigProperty(config, prefix, "PersistenceUnitRootLocation", "classpath:")); |
70 | 0 | persistenceUnitManager.afterPropertiesSet(); |
71 | 0 | return persistenceUnitManager; |
72 | |
} |
73 | |
|
74 | |
private JpaVendorAdapter prepareJpaVendorAdapter(Config config, String prefix) { |
75 | 0 | DevHibernateJpaVendorAdapter jpaVendorAdapter = new DevHibernateJpaVendorAdapter(); |
76 | 0 | jpaVendorAdapter.setDatabasePlatform(determineConfigProperty(config, prefix, "DatabasePlatform", "org.hibernate.dialect.MySQLDialect")); |
77 | 0 | jpaVendorAdapter.setGenerateDdl(new Boolean(determineConfigProperty(config, prefix, "GenerateDdl", "false"))); |
78 | 0 | jpaVendorAdapter.setSerializationFilename(determineConfigProperty(config, prefix, "SerializationFilename", "/tmp/Ejb3Configuration.out")); |
79 | 0 | jpaVendorAdapter.setUseSerialization(new Boolean(determineConfigProperty(config, prefix, "UseSerialization", "true"))); |
80 | |
try { |
81 | 0 | jpaVendorAdapter.afterPropertiesSet(); |
82 | 0 | } catch (Exception e) {} |
83 | 0 | return jpaVendorAdapter; |
84 | |
} |
85 | |
|
86 | |
private Map<String, String> prepareJpaProperties(Config config, String prefix) { |
87 | 0 | Map<String, String> jpaProperties = new HashMap<String, String>(); |
88 | |
|
89 | |
|
90 | 0 | jpaProperties.putAll(config.getPropertiesWithPrefix(prefix + "JpaProperties.", true)); |
91 | |
|
92 | |
|
93 | |
|
94 | 0 | jpaProperties.put("hibernate.show_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.show_sql", "false")); |
95 | 0 | jpaProperties.put("hibernate.format_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.format_sql", "false")); |
96 | 0 | jpaProperties.put("hibernate.use_sql_comments", determineConfigProperty(config, prefix, "JpaProperties.hibernate.use_sql_comments", "false")); |
97 | |
|
98 | |
|
99 | |
|
100 | 0 | jpaProperties.put("hibernate.transaction.manager_lookup_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JOTMTransactionManagerLookup")); |
101 | |
|
102 | 0 | jpaProperties.put("hibernate.current_session_context_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.current_session_context_class", "org.hibernate.context.JTASessionContext")); |
103 | 0 | jpaProperties.put("hibernate.connection.release_mode", determineConfigProperty(config, prefix, "JpaProperties.hibernate.connection.release_mode", "auto")); |
104 | 0 | jpaProperties.put("hibernate.transaction.flush_before_completion", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.flush_before_completion", "true")); |
105 | 0 | jpaProperties.put("hibernate.bytecode.use_reflection_optimizer", determineConfigProperty(config, prefix, "JpaProperties.hibernate.bytecode.use_reflection_optimizer", "false")); |
106 | 0 | jpaProperties.put("hibernate.transaction.auto_close_session", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.auto_close_session", "false")); |
107 | |
|
108 | |
|
109 | |
|
110 | 0 | return jpaProperties; |
111 | |
} |
112 | |
|
113 | |
private String determineConfigProperty(Config config, String prefix, String key, String defaultValue) { |
114 | 0 | String value = config.getProperty(prefix + key); |
115 | |
|
116 | 0 | if (value == null) { |
117 | 0 | value = config.getProperty("rice.jpa." + key); |
118 | |
} |
119 | |
|
120 | 0 | return value == null ? defaultValue : value; |
121 | |
} |
122 | |
|
123 | |
} |