Skip to content

Commit 5eb5ab4

Browse files
committed
HHH-10056 - Separate settings for notions of (1) disabling EnversService and (2) auto-registering Envers listeners
(cherry picked from commit 29de3c8)
1 parent 336b548 commit 5eb5ab4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

hibernate-envers/src/main/java/org/hibernate/envers/boot/internal/EnversIntegrator.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.hibernate.HibernateException;
1010
import org.hibernate.boot.Metadata;
11+
import org.hibernate.engine.config.spi.ConfigurationService;
12+
import org.hibernate.engine.config.spi.StandardConverters;
1113
import org.hibernate.engine.spi.SessionFactoryImplementor;
1214
import org.hibernate.envers.event.spi.EnversListenerDuplicationStrategy;
1315
import org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl;
@@ -21,29 +23,60 @@
2123
import org.hibernate.integrator.spi.Integrator;
2224
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
2325

26+
import org.jboss.logging.Logger;
27+
2428
/**
2529
* Hooks up Envers event listeners.
2630
*
2731
* @author Steve Ebersole
2832
*/
2933
public class EnversIntegrator implements Integrator {
30-
public static final String AUTO_REGISTER = EnversService.LEGACY_AUTO_REGISTER;
34+
private static final Logger log = Logger.getLogger( EnversIntegrator.class );
35+
36+
public static final String AUTO_REGISTER = "hibernate.envers.autoRegisterListeners";
3137

3238
public void integrate(
3339
Metadata metadata,
3440
SessionFactoryImplementor sessionFactory,
3541
SessionFactoryServiceRegistry serviceRegistry) {
3642
final EnversService enversService = serviceRegistry.getService( EnversService.class );
43+
44+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
// Opt-out of registration if EnversService is disabled
3746
if ( !enversService.isEnabled() ) {
47+
log.debug( "Skipping Envers listener registrations : EnversService disabled" );
3848
return;
3949
}
4050

51+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52+
// Opt-out of registration if asked to not register
53+
final boolean autoRegister = serviceRegistry.getService( ConfigurationService.class ).getSetting(
54+
AUTO_REGISTER,
55+
StandardConverters.BOOLEAN,
56+
true
57+
);
58+
if ( !autoRegister ) {
59+
log.debug( "Skipping Envers listener registrations : Listener auto-registration disabled" );
60+
return;
61+
}
62+
63+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
// Verify that the EnversService is fully initialized and ready to go.
4165
if ( !enversService.isInitialized() ) {
4266
throw new HibernateException(
4367
"Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate"
4468
);
4569
}
4670

71+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
// Opt-out of registration if no audited entities found
73+
if ( !enversService.getEntitiesConfigurations().hasAuditedEntities() ) {
74+
log.debug( "Skipping Envers listener registrations : No audited entities found" );
75+
return;
76+
}
77+
78+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
// Do the registrations
4780
final EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
4881
listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE );
4982

0 commit comments

Comments
 (0)