Skip to content
Browse files

HHH-7621 - allow DataSource to be passed into EntityManagerFactoryBui…

…lder
  • Loading branch information...
1 parent d0f8834 commit 5aad2bf84b8fee8ceb1c588cddc4a654007022a2 @sebersole sebersole committed Sep 20, 2012
View
14 ...anager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java
@@ -32,6 +32,7 @@
import javax.persistence.MappedSuperclass;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
@@ -135,6 +136,7 @@
// Explicit "injectables"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private Object validatorFactory;
+ private DataSource dataSource;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private final PersistenceUnitDescriptor persistenceUnit;
@@ -718,6 +720,13 @@ public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory)
}
@Override
+ public EntityManagerFactoryBuilder withDataSource(DataSource dataSource) {
+ this.dataSource = dataSource;
+
+ return this;
+ }
+
+ @Override
public void cancel() {
// todo : close the bootstrap registry (not critical, but nice to do)
@@ -827,7 +836,10 @@ else if ( keyString.startsWith( AvailableSettings.JACC_PREFIX )
}
private void applyJdbcConnectionProperties() {
- if ( persistenceUnit.getJtaDataSource() != null ) {
+ if ( dataSource != null ) {
+ serviceRegistryBuilder.applySetting( Environment.DATASOURCE, dataSource );
+ }
+ else if ( persistenceUnit.getJtaDataSource() != null ) {
serviceRegistryBuilder.applySetting( Environment.DATASOURCE, persistenceUnit.getJtaDataSource() );
}
else if ( persistenceUnit.getNonJtaDataSource() != null ) {
View
11 ...e-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java
@@ -24,6 +24,7 @@
package org.hibernate.jpa.boot.spi;
import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
/**
* Represents a 2-phase JPA bootstrap process for building a Hibernate EntityManagerFactory.
@@ -51,6 +52,16 @@
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory);
/**
+ * Allows passing in a DataSource (delayed from constructing the builder, AKA phase 2) to be used
+ * in building the EntityManagerFactory
+ *
+ * @param dataSource The DataSource to use
+ *
+ * @return {@code this}, for method chaining
+ */
+ public EntityManagerFactoryBuilder withDataSource(DataSource dataSource);
+
+ /**
* Build {@link EntityManagerFactory} instance
*
* @return The built {@link EntityManagerFactory}
View
12 hibernate-entitymanager/src/main/java/org/hibernate/jpa/event/spi/JpaIntegrator.java
@@ -213,11 +213,13 @@ public String toString() {
}
}
+ private static final String CDI_LISTENER_FACTORY_CLASS = "org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory";
+
private ListenerFactory buildBeanManagerListenerFactory(Object beanManagerRef) {
try {
// specifically using our classloader here...
final Class beanManagerListenerFactoryClass = getClass().getClassLoader()
- .loadClass( "org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory" );
+ .loadClass( CDI_LISTENER_FACTORY_CLASS );
final Method beanManagerListenerFactoryBuilderMethod = beanManagerListenerFactoryClass.getMethod(
"fromBeanManagerReference",
Object.class
@@ -230,14 +232,14 @@ private ListenerFactory buildBeanManagerListenerFactory(Object beanManagerRef) {
throw e.getTargetException();
}
}
- catch (ReflectiveOperationException e) {
- throw new HibernateException( "Could not access BeanManagerListenerFactory class to handle CDI extensions", e );
+ catch (ClassNotFoundException e) {
+ throw new HibernateException( "Could not locate BeanManagerListenerFactory class to handle CDI extensions", e );
}
- catch (RuntimeException e) {
+ catch (HibernateException e) {
throw e;
}
catch (Throwable e) {
- throw new HibernateException( "Problem calling BeanManagerListenerFactory class to handle CDI extensions", e );
+ throw new HibernateException( "Could not access BeanManagerListenerFactory class to handle CDI extensions", e );
}
}

0 comments on commit 5aad2bf

Please sign in to comment.
Something went wrong with that request. Please try again.