HHH-10056 - Separate settings for notions of (1) disabling EnversService and (2) auto-registering Envers listeners

(cherry picked from commit 29de3c87ab)
This commit is contained in:
Steve Ebersole 2015-09-02 08:51:26 -05:00
parent 336b548c1c
commit 5eb5ab4920
1 changed files with 34 additions and 1 deletions

View File

@ -8,6 +8,8 @@ package org.hibernate.envers.boot.internal;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.envers.event.spi.EnversListenerDuplicationStrategy; import org.hibernate.envers.event.spi.EnversListenerDuplicationStrategy;
import org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl; import org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl;
@ -21,29 +23,60 @@ import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator; import org.hibernate.integrator.spi.Integrator;
import org.hibernate.service.spi.SessionFactoryServiceRegistry; import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.jboss.logging.Logger;
/** /**
* Hooks up Envers event listeners. * Hooks up Envers event listeners.
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class EnversIntegrator implements Integrator { public class EnversIntegrator implements Integrator {
public static final String AUTO_REGISTER = EnversService.LEGACY_AUTO_REGISTER; private static final Logger log = Logger.getLogger( EnversIntegrator.class );
public static final String AUTO_REGISTER = "hibernate.envers.autoRegisterListeners";
public void integrate( public void integrate(
Metadata metadata, Metadata metadata,
SessionFactoryImplementor sessionFactory, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) { SessionFactoryServiceRegistry serviceRegistry) {
final EnversService enversService = serviceRegistry.getService( EnversService.class ); final EnversService enversService = serviceRegistry.getService( EnversService.class );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Opt-out of registration if EnversService is disabled
if ( !enversService.isEnabled() ) { if ( !enversService.isEnabled() ) {
log.debug( "Skipping Envers listener registrations : EnversService disabled" );
return; return;
} }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Opt-out of registration if asked to not register
final boolean autoRegister = serviceRegistry.getService( ConfigurationService.class ).getSetting(
AUTO_REGISTER,
StandardConverters.BOOLEAN,
true
);
if ( !autoRegister ) {
log.debug( "Skipping Envers listener registrations : Listener auto-registration disabled" );
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Verify that the EnversService is fully initialized and ready to go.
if ( !enversService.isInitialized() ) { if ( !enversService.isInitialized() ) {
throw new HibernateException( throw new HibernateException(
"Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate" "Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate"
); );
} }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Opt-out of registration if no audited entities found
if ( !enversService.getEntitiesConfigurations().hasAuditedEntities() ) {
log.debug( "Skipping Envers listener registrations : No audited entities found" );
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Do the registrations
final EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); final EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE ); listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE );