|
8 | 8 |
|
9 | 9 | import org.hibernate.HibernateException; |
10 | 10 | import org.hibernate.boot.Metadata; |
| 11 | +import org.hibernate.engine.config.spi.ConfigurationService; |
| 12 | +import org.hibernate.engine.config.spi.StandardConverters; |
11 | 13 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
12 | 14 | import org.hibernate.envers.event.spi.EnversListenerDuplicationStrategy; |
13 | 15 | import org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl; |
|
21 | 23 | import org.hibernate.integrator.spi.Integrator; |
22 | 24 | import org.hibernate.service.spi.SessionFactoryServiceRegistry; |
23 | 25 |
|
| 26 | +import org.jboss.logging.Logger; |
| 27 | + |
24 | 28 | /** |
25 | 29 | * Hooks up Envers event listeners. |
26 | 30 | * |
27 | 31 | * @author Steve Ebersole |
28 | 32 | */ |
29 | 33 | 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"; |
31 | 37 |
|
32 | 38 | public void integrate( |
33 | 39 | Metadata metadata, |
34 | 40 | SessionFactoryImplementor sessionFactory, |
35 | 41 | SessionFactoryServiceRegistry serviceRegistry) { |
36 | 42 | final EnversService enversService = serviceRegistry.getService( EnversService.class ); |
| 43 | + |
| 44 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 | + // Opt-out of registration if EnversService is disabled |
37 | 46 | if ( !enversService.isEnabled() ) { |
| 47 | + log.debug( "Skipping Envers listener registrations : EnversService disabled" ); |
38 | 48 | return; |
39 | 49 | } |
40 | 50 |
|
| 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. |
41 | 65 | if ( !enversService.isInitialized() ) { |
42 | 66 | throw new HibernateException( |
43 | 67 | "Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate" |
44 | 68 | ); |
45 | 69 | } |
46 | 70 |
|
| 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 |
47 | 80 | final EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); |
48 | 81 | listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE ); |
49 | 82 |
|
|
0 commit comments