HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (Interceptor handling)

This commit is contained in:
Steve Ebersole 2016-04-29 10:08:27 -05:00
parent 8f812730d1
commit bedb33decf
4 changed files with 162 additions and 74 deletions

View File

@ -599,25 +599,8 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplement
this.autoCloseSessionEnabled = cfgService.getSetting( AUTO_CLOSE_SESSION, BOOLEAN, false );
this.statisticsEnabled = cfgService.getSetting( GENERATE_STATISTICS, BOOLEAN, false );
this.interceptor = strategySelector.resolveDefaultableStrategy(
Interceptor.class,
configurationSettings.get( INTERCEPTOR ),
EmptyInterceptor.INSTANCE
);
final Object statelessInterceptorSetting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );
if ( statelessInterceptorSetting == null ) {
this.statelessInterceptorClass = null;
}
else if ( statelessInterceptorSetting instanceof Class ) {
this.statelessInterceptorClass = (Class<? extends Interceptor>) statelessInterceptorSetting;
}
else {
this.statelessInterceptorClass = strategySelector.selectStrategyImplementor(
Interceptor.class,
statelessInterceptorSetting.toString()
);
}
this.interceptor = determineInterceptor( configurationSettings, strategySelector );
this.statelessInterceptorClass = determineStatelessInterceptorClass( configurationSettings, strategySelector );
this.statementInspector = strategySelector.resolveStrategy(
StatementInspector.class,
configurationSettings.get( STATEMENT_INSPECTOR )
@ -745,6 +728,56 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplement
this.preferUserTransaction = ConfigurationHelper.getBoolean( PREFER_USER_TRANSACTION, configurationSettings, false );
}
private static Interceptor determineInterceptor(Map configurationSettings, StrategySelector strategySelector) {
Object setting = configurationSettings.get( INTERCEPTOR );
if ( setting == null ) {
// try the legacy (deprecated) JPA name
setting = configurationSettings.get( org.hibernate.jpa.AvailableSettings.INTERCEPTOR );
if ( setting != null ) {
DeprecationLogger.DEPRECATION_LOGGER.deprecatedSetting(
org.hibernate.jpa.AvailableSettings.INTERCEPTOR,
INTERCEPTOR
);
}
}
return strategySelector.resolveStrategy(
Interceptor.class,
setting
);
}
@SuppressWarnings("unchecked")
private static Class<? extends Interceptor> determineStatelessInterceptorClass(
Map configurationSettings,
StrategySelector strategySelector) {
Object setting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );
if ( setting == null ) {
// try the legacy (deprecated) JPA name
setting = configurationSettings.get( org.hibernate.jpa.AvailableSettings.SESSION_INTERCEPTOR );
if ( setting != null ) {
DeprecationLogger.DEPRECATION_LOGGER.deprecatedSetting(
org.hibernate.jpa.AvailableSettings.SESSION_INTERCEPTOR,
SESSION_SCOPED_INTERCEPTOR
);
}
}
if ( setting == null ) {
return null;
}
else if ( setting instanceof Class ) {
return (Class<? extends Interceptor>) setting;
}
else {
return strategySelector.selectStrategyImplementor(
Interceptor.class,
setting.toString()
);
}
}
private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
Map configurationSettings,
StandardServiceRegistry serviceRegistry) {

View File

@ -992,6 +992,34 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
}
}
public static Interceptor configuredInterceptor(Interceptor interceptor, SessionFactoryOptions options) {
// NOTE : DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session"
// we "filter" that one out here. The return from here should represent the
// explicitly configured Interceptor (if one). Return null from here instead; Session
// will handle it
if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
return interceptor;
}
// prefer the SF-scoped interceptor, prefer that to any Session-scoped interceptor prototype
if ( options.getInterceptor() != null && options.getInterceptor() != EmptyInterceptor.INSTANCE ) {
return options.getInterceptor();
}
// then check the Session-scoped interceptor prototype
if ( options.getStatelessInterceptorImplementor() != null ) {
try {
return options.getStatelessInterceptorImplementor().newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new HibernateException( "Could not instantiate session-scoped SessionFactory Interceptor", e );
}
}
return null;
}
static class SessionBuilderImpl<T extends SessionBuilder> implements SessionBuilderImplementor<T>, SessionCreationOptions {
private static final Logger log = CoreLogging.logger( SessionBuilderImpl.class );
@ -1099,25 +1127,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
@Override
public Interceptor getInterceptor() {
if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
return interceptor;
}
// prefer the SF-scoped interceptor, prefer that to any Session-scoped interceptor prototype
if ( sessionFactory.getSessionFactoryOptions().getInterceptor() != null ) {
return sessionFactory.getSessionFactoryOptions().getInterceptor();
}
if ( sessionFactory.getSessionFactoryOptions().getStatelessInterceptorImplementor() != null ) {
try {
return sessionFactory.getSessionFactoryOptions().getStatelessInterceptorImplementor().newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new HibernateException( "Could not instantiate session-scoped SessionFactory Interceptor", e );
}
}
return null;
return configuredInterceptor( interceptor, sessionFactory.getSessionFactoryOptions() );
}
@Override
@ -1314,21 +1324,8 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
@Override
public Interceptor getInterceptor() {
// prefer the SF-scoped interceptor, prefer that to any Session-scoped interceptor prototype
if ( sessionFactory.getSessionFactoryOptions().getInterceptor() != null ) {
return sessionFactory.getSessionFactoryOptions().getInterceptor();
}
return configuredInterceptor( EmptyInterceptor.INSTANCE, sessionFactory.getSessionFactoryOptions() );
if ( sessionFactory.getSessionFactoryOptions().getStatelessInterceptorImplementor() != null ) {
try {
return sessionFactory.getSessionFactoryOptions().getStatelessInterceptorImplementor().newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new HibernateException( "Could not instantiate session-scoped SessionFactory Interceptor", e );
}
}
return null;
}
@Override

