From 8a0eeedec83b2427bcdf95303d9882da783f6e8d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 26 May 2011 13:48:38 -0700 Subject: [PATCH 1/3] HHH-6267 : Plumb MetadataImplementor into service initiators registered in SessionFactoryServiceRegistry --- .../EventListenerServiceInitiator.java | 8 ++++ .../internal/SessionFactoryImpl.java | 38 ++++++++++++++++--- ...sionFactoryServiceRegistryFactoryImpl.java | 9 +++++ .../SessionFactoryServiceRegistryImpl.java | 33 +++++++++++++++- .../spi/SessionFactoryServiceInitiator.java | 16 ++++++++ .../SessionFactoryServiceRegistryFactory.java | 17 +++++++++ .../stat/internal/StatisticsInitiator.java | 19 ++++++++++ 7 files changed, 133 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerServiceInitiator.java index 8717b6e600..b773609c9f 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerServiceInitiator.java @@ -26,6 +26,7 @@ package org.hibernate.event.service.internal; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.event.service.spi.EventListenerRegistry; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; @@ -50,4 +51,11 @@ public class EventListenerServiceInitiator implements SessionFactoryServiceIniti return new EventListenerRegistryImpl(); } + @Override + public EventListenerRegistry initiateService( + SessionFactoryImplementor sessionFactory, + MetadataImplementor metadata, + ServiceRegistryImplementor registry) { + return new EventListenerRegistryImpl(); + } } \ No newline at end of file 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 8de17b587f..f589c29f8a 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -75,6 +75,7 @@ import org.hibernate.cache.spi.UpdateTimestampsCache; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.cfg.Settings; @@ -123,6 +124,7 @@ import org.hibernate.pretty.MessageHelper; import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.service.ServiceRegistry; import org.hibernate.integrator.spi.IntegratorService; +import org.hibernate.service.config.spi.ConfigurationService; import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; import org.hibernate.service.jndi.spi.JndiService; import org.hibernate.service.jta.platform.spi.JtaPlatform; @@ -523,13 +525,9 @@ public final class SessionFactoryImpl this.namedQueries = null; this.namedSqlQueries = null; this.sqlResultSetMappings = null; - this.filters = null; this.fetchProfiles = null; this.imports = null; this.interceptor = null; - this.serviceRegistry = null; - this.settings = null; - this.properties = null; this.queryCache = null; this.updateTimestampsCache = null; this.queryCaches = null; @@ -537,10 +535,38 @@ public final class SessionFactoryImpl this.entityNotFoundDelegate = null; this.sqlFunctionRegistry = null; this.queryPlanCache = null; - this.typeResolver = null; - this.typeHelper = null; this.transactionEnvironment = null; + ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class ); + this.settings = null; + + this.serviceRegistry = serviceRegistry.getService( SessionFactoryServiceRegistryFactory.class ).buildServiceRegistry( + this, + metadata + ); + + // TODO: get Interceptor from ConfurationService + //this.interceptor = cfg.getInterceptor(); + + // TODO: find references to properties and make sure everything needed is available to services via + // ConfigurationService + this.properties = null; + + // TODO: should this be build along w/ metadata? seems like it should so app has more control over it... + //this.sqlFunctionRegistry = new SQLFunctionRegistry( getDialect(), metadata.getSqlFunctions() ); + if ( observer != null ) { + this.observer.addObserver( observer ); + } + + this.typeResolver = metadata.getTypeResolver().scope( this ); + this.typeHelper = new TypeLocatorImpl( typeResolver ); + + this.filters = new HashMap(); + this.filters.putAll( metadata.getFilterDefinitions() ); + + LOG.debugf("Session factory constructed with filter configurations : %s", filters); + LOG.debugf("Instantiating session factory with properties: %s", configurationService.getSettings() ); + // TODO: implement } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryFactoryImpl.java index 245eef1942..91c8a9c8a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryFactoryImpl.java @@ -25,6 +25,7 @@ package org.hibernate.service.internal; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.Service; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceRegistryFactory; @@ -48,4 +49,12 @@ public class SessionFactoryServiceRegistryFactoryImpl implements SessionFactoryS Configuration configuration) { return new SessionFactoryServiceRegistryImpl( theBasicServiceRegistry, sessionFactory, configuration ); } + + @Override + public SessionFactoryServiceRegistryImpl buildServiceRegistry( + SessionFactoryImplementor sessionFactory, + MetadataImplementor metadata) { + return new SessionFactoryServiceRegistryImpl( theBasicServiceRegistry, sessionFactory, metadata ); + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java index 45199c861d..2433ae1a48 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java @@ -25,6 +25,7 @@ package org.hibernate.service.internal; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.Service; import org.hibernate.service.StandardSessionFactoryServiceInitiators; import org.hibernate.service.spi.ServiceInitiator; @@ -40,6 +41,7 @@ public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryIm // for now we need to hold on to the Configuration... :( private Configuration configuration; + private MetadataImplementor metadata; private final SessionFactoryImplementor sessionFactory; @SuppressWarnings( {"unchecked"}) @@ -51,6 +53,25 @@ public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryIm this.sessionFactory = sessionFactory; this.configuration = configuration; + this.metadata = null; + + // for now, just use the standard initiator list + for ( SessionFactoryServiceInitiator initiator : StandardSessionFactoryServiceInitiators.LIST ) { + // create the bindings up front to help identify to which registry services belong + createServiceBinding( initiator ); + } + } + + @SuppressWarnings( {"unchecked"}) + public SessionFactoryServiceRegistryImpl( + ServiceRegistryImplementor parent, + SessionFactoryImplementor sessionFactory, + MetadataImplementor metadata) { + super( parent ); + + this.sessionFactory = sessionFactory; + this.configuration = null; + this.metadata = metadata; // for now, just use the standard initiator list for ( SessionFactoryServiceInitiator initiator : StandardSessionFactoryServiceInitiators.LIST ) { @@ -62,7 +83,17 @@ public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryIm @Override public R initiateService(ServiceInitiator serviceInitiator) { // todo : add check/error for unexpected initiator types? - return ( (SessionFactoryServiceInitiator) serviceInitiator ).initiateService( sessionFactory, configuration, this ); + SessionFactoryServiceInitiator sessionFactoryServiceInitiator = + (SessionFactoryServiceInitiator) serviceInitiator; + if ( metadata != null ) { + return sessionFactoryServiceInitiator.initiateService( sessionFactory, metadata, this ); + } + else if ( configuration != null ) { + return sessionFactoryServiceInitiator.initiateService( sessionFactory, configuration, this ); + } + else { + throw new IllegalStateException( "Both metadata and configuration are null." ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java index 8df4a41182..047ff3f90f 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java @@ -25,6 +25,7 @@ package org.hibernate.service.spi; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.Service; /** @@ -44,4 +45,19 @@ public interface SessionFactoryServiceInitiator extends Servi * @return The initiated service. */ public R initiateService(SessionFactoryImplementor sessionFactory, Configuration configuration, ServiceRegistryImplementor registry); + + /** + * Initiates the managed service. + *

