From 49fe4f649408dbe51013dc95d048a53aa1d6a8d0 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Thu, 28 Nov 2019 20:13:48 -0500 Subject: [PATCH] HHH-13726 Extract 'prepareEventListeners' method out of SessionFactoryImpl --- .../InFlightMetadataCollectorImpl.java | 9 ++++ .../hibernate/boot/internal/MetadataImpl.java | 50 ++++++++++++++++++- .../boot/spi/AbstractDelegatingMetadata.java | 8 ++- .../boot/spi/MetadataImplementor.java | 5 +- .../internal/SessionFactoryImpl.java | 43 +--------------- 5 files changed, 69 insertions(+), 46 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java index a8c97c3eaa..4a39a48957 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java @@ -79,6 +79,7 @@ import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.NamedSQLQueryDefinition; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; @@ -247,6 +248,14 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector return new HashSet<>( mappedSuperClasses.values() ); } + @Override + public void initSessionFactory(SessionFactoryImplementor sessionFactory) { + throw new UnsupportedOperationException( + "You should not be building a SessionFactory from an in-flight metadata collector; and of course " + + "we should better segment this in the API :)" + ); + } + @Override public IdentifierGeneratorFactory getIdentifierGeneratorFactory() { return identifierGeneratorFactory; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java index eaa0cd5119..d3b966adaa 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.regex.Pattern; import org.hibernate.HibernateException; import org.hibernate.MappingException; @@ -33,10 +34,14 @@ import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.cfg.annotations.NamedProcedureCallDefinition; import org.hibernate.dialect.function.SQLFunction; import org.hibernate.engine.ResultSetMappingDefinition; +import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.NamedSQLQueryDefinition; -import org.hibernate.id.factory.IdentifierGeneratorFactory; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.event.service.spi.EventListenerGroup; +import org.hibernate.event.service.spi.EventListenerRegistry; +import org.hibernate.event.spi.EventType; import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.mapping.Collection; @@ -47,6 +52,7 @@ import org.hibernate.mapping.Property; import org.hibernate.mapping.Table; import org.hibernate.procedure.ProcedureCallMemento; import org.hibernate.query.spi.NamedQueryRepository; +import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.type.Type; import org.hibernate.type.TypeResolver; import org.hibernate.type.spi.TypeConfiguration; @@ -59,6 +65,8 @@ import org.hibernate.type.spi.TypeConfiguration; * @author Gail Badner */ public class MetadataImpl implements MetadataImplementor, Serializable { + private static final Pattern LISTENER_SEPARATION_PATTERN = Pattern.compile( " ," ); + private final UUID uuid; private final MetadataBuildingOptions metadataBuildingOptions; private final BootstrapContext bootstrapContext; @@ -355,6 +363,46 @@ public class MetadataImpl implements MetadataImplementor, Serializable { : new HashSet<>( mappedSuperclassMap.values() ); } + @Override + public void initSessionFactory(SessionFactoryImplementor sessionFactory) { + final ServiceRegistryImplementor sessionFactoryServiceRegistry = sessionFactory.getServiceRegistry(); + + assert sessionFactoryServiceRegistry != null; + + final EventListenerRegistry eventListenerRegistry = sessionFactoryServiceRegistry.getService( EventListenerRegistry.class ); + final ConfigurationService cfgService = sessionFactoryServiceRegistry.getService( ConfigurationService.class ); + final ClassLoaderService classLoaderService = sessionFactoryServiceRegistry.getService( ClassLoaderService.class ); + + eventListenerRegistry.prepare( this ); + + 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 : LISTENER_SEPARATION_PATTERN.split( ( (String) entry.getValue() ) ) ) { + eventListenerGroup.appendListener( instantiate( listenerImpl, classLoaderService ) ); + } + } + } + + private Object instantiate(String listenerImpl, ClassLoaderService classLoaderService) { + try { + return classLoaderService.classForName( listenerImpl ).newInstance(); + } + catch (Exception e) { + throw new HibernateException( "Could not instantiate requested listener [" + listenerImpl + "]", e ); + } + } + @Override public org.hibernate.type.Type getIdentifierType(String entityName) throws MappingException { final PersistentClass pc = entityBindingMap.get( entityName ); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadata.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadata.java index 5fdbc749f0..7dba57d0d3 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadata.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadata.java @@ -17,7 +17,6 @@ import org.hibernate.boot.SessionFactoryBuilder; import org.hibernate.boot.model.IdentifierGeneratorDefinition; import org.hibernate.boot.model.TypeDefinition; import org.hibernate.boot.model.relational.Database; -import org.hibernate.cache.cfg.internal.DomainDataRegionConfigImpl; import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.cfg.annotations.NamedProcedureCallDefinition; import org.hibernate.dialect.function.SQLFunction; @@ -25,6 +24,7 @@ import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.NamedSQLQueryDefinition; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.mapping.FetchProfile; @@ -240,4 +240,10 @@ public abstract class AbstractDelegatingMetadata implements MetadataImplementor public Set getMappedSuperclassMappingsCopy() { return delegate.getMappedSuperclassMappingsCopy(); } + + @Override + public void initSessionFactory(SessionFactoryImplementor sessionFactory) { + delegate.initSessionFactory( sessionFactory ); + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java index ddc6af6fab..22d58fd6ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/MetadataImplementor.java @@ -6,13 +6,12 @@ */ package org.hibernate.boot.spi; -import java.util.Collection; import java.util.Set; import org.hibernate.MappingException; import org.hibernate.boot.Metadata; -import org.hibernate.cache.cfg.internal.DomainDataRegionConfigImpl; import org.hibernate.engine.spi.Mapping; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.mapping.MappedSuperclass; import org.hibernate.query.spi.NamedQueryRepository; @@ -59,4 +58,6 @@ public interface MetadataImplementor extends Metadata, Mapping { void validate() throws MappingException; Set getMappedSuperclassMappingsCopy(); + + void initSessionFactory(SessionFactoryImplementor sessionFactoryImplementor); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 3baeba0b7d..4eb8540b45 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -11,7 +11,6 @@ import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.sql.Connection; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -20,7 +19,6 @@ import java.util.Map; import java.util.Set; import java.util.TimeZone; import java.util.function.Supplier; -import java.util.regex.Pattern; import javax.naming.Reference; import javax.naming.StringRefAddr; import javax.persistence.EntityGraph; @@ -31,7 +29,6 @@ import javax.persistence.PersistenceUnitUtil; import javax.persistence.Query; import javax.persistence.SynchronizationType; import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.spi.PersistenceUnitTransactionType; import org.hibernate.ConnectionAcquisitionMode; import org.hibernate.ConnectionReleaseMode; @@ -82,7 +79,6 @@ import org.hibernate.engine.spi.NamedQueryDefinitionBuilder; import org.hibernate.engine.spi.NamedSQLQueryDefinition; import org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder; import org.hibernate.engine.spi.SessionBuilderImplementor; -import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionOwner; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; @@ -136,7 +132,6 @@ import org.jboss.logging.Logger; import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting; - /** * Concrete implementation of the SessionFactory interface. Has the following * responsibilities @@ -159,7 +154,6 @@ import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.det */ public final class SessionFactoryImpl implements SessionFactoryImplementor { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class ); - private static final Pattern LISTENER_SEPARATION_PATTERN = Pattern.compile( " ," ); private final String name; private final String uuid; @@ -214,7 +208,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { .getService( SessionFactoryServiceRegistryFactory.class ) .buildServiceRegistry( this, options ); - prepareEventListeners( metadata ); + metadata.initSessionFactory( this ); final CfgXmlAccessService cfgXmlAccessService = serviceRegistry.getService( CfgXmlAccessService.class ); @@ -410,41 +404,6 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { .connectionHandlingMode( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT ); } - 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 : LISTENER_SEPARATION_PATTERN.split( ( (String) entry.getValue() ) ) ) { - eventListenerGroup.appendListener( instantiate( listenerImpl, classLoaderService ) ); - } - } - } - - private Object instantiate(String listenerImpl, ClassLoaderService classLoaderService) { - try { - return classLoaderService.classForName( listenerImpl ).newInstance(); - } - catch (Exception e) { - throw new HibernateException( "Could not instantiate requested listener [" + listenerImpl + "]", e ); - } - } - private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) { final JaccService jaccService = serviceRegistry.getService( JaccService.class ); if ( jaccService.getContextId() != null ) {