Search in sources :

Example 36 with EventListenerRegistry

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

the class JpaIntegrator method integrate.

/**
	 * Perform integration.
	 *
	 * @param metadata The "compiled" representation of the mapping information
	 * @param sessionFactory The session factory being created
	 * @param serviceRegistry The session factory's service registry
	 */
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    // first, register the JPA-specific persist cascade style
    try {
        oldPersistCascadeStyle = CascadeStyles.getCascadeStyle("persist");
    } catch (Exception e) {
    }
    CascadeStyles.registerCascadeStyle("persist", new PersistCascadeStyle());
    // then prepare listeners
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.addDuplicationStrategy(JPA_DUPLICATION_STRATEGY);
    // op listeners
    eventListenerRegistry.setListeners(EventType.AUTO_FLUSH, JpaAutoFlushEventListener.INSTANCE);
    eventListenerRegistry.setListeners(EventType.DELETE, new JpaDeleteEventListener());
    eventListenerRegistry.setListeners(EventType.FLUSH_ENTITY, new JpaFlushEntityEventListener());
    eventListenerRegistry.setListeners(EventType.FLUSH, JpaFlushEventListener.INSTANCE);
    eventListenerRegistry.setListeners(EventType.MERGE, new JpaMergeEventListener());
    eventListenerRegistry.setListeners(EventType.PERSIST, new JpaPersistEventListener());
    eventListenerRegistry.setListeners(EventType.PERSIST_ONFLUSH, new JpaPersistOnFlushEventListener());
    eventListenerRegistry.setListeners(EventType.SAVE, new JpaSaveEventListener());
    eventListenerRegistry.setListeners(EventType.SAVE_UPDATE, new JpaSaveOrUpdateEventListener());
    // post op listeners
    eventListenerRegistry.prependListeners(EventType.POST_DELETE, new JpaPostDeleteEventListener());
    eventListenerRegistry.prependListeners(EventType.POST_INSERT, new JpaPostInsertEventListener());
    eventListenerRegistry.prependListeners(EventType.POST_LOAD, new JpaPostLoadEventListener());
    eventListenerRegistry.prependListeners(EventType.POST_UPDATE, new JpaPostUpdateEventListener());
    final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
    for (Map.Entry entry : ((Map<?, ?>) cfgService.getSettings()).entrySet()) {
        if (!String.class.isInstance(entry.getKey())) {
            continue;
        }
        final String propertyName = (String) entry.getKey();
        if (!propertyName.startsWith(AvailableSettings.EVENT_LISTENER_PREFIX)) {
            continue;
        }
        final String eventTypeName = propertyName.substring(AvailableSettings.EVENT_LISTENER_PREFIX.length() + 1);
        final EventType eventType = EventType.resolveEventTypeByName(eventTypeName);
        final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup(eventType);
        for (String listenerImpl : ((String) entry.getValue()).split(" ,")) {
            eventListenerGroup.appendListener(instantiate(listenerImpl, serviceRegistry));
        }
    }
    // handle JPA "entity listener classes"...
    final ReflectionManager reflectionManager = ((MetadataImpl) metadata).getMetadataBuildingOptions().getReflectionManager();
    this.callbackRegistry = new CallbackRegistryImpl();
    this.jpaListenerFactory = ListenerFactoryBuilder.buildListenerFactory(sessionFactory.getSessionFactoryOptions());
    this.callbackBuilder = new CallbackBuilderLegacyImpl(jpaListenerFactory, reflectionManager);
    for (PersistentClass persistentClass : metadata.getEntityBindings()) {
        if (persistentClass.getClassName() == null) {
            // we can have non java class persisted by hibernate
            continue;
        }
        callbackBuilder.buildCallbacksForEntity(persistentClass.getClassName(), callbackRegistry);
    }
    for (EventType eventType : EventType.values()) {
        final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup(eventType);
        for (Object listener : eventListenerGroup.listeners()) {
            if (CallbackRegistryConsumer.class.isInstance(listener)) {
                ((CallbackRegistryConsumer) listener).injectCallbackRegistry(callbackRegistry);
            }
        }
    }
}
Also used : JpaPersistEventListener(org.hibernate.jpa.event.internal.core.JpaPersistEventListener) EventType(org.hibernate.event.spi.EventType) JpaPostInsertEventListener(org.hibernate.jpa.event.internal.core.JpaPostInsertEventListener) CallbackRegistryConsumer(org.hibernate.jpa.event.spi.jpa.CallbackRegistryConsumer) CallbackBuilderLegacyImpl(org.hibernate.jpa.event.internal.jpa.CallbackBuilderLegacyImpl) JpaPostDeleteEventListener(org.hibernate.jpa.event.internal.core.JpaPostDeleteEventListener) JpaPostLoadEventListener(org.hibernate.jpa.event.internal.core.JpaPostLoadEventListener) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) JpaSaveEventListener(org.hibernate.jpa.event.internal.core.JpaSaveEventListener) PersistentClass(org.hibernate.mapping.PersistentClass) EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) JpaDeleteEventListener(org.hibernate.jpa.event.internal.core.JpaDeleteEventListener) JpaSaveOrUpdateEventListener(org.hibernate.jpa.event.internal.core.JpaSaveOrUpdateEventListener) HibernateException(org.hibernate.HibernateException) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) JpaPersistOnFlushEventListener(org.hibernate.jpa.event.internal.core.JpaPersistOnFlushEventListener) CallbackRegistryImpl(org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl) JpaPostUpdateEventListener(org.hibernate.jpa.event.internal.core.JpaPostUpdateEventListener) JpaFlushEntityEventListener(org.hibernate.jpa.event.internal.core.JpaFlushEntityEventListener) JpaMergeEventListener(org.hibernate.jpa.event.internal.core.JpaMergeEventListener) Map(java.util.Map)