+ * Note for implementors: signature is guaranteed to change once redesign of SessionFactory building is complete + * + * @param sessionFactory The session factory. Note the the session factory is still in flux; care needs to be taken + * in regards to what you call. + * @param metadata The configuration. + * @param registry The service registry. Can be used to locate services needed to fulfill initiation. + * + * @return The initiated service. + */ + public R initiateService(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata, ServiceRegistryImplementor registry); + } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistryFactory.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistryFactory.java index 800a3868f8..d73c8fcb43 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistryFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistryFactory.java @@ -25,6 +25,7 @@ package org.hibernate.service.spi; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.Service; import org.hibernate.service.internal.SessionFactoryServiceRegistryImpl; @@ -50,4 +51,20 @@ public interface SessionFactoryServiceRegistryFactory extends Service { public SessionFactoryServiceRegistryImpl buildServiceRegistry( SessionFactoryImplementor sessionFactory, Configuration configuration); + + /** + * Create the registry. + * + * @todo : fully expect this signature to change! + * + * @param sessionFactory The (in flux) session factory. Generally this is useful for grabbing a reference for later + * use. However, care should be taken when invoking on the session factory until after it has been fully + * initialized. + * @param metadata The configuration object. + * + * @return The registry + */ + public SessionFactoryServiceRegistryImpl buildServiceRegistry( + SessionFactoryImplementor sessionFactory, + MetadataImplementor metadata); } diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java index f6a81c9073..bceb032269 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java @@ -29,7 +29,9 @@ import org.hibernate.HibernateException; import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.classloading.spi.ClassLoaderService; +import org.hibernate.service.config.spi.ConfigurationService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; import org.hibernate.stat.spi.StatisticsFactory; @@ -60,6 +62,23 @@ public class StatisticsInitiator implements SessionFactoryServiceInitiator Date: Thu, 26 May 2011 15:23:05 -0700 Subject: [PATCH 2/3] HHH-6267 : Minor fixes for Plumb MetadataImplementor into service initiators --- .../main/java/org/hibernate/internal/SessionFactoryImpl.java | 3 +-- .../service/internal/SessionFactoryServiceRegistryImpl.java | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) 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 f589c29f8a..f356db973a 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -561,8 +561,7 @@ public final class SessionFactoryImpl this.typeResolver = metadata.getTypeResolver().scope( this ); this.typeHelper = new TypeLocatorImpl( typeResolver ); - this.filters = new HashMap(); - this.filters.putAll( metadata.getFilterDefinitions() ); + this.filters = new HashMap( metadata.getFilterDefinitions() ); LOG.debugf("Session factory constructed with filter configurations : %s", filters); LOG.debugf("Instantiating session factory with properties: %s", configurationService.getSettings() ); diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java index 2433ae1a48..38bb2053ef 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java @@ -40,8 +40,8 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry; public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryImpl implements SessionFactoryServiceRegistry { // for now we need to hold on to the Configuration... :( - private Configuration configuration; - private MetadataImplementor metadata; + private final Configuration configuration; + private final MetadataImplementor metadata; private final SessionFactoryImplementor sessionFactory; @SuppressWarnings( {"unchecked"}) From bd8e7e37f0a5182ea1132561bb38d76133846d8d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 26 May 2011 16:10:22 -0700 Subject: [PATCH 3/3] HHH-6267 : Minor fixes for Plumb MetadataImplementor into service initiators --- .../org/hibernate/internal/SessionFactoryImpl.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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 f356db973a..0f520a19f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -183,7 +183,7 @@ public final class SessionFactoryImpl private final transient Map namedQueries; private final transient Map namedSqlQueries; private final transient Map sqlResultSetMappings; - private final transient Map filters; + private final transient Map filters; private final transient Map fetchProfiles; private final transient Map imports; private final transient Interceptor interceptor; @@ -207,6 +207,7 @@ public final class SessionFactoryImpl private final transient TypeHelper typeHelper; private final transient TransactionEnvironment transactionEnvironment; + @SuppressWarnings( {"unchecked"} ) public SessionFactoryImpl( Configuration cfg, Mapping mapping, @@ -233,7 +234,7 @@ public final class SessionFactoryImpl this.typeResolver = cfg.getTypeResolver().scope( this ); this.typeHelper = new TypeLocatorImpl( typeResolver ); - this.filters = new HashMap(); + this.filters = new HashMap(); this.filters.putAll( cfg.getFilterDefinitions() ); LOG.debugf("Session factory constructed with filter configurations : %s", filters); @@ -561,7 +562,10 @@ public final class SessionFactoryImpl this.typeResolver = metadata.getTypeResolver().scope( this ); this.typeHelper = new TypeLocatorImpl( typeResolver ); - this.filters = new HashMap( metadata.getFilterDefinitions() ); + this.filters = new HashMap(); + for ( FilterDefinition filterDefinition : metadata.getFilterDefinitions() ) { + filters.put( filterDefinition.getFilterName(), filterDefinition ); + } LOG.debugf("Session factory constructed with filter configurations : %s", filters); LOG.debugf("Instantiating session factory with properties: %s", configurationService.getSettings() );