|
17 | 17 | package org.springframework.boot.autoconfigure.orm.jpa;
|
18 | 18 |
|
19 | 19 | import java.util.Arrays;
|
20 |
| -import java.util.Collections; |
21 |
| -import java.util.LinkedHashMap; |
22 |
| -import java.util.List; |
23 |
| -import java.util.Map; |
24 | 20 |
|
25 | 21 | import javax.persistence.EntityManager;
|
26 |
| -import javax.sql.DataSource; |
27 | 22 |
|
28 |
| -import org.apache.commons.logging.Log; |
29 |
| -import org.apache.commons.logging.LogFactory; |
30 |
| - |
31 |
| -import org.springframework.beans.factory.ObjectProvider; |
32 | 23 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
33 | 24 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
34 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
|
38 | 29 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
39 | 30 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
40 | 31 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.HibernateEntityManagerCondition;
|
41 |
| -import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; |
42 |
| -import org.springframework.boot.jdbc.SchemaManagementProvider; |
43 |
| -import org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform; |
| 32 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
44 | 33 | import org.springframework.context.annotation.ConditionContext;
|
45 | 34 | import org.springframework.context.annotation.Conditional;
|
46 | 35 | import org.springframework.context.annotation.Configuration;
|
| 36 | +import org.springframework.context.annotation.Import; |
47 | 37 | import org.springframework.core.Ordered;
|
48 | 38 | import org.springframework.core.annotation.Order;
|
49 | 39 | import org.springframework.core.type.AnnotatedTypeMetadata;
|
50 |
| -import org.springframework.jndi.JndiLocatorDelegate; |
51 | 40 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
52 |
| -import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; |
53 |
| -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
54 |
| -import org.springframework.transaction.jta.JtaTransactionManager; |
55 | 41 | import org.springframework.util.ClassUtils;
|
56 | 42 |
|
57 | 43 | /**
|
|
65 | 51 | @Configuration
|
66 | 52 | @ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, EntityManager.class })
|
67 | 53 | @Conditional(HibernateEntityManagerCondition.class)
|
| 54 | +@EnableConfigurationProperties(JpaProperties.class) |
68 | 55 | @AutoConfigureAfter({ DataSourceAutoConfiguration.class })
|
69 |
| -public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { |
70 |
| - |
71 |
| - private static final Log logger = LogFactory |
72 |
| - .getLog(HibernateJpaAutoConfiguration.class); |
73 |
| - |
74 |
| - private static final String JTA_PLATFORM = "hibernate.transaction.jta.platform"; |
75 |
| - |
76 |
| - /** |
77 |
| - * {@code NoJtaPlatform} implementations for various Hibernate versions. |
78 |
| - */ |
79 |
| - private static final String[] NO_JTA_PLATFORM_CLASSES = { |
80 |
| - "org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform", |
81 |
| - "org.hibernate.service.jta.platform.internal.NoJtaPlatform" }; |
82 |
| - |
83 |
| - /** |
84 |
| - * {@code WebSphereExtendedJtaPlatform} implementations for various Hibernate |
85 |
| - * versions. |
86 |
| - */ |
87 |
| - private static final String[] WEBSPHERE_JTA_PLATFORM_CLASSES = { |
88 |
| - "org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform", |
89 |
| - "org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform", }; |
90 |
| - |
91 |
| - private final HibernateDefaultDdlAutoProvider defaultDdlAutoProvider; |
92 |
| - |
93 |
| - public HibernateJpaAutoConfiguration(DataSource dataSource, |
94 |
| - JpaProperties jpaProperties, |
95 |
| - ObjectProvider<JtaTransactionManager> jtaTransactionManager, |
96 |
| - ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, |
97 |
| - ObjectProvider<List<SchemaManagementProvider>> providers) { |
98 |
| - super(dataSource, jpaProperties, jtaTransactionManager, |
99 |
| - transactionManagerCustomizers); |
100 |
| - this.defaultDdlAutoProvider = new HibernateDefaultDdlAutoProvider( |
101 |
| - providers.getIfAvailable(Collections::emptyList)); |
102 |
| - } |
103 |
| - |
104 |
| - @Override |
105 |
| - protected AbstractJpaVendorAdapter createJpaVendorAdapter() { |
106 |
| - return new HibernateJpaVendorAdapter(); |
107 |
| - } |
108 |
| - |
109 |
| - @Override |
110 |
| - protected Map<String, Object> getVendorProperties() { |
111 |
| - Map<String, Object> vendorProperties = new LinkedHashMap<>(); |
112 |
| - String defaultDdlMode = this.defaultDdlAutoProvider |
113 |
| - .getDefaultDdlAuto(getDataSource()); |
114 |
| - vendorProperties.putAll(getProperties().getHibernateProperties(defaultDdlMode)); |
115 |
| - return vendorProperties; |
116 |
| - } |
117 |
| - |
118 |
| - @Override |
119 |
| - protected void customizeVendorProperties(Map<String, Object> vendorProperties) { |
120 |
| - super.customizeVendorProperties(vendorProperties); |
121 |
| - if (!vendorProperties.containsKey(JTA_PLATFORM)) { |
122 |
| - configureJtaPlatform(vendorProperties); |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - private void configureJtaPlatform(Map<String, Object> vendorProperties) |
127 |
| - throws LinkageError { |
128 |
| - JtaTransactionManager jtaTransactionManager = getJtaTransactionManager(); |
129 |
| - if (jtaTransactionManager != null) { |
130 |
| - if (runningOnWebSphere()) { |
131 |
| - // We can never use SpringJtaPlatform on WebSphere as |
132 |
| - // WebSphereUowTransactionManager has a null TransactionManager |
133 |
| - // which will cause Hibernate to NPE |
134 |
| - configureWebSphereTransactionPlatform(vendorProperties); |
135 |
| - } |
136 |
| - else { |
137 |
| - configureSpringJtaPlatform(vendorProperties, jtaTransactionManager); |
138 |
| - } |
139 |
| - } |
140 |
| - else { |
141 |
| - vendorProperties.put(JTA_PLATFORM, getNoJtaPlatformManager()); |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - private boolean runningOnWebSphere() { |
146 |
| - return ClassUtils.isPresent( |
147 |
| - "com.ibm.websphere.jtaextensions." + "ExtendedJTATransaction", |
148 |
| - getClass().getClassLoader()); |
149 |
| - } |
150 |
| - |
151 |
| - private void configureWebSphereTransactionPlatform( |
152 |
| - Map<String, Object> vendorProperties) { |
153 |
| - vendorProperties.put(JTA_PLATFORM, getWebSphereJtaPlatformManager()); |
154 |
| - } |
155 |
| - |
156 |
| - private Object getWebSphereJtaPlatformManager() { |
157 |
| - return getJtaPlatformManager(WEBSPHERE_JTA_PLATFORM_CLASSES); |
158 |
| - } |
159 |
| - |
160 |
| - private void configureSpringJtaPlatform(Map<String, Object> vendorProperties, |
161 |
| - JtaTransactionManager jtaTransactionManager) { |
162 |
| - try { |
163 |
| - vendorProperties.put(JTA_PLATFORM, |
164 |
| - new SpringJtaPlatform(jtaTransactionManager)); |
165 |
| - } |
166 |
| - catch (LinkageError ex) { |
167 |
| - // NoClassDefFoundError can happen if Hibernate 4.2 is used and some |
168 |
| - // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError |
169 |
| - if (!isUsingJndi()) { |
170 |
| - throw new IllegalStateException("Unable to set Hibernate JTA " |
171 |
| - + "platform, are you using the correct " |
172 |
| - + "version of Hibernate?", ex); |
173 |
| - } |
174 |
| - // Assume that Hibernate will use JNDI |
175 |
| - if (logger.isDebugEnabled()) { |
176 |
| - logger.debug("Unable to set Hibernate JTA platform : " + ex.getMessage()); |
177 |
| - } |
178 |
| - } |
179 |
| - } |
180 |
| - |
181 |
| - private boolean isUsingJndi() { |
182 |
| - try { |
183 |
| - return JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable(); |
184 |
| - } |
185 |
| - catch (Error ex) { |
186 |
| - return false; |
187 |
| - } |
188 |
| - } |
189 |
| - |
190 |
| - private Object getNoJtaPlatformManager() { |
191 |
| - return getJtaPlatformManager(NO_JTA_PLATFORM_CLASSES); |
192 |
| - } |
193 |
| - |
194 |
| - private Object getJtaPlatformManager(String[] candidates) { |
195 |
| - for (String candidate : candidates) { |
196 |
| - try { |
197 |
| - return Class.forName(candidate).newInstance(); |
198 |
| - } |
199 |
| - catch (Exception ex) { |
200 |
| - // Continue searching |
201 |
| - } |
202 |
| - } |
203 |
| - throw new IllegalStateException("Could not configure JTA platform"); |
204 |
| - } |
| 56 | +@Import(JpaHibernateConfiguration.class) |
| 57 | +public class HibernateJpaAutoConfiguration { |
205 | 58 |
|
206 | 59 | @Order(Ordered.HIGHEST_PRECEDENCE + 20)
|
207 | 60 | static class HibernateEntityManagerCondition extends SpringBootCondition {
|
|
0 commit comments