From bedb33decf554925d5102a5c22a1c6d58f6eed1e Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 29 Apr 2016 10:08:27 -0500 Subject: [PATCH] HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (Interceptor handling) --- .../internal/SessionFactoryBuilderImpl.java | 71 +++++++++++++----- .../internal/SessionFactoryImpl.java | 63 ++++++++-------- .../EntityManagerFactoryBuilderImpl.java | 28 +++---- .../ejb3configuration/InterceptorTest.java | 74 +++++++++++++++++-- 4 files changed, 162 insertions(+), 74 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java index f31238e281..d2ca450af5 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java @@ -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) 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 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) setting; + } + else { + return strategySelector.selectStrategyImplementor( + Interceptor.class, + setting.toString() + ); + } + + } + private PhysicalConnectionHandlingMode interpretConnectionHandlingMode( Map configurationSettings, StandardServiceRegistry serviceRegistry) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index cfb64b4057..7dafca8e85 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -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 implements SessionBuilderImplementor, 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 diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 4eac9a403d..4f580c4453 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -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 ); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java index ce24364e6f..b2ff8513dc 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java @@ -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();