View File

@ -561,13 +561,13 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
settings.setReleaseResourcesOnCloseEnabled( "true".equals( value ) );
}
final StrategySelector strategySelector = ssrBuilder.getBootstrapServiceRegistry().getService( StrategySelector.class );
final Object interceptorSetting = configurationValues.remove( AvailableSettings.SESSION_INTERCEPTOR );
if ( interceptorSetting != null ) {
settings.setSessionInterceptorClass(
loadSessionInterceptorClass( interceptorSetting, strategySelector )
);
}
// final StrategySelector strategySelector = ssrBuilder.getBootstrapServiceRegistry().getService( StrategySelector.class );
// final Object interceptorSetting = configurationValues.remove( AvailableSettings.SESSION_INTERCEPTOR );
// if ( interceptorSetting != null ) {
// settings.setSessionInterceptorClass(
// loadSessionInterceptorClass( interceptorSetting, strategySelector )
// );
// }
return settings;
}
@ -873,13 +873,13 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
final StrategySelector strategySelector = ssr.getService( StrategySelector.class );
// Locate and apply the requested SessionFactory-level interceptor (if one)
final Object sessionFactoryInterceptorSetting = configurationValues.remove( org.hibernate.cfg.AvailableSettings.INTERCEPTOR );
if ( sessionFactoryInterceptorSetting != null ) {
final Interceptor sessionFactoryInterceptor =
strategySelector.resolveStrategy( Interceptor.class, sessionFactoryInterceptorSetting );
sfBuilder.applyInterceptor( sessionFactoryInterceptor );
}
// // Locate and apply the requested SessionFactory-level interceptor (if one)
// final Object sessionFactoryInterceptorSetting = configurationValues.remove( org.hibernate.cfg.AvailableSettings.INTERCEPTOR );
// if ( sessionFactoryInterceptorSetting != null ) {
// final Interceptor sessionFactoryInterceptor =
// strategySelector.resolveStrategy( Interceptor.class, sessionFactoryInterceptorSetting );
// sfBuilder.applyInterceptor( sessionFactoryInterceptor );
// }
// Locate and apply any requested SessionFactoryObserver
final Object sessionFactoryObserverSetting = configurationValues.remove( AvailableSettings.SESSION_FACTORY_OBSERVER );

View File

@ -6,20 +6,19 @@
*/
package org.hibernate.jpa.test.ejb3configuration;
import java.util.Arrays;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.Arrays;
import java.util.Map;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.jpa.test.SettingsGenerator;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.test.Distributor;
import org.hibernate.jpa.test.Item;
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.test.SettingsGenerator;
import org.junit.Test;
@ -31,8 +30,11 @@ import static org.junit.Assert.fail;
*/
public class InterceptorTest {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test deprecated Interceptor settings
@Test
public void testConfiguredInterceptor() {
public void testDeprecatedConfiguredInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
@ -57,10 +59,66 @@ public class InterceptorTest {
}
}
@Test
public void testDeprecatedConfiguredSessionInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
try {
em.getTransaction().begin();
em.persist( i );
em.getTransaction().commit();
fail( "No interceptor" );
}
catch ( IllegalStateException e ) {
assertEquals( LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage() );
}
finally {
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test Interceptor settings
@Test
public void testConfiguredInterceptor() {
Map settings = basicSettings();
settings.put( org.hibernate.cfg.AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();
i.setName( "Laptop" );
try {
em.getTransaction().begin();
em.persist( i );
em.getTransaction().commit();
fail( "No interceptor" );
}
catch ( IllegalStateException e ) {
assertEquals( ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage() );
}
finally {
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
@Test
public void testConfiguredSessionInterceptor() {
Map settings = basicSettings();
settings.put( AvailableSettings.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName() );
settings.put( org.hibernate.cfg.AvailableSettings.SESSION_SCOPED_INTERCEPTOR, LocalExceptionInterceptor.class.getName() );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build();
EntityManager em = emf.createEntityManager();
Item i = new Item();