Search in sources :

Example 11 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.

the class TypeSafeActivator method applyCallbackListeners.

@SuppressWarnings({ "UnusedDeclaration" })
public static void applyCallbackListeners(ValidatorFactory validatorFactory, ActivationContext activationContext) {
    final Set<ValidationMode> modes = activationContext.getValidationModes();
    if (!(modes.contains(ValidationMode.CALLBACK) || modes.contains(ValidationMode.AUTO))) {
        return;
    }
    final ConfigurationService cfgService = activationContext.getServiceRegistry().getService(ConfigurationService.class);
    final ClassLoaderService classLoaderService = activationContext.getServiceRegistry().getService(ClassLoaderService.class);
    // asks for it
    if (cfgService.getSettings().get(Environment.CHECK_NULLABILITY) == null) {
        activationContext.getSessionFactory().getSessionFactoryOptions().setCheckNullability(false);
    }
    final BeanValidationEventListener listener = new BeanValidationEventListener(validatorFactory, cfgService.getSettings(), classLoaderService);
    final EventListenerRegistry listenerRegistry = activationContext.getServiceRegistry().getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(DuplicationStrategyImpl.INSTANCE);
    listenerRegistry.appendListeners(EventType.PRE_INSERT, listener);
    listenerRegistry.appendListeners(EventType.PRE_UPDATE, listener);
    listenerRegistry.appendListeners(EventType.PRE_DELETE, listener);
    listener.initialize(cfgService.getSettings(), classLoaderService);
}
Also used : ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 12 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.

the class JaccIntegrator method doIntegration.

private void doIntegration(Map properties, JaccPermissionDeclarations permissionDeclarations, SessionFactoryServiceRegistry serviceRegistry) {
    boolean isSecurityEnabled = properties.containsKey(AvailableSettings.JACC_ENABLED);
    if (!isSecurityEnabled) {
        log.debug("Skipping JACC integration as it was not enabled");
        return;
    }
    final String contextId = (String) properties.get(AvailableSettings.JACC_CONTEXT_ID);
    if (contextId == null) {
        throw new IntegrationException("JACC context id must be specified");
    }
    final JaccService jaccService = serviceRegistry.getService(JaccService.class);
    if (jaccService == null) {
        throw new IntegrationException("JaccService was not set up");
    }
    if (permissionDeclarations != null) {
        for (GrantedPermission declaration : permissionDeclarations.getPermissionDeclarations()) {
            jaccService.addPermission(declaration);
        }
    }
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.addDuplicationStrategy(DUPLICATION_STRATEGY);
    eventListenerRegistry.prependListeners(EventType.PRE_DELETE, new JaccPreDeleteEventListener());
    eventListenerRegistry.prependListeners(EventType.PRE_INSERT, new JaccPreInsertEventListener());
    eventListenerRegistry.prependListeners(EventType.PRE_UPDATE, new JaccPreUpdateEventListener());
    eventListenerRegistry.prependListeners(EventType.PRE_LOAD, new JaccPreLoadEventListener());
}
Also used : JaccPreDeleteEventListener(org.hibernate.secure.internal.JaccPreDeleteEventListener) JaccPreUpdateEventListener(org.hibernate.secure.internal.JaccPreUpdateEventListener) JaccPreLoadEventListener(org.hibernate.secure.internal.JaccPreLoadEventListener) JaccPreInsertEventListener(org.hibernate.secure.internal.JaccPreInsertEventListener) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 13 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.

the class RegisterUserEventListenersTest method testTransactionProcessSynchronization.

@Test
@TestForIssue(jiraKey = "HHH-7478")
public void testTransactionProcessSynchronization() {
    final EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    final CountingPostInsertTransactionBoundaryListener listener = new CountingPostInsertTransactionBoundaryListener();
    registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(listener);
    Session session = openSession();
    session.getTransaction().begin();
    StrTestEntity entity = new StrTestEntity("str1");
    session.save(entity);
    session.getTransaction().commit();
    session.close();
    // Post insert listener invoked three times - beforeQuery/afterQuery insertion of original data,
    // revision entity and audit row.
    Assert.assertEquals(3, listener.getBeforeCount());
    Assert.assertEquals(3, listener.getAfterCount());
}
Also used : StrTestEntity(org.hibernate.envers.test.entities.StrTestEntity) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.

the class JaccEventListenerIntegrator method integrate.

@Override
@SuppressWarnings({ "unchecked" })
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    boolean isSecurityEnabled = configuration.getProperties().containsKey(AvailableSettings.JACC_ENABLED);
    if (!isSecurityEnabled) {
        return;
    }
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.addDuplicationStrategy(JACC_DUPLICATION_STRATEGY);
    final String jaccContextId = configuration.getProperty(Environment.JACC_CONTEXTID);
    eventListenerRegistry.prependListeners(EventType.PRE_DELETE, new JACCPreDeleteEventListener(jaccContextId));
    eventListenerRegistry.prependListeners(EventType.PRE_INSERT, new JACCPreInsertEventListener(jaccContextId));
    eventListenerRegistry.prependListeners(EventType.PRE_UPDATE, new JACCPreUpdateEventListener(jaccContextId));
    eventListenerRegistry.prependListeners(EventType.PRE_LOAD, new JACCPreLoadEventListener(jaccContextId));
}
Also used : JACCPreLoadEventListener(org.hibernate.secure.internal.JACCPreLoadEventListener) JACCPreDeleteEventListener(org.hibernate.secure.internal.JACCPreDeleteEventListener) JACCPreUpdateEventListener(org.hibernate.secure.internal.JACCPreUpdateEventListener) JACCPreInsertEventListener(org.hibernate.secure.internal.JACCPreInsertEventListener) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 15 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project dhis2-core by dhis2.

the class DeletedObjectIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EventListenerRegistry registry = serviceRegistry.getService(EventListenerRegistry.class);
    DeletedObjectPostDeleteEventListener listener = new DeletedObjectPostDeleteEventListener();
    registry.appendListeners(EventType.POST_DELETE, listener);
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)15 HibernateException (org.hibernate.HibernateException)3 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)3 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)3 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EntityManager (javax.persistence.EntityManager)2 Session (org.hibernate.Session)2 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)2 EventListenerGroup (org.hibernate.event.service.spi.EventListenerGroup)2 EventType (org.hibernate.event.spi.EventType)2 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)2 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)2 TestForIssue (org.hibernate.testing.TestForIssue)2 File (java.io.File)1 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1