Skip to content

Commit

Permalink
HHH-7620 - allow ValidatorFactory to be passed into EntityManagerFact…
Browse files Browse the repository at this point in the history
…oryBuilder
  • Loading branch information
sebersole committed Sep 20, 2012
1 parent 500e022 commit 595f068
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public EntityManagerFactory createEntityManagerFactory(String persistenceUnitNam
continue;
}

return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration ).buildEntityManagerFactory();
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration ).build();
}

return null;
Expand All @@ -100,7 +100,7 @@ private static Map wrap(Map properties) {
*/
@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integration) {
return Bootstrap.getEntityManagerFactoryBuilder( info, integration ).buildEntityManagerFactory();
return Bootstrap.getEntityManagerFactoryBuilder( info, integration ).build();
}

private final ProviderUtil providerUtil = new ProviderUtil() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
private final PersistenceUnitDescriptor persistenceUnit;
private final SettingsImpl settings = new SettingsImpl();
private final StandardServiceRegistryBuilder serviceRegistryBuilder;
private final Map<?,?> configurationValues;
private final Map configurationValues;

private final List<JaccDefinition> jaccDefinitions = new ArrayList<JaccDefinition>();
private final List<CacheRegionDefinition> cacheRegionDefinitions = new ArrayList<CacheRegionDefinition>();
Expand All @@ -148,6 +148,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil

private static EntityNotFoundDelegate jpaEntityNotFoundDelegate = new JpaEntityNotFoundDelegate();

private Object validatorFactory;

