From 7976e2396a8ac09c12ff34a3dafdb89115763f0f Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 29 Jan 2013 13:52:35 -0500 Subject: [PATCH] HHH-7580 - Complete 2-phase SessionFactory building design Conflicts: hibernate-core/src/main/java/org/hibernate/SessionFactory.java hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java hibernate-core/src/main/java/org/hibernate/metamodel/internal/SessionFactoryBuilderImpl.java hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java hibernate-core/src/test/java/org/hibernate/cfg/beanvalidation/ApplySchemaConstraintTest.java hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/AssertSourcesTest.java hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/AbstractBasicBindingTests.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/AbstractUnsavedValueTests.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/JoinedSubclassBindingTests.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/basiccollections/AbstractBasicCollectionBindingTests.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/onetomany/AbstractUnidirectionalOneToManyBindingTests.java hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/AbstractGeneratedIdColumnTests.java hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java hibernate-core/src/test/java/org/hibernate/test/service/schema/internal/ExistingDatabaseMetaDataImplTest.java hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderUsingMetamodelImpl.java hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java --- .../java/org/hibernate/SessionFactory.java | 7 +- .../BootstrapServiceRegistryBuilder.java | 53 +++-- .../registry/StandardServiceRegistry.java | 34 +++ .../StandardServiceRegistryBuilder.java | 40 ++-- .../internal/ClassLoaderServiceImpl.java | 222 +++++++++--------- .../internal/StandardServiceRegistryImpl.java | 4 +- .../org/hibernate/cfg/AvailableSettings.java | 17 ++ .../java/org/hibernate/cfg/Configuration.java | 2 +- .../java/org/hibernate/cfg/Environment.java | 2 +- .../java/org/hibernate/cfg/HbmBinder.java | 2 +- .../internal/SessionFactoryImpl.java | 28 ++- .../org/hibernate/metamodel/Metadata.java | 3 + .../hibernate/metamodel/MetadataBuilder.java | 2 +- .../hibernate/metamodel/MetadataSources.java | 57 ++++- .../metamodel/SessionFactoryBuilder.java | 7 +- .../metamodel/source/internal/JaxbHelper.java | 12 +- .../source/internal/MetadataBuilderImpl.java | 57 ++++- .../source/internal/MetadataImpl.java | 17 +- .../internal/SessionFactoryBuilderImpl.java | 15 +- .../service/ServiceRegistryBuilder.java | 6 +- .../service/spi/ServiceContributor.java | 40 ++++ .../ManagedProviderConnectionHelper.java | 2 +- .../hibernate/tool/hbm2ddl/SchemaExport.java | 2 +- .../hibernate/tool/hbm2ddl/SchemaUpdate.java | 2 +- .../tool/hbm2ddl/SchemaValidator.java | 2 +- .../binding/AbstractBasicBindingTests.java | 2 +- .../binding/BasicCollectionBindingTests.java | 4 +- .../entity/BaseAnnotationBindingTestCase.java | 23 +- .../entity/IdentifierGeneratorTest.java | 2 +- .../source/annotations/entity/MapsIdTest.java | 2 +- .../global/FetchProfileBinderTest.java | 2 +- .../annotations/global/QueryBinderTest.java | 2 +- .../util/BaseAnnotationIndexTestCase.java | 2 +- .../annotations/util/JandexHelperTest.java | 2 +- .../annotations/xml/OrmXmlParserTests.java | 12 +- .../source/internal/MetadataImplTest.java | 26 +- .../SessionFactoryBuilderImplTest.java | 6 +- .../embeddables/EmbeddableIntegratorTest.java | 4 +- .../persister/PersisterClassProviderTest.java | 8 +- .../test/jdbc/internal/BatchingTest.java | 1 - .../ConfigurationValidationTest.java | 4 +- .../schema/SchemaBasedMultiTenancyTest.java | 2 +- .../service/ServiceBootstrappingTest.java | 9 +- .../SessionFactoryBuildingUseCasesTest.java | 144 ++++++++++++ .../transaction/jdbc/TestExpectedUsage.java | 2 +- .../transaction/jta/BasicDrivingTest.java | 2 +- .../transaction/jta/ManagedDrivingTest.java | 2 +- .../EntityManagerFactoryBuilderImpl.java | 51 ++-- .../AbstractGeneralDataRegionTestCase.java | 8 +- .../cache/infinispan/NodeEnvironment.java | 4 +- .../query/QueryRegionImplTestCase.java | 4 +- .../TimestampsRegionImplTestCase.java | 4 +- .../testing/ServiceRegistryBuilder.java | 2 +- .../junit4/BaseCoreFunctionalTestCase.java | 91 ++++--- 54 files changed, 711 insertions(+), 351 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistry.java create mode 100644 hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/smoke/SessionFactoryBuildingUseCasesTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java index a4020d905f..decc751ba4 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionFactory.java @@ -27,8 +27,10 @@ import java.io.Serializable; import java.sql.Connection; import java.util.Map; import java.util.Set; + import javax.naming.Referenceable; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.CollectionMetadata; @@ -54,8 +56,9 @@ import org.hibernate.stat.Statistics; public interface SessionFactory extends Referenceable, Serializable { public interface SessionFactoryOptions { - Interceptor getInterceptor(); - EntityNotFoundDelegate getEntityNotFoundDelegate(); + public StandardServiceRegistry getServiceRegistry(); + public Interceptor getInterceptor(); + public EntityNotFoundDelegate getEntityNotFoundDelegate(); } public SessionFactoryOptions getSessionFactoryOptions(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java index c8a97c0660..eb7685f634 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java @@ -23,7 +23,9 @@ */ package org.hibernate.boot.registry; +import java.util.ArrayList; import java.util.LinkedHashSet; +import java.util.List; import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl; import org.hibernate.boot.registry.selector.Availability; @@ -43,10 +45,7 @@ import org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder; */ public class BootstrapServiceRegistryBuilder { private final LinkedHashSet providedIntegrators = new LinkedHashSet(); - private ClassLoader applicationClassLoader; - private ClassLoader resourcesClassLoader; - private ClassLoader hibernateClassLoader; - private ClassLoader environmentClassLoader; + private List providedClassLoaders; private StrategySelectorBuilder strategySelectorBuilder = new StrategySelectorBuilder(); @@ -61,16 +60,33 @@ public class BootstrapServiceRegistryBuilder { return this; } + /** + * Adds a provided {@link ClassLoader} for use in class-loading and resource-lookup + * + * @param classLoader The class loader to use + * + * @return {@code this}, for method chaining + */ + public BootstrapServiceRegistryBuilder with(ClassLoader classLoader) { + if ( providedClassLoaders == null ) { + providedClassLoaders = new ArrayList(); + } + providedClassLoaders.add( classLoader ); + return this; + } + /** * Applies the specified {@link ClassLoader} as the application class loader for the bootstrap registry * * @param classLoader The class loader to use * @return {@code this}, for method chaining + * + * @deprecated Use {@link #with(ClassLoader)} instead */ + @Deprecated @SuppressWarnings( {"UnusedDeclaration"}) public BootstrapServiceRegistryBuilder withApplicationClassLoader(ClassLoader classLoader) { - this.applicationClassLoader = classLoader; - return this; + return with( classLoader ); } /** @@ -78,11 +94,13 @@ public class BootstrapServiceRegistryBuilder { * * @param classLoader The class loader to use * @return {@code this}, for method chaining + * + * @deprecated Use {@link #with(ClassLoader)} instead */ + @Deprecated @SuppressWarnings( {"UnusedDeclaration"}) public BootstrapServiceRegistryBuilder withResourceClassLoader(ClassLoader classLoader) { - this.resourcesClassLoader = classLoader; - return this; + return with( classLoader ); } /** @@ -90,11 +108,13 @@ public class BootstrapServiceRegistryBuilder { * * @param classLoader The class loader to use * @return {@code this}, for method chaining + * + * @deprecated Use {@link #with(ClassLoader)} instead */ + @Deprecated @SuppressWarnings( {"UnusedDeclaration"}) public BootstrapServiceRegistryBuilder withHibernateClassLoader(ClassLoader classLoader) { - this.hibernateClassLoader = classLoader; - return this; + return with( classLoader ); } /** @@ -102,11 +122,13 @@ public class BootstrapServiceRegistryBuilder { * * @param classLoader The class loader to use * @return {@code this}, for method chaining + * + * @deprecated Use {@link #with(ClassLoader)} instead */ + @Deprecated @SuppressWarnings( {"UnusedDeclaration"}) public BootstrapServiceRegistryBuilder withEnvironmentClassLoader(ClassLoader classLoader) { - this.environmentClassLoader = classLoader; - return this; + return with( classLoader ); } /** @@ -149,12 +171,7 @@ public class BootstrapServiceRegistryBuilder { * @return The built bootstrap registry */ public BootstrapServiceRegistry build() { - final ClassLoaderServiceImpl classLoaderService = new ClassLoaderServiceImpl( - applicationClassLoader, - resourcesClassLoader, - hibernateClassLoader, - environmentClassLoader - ); + final ClassLoaderServiceImpl classLoaderService = new ClassLoaderServiceImpl( providedClassLoaders ); final IntegratorServiceImpl integratorService = new IntegratorServiceImpl( providedIntegrators, diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistry.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistry.java new file mode 100644 index 0000000000..67f1ba97ab --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistry.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.boot.registry; + +import org.hibernate.service.ServiceRegistry; + +/** + * Specialization of the {@link org.hibernate.service.ServiceRegistry} contract mainly for type safety. + * + * @author Steve Ebersole + */ +public interface StandardServiceRegistry extends ServiceRegistry { +} diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java index 926c46613d..869455622f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java @@ -25,23 +25,24 @@ package org.hibernate.boot.registry; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.Environment; import org.hibernate.integrator.spi.Integrator; import org.hibernate.integrator.spi.IntegratorService; -import org.hibernate.integrator.spi.ServiceContributingIntegrator; import org.hibernate.internal.jaxb.cfg.JaxbHibernateConfiguration; import org.hibernate.internal.util.config.ConfigurationHelper; -import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl; import org.hibernate.service.ConfigLoader; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.StandardServiceInitiators; import org.hibernate.service.internal.ProvidedService; -import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; +import org.hibernate.service.spi.ServiceContributor; /** * Builder for standard {@link org.hibernate.service.ServiceRegistry} instances. @@ -65,7 +66,7 @@ public class StandardServiceRegistryBuilder { * Create a default builder */ public StandardServiceRegistryBuilder() { - this( new BootstrapServiceRegistryImpl() ); + this( new BootstrapServiceRegistryBuilder().build() ); } /** @@ -197,26 +198,35 @@ public class StandardServiceRegistryBuilder { return this; } - /** - * Build the service registry accounting for all settings and service initiators and services. - * - * @return The built service registry - */ - public ServiceRegistry buildServiceRegistry() { + public StandardServiceRegistry build() { Map settingsCopy = new HashMap(); settingsCopy.putAll( settings ); Environment.verifyProperties( settingsCopy ); ConfigurationHelper.resolvePlaceHolders( settingsCopy ); - for ( Integrator integrator : bootstrapServiceRegistry.getService( IntegratorService.class ).getIntegrators() ) { - if ( ServiceContributingIntegrator.class.isInstance( integrator ) ) { - ServiceContributingIntegrator.class.cast( integrator ).prepareServices( this ); - } - } + applyServiceContributingIntegrators(); + applyServiceContributors(); return new StandardServiceRegistryImpl( bootstrapServiceRegistry, initiators, providedServices, settingsCopy ); } + @SuppressWarnings("deprecation") + private void applyServiceContributingIntegrators() { + for ( Integrator integrator : bootstrapServiceRegistry.getService( IntegratorService.class ).getIntegrators() ) { + if ( org.hibernate.integrator.spi.ServiceContributingIntegrator.class.isInstance( integrator ) ) { + org.hibernate.integrator.spi.ServiceContributingIntegrator.class.cast( integrator ).prepareServices( this ); + } + } + } + + private void applyServiceContributors() { + LinkedHashSet serviceContributors = + bootstrapServiceRegistry.getService( ClassLoaderService.class ).loadJavaServices( ServiceContributor.class ); + for ( ServiceContributor serviceContributor : serviceContributors ) { + serviceContributor.contribute( this ); + } + } + /** * Temporarily exposed since Configuration is still around and much code still uses Configuration. This allows * code to configure the builder and access that to configure Configuration object (used from HEM atm). diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java index bf2b37a391..dc2b5c301b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java @@ -27,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; @@ -49,75 +51,71 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; public class ClassLoaderServiceImpl implements ClassLoaderService { private static final Logger log = Logger.getLogger( ClassLoaderServiceImpl.class ); - private final ClassLoader classClassLoader; - private final ClassLoader resourcesClassLoader; - private final ClassLoader serviceLoaderClassLoader; + private final ClassLoader aggregatedClassLoader; public ClassLoaderServiceImpl() { this( ClassLoaderServiceImpl.class.getClassLoader() ); } public ClassLoaderServiceImpl(ClassLoader classLoader) { - this( classLoader, classLoader, classLoader, classLoader ); + this( Collections.singletonList( classLoader ) ); } - public ClassLoaderServiceImpl( - ClassLoader applicationClassLoader, - ClassLoader resourcesClassLoader, - ClassLoader hibernateClassLoader, - ClassLoader environmentClassLoader) { - // Normalize missing loaders - if ( hibernateClassLoader == null ) { - hibernateClassLoader = ClassLoaderServiceImpl.class.getClassLoader(); - } + public ClassLoaderServiceImpl(List providedClassLoaders) { + final LinkedHashSet orderedClassLoaderSet = new LinkedHashSet(); - if ( environmentClassLoader == null || applicationClassLoader == null ) { - ClassLoader sysClassLoader = locateSystemClassLoader(); - ClassLoader tccl = locateTCCL(); - if ( environmentClassLoader == null ) { - environmentClassLoader = sysClassLoader != null ? sysClassLoader : hibernateClassLoader; - } - if ( applicationClassLoader == null ) { - applicationClassLoader = tccl != null ? tccl : hibernateClassLoader; - } - } - - if ( resourcesClassLoader == null ) { - resourcesClassLoader = applicationClassLoader; - } - - final LinkedHashSet classLoadingClassLoaders = new LinkedHashSet(); - classLoadingClassLoaders.add( applicationClassLoader ); - classLoadingClassLoaders.add( hibernateClassLoader ); - classLoadingClassLoaders.add( environmentClassLoader ); - - this.classClassLoader = new ClassLoader(null) { - @Override - protected Class findClass(String name) throws ClassNotFoundException { - for ( ClassLoader loader : classLoadingClassLoaders ) { - try { - return loader.loadClass( name ); - } - catch (Exception ignore) { - } + // first add all provided class loaders, if any + if ( providedClassLoaders != null ) { + for ( ClassLoader classLoader : providedClassLoaders ) { + if ( classLoader != null ) { + orderedClassLoaderSet.add( classLoader ); } - throw new ClassNotFoundException( "Could not load requested class : " + name ); } - }; + } - this.resourcesClassLoader = resourcesClassLoader; + // normalize adding known class-loaders... + // first the Hibernate class loader + orderedClassLoaderSet.add( ClassLoaderServiceImpl.class.getClassLoader() ); + // then the TCCL, if one... + final ClassLoader tccl = locateTCCL(); + if ( tccl != null ) { + orderedClassLoaderSet.add( tccl ); + } + // finally the system classloader + final ClassLoader sysClassLoader = locateSystemClassLoader(); + if ( sysClassLoader != null ) { + orderedClassLoaderSet.add( sysClassLoader ); + } - this.serviceLoaderClassLoader = buildServiceLoaderClassLoader(); + // now build the aggregated class loader... + this.aggregatedClassLoader = new AggregatedClassLoader( orderedClassLoaderSet ); } - @SuppressWarnings( {"UnusedDeclaration"}) + @SuppressWarnings({"UnusedDeclaration", "unchecked", "deprecation"}) + @Deprecated public static ClassLoaderServiceImpl fromConfigSettings(Map configVales) { - return new ClassLoaderServiceImpl( - (ClassLoader) configVales.get( AvailableSettings.APP_CLASSLOADER ), - (ClassLoader) configVales.get( AvailableSettings.RESOURCES_CLASSLOADER ), - (ClassLoader) configVales.get( AvailableSettings.HIBERNATE_CLASSLOADER ), - (ClassLoader) configVales.get( AvailableSettings.ENVIRONMENT_CLASSLOADER ) - ); + final List providedClassLoaders = new ArrayList(); + + final Collection classLoaders = (Collection) configVales.get( AvailableSettings.CLASSLOADERS ); + if ( classLoaders != null ) { + for ( ClassLoader classLoader : classLoaders ) { + providedClassLoaders.add( classLoader ); + } + } + + addIfSet( providedClassLoaders, AvailableSettings.APP_CLASSLOADER, configVales ); + addIfSet( providedClassLoaders, AvailableSettings.RESOURCES_CLASSLOADER, configVales ); + addIfSet( providedClassLoaders, AvailableSettings.HIBERNATE_CLASSLOADER, configVales ); + addIfSet( providedClassLoaders, AvailableSettings.ENVIRONMENT_CLASSLOADER, configVales ); + + return new ClassLoaderServiceImpl( providedClassLoaders ); + } + + private static void addIfSet(List providedClassLoaders, String name, Map configVales) { + final ClassLoader providedClassLoader = (ClassLoader) configVales.get( name ); + if ( providedClassLoader != null ) { + providedClassLoaders.add( providedClassLoader ); + } } private static ClassLoader locateSystemClassLoader() { @@ -138,73 +136,69 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { } } - private ClassLoader buildServiceLoaderClassLoader() { - return new ClassLoader(null) { - final ClassLoader[] classLoaderArray = new ClassLoader[] { - // first look on the hibernate class loader - getClass().getClassLoader(), - // next look on the resource class loader - resourcesClassLoader, - // finally look on the combined class class loader - classClassLoader + private static class AggregatedClassLoader extends ClassLoader { + private final ClassLoader[] individualClassLoaders; + + private AggregatedClassLoader(final LinkedHashSet orderedClassLoaderSet) { + super( null ); + individualClassLoaders = orderedClassLoaderSet.toArray( new ClassLoader[ orderedClassLoaderSet.size() ] ); + } + + @Override + public Enumeration getResources(String name) throws IOException { + final HashSet resourceUrls = new HashSet(); + + for ( ClassLoader classLoader : individualClassLoaders ) { + final Enumeration urls = classLoader.getResources( name ); + while ( urls.hasMoreElements() ) { + resourceUrls.add( urls.nextElement() ); + } + } + + return new Enumeration() { + final Iterator resourceUrlIterator = resourceUrls.iterator(); + @Override + public boolean hasMoreElements() { + return resourceUrlIterator.hasNext(); + } + + @Override + public URL nextElement() { + return resourceUrlIterator.next(); + } }; + } - @Override - public Enumeration getResources(String name) throws IOException { - final HashSet resourceUrls = new HashSet(); - - for ( ClassLoader classLoader : classLoaderArray ) { - final Enumeration urls = classLoader.getResources( name ); - while ( urls.hasMoreElements() ) { - resourceUrls.add( urls.nextElement() ); - } + @Override + protected URL findResource(String name) { + for ( ClassLoader classLoader : individualClassLoaders ) { + final URL resource = classLoader.getResource( name ); + if ( resource != null ) { + return resource; } + } + return super.findResource( name ); + } - return new Enumeration() { - final Iterator resourceUrlIterator = resourceUrls.iterator(); - @Override - public boolean hasMoreElements() { - return resourceUrlIterator.hasNext(); - } - - @Override - public URL nextElement() { - return resourceUrlIterator.next(); - } - }; + @Override + protected Class findClass(String name) throws ClassNotFoundException { + for ( ClassLoader classLoader : individualClassLoaders ) { + try { + return classLoader.loadClass( name ); + } + catch (Exception ignore) { + } } - @Override - protected URL findResource(String name) { - for ( ClassLoader classLoader : classLoaderArray ) { - final URL resource = classLoader.getResource( name ); - if ( resource != null ) { - return resource; - } - } - return super.findResource( name ); - } - - @Override - protected Class findClass(String name) throws ClassNotFoundException { - for ( ClassLoader classLoader : classLoaderArray ) { - try { - return classLoader.loadClass( name ); - } - catch (Exception ignore) { - } - } - - throw new ClassNotFoundException( "Could not load requested class : " + name ); - } - }; + throw new ClassNotFoundException( "Could not load requested class : " + name ); + } } @Override @SuppressWarnings( {"unchecked"}) public Class classForName(String className) { try { - return (Class) Class.forName( className, true, classClassLoader ); + return (Class) Class.forName( className, true, aggregatedClassLoader ); } catch (Exception e) { throw new ClassLoadingException( "Unable to load class [" + className + "]", e ); @@ -221,7 +215,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { } try { - return resourcesClassLoader.getResource( name ); + return aggregatedClassLoader.getResource( name ); } catch ( Exception ignore ) { } @@ -241,7 +235,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { try { log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", name ); - InputStream stream = resourcesClassLoader.getResourceAsStream( name ); + InputStream stream = aggregatedClassLoader.getResourceAsStream( name ); if ( stream != null ) { return stream; } @@ -261,7 +255,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { try { log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", stripped ); - InputStream stream = resourcesClassLoader.getResourceAsStream( stripped ); + InputStream stream = aggregatedClassLoader.getResourceAsStream( stripped ); if ( stream != null ) { return stream; } @@ -277,7 +271,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { public List locateResources(String name) { ArrayList urls = new ArrayList(); try { - Enumeration urlEnumeration = resourcesClassLoader.getResources( name ); + Enumeration urlEnumeration = aggregatedClassLoader.getResources( name ); if ( urlEnumeration != null && urlEnumeration.hasMoreElements() ) { while ( urlEnumeration.hasMoreElements() ) { urls.add( urlEnumeration.nextElement() ); @@ -292,7 +286,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { @Override public LinkedHashSet loadJavaServices(Class serviceContract) { - final ServiceLoader loader = ServiceLoader.load( serviceContract, serviceLoaderClassLoader ); + final ServiceLoader loader = ServiceLoader.load( serviceContract, aggregatedClassLoader ); final LinkedHashSet services = new LinkedHashSet(); for ( S service : loader ) { services.add( service ); @@ -315,7 +309,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { boolean set = false; try { - Thread.currentThread().setContextClassLoader( serviceLoaderClassLoader); + Thread.currentThread().setContextClassLoader( aggregatedClassLoader ); set = true; } catch (Exception ignore) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/internal/StandardServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/internal/StandardServiceRegistryImpl.java index 23717955bc..966f2bebd9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/internal/StandardServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/internal/StandardServiceRegistryImpl.java @@ -28,8 +28,8 @@ import java.util.Map; import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.service.Service; -import org.hibernate.service.ServiceRegistry; import org.hibernate.service.internal.AbstractServiceRegistryImpl; import org.hibernate.service.internal.ProvidedService; import org.hibernate.service.spi.Configurable; @@ -41,7 +41,7 @@ import org.hibernate.service.spi.ServiceInitiator; * * @author Steve Ebersole */ -public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl implements ServiceRegistry { +public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl implements StandardServiceRegistry { private final Map configurationValues; @SuppressWarnings( {"unchecked"}) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 6174541e47..10b4a7f515 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -477,30 +477,47 @@ public interface AvailableSettings { */ public static final String NON_CONTEXTUAL_LOB_CREATION = "hibernate.jdbc.lob.non_contextual_creation"; + /** + * Used to define a {@link java.util.Collection} of the {@link ClassLoader} instances Hibernate should use for + * class-loading and resource-lookups. + * + * @since 5.0 + */ + public static final String CLASSLOADERS = "hibernate.classLoaders"; + /** * Names the {@link ClassLoader} used to load user application classes. * @since 4.0 + * + * @deprecated Use {@link #CLASSLOADERS} instead */ + @Deprecated public static final String APP_CLASSLOADER = "hibernate.classLoader.application"; /** * Names the {@link ClassLoader} Hibernate should use to perform resource loading. * @since 4.0 + * @deprecated Use {@link #CLASSLOADERS} instead */ + @Deprecated public static final String RESOURCES_CLASSLOADER = "hibernate.classLoader.resources"; /** * Names the {@link ClassLoader} responsible for loading Hibernate classes. By default this is * the {@link ClassLoader} that loaded this class. * @since 4.0 + * @deprecated Use {@link #CLASSLOADERS} instead */ + @Deprecated public static final String HIBERNATE_CLASSLOADER = "hibernate.classLoader.hibernate"; /** * Names the {@link ClassLoader} used when Hibernate is unable to locates classes on the * {@link #APP_CLASSLOADER} or {@link #HIBERNATE_CLASSLOADER}. * @since 4.0 + * @deprecated Use {@link #CLASSLOADERS} instead */ + @Deprecated public static final String ENVIRONMENT_CLASSLOADER = "hibernate.classLoader.environment"; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index 1671f0d0f9..fdc0c029a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -1768,7 +1768,7 @@ public class Configuration implements Serializable { ConfigurationHelper.resolvePlaceHolders( properties ); final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( properties ) - .buildServiceRegistry(); + .build(); setSessionFactoryObserver( new SessionFactoryObserver() { @Override diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java index 8cc6667472..677df04ed4 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java @@ -66,7 +66,7 @@ import org.hibernate.internal.util.config.ConfigurationHelper; * Properties may be either be System properties, properties * defined in a resource named /hibernate.properties or an instance of * java.util.Properties passed to - * Configuration.buildSessionFactory()
+ * Configuration.build()
*
* * diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java index 624552cacb..3d0a9e6db3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java @@ -3040,7 +3040,7 @@ public final class HbmBinder { //TODO: bad implementation, cos it depends upon ordering of mapping doc // fixing this requires that Collection/PersistentClass gain access // to the Mappings reference from Configuration (or the filterDefinitions - // map directly) sometime during Configuration.buildSessionFactory + // map directly) sometime during Configuration.build // (after all the types/filter-defs are known and before building // persisters). if ( StringHelper.isEmpty(condition) ) { 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 88d7c1ff5d..1de346d488 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -23,8 +23,6 @@ */ package org.hibernate.internal; -import javax.naming.Reference; -import javax.naming.StringRefAddr; import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectInputStream; @@ -42,7 +40,8 @@ import java.util.Properties; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.jboss.logging.Logger; +import javax.naming.Reference; +import javax.naming.StringRefAddr; import org.hibernate.AssertionFailure; import org.hibernate.Cache; @@ -63,6 +62,9 @@ import org.hibernate.SessionFactoryObserver; import org.hibernate.StatelessSession; import org.hibernate.StatelessSessionBuilder; import org.hibernate.TypeHelper; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.EntityRegion; @@ -90,10 +92,13 @@ import org.hibernate.dialect.Dialect; import org.hibernate.dialect.function.SQLFunction; import org.hibernate.dialect.function.SQLFunctionRegistry; import org.hibernate.engine.ResultSetMappingDefinition; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; +import org.hibernate.engine.jndi.spi.JndiService; import org.hibernate.engine.profile.Association; import org.hibernate.engine.profile.Fetch; import org.hibernate.engine.profile.FetchProfile; @@ -108,6 +113,7 @@ import org.hibernate.engine.spi.SessionBuilderImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionOwner; import org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl; +import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; import org.hibernate.engine.transaction.spi.TransactionEnvironment; import org.hibernate.engine.transaction.spi.TransactionFactory; import org.hibernate.exception.spi.SQLExceptionConverter; @@ -131,12 +137,6 @@ import org.hibernate.persister.entity.Queryable; import org.hibernate.persister.spi.PersisterFactory; import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.service.ServiceRegistry; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; -import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; -import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; -import org.hibernate.engine.jndi.spi.JndiService; -import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceRegistry; import org.hibernate.service.spi.SessionFactoryServiceRegistryFactory; @@ -150,6 +150,7 @@ import org.hibernate.tuple.entity.EntityTuplizer; import org.hibernate.type.AssociationType; import org.hibernate.type.Type; import org.hibernate.type.TypeResolver; +import org.jboss.logging.Logger; /** @@ -220,7 +221,7 @@ public final class SessionFactoryImpl public SessionFactoryImpl( final Configuration cfg, Mapping mapping, - ServiceRegistry serviceRegistry, + final ServiceRegistry serviceRegistry, Settings settings, SessionFactoryObserver observer) throws HibernateException { LOG.debug( "Building session factory" ); @@ -228,6 +229,11 @@ public final class SessionFactoryImpl sessionFactoryOptions = new SessionFactoryOptions() { private EntityNotFoundDelegate entityNotFoundDelegate; + @Override + public StandardServiceRegistry getServiceRegistry() { + return (StandardServiceRegistry) serviceRegistry; + } + @Override public Interceptor getInterceptor() { return cfg.getInterceptor(); @@ -666,7 +672,7 @@ public final class SessionFactoryImpl ); this.serviceRegistry = - metadata.getServiceRegistry() + sessionFactoryOptions.getServiceRegistry() .getService( SessionFactoryServiceRegistryFactory.class ) .buildServiceRegistry( this, metadata ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java index 0313e266ce..ce58902d7d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java @@ -28,6 +28,7 @@ import java.util.Map; import javax.persistence.SharedCacheMode; import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.NamingStrategy; import org.hibernate.engine.ResultSetMappingDefinition; @@ -48,6 +49,8 @@ public interface Metadata { * Exposes the options used to produce a {@link Metadata} instance. */ public static interface Options { + public StandardServiceRegistry getServiceRegistry(); + public MetadataSourceProcessingOrder getMetadataSourceProcessingOrder(); public NamingStrategy getNamingStrategy(); public SharedCacheMode getSharedCacheMode(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java index d8d9351afc..1295a80923 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java @@ -43,5 +43,5 @@ public interface MetadataBuilder { public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled); - public Metadata buildMetadata(); + public Metadata build(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java index ba0b78e69c..c8a3db5ccd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java @@ -36,10 +36,9 @@ import java.util.List; import java.util.jar.JarFile; import java.util.zip.ZipEntry; -import org.jboss.logging.Logger; -import org.w3c.dom.Document; -import org.xml.sax.EntityResolver; - +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cfg.EJB3DTDEntityResolver; import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.NamingStrategy; @@ -51,7 +50,9 @@ import org.hibernate.metamodel.source.MappingNotFoundException; import org.hibernate.metamodel.source.internal.JaxbHelper; import org.hibernate.metamodel.source.internal.MetadataBuilderImpl; import org.hibernate.service.ServiceRegistry; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.jboss.logging.Logger; +import org.w3c.dom.Document; +import org.xml.sax.EntityResolver; /** * @author Steve Ebersole @@ -59,13 +60,14 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; public class MetadataSources { private static final Logger LOG = Logger.getLogger( MetadataSources.class ); + private final ServiceRegistry serviceRegistry; + private List jaxbRootList = new ArrayList(); private LinkedHashSet> annotatedClasses = new LinkedHashSet>(); private LinkedHashSet annotatedPackages = new LinkedHashSet(); private final JaxbHelper jaxbHelper; - private final ServiceRegistry serviceRegistry; private final EntityResolver entityResolver; private final NamingStrategy namingStrategy; @@ -82,6 +84,20 @@ public class MetadataSources { this.jaxbHelper = new JaxbHelper( this ); this.metadataBuilder = new MetadataBuilderImpl( this ); + + // service registry really should be either BootstrapServiceRegistry or StandardServiceRegistry type... + if ( ! isExpectedServiceRegistryType( serviceRegistry ) ) { + LOG.debugf( + "Unexpected ServiceRegistry type [%s] encountered during building of MetadataSources; may cause " + + "problems later attempting to construct MetadataBuilder", + serviceRegistry.getClass().getName() + ); + } + } + + protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) { + return BootstrapServiceRegistry.class.isInstance( serviceRegistry ) + || StandardServiceRegistry.class.isInstance( serviceRegistry ); } public List getJaxbRootList() { @@ -104,12 +120,37 @@ public class MetadataSources { return namingStrategy; } + /** + * Get a builder for metadata where non-default options can be specified. + * + * @return The built metadata. + */ public MetadataBuilder getMetadataBuilder() { - return metadataBuilder; + return new MetadataBuilderImpl( this ); } + /** + * Get a builder for metadata where non-default options can be specified. + * + * @return The built metadata. + */ + public MetadataBuilder getMetadataBuilder(StandardServiceRegistry serviceRegistry) { + return new MetadataBuilderImpl( this, serviceRegistry ); + } + + /** + * Short-hand form of calling {@link #getMetadataBuilder()} and using its + * {@link org.hibernate.metamodel.MetadataBuilder#build()} method in cases where the application wants + * to accept the defaults. + * + * @return The built metadata. + */ public Metadata buildMetadata() { - return getMetadataBuilder().buildMetadata(); + return getMetadataBuilder().build(); + } + + public Metadata buildMetadata(StandardServiceRegistry serviceRegistry) { + return getMetadataBuilder( serviceRegistry ).build(); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java index c97ede7191..c3cb204e36 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java @@ -35,5 +35,10 @@ public interface SessionFactoryBuilder { public SessionFactoryBuilder with(EntityNotFoundDelegate entityNotFoundDelegate); - public SessionFactory buildSessionFactory(); + /** + * After all options have been set, build the SessionFactory. + * + * @return The built SessionFactory. + */ + public SessionFactory build(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java index f1fdb0bf6d..1e56b113d1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java @@ -27,6 +27,7 @@ package org.hibernate.metamodel.source.internal; import java.io.IOException; import java.io.InputStream; import java.net.URL; + import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -45,11 +46,7 @@ import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; -import org.jboss.logging.Logger; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.SAXException; - +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.internal.jaxb.JaxbRoot; import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; @@ -57,7 +54,10 @@ import org.hibernate.internal.jaxb.mapping.orm.JaxbEntityMappings; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.XsdException; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.jboss.logging.Logger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; /** * Helper class for unmarshalling xml configuration using StAX and JAXB. diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java index 5af4980c7e..8b61fefaaf 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java @@ -25,27 +25,66 @@ package org.hibernate.metamodel.source.internal; import javax.persistence.SharedCacheMode; +import org.hibernate.HibernateException; +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.NamingStrategy; +import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.MetadataBuilder; import org.hibernate.metamodel.MetadataSourceProcessingOrder; import org.hibernate.metamodel.MetadataSources; import org.hibernate.service.ServiceRegistry; -import org.hibernate.engine.config.spi.ConfigurationService; +import org.jboss.logging.Logger; /** * @author Steve Ebersole */ public class MetadataBuilderImpl implements MetadataBuilder { + private static final Logger log = Logger.getLogger( MetadataBuilderImpl.class ); + private final MetadataSources sources; private final OptionsImpl options; public MetadataBuilderImpl(MetadataSources sources) { + this( + sources, + getStandardServiceRegistry( sources.getServiceRegistry() ) + ); + } + + private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) { + if ( serviceRegistry == null ) { + throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" ); + } + + if ( StandardServiceRegistry.class.isInstance( serviceRegistry ) ) { + return ( StandardServiceRegistry ) serviceRegistry; + } + else if ( BootstrapServiceRegistry.class.isInstance( serviceRegistry ) ) { + log.debugf( + "ServiceRegistry passed to MetadataBuilder was a BootstrapServiceRegistry; this likely wont end well" + + "if attempt is made to build SessionFactory" + ); + return new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) serviceRegistry ).build(); + } + else { + throw new HibernateException( + String.format( + "Unexpected type of ServiceRegistry [%s] encountered in attempt to build MetadataBuilder", + serviceRegistry.getClass().getName() + ) + ); + } + } + + public MetadataBuilderImpl(MetadataSources sources, StandardServiceRegistry serviceRegistry) { this.sources = sources; - this.options = new OptionsImpl( sources.getServiceRegistry() ); + this.options = new OptionsImpl( serviceRegistry ); } @Override @@ -79,11 +118,13 @@ public class MetadataBuilderImpl implements MetadataBuilder { } @Override - public Metadata buildMetadata() { + public Metadata build() { return new MetadataImpl( sources, options ); } - private static class OptionsImpl implements Metadata.Options { + public static class OptionsImpl implements Metadata.Options { + private final StandardServiceRegistry serviceRegistry; + private MetadataSourceProcessingOrder metadataSourceProcessingOrder = MetadataSourceProcessingOrder.HBM_FIRST; private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE; private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE; @@ -93,7 +134,9 @@ public class MetadataBuilderImpl implements MetadataBuilder { private String defaultSchemaName; private String defaultCatalogName; - public OptionsImpl(ServiceRegistry serviceRegistry) { + public OptionsImpl(StandardServiceRegistry serviceRegistry) { + this.serviceRegistry = serviceRegistry; + ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class ); // cache access type @@ -152,6 +195,10 @@ public class MetadataBuilderImpl implements MetadataBuilder { ); } + @Override + public StandardServiceRegistry getServiceRegistry() { + return serviceRegistry; + } @Override public MetadataSourceProcessingOrder getMetadataSourceProcessingOrder() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java index 0e25b152ed..7693ed0ea8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java @@ -29,12 +29,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.jboss.logging.Logger; - import org.hibernate.AssertionFailure; import org.hibernate.DuplicateMappingException; import org.hibernate.MappingException; import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.NamingStrategy; @@ -66,8 +65,8 @@ import org.hibernate.metamodel.source.annotations.AnnotationMetadataSourceProces import org.hibernate.metamodel.source.hbm.HbmMetadataSourceProcessorImpl; import org.hibernate.persister.spi.PersisterClassResolver; import org.hibernate.service.ServiceRegistry; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.type.TypeResolver; +import org.jboss.logging.Logger; /** * Container for configuration data collected during binding the metamodel. @@ -117,7 +116,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable { private boolean globallyQuotedIdentifiers = false; public MetadataImpl(MetadataSources metadataSources, Options options) { - this.serviceRegistry = metadataSources.getServiceRegistry(); + this.serviceRegistry = options.getServiceRegistry(); this.options = options; this.identifierGeneratorFactory = serviceRegistry.getService( MutableIdentifierGeneratorFactory.class ); //new DefaultIdentifierGeneratorFactory( dialect ); @@ -335,11 +334,6 @@ public class MetadataImpl implements MetadataImplementor, Serializable { return options; } - @Override - public SessionFactory buildSessionFactory() { - return sessionFactoryBuilder.buildSessionFactory(); - } - @Override public ServiceRegistry getServiceRegistry() { return serviceRegistry; @@ -461,6 +455,11 @@ public class MetadataImpl implements MetadataImplementor, Serializable { return sessionFactoryBuilder; } + @Override + public SessionFactory buildSessionFactory() { + return getSessionFactoryBuilder().build(); + } + @Override public NamingStrategy getNamingStrategy() { return options.getNamingStrategy(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImpl.java index 3be406488b..2a1f3cf45f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImpl.java @@ -29,6 +29,7 @@ import org.hibernate.EmptyInterceptor; import org.hibernate.Interceptor; import org.hibernate.ObjectNotFoundException; import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.metamodel.SessionFactoryBuilder; import org.hibernate.metamodel.source.MetadataImplementor; @@ -45,7 +46,7 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder { /* package-protected */ SessionFactoryBuilderImpl(MetadataImplementor metadata) { this.metadata = metadata; - options = new SessionFactoryOptionsImpl(); + options = new SessionFactoryOptionsImpl( metadata.getOptions().getServiceRegistry() ); } @Override @@ -61,12 +62,17 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder { } @Override - public SessionFactory buildSessionFactory() { + public SessionFactory build() { return new SessionFactoryImpl(metadata, options, null ); } private static class SessionFactoryOptionsImpl implements SessionFactory.SessionFactoryOptions { + private final StandardServiceRegistry serviceRegistry; private Interceptor interceptor = EmptyInterceptor.INSTANCE; + + public SessionFactoryOptionsImpl(StandardServiceRegistry serviceRegistry) { + this.serviceRegistry = serviceRegistry; + } // TODO: should there be a DefaultEntityNotFoundDelegate.INSTANCE? private EntityNotFoundDelegate entityNotFoundDelegate = new EntityNotFoundDelegate() { @@ -75,6 +81,11 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder { } }; + @Override + public StandardServiceRegistry getServiceRegistry() { + return serviceRegistry; + } + @Override public Interceptor getInterceptor() { return interceptor; diff --git a/hibernate-core/src/main/java/org/hibernate/service/ServiceRegistryBuilder.java b/hibernate-core/src/main/java/org/hibernate/service/ServiceRegistryBuilder.java index 6d9c358aef..4039dfe4d8 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/ServiceRegistryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/service/ServiceRegistryBuilder.java @@ -26,6 +26,7 @@ package org.hibernate.service; import java.util.Map; import org.hibernate.boot.registry.StandardServiceInitiator; +import org.hibernate.boot.registry.StandardServiceRegistry; /** * @deprecated Use {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder} instead @@ -82,8 +83,7 @@ public class ServiceRegistryBuilder extends org.hibernate.boot.registry.Standard return this; } - @Override - public ServiceRegistry buildServiceRegistry() { - return super.buildServiceRegistry(); + public StandardServiceRegistry build() { + return super.build(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java new file mode 100644 index 0000000000..538687a925 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java @@ -0,0 +1,40 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.service.spi; + +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; + +/** + * Contract for contributing services. + * + * @author Steve Ebersole + */ +public interface ServiceContributor { + /** + * Contribute services to the indicated registry builder. + * + * @param serviceRegistryBuilder The builder to which services (or initiators) should be contributed. + */ + public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder); +} diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java index 411866f517..9f722270d8 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java @@ -62,7 +62,7 @@ class ManagedProviderConnectionHelper implements ConnectionHelper { private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) { Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); - return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry(); + return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build(); } public Connection getConnection() throws SQLException { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java index 271d6fec23..adada24997 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java @@ -504,7 +504,7 @@ public class SchemaExport { private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) { Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); - return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry(); + return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build(); } public static void main(String[] args) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java index 451fae85bf..5a2ea85822 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java @@ -110,7 +110,7 @@ public class SchemaUpdate { private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) { Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); - return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry(); + return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build(); } public static void main(String[] args) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java index 8a748cffdc..9137c91976 100755 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java @@ -79,7 +79,7 @@ public class SchemaValidator { private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) { Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); - return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry(); + return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build(); } public static void main(String[] args) { diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java index 1fecfaeabd..2598e426dd 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java @@ -64,7 +64,7 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase { @Before public void setUp() { - serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry(); + serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build(); } @After diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java index 98b8e26dc3..2deb0afe3f 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java @@ -46,7 +46,7 @@ public class BasicCollectionBindingTests extends BaseUnitTestCase { @Before public void setUp() { - serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry(); + serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build(); } @After @@ -68,7 +68,7 @@ public class BasicCollectionBindingTests extends BaseUnitTestCase { MetadataSources sources = new MetadataSources( serviceRegistry ); // sources.addAnnotatedClass( EntityWithBasicCollections.class ); sources.addResource( "org/hibernate/metamodel/binding/EntityWithBasicCollections.hbm.xml" ); - MetadataImpl metadata = (MetadataImpl) sources.getMetadataBuilder().with( processingOrder ).buildMetadata(); + MetadataImpl metadata = (MetadataImpl) sources.getMetadataBuilder().with( processingOrder ).build(); final EntityBinding entityBinding = metadata.getEntityBinding( EntityWithBasicCollections.class.getName() ); assertNotNull( entityBinding ); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java index 09e8ccd094..52a040af39 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2010, Red Hat Inc. or third-party contributors as + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -23,6 +23,12 @@ */ package org.hibernate.metamodel.source.annotations.entity; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.metamodel.MetadataBuilder; +import org.hibernate.metamodel.MetadataSources; +import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -30,13 +36,6 @@ import org.junit.rules.MethodRule; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.metamodel.MetadataSources; -import org.hibernate.metamodel.binding.EntityBinding; -import org.hibernate.metamodel.source.internal.MetadataImpl; - -import org.hibernate.testing.junit4.BaseUnitTestCase; - /** * @author Hardy Ferentschik */ @@ -100,10 +99,11 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase { private void createBindings() { try { - sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); + MetadataBuilder metadataBuilder = sources.getMetadataBuilder(); Resources resourcesAnnotation = origFrameworkMethod.getAnnotation( Resources.class ); if ( resourcesAnnotation != null ) { - sources.getMetadataBuilder().with( resourcesAnnotation.cacheMode() ); + metadataBuilder.with( resourcesAnnotation.cacheMode() ); for ( Class annotatedClass : resourcesAnnotation.annotatedClasses() ) { sources.addAnnotatedClass( annotatedClass ); @@ -112,7 +112,8 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase { sources.addResource( resourcesAnnotation.ormXmlPath() ); } } - meta = (MetadataImpl) sources.buildMetadata(); + + meta = ( MetadataImpl ) metadataBuilder.build(); } catch ( final Throwable t ) { setupError = t; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java index 3180f826e7..c5f0afbbf0 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java @@ -149,7 +149,7 @@ public class IdentifierGeneratorTest extends BaseAnnotationBindingTestCase { @Test public void testUndefinedGenerator() { try { - sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addAnnotatedClass( NamedGeneratorEntity.class ); sources.buildMetadata(); fail(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/MapsIdTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/MapsIdTest.java index 810e552820..81b0a3b8b5 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/MapsIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/MapsIdTest.java @@ -72,7 +72,7 @@ public class MapsIdTest extends BaseAnnotationBindingTestCase { @Resources(annotatedClasses = DependentId.class) public void testMapsIsOnOneToManyThrowsException() { try { - sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addAnnotatedClass( DependentId.class ); sources.addAnnotatedClass( Dependent.class ); sources.addAnnotatedClass( Employee.class ); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/FetchProfileBinderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/FetchProfileBinderTest.java index c82551a5f8..a686f1b03e 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/FetchProfileBinderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/FetchProfileBinderTest.java @@ -58,7 +58,7 @@ public class FetchProfileBinderTest extends BaseUnitTestCase { @Before public void setUp() { - serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry(); + serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build(); service = serviceRegistry.getService( ClassLoaderService.class ); meta = (MetadataImpl) new MetadataSources( serviceRegistry ).buildMetadata(); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/QueryBinderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/QueryBinderTest.java index f84ee9b688..dc27113eb1 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/QueryBinderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/global/QueryBinderTest.java @@ -58,7 +58,7 @@ public class QueryBinderTest extends BaseUnitTestCase { @Before public void setUp() { - serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry(); + serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build(); service = serviceRegistry.getService( ClassLoaderService.class ); meta = (MetadataImpl) new MetadataSources( serviceRegistry ).buildMetadata(); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/BaseAnnotationIndexTestCase.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/BaseAnnotationIndexTestCase.java index d067e55ecd..fd57f6d3fe 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/BaseAnnotationIndexTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/BaseAnnotationIndexTestCase.java @@ -50,7 +50,7 @@ public abstract class BaseAnnotationIndexTestCase extends BaseUnitTestCase { @Before public void setUp() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); meta = (MetadataImpl) sources.buildMetadata(); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java index a77347ccab..9817f8281a 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java @@ -67,7 +67,7 @@ public class JandexHelperTest extends BaseUnitTestCase { @Before public void setUp() { - serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry(); + serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build(); classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java index c1935b1efd..e31cea2ea4 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java @@ -23,17 +23,15 @@ */ package org.hibernate.metamodel.source.annotations.xml; -import org.junit.Test; +import static junit.framework.Assert.assertNotNull; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.internal.MetadataImpl; - import org.hibernate.testing.junit4.BaseUnitTestCase; - -import static junit.framework.Assert.assertNotNull; +import org.junit.Test; /** * @author Hardy Ferentschik @@ -41,7 +39,7 @@ import static junit.framework.Assert.assertNotNull; public class OrmXmlParserTests extends BaseUnitTestCase { @Test public void testSimpleOrmVersion2() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-father.xml" ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); @@ -51,7 +49,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase { @Test public void testSimpleOrmVersion1() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-star.xml" ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); @@ -61,7 +59,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase { @Test(expected = MappingException.class) public void testInvalidOrmXmlThrowsException() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-invalid.xml" ); sources.buildMetadata(); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java index 4fb1caa912..73d450352e 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java @@ -23,9 +23,12 @@ */ package org.hibernate.metamodel.source.internal; -import java.util.Iterator; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; -import org.junit.Test; +import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; @@ -34,13 +37,8 @@ import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.SessionFactoryBuilder; import org.hibernate.metamodel.binding.FetchProfile; - import org.hibernate.testing.junit4.BaseUnitTestCase; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertTrue; +import org.junit.Test; /** * @author Hardy Ferentschik @@ -49,28 +47,28 @@ public class MetadataImplTest extends BaseUnitTestCase { @Test(expected = IllegalArgumentException.class) public void testAddingNullClass() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addClass( null ); sources.buildMetadata(); } @Test(expected = IllegalArgumentException.class) public void testAddingNullPackageName() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addPackage( null ); sources.buildMetadata(); } @Test(expected = HibernateException.class) public void testAddingNonExistingPackageName() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addPackage( "not.a.package" ); sources.buildMetadata(); } @Test public void testAddingPackageName() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addPackage( "org.hibernate.metamodel.source.internal" ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); @@ -79,7 +77,7 @@ public class MetadataImplTest extends BaseUnitTestCase { @Test public void testAddingPackageNameWithTrailingDot() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addPackage( "org.hibernate.metamodel.source.internal." ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); @@ -88,7 +86,7 @@ public class MetadataImplTest extends BaseUnitTestCase { @Test public void testGettingSessionFactoryBuilder() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); Metadata metadata = sources.buildMetadata(); SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImplTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImplTest.java index 5f91b112c2..43e87b6fb5 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImplTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/SessionFactoryBuilderImplTest.java @@ -62,7 +62,7 @@ public class SessionFactoryBuilderImplTest extends BaseUnitTestCase { @Test public void testBuildSessionFactoryWithDefaultOptions() { SessionFactoryBuilder sessionFactoryBuilder = getSessionFactoryBuilder(); - SessionFactory sessionFactory = sessionFactoryBuilder.buildSessionFactory(); + SessionFactory sessionFactory = sessionFactoryBuilder.build(); assertSame( EmptyInterceptor.INSTANCE, sessionFactory.getSessionFactoryOptions().getInterceptor() ); assertTrue( EntityNotFoundDelegate.class.isInstance( sessionFactory.getSessionFactoryOptions().getEntityNotFoundDelegate() @@ -82,14 +82,14 @@ public class SessionFactoryBuilderImplTest extends BaseUnitTestCase { }; sessionFactoryBuilder.with( interceptor ); sessionFactoryBuilder.with( entityNotFoundDelegate ); - SessionFactory sessionFactory = sessionFactoryBuilder.buildSessionFactory(); + SessionFactory sessionFactory = sessionFactoryBuilder.build(); assertSame( interceptor, sessionFactory.getSessionFactoryOptions().getInterceptor() ); assertSame( entityNotFoundDelegate, sessionFactory.getSessionFactoryOptions().getEntityNotFoundDelegate() ); sessionFactory.close(); } private SessionFactoryBuilder getSessionFactoryBuilder() { - MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() ); + MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() ); sources.addAnnotatedClass( SimpleEntity.class ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); return metadata.getSessionFactoryBuilder(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java index 6f3defb65a..24db6ca2e5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java @@ -56,7 +56,7 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase { public void testWithoutIntegrator() { ServiceRegistry reg = new StandardServiceRegistryBuilder(new BootstrapServiceRegistryImpl()) - .buildServiceRegistry(); + .build(); SessionFactory sf = new Configuration() .addAnnotatedClass( Investor.class ) @@ -81,7 +81,7 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase { public void testWithIntegrator() { ServiceRegistry reg = new StandardServiceRegistryBuilder( new BootstrapServiceRegistryBuilder().with( new InvestorIntegrator() ).build() - ).buildServiceRegistry(); + ).build(); SessionFactory sf = new Configuration() .addAnnotatedClass( Investor.class ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/PersisterClassProviderTest.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/PersisterClassProviderTest.java index 706391588b..7378540fd2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/PersisterClassProviderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/PersisterClassProviderTest.java @@ -45,7 +45,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase { cfg.addAnnotatedClass( Gate.class ); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( cfg.getProperties() ) - .buildServiceRegistry(); + .build(); //no exception as the GoofyPersisterClassProvider is not set SessionFactory sessionFactory = cfg.buildSessionFactory( serviceRegistry ); sessionFactory.close(); @@ -54,7 +54,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase { serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( cfg.getProperties() ) .addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() ) - .buildServiceRegistry(); + .build(); cfg = new Configuration(); cfg.addAnnotatedClass( Gate.class ); try { @@ -79,7 +79,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase { serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( cfg.getProperties() ) .addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() ) - .buildServiceRegistry(); + .build(); try { sessionFactory = cfg.buildSessionFactory( serviceRegistry ); sessionFactory.close(); @@ -102,7 +102,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase { serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( cfg.getProperties() ) .addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() ) - .buildServiceRegistry(); + .build(); try { sessionFactory = cfg.buildSessionFactory( serviceRegistry ); sessionFactory.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java index 2db01943b3..9f504c0f46 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java @@ -32,7 +32,6 @@ import java.sql.PreparedStatement; import java.sql.Statement; import org.hibernate.Session; -import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey; import org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl; import org.hibernate.engine.jdbc.batch.internal.BatchingBatch; diff --git a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/ConfigurationValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/ConfigurationValidationTest.java index b84e8e4435..f05fde3e99 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/ConfigurationValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/ConfigurationValidationTest.java @@ -26,7 +26,7 @@ public class ConfigurationValidationTest extends BaseUnitTestCase { cfg.setProperty( Environment.MULTI_TENANT_CONNECTION_PROVIDER, "class.not.present.in.classpath" ); cfg.buildMappings(); ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() - .applySettings( cfg.getProperties() ).buildServiceRegistry(); + .applySettings( cfg.getProperties() ).build(); cfg.buildSessionFactory( serviceRegistry ); } @@ -48,7 +48,7 @@ public class ConfigurationValidationTest extends BaseUnitTestCase { ) ) ) - .buildServiceRegistry(); + .build(); cfg.buildSessionFactory( serviceRegistry ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java index 5ddcfc1837..86bde2f2e1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java @@ -69,7 +69,7 @@ public class SchemaBasedMultiTenancyTest extends BaseUnitTestCase { serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() .applySettings( cfg.getProperties() ) .addService( MultiTenantConnectionProvider.class, multiTenantConnectionProvider ) - .buildServiceRegistry(); + .build(); sessionFactory = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java b/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java index f92c6f060a..5a3dc82bc2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/service/ServiceBootstrappingTest.java @@ -37,7 +37,6 @@ import org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProv import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.env.ConnectionProviderBuilder; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.Test; @@ -51,7 +50,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase { public void testBasicBuild() { StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() ) - .buildServiceRegistry(); + .build(); JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); assertTrue( jdbcServices.getDialect() instanceof H2Dialect ); @@ -68,7 +67,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase { StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( props ) - .buildServiceRegistry(); + .build(); JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); @@ -83,7 +82,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase { public void testBuildWithServiceOverride() { StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() ) - .buildServiceRegistry(); + .build(); JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); assertTrue( jdbcServices.getDialect() instanceof H2Dialect ); @@ -95,7 +94,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase { serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( props ) .addService( ConnectionProvider.class, new UserSuppliedConnectionProviderImpl() ) - .buildServiceRegistry(); + .build(); jdbcServices = serviceRegistry.getService( JdbcServices.class ); assertTrue( jdbcServices.getDialect() instanceof H2Dialect ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/smoke/SessionFactoryBuildingUseCasesTest.java b/hibernate-core/src/test/java/org/hibernate/test/smoke/SessionFactoryBuildingUseCasesTest.java new file mode 100644 index 0000000000..5432bc6d6d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/smoke/SessionFactoryBuildingUseCasesTest.java @@ -0,0 +1,144 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.smoke; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.metamodel.Metadata; +import org.hibernate.metamodel.MetadataSources; + +import org.junit.Test; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-7580" ) +public class SessionFactoryBuildingUseCasesTest extends BaseUnitTestCase { + @Test + public void testTheSimplestUseCase() { + StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) + .build(); + MetadataSources metadataSources = new MetadataSources( registry ).addAnnotatedClass( MyEntity.class ); + SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); + + useSessionFactory( sessionFactory ); + + sessionFactory.close(); + StandardServiceRegistryBuilder.destroy( registry ); + } + + private void useSessionFactory(SessionFactory sessionFactory) { + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.createQuery( "from MyEntity" ).list(); + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testStillSimpleOnePhaseUseCase() { + BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build(); + StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry ) + .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) + .build(); + MetadataSources metadataSources = new MetadataSources( registry ).addAnnotatedClass( MyEntity.class ); + SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); + + useSessionFactory( sessionFactory ); + + sessionFactory.close(); + StandardServiceRegistryBuilder.destroy( registry ); + } + + @Test + public void testSimpleTwoPhaseUseCase() { + BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build(); + MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class ); + + StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry ) + .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) + .build(); + SessionFactory sessionFactory = metadataSources.buildMetadata( registry ).buildSessionFactory(); + + useSessionFactory( sessionFactory ); + + sessionFactory.close(); + StandardServiceRegistryBuilder.destroy( registry ); + } + + @Test + public void testFullTwoPhaseUseCase() { + BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build(); + MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class ); + + StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry ) + .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) + .build(); + Metadata metadata = metadataSources.getMetadataBuilder( registry ).build(); + SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); // todo rename + + useSessionFactory( sessionFactory ); + + sessionFactory.close(); + StandardServiceRegistryBuilder.destroy( registry ); + } + + @Test + @FailureExpected( jiraKey = "HHH-7580" ) + public void testInvalidQuasiUseCase() { + BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build(); + MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class ); + + StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry ) + .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) + .build(); + SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); + + useSessionFactory( sessionFactory ); + + sessionFactory.close(); + StandardServiceRegistryBuilder.destroy( registry ); + } + + @Entity( name = "MyEntity" ) + @Table( name = "TST_ENT" ) + public static class MyEntity { + @Id + private Long id; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/transaction/jdbc/TestExpectedUsage.java b/hibernate-core/src/test/java/org/hibernate/test/transaction/jdbc/TestExpectedUsage.java index 629381f3e1..42a4f7e03b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/transaction/jdbc/TestExpectedUsage.java +++ b/hibernate-core/src/test/java/org/hibernate/test/transaction/jdbc/TestExpectedUsage.java @@ -59,7 +59,7 @@ public class TestExpectedUsage extends BaseUnitTestCase { public void setUp() throws Exception { serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() ) - .buildServiceRegistry(); + .build(); } @After diff --git a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/BasicDrivingTest.java b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/BasicDrivingTest.java index c395dbdb29..386b98e42f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/BasicDrivingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/BasicDrivingTest.java @@ -71,7 +71,7 @@ public class BasicDrivingTest extends BaseUnitTestCase { TestingJtaBootstrap.prepare( configValues ); serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( configValues ) - .buildServiceRegistry(); + .build(); } @After diff --git a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java index ec8a0293b7..60a33288a0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java @@ -76,7 +76,7 @@ public class ManagedDrivingTest extends BaseUnitTestCase { serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( configValues ) - .buildServiceRegistry(); + .build(); } @After diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 6d729f297c..5f9fe063b2 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -23,16 +23,6 @@ */ package org.hibernate.jpa.boot.internal; -import javax.persistence.AttributeConverter; -import javax.persistence.Converter; -import javax.persistence.Embeddable; -import javax.persistence.Entity; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityNotFoundException; -import javax.persistence.MappedSuperclass; -import javax.persistence.PersistenceException; -import javax.persistence.spi.PersistenceUnitTransactionType; -import javax.sql.DataSource; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; @@ -52,43 +42,47 @@ import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; -import org.jboss.jandex.AnnotationInstance; -import org.jboss.jandex.ClassInfo; -import org.jboss.jandex.CompositeIndex; -import org.jboss.jandex.DotName; -import org.jboss.jandex.Index; -import org.jboss.jandex.IndexView; -import org.jboss.jandex.Indexer; - -import org.jboss.logging.Logger; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; +import javax.persistence.Embeddable; +import javax.persistence.Entity; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.MappedSuperclass; +import javax.persistence.PersistenceException; +import javax.persistence.spi.PersistenceUnitTransactionType; +import javax.sql.DataSource; import org.hibernate.Interceptor; import org.hibernate.MappingException; import org.hibernate.MappingNotFoundException; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; +import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator; -import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; -import org.hibernate.jpa.AvailableSettings; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory; import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory; +import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; import org.hibernate.integrator.spi.Integrator; import org.hibernate.internal.jaxb.cfg.JaxbHibernateConfiguration; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.ValueHolder; +import org.hibernate.jpa.AvailableSettings; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.IntegratorProvider; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; +import org.hibernate.jpa.event.spi.JpaIntegrator; import org.hibernate.jpa.internal.EntityManagerFactoryImpl; import org.hibernate.jpa.internal.EntityManagerMessageLogger; -import org.hibernate.jpa.event.spi.JpaIntegrator; import org.hibernate.jpa.internal.util.LogHelper; import org.hibernate.jpa.internal.util.PersistenceUnitTransactionTypeHelper; import org.hibernate.jpa.packaging.internal.NativeScanner; @@ -99,12 +93,17 @@ import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.secure.internal.JACCConfiguration; -import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.service.ConfigLoader; import org.hibernate.service.ServiceRegistry; -import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.ClassInfo; +import org.jboss.jandex.CompositeIndex; +import org.jboss.jandex.DotName; +import org.jboss.jandex.Index; +import org.jboss.jandex.IndexView; +import org.jboss.jandex.Indexer; +import org.jboss.logging.Logger; /** * @author Steve Ebersole @@ -903,7 +902,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } public ServiceRegistry buildServiceRegistry() { - return serviceRegistryBuilder.buildServiceRegistry(); + return serviceRegistryBuilder.build(); } public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) { diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java index d2ed123d8e..338340285e 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java @@ -78,7 +78,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm private void evictOrRemoveTest() throws Exception { Configuration cfg = createConfiguration(); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); @@ -94,7 +94,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm cfg = createConfiguration(); regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); @@ -148,7 +148,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm private void evictOrRemoveAllTest(String configName) throws Exception { Configuration cfg = createConfiguration(); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); @@ -166,7 +166,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm cfg = createConfiguration(); regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java index 5a5c884748..695ff8cfcf 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java @@ -27,12 +27,12 @@ import java.util.HashMap; import java.util.Map; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.collection.CollectionRegionImpl; import org.hibernate.cache.infinispan.entity.EntityRegionImpl; import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cfg.Configuration; -import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; /** @@ -110,7 +110,7 @@ public class NodeEnvironment { public void prepare() throws Exception { serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()) - .buildServiceRegistry(); + .build(); regionFactory = CacheTestUtil.startRegionFactory(serviceRegistry, configuration); } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/query/QueryRegionImplTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/query/QueryRegionImplTestCase.java index 6a803ddc9f..3ec976b6b1 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/query/QueryRegionImplTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/query/QueryRegionImplTestCase.java @@ -112,7 +112,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase { private void putDoesNotBlockGetTest() throws Exception { Configuration cfg = createConfiguration(); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); @@ -212,7 +212,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase { private void getDoesNotBlockPutTest() throws Exception { Configuration cfg = createConfiguration(); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/timestamp/TimestampsRegionImplTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/timestamp/TimestampsRegionImplTestCase.java index f8a661a56e..a96b9ec8c0 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/timestamp/TimestampsRegionImplTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/timestamp/TimestampsRegionImplTestCase.java @@ -81,7 +81,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC public void testClearTimestampsRegionInIsolated() throws Exception { Configuration cfg = createConfiguration(); InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg, getCacheTestSupport() ); @@ -90,7 +90,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC Configuration cfg2 = createConfiguration(); InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory( - new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(), + new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(), cfg2, getCacheTestSupport() ); diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/ServiceRegistryBuilder.java b/hibernate-testing/src/main/java/org/hibernate/testing/ServiceRegistryBuilder.java index efab4e2d63..bab28e4d33 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/ServiceRegistryBuilder.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/ServiceRegistryBuilder.java @@ -41,7 +41,7 @@ public class ServiceRegistryBuilder { public static StandardServiceRegistryImpl buildServiceRegistry(Map serviceRegistryConfig) { return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings( serviceRegistryConfig ) - .buildServiceRegistry(); + .build(); } public static void destroy(ServiceRegistry serviceRegistry) { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java index f52184d3b5..30a9d64eed 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java @@ -23,6 +23,8 @@ */ package org.hibernate.testing.junit4; +import static org.junit.Assert.fail; + import java.io.InputStream; import java.sql.Blob; import java.sql.Clob; @@ -34,19 +36,20 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import org.junit.After; -import org.junit.Before; - import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.Session; +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.cfg.Mappings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.internal.util.StringHelper; @@ -59,19 +62,14 @@ import org.hibernate.mapping.Property; import org.hibernate.mapping.SimpleValue; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.source.MetadataImplementor; -import org.hibernate.boot.registry.BootstrapServiceRegistry; -import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.testing.AfterClassOnce; import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.OnExpectedFailure; import org.hibernate.testing.OnFailure; import org.hibernate.testing.SkipLog; import org.hibernate.testing.cache.CachingRegionFactory; - -import static org.junit.Assert.fail; +import org.junit.After; +import org.junit.Before; /** * Applies functional testing logic for core Hibernate testing on top of {@link BaseUnitTestCase} @@ -126,7 +124,8 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { protected void buildSessionFactory() { // for now, build the configuration to get all the property settings configuration = constructAndConfigureConfiguration(); - serviceRegistry = buildServiceRegistry( configuration ); + BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry(); + serviceRegistry = buildServiceRegistry( bootRegistry, configuration ); isMetadataUsed = serviceRegistry.getService( ConfigurationService.class ).getSetting( USE_NEW_METADATA_MAPPINGS, new ConfigurationService.Converter() { @@ -138,7 +137,9 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { false ); if ( isMetadataUsed ) { - sessionFactory = ( SessionFactoryImplementor ) buildMetadata( serviceRegistry ).buildSessionFactory(); + MetadataImplementor metadataImplementor = buildMetadata( bootRegistry, serviceRegistry ); + afterConstructAndConfigureMetadata( metadataImplementor ); + sessionFactory = ( SessionFactoryImplementor ) metadataImplementor.buildSessionFactory(); } else { // this is done here because Configuration does not currently support 4.0 xsd @@ -148,10 +149,24 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { afterSessionFactoryBuilt(); } - private MetadataImplementor buildMetadata(ServiceRegistry serviceRegistry) { - MetadataSources sources = new MetadataSources( serviceRegistry ); - addMappings( sources ); - return (MetadataImplementor) sources.buildMetadata(); + protected void rebuildSessionFactory() { + if ( sessionFactory == null ) { + return; + } + buildSessionFactory(); + } + + + protected void afterConstructAndConfigureMetadata(MetadataImplementor metadataImplementor) { + + } + + private MetadataImplementor buildMetadata( + BootstrapServiceRegistry bootRegistry, + StandardServiceRegistryImpl serviceRegistry) { + MetadataSources sources = new MetadataSources( bootRegistry ); + addMappings( sources ); + return (MetadataImplementor) sources.getMetadataBuilder( serviceRegistry ).build(); } // TODO: is this still needed? @@ -327,20 +342,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { protected void afterConfigurationBuilt(Mappings mappings, Dialect dialect) { } - protected StandardServiceRegistryImpl buildServiceRegistry(Configuration configuration) { - Properties properties = new Properties(); - properties.putAll( configuration.getProperties() ); - Environment.verifyProperties( properties ); - ConfigurationHelper.resolvePlaceHolders( properties ); - - final BootstrapServiceRegistry bootstrapServiceRegistry = generateBootstrapRegistry( properties ); - StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootstrapServiceRegistry ) - .applySettings( properties ); - prepareBasicRegistryBuilder( registryBuilder ); - return (StandardServiceRegistryImpl) registryBuilder.buildServiceRegistry(); - } - - protected BootstrapServiceRegistry generateBootstrapRegistry(Properties properties) { + protected BootstrapServiceRegistry buildBootstrapServiceRegistry() { final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); prepareBootstrapRegistryBuilder( builder ); return builder.build(); @@ -349,6 +351,17 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) { } + protected StandardServiceRegistryImpl buildServiceRegistry(BootstrapServiceRegistry bootRegistry, Configuration configuration) { + Properties properties = new Properties(); + properties.putAll( configuration.getProperties() ); + Environment.verifyProperties( properties ); + ConfigurationHelper.resolvePlaceHolders( properties ); + + StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootRegistry ).applySettings( properties ); + prepareBasicRegistryBuilder( registryBuilder ); + return (StandardServiceRegistryImpl) registryBuilder.build(); + } + protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { } @@ -396,24 +409,6 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { } } - protected void rebuildSessionFactory() { - if ( sessionFactory == null ) { - return; - } - sessionFactory.close(); - serviceRegistry.destroy(); - - serviceRegistry = buildServiceRegistry( configuration ); - if ( isMetadataUsed ) { - // need to rebuild metadata because serviceRegistry was recreated - sessionFactory = ( SessionFactoryImplementor ) buildMetadata( serviceRegistry ).buildSessionFactory(); - } - else { - sessionFactory = ( SessionFactoryImplementor ) configuration.buildSessionFactory( serviceRegistry ); - } - afterSessionFactoryBuilt(); - } - // before/after each test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
propertymeaning