Skip to content

Commit

Permalink
HHH-10056 - Separate settings for notions of (1) disabling EnversServ…
Browse files Browse the repository at this point in the history
…ice and (2) auto-registering Envers listeners
  • Loading branch information
sebersole committed Sep 2, 2015
1 parent 518033e commit 29de3c8
Showing 1 changed file with 34 additions and 1 deletion.
Expand Up @@ -8,6 +8,8 @@

import org.hibernate.HibernateException;
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.envers.event.spi.EnversListenerDuplicationStrategy;
import org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl;
Expand All @@ -21,29 +23,60 @@
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;

import org.jboss.logging.Logger;

/**
* Hooks up Envers event listeners.
*
* @author Steve Ebersole
*/
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(
Metadata metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
final EnversService enversService = serviceRegistry.getService( EnversService.class );

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Opt-out of registration if EnversService is disabled
if ( !enversService.isEnabled() ) {
log.debug( "Skipping Envers listener registrations : EnversService disabled" );
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() ) {
throw new HibernateException(
"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 );
listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE );

Expand Down

0 comments on commit 29de3c8

Please sign in to comment.