Example 37 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 38 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 39 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)

Example 40 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project flytecnologia-api by jullierme.

the class FlyEnversIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    if (!enversService.isEnabled()) {
        return;
    }
    if (!enversService.isInitialized()) {
        throw new HibernateException("Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate");
    }
    final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
    if (enversService.getEntitiesConfigurations().hasAuditedEntities()) {
        listenerRegistry.appendListeners(EventType.PRE_UPDATE, new EnversPreUpdateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new EnversPreCollectionRemoveEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new EnversPreCollectionUpdateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_UPDATE, new FlyEnversPostUpdateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_DELETE, new FlyEnversPostDeleteEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_INSERT, new FlyEnversPostInsertEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new EnversPostCollectionRecreateEventListenerImpl(enversService));
    }
}
Also used : HibernateException(org.hibernate.HibernateException) EnversPreUpdateEventListenerImpl(org.hibernate.envers.event.spi.EnversPreUpdateEventListenerImpl) EnversPreCollectionRemoveEventListenerImpl(org.hibernate.envers.event.spi.EnversPreCollectionRemoveEventListenerImpl) EnversPreCollectionUpdateEventListenerImpl(org.hibernate.envers.event.spi.EnversPreCollectionUpdateEventListenerImpl) EnversService(org.hibernate.envers.boot.internal.EnversService) EnversPostCollectionRecreateEventListenerImpl(org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)63 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)13 Test (org.junit.Test)10 PostConstruct (javax.annotation.PostConstruct)9 EventType (org.hibernate.event.spi.EventType)9 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)8 TestForIssue (org.hibernate.testing.TestForIssue)8 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 HashMap (java.util.HashMap)6 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)6 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)6 Map (java.util.Map)5 EntityManager (javax.persistence.EntityManager)5 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 RevisionHistoryEventListener (com.gmoon.hibernateenvers.global.envers.listener.RevisionHistoryEventListener)4 EntityActionVetoException (org.hibernate.action.internal.EntityActionVetoException)4 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)4