private static class JpaEntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable {
public void handleEntityNotFound(String entityName, Serializable id) {
throw new EntityNotFoundException( "Unable to find " + entityName + " with id " + id );
Expand Down Expand Up @@ -198,7 +200,8 @@ public EntityManagerFactoryBuilderImpl(PersistenceUnitDescriptor persistenceUnit

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// temporary!
public Map<?, ?> getConfigurationValues() {
@SuppressWarnings("unchecked")
public Map getConfigurationValues() {
return Collections.unmodifiableMap( configurationValues );
}

Expand Down Expand Up @@ -488,6 +491,8 @@ private void processProperties(BootstrapServiceRegistry bootstrapServiceRegistry
applyJdbcConnectionProperties();
applyTransactionProperties();

// this check is needed for tests. Second form happens later (mainly against the explicitly passed validator)
// when building EMF...
final Object validationFactory = configurationValues.get( AvailableSettings.VALIDATION_FACTORY );
if ( validationFactory != null ) {
BeanValidationIntegrator.validateFactory( validationFactory );
Expand All @@ -499,7 +504,8 @@ private void processProperties(BootstrapServiceRegistry bootstrapServiceRegistry
LOG.definingFlushBeforeCompletionIgnoredInHem( Environment.FLUSH_BEFORE_COMPLETION );
}

for ( Map.Entry entry : configurationValues.entrySet() ) {
for ( Object oEntry : configurationValues.entrySet() ) {
Map.Entry entry = (Map.Entry) oEntry;
if ( entry.getKey() instanceof String ) {
final String keyString = (String) entry.getKey();

Expand Down Expand Up @@ -912,18 +918,34 @@ private void scanInContext(
}
}

@Override
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory) {
this.validatorFactory = validatorFactory;
return this;
}

@Override
public void cancel() {
// todo : close the bootstrap registry (not critical, but nice to do)

}

@SuppressWarnings("unchecked")
public EntityManagerFactory buildEntityManagerFactory() {
public EntityManagerFactory build() {
// IMPL NOTE : TCCL handling here is temporary.
// It is needed because this code still uses Hibernate Configuration and Hibernate commons-annotations
// in turn which relies on TCCL being set.

if ( validatorFactory != null ) {
// NOTE : need to add it to both
configurationValues.put( AvailableSettings.VALIDATION_FACTORY, validatorFactory );
}

final Object validationFactory = configurationValues.get( AvailableSettings.VALIDATION_FACTORY );
if ( validationFactory != null ) {
BeanValidationIntegrator.validateFactory( validationFactory );
}

final ServiceRegistry serviceRegistry = buildServiceRegistry();
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@

import javax.persistence.EntityManagerFactory;

import org.hibernate.cfg.Configuration;

/**
* Represents a 2-phase JPA bootstrap process for building a Hibernate EntityManagerFactory.
*
* The first phase is the process of instantiating this builder. During the first phase, loading of Class references
* is highly discouraged.
*
* The second phase is building the EntityManagerFactory instance via {@link #buildEntityManagerFactory()}.
* The second phase is building the EntityManagerFactory instance via {@link #build}.
*
* If anything goes wrong during either phase and the bootstrap process needs to be aborted, {@link #cancel()} should
* be called.
Expand All @@ -42,12 +40,22 @@
* @author Scott Marlow
*/
public interface EntityManagerFactoryBuilder {
/**
* Allows passing in a Java EE ValidatorFactory (delayed from constructing the builder, AKA phase 2) to be used
* in building the EntityManagerFactory
*
* @param validatorFactory The ValidatorFactory
*
* @return {@code this}, for method chaining
*/
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory);

/**
* Build {@link EntityManagerFactory} instance
*
* @return The built {@link EntityManagerFactory}
*/
public EntityManagerFactory buildEntityManagerFactory();
public EntityManagerFactory build();

/**
* Cancel the building processing. This is used to signal the builder to release any resources in the case of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void buildEntityManagerFactory() throws Exception {
entityManagerFactory = (EntityManagerFactoryImpl) Bootstrap.getEntityManagerFactoryBuilder(
buildPersistenceUnitDescriptor(),
buildSettings()
).buildEntityManagerFactory();
).build();

serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getSessionFactory()
.getServiceRegistry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public static EntityManagerFactory generateEntityManagerFactory(PersistenceUnitD
}

public static EntityManagerFactory generateEntityManagerFactory(PersistenceUnitDescriptor descriptor, Map settings) {
return Bootstrap.getEntityManagerFactoryBuilder( descriptor, settings ).buildEntityManagerFactory();
return Bootstrap.getEntityManagerFactoryBuilder( descriptor, settings ).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class InterceptorTest {
public void testConfiguredInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory();
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
Expand All @@ -78,7 +78,7 @@ public void testConfiguredInterceptor() {
public void testConfiguredSessionInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory();
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
Expand All @@ -104,7 +104,7 @@ public void testConfiguredSessionInterceptor() {
public void testEmptyCreateEntityManagerFactoryAndPropertyUse() {
Map settings = basicSettings();
settings.put( AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory();
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
Expand All @@ -130,7 +130,7 @@ public void testEmptyCreateEntityManagerFactoryAndPropertyUse() {
public void testOnLoadCallInInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.INTERCEPTOR, new ExceptionInterceptor( true ) );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory();
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void testPersisterClassProvider() {
EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
new PersistenceUnitDescriptorAdapter(),
settings
).buildEntityManagerFactory();
).build();
entityManagerFactory.close();
}
catch ( PersistenceException e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testSessionFactoryObserverProperty() {
);

try {
final EntityManagerFactory entityManagerFactory = builder.buildEntityManagerFactory();
final EntityManagerFactory entityManagerFactory = builder.build();
entityManagerFactory.close();
Assert.fail( "GoofyException should have been thrown" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testIdentifierGeneratorStrategyProvider() {
final EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
new PersistenceUnitInfoAdapter(),
settings
).buildEntityManagerFactory();
).build();

final EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void buildEntityManagerFactory() throws Exception {
buildPersistenceUnitDescriptor(),
buildSettings()
);
entityManagerFactory = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.buildEntityManagerFactory();
entityManagerFactory = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.build();

serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getSessionFactory()
.getServiceRegistry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected void init(boolean audited, String auditStrategy) throws IOException {
configurationProperties
);

emf = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.buildEntityManagerFactory();
emf = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.build();

serviceRegistry = (StandardServiceRegistryImpl) emf.getSessionFactory().getServiceRegistry().getParentServiceRegistry();

Expand Down

0 comments on commit 595f068

Please sign in to comment.