Search in sources :

Example 1 with EventType

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

the class SessionFactoryImpl method applyCfgXmlValues.

private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) {
    final JaccService jaccService = serviceRegistry.getService(JaccService.class);
    if (jaccService.getContextId() != null) {
        final JaccPermissionDeclarations permissions = aggregatedConfig.getJaccPermissions(jaccService.getContextId());
        if (permissions != null) {
            for (GrantedPermission grantedPermission : permissions.getPermissionDeclarations()) {
                jaccService.addPermission(grantedPermission);
            }
        }
    }
    if (aggregatedConfig.getEventListenerMap() != null) {
        final ClassLoaderService cls = serviceRegistry.getService(ClassLoaderService.class);
        final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
        for (Map.Entry<EventType, Set<String>> entry : aggregatedConfig.getEventListenerMap().entrySet()) {
            final EventListenerGroup group = eventListenerRegistry.getEventListenerGroup(entry.getKey());
            for (String listenerClassName : entry.getValue()) {
                try {
                    group.appendListener(cls.classForName(listenerClassName).newInstance());
                } catch (Exception e) {
                    throw new ConfigurationException("Unable to instantiate event listener class : " + listenerClassName, e);
                }
            }
        }
    }
}
Also used : Set(java.util.Set) EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) EventType(org.hibernate.event.spi.EventType) JaccService(org.hibernate.secure.spi.JaccService) GrantedPermission(org.hibernate.secure.spi.GrantedPermission) InvalidObjectException(java.io.InvalidObjectException) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) ConfigurationException(org.hibernate.internal.util.config.ConfigurationException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) MappingException(org.hibernate.MappingException) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) JaccPermissionDeclarations(org.hibernate.secure.spi.JaccPermissionDeclarations) ConfigurationException(org.hibernate.internal.util.config.ConfigurationException) Map(java.util.Map) HashMap(java.util.HashMap) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 2 with EventType

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

the class SessionFactoryImpl method prepareEventListeners.

private void prepareEventListeners(MetadataImplementor metadata) {
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    eventListenerRegistry.prepare(metadata);
    for (Map.Entry entry : ((Map<?, ?>) cfgService.getSettings()).entrySet()) {
        if (!String.class.isInstance(entry.getKey())) {
            continue;
        }
        final String propertyName = (String) entry.getKey();
        if (!propertyName.startsWith(org.hibernate.jpa.AvailableSettings.EVENT_LISTENER_PREFIX)) {
            continue;
        }
        final String eventTypeName = propertyName.substring(org.hibernate.jpa.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, classLoaderService));
        }
    }
}
Also used : EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) EventType(org.hibernate.event.spi.EventType) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Map(java.util.Map) HashMap(java.util.HashMap) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 3 with EventType

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

the class LoadedConfig method consume.

/**
 * Consumes the JAXB representation of a {@code cfg.xml} file and builds the
 * LoadedConfig representation.
 *
 * @param jaxbCfg The JAXB representation of a {@code cfg.xml} file
 *
 * @return The parsed representation
 */
public static LoadedConfig consume(JaxbCfgHibernateConfiguration jaxbCfg) {
    final LoadedConfig cfg = new LoadedConfig(jaxbCfg.getSessionFactory().getName());
    for (JaxbCfgConfigPropertyType jaxbProperty : jaxbCfg.getSessionFactory().getProperty()) {
        cfg.addConfigurationValue(jaxbProperty.getName(), jaxbProperty.getValue());
    }
    for (JaxbCfgMappingReferenceType jaxbMapping : jaxbCfg.getSessionFactory().getMapping()) {
        cfg.addMappingReference(MappingReference.consume(jaxbMapping));
    }
    for (Object cacheDeclaration : jaxbCfg.getSessionFactory().getClassCacheOrCollectionCache()) {
        cfg.addCacheRegionDefinition(parseCacheRegionDefinition(cacheDeclaration));
    }
    if (jaxbCfg.getSecurity() != null) {
        for (JaxbCfgHibernateConfiguration.JaxbCfgSecurity.JaxbCfgGrant grant : jaxbCfg.getSecurity().getGrant()) {
            final JaccPermissionDeclarations jaccPermissions = cfg.getOrCreateJaccPermissions(jaxbCfg.getSecurity().getContext());
            jaccPermissions.addPermissionDeclaration(new GrantedPermission(grant.getRole(), grant.getEntityName(), grant.getActions()));
        }
    }
    if (!jaxbCfg.getSessionFactory().getListener().isEmpty()) {
        for (JaxbCfgEventListenerType listener : jaxbCfg.getSessionFactory().getListener()) {
            final EventType eventType = EventType.resolveEventTypeByName(listener.getType().value());
            cfg.addEventListener(eventType, listener.getClazz());
        }
    }
    if (!jaxbCfg.getSessionFactory().getEvent().isEmpty()) {
        for (JaxbCfgEventListenerGroupType listenerGroup : jaxbCfg.getSessionFactory().getEvent()) {
            if (listenerGroup.getListener().isEmpty()) {
                continue;
            }
            final String eventTypeName = listenerGroup.getType().value();
            final EventType eventType = EventType.resolveEventTypeByName(eventTypeName);
            for (JaxbCfgEventListenerType listener : listenerGroup.getListener()) {
                if (listener.getType() != null) {
                    log.debugf("Listener [%s] defined as part of a group also defined event type", listener.getClazz());
                }
                cfg.addEventListener(eventType, listener.getClazz());
            }
        }
    }
    return cfg;
}
Also used : JaxbCfgEventListenerType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgEventListenerType) JaccPermissionDeclarations(org.hibernate.secure.spi.JaccPermissionDeclarations) EventType(org.hibernate.event.spi.EventType) JaxbCfgConfigPropertyType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgConfigPropertyType) GrantedPermission(org.hibernate.secure.spi.GrantedPermission) JaxbCfgMappingReferenceType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgMappingReferenceType) JaxbCfgEventListenerGroupType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgEventListenerGroupType)

Example 4 with EventType

use of org.hibernate.event.spi.EventType 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)

Aggregations

EventType (org.hibernate.event.spi.EventType)4 Map (java.util.Map)3 EventListenerGroup (org.hibernate.event.service.spi.EventListenerGroup)3 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)3 HashMap (java.util.HashMap)2 HibernateException (org.hibernate.HibernateException)2 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)2 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)2 GrantedPermission (org.hibernate.secure.spi.GrantedPermission)2 JaccPermissionDeclarations (org.hibernate.secure.spi.JaccPermissionDeclarations)2 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 SQLException (java.sql.SQLException)1 Set (java.util.Set)1 PersistenceException (javax.persistence.PersistenceException)1 MappingException (org.hibernate.MappingException)1 ReflectionManager (org.hibernate.annotations.common.reflection.ReflectionManager)1 JaxbCfgConfigPropertyType (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgConfigPropertyType)1 JaxbCfgEventListenerGroupType (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgEventListenerGroupType)1 JaxbCfgEventListenerType (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgEventListenerType)1