diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/TypeBeanInstanceProducer.java b/hibernate-core/src/main/java/org/hibernate/boot/model/TypeBeanInstanceProducer.java new file mode 100644 index 0000000000..cd7bebeb71 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/TypeBeanInstanceProducer.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.boot.model; + +import java.lang.reflect.Constructor; +import java.util.Map; + +import org.hibernate.MappingException; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.resource.beans.spi.BeanInstanceProducer; +import org.hibernate.type.spi.TypeBootstrapContext; +import org.hibernate.type.spi.TypeConfiguration; + +/** + * @author Christian Beikov + */ +public class TypeBeanInstanceProducer implements BeanInstanceProducer, TypeBootstrapContext { + + private final TypeConfiguration typeConfiguration; + + public TypeBeanInstanceProducer(TypeConfiguration typeConfiguration) { + this.typeConfiguration = typeConfiguration; + } + + @Override + public B produceBeanInstance(Class beanType) { + try { + final B type; + final Constructor bootstrapContextAwareTypeConstructor = ReflectHelper.getConstructor( + beanType, + TypeBootstrapContext.class + ); + if ( bootstrapContextAwareTypeConstructor != null ) { + type = bootstrapContextAwareTypeConstructor.newInstance( this ); + } + else { + type = beanType.newInstance(); + } + return type; + } + catch (Exception e) { + throw new MappingException( "Could not instantiate Type: " + beanType.getName(), e ); + } + } + + @Override + public B produceBeanInstance(String name, Class beanType) { + return produceBeanInstance( beanType ); + } + + @Override + @SuppressWarnings("unchecked") + public Map getConfigurationSettings() { + return typeConfiguration.getServiceRegistry().getService( ConfigurationService.class ).getSettings(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java b/hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java index 190993ab81..2cd08e243f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java @@ -22,6 +22,7 @@ import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.mapping.BasicValue; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.model.convert.spi.BasicValueConverter; +import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.ManagedBean; import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.type.BasicType; @@ -135,7 +136,7 @@ public class TypeDefinition implements Serializable { JdbcTypeDescriptorIndicators indicators, MetadataBuildingContext context) { final TypeConfiguration typeConfiguration = context.getBootstrapContext().getTypeConfiguration(); - + final TypeBeanInstanceProducer instanceProducer = new TypeBeanInstanceProducer( typeConfiguration ); final boolean isKnownType = Type.class.isAssignableFrom( typeImplementorClass ) || UserType.class.isAssignableFrom( typeImplementorClass ); @@ -146,13 +147,13 @@ public class TypeDefinition implements Serializable { typeBean = context.getBootstrapContext() .getServiceRegistry() .getService( ManagedBeanRegistry.class ) - .getBean( name, typeImplementorClass ); + .getBean( name, typeImplementorClass, instanceProducer ); } else { typeBean = context.getBootstrapContext() .getServiceRegistry() .getService( ManagedBeanRegistry.class ) - .getBean( typeImplementorClass ); + .getBean( typeImplementorClass, instanceProducer ); } final Object typeInstance = typeBean.getBeanInstance(); diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java index 8c8ec7b4a1..28f4151b7b 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java @@ -12,6 +12,7 @@ import java.util.Map; import org.hibernate.resource.beans.container.spi.BeanContainer; import org.hibernate.resource.beans.container.spi.ContainedBean; import org.hibernate.resource.beans.container.spi.FallbackContainedBean; +import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.ManagedBean; import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.spi.Stoppable; @@ -48,6 +49,11 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, BeanContain @Override @SuppressWarnings("unchecked") public ManagedBean getBean(Class beanClass) { + return getBean( beanClass, FallbackBeanInstanceProducer.INSTANCE ); + } + + @Override + public ManagedBean getBean(Class beanClass, BeanInstanceProducer fallbackBeanInstanceProducer) { final ManagedBean existing = registrations.get( beanClass.getName() ); if ( existing != null ) { return existing; @@ -55,13 +61,13 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, BeanContain final ManagedBean bean; if ( beanContainer == null ) { - bean = new FallbackContainedBean( beanClass, FallbackBeanInstanceProducer.INSTANCE ); + bean = new FallbackContainedBean( beanClass, fallbackBeanInstanceProducer ); } else { final ContainedBean containedBean = beanContainer.getBean( beanClass, this, - FallbackBeanInstanceProducer.INSTANCE + fallbackBeanInstanceProducer ); if ( containedBean instanceof ManagedBean ) { @@ -78,8 +84,16 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, BeanContain } @Override - @SuppressWarnings("unchecked") public ManagedBean getBean(String beanName, Class beanContract) { + return getBean( beanName, beanContract, FallbackBeanInstanceProducer.INSTANCE ); + } + + @Override + @SuppressWarnings("unchecked") + public ManagedBean getBean( + String beanName, + Class beanContract, + BeanInstanceProducer fallbackBeanInstanceProducer) { final String key = beanContract.getName() + ':' + beanName; final ManagedBean existing = registrations.get( key ); @@ -89,14 +103,14 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, BeanContain final ManagedBean bean; if ( beanContainer == null ) { - bean = new FallbackContainedBean( beanName, beanContract, FallbackBeanInstanceProducer.INSTANCE ); + bean = new FallbackContainedBean( beanName, beanContract, fallbackBeanInstanceProducer ); } else { final ContainedBean containedBean = beanContainer.getBean( beanName, beanContract, this, - FallbackBeanInstanceProducer.INSTANCE + fallbackBeanInstanceProducer ); if ( containedBean instanceof ManagedBean ) { diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistry.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistry.java index c8f2d81874..7b61a0ff7f 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistry.java @@ -28,6 +28,19 @@ public interface ManagedBeanRegistry extends Service { */ ManagedBean getBean(String beanName, Class beanContract); + /** + * Get a bean reference by class with an explicit fallback bean instance producer. + */ + ManagedBean getBean(Class beanContract, BeanInstanceProducer fallbackBeanInstanceProducer); + + /** + * Get a bean reference by name and contract with an explicit fallback bean instance producer. + */ + ManagedBean getBean( + String beanName, + Class beanContract, + BeanInstanceProducer fallbackBeanInstanceProducer); + /** * Get a reference to the underlying BeanContainer. May return {@code null} * indicating that no back-end container has been configured diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/Array.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/Array.java similarity index 69% rename from hibernate-core/src/test/java/org/hibernate/test/type/contributor/Array.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/Array.java index f3919b8012..1388594fbd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/Array.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/Array.java @@ -1,4 +1,4 @@ -package org.hibernate.test.type.contributor; +package org.hibernate.orm.test.type.contributor; import java.util.ArrayList; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayType.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayType.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayType.java index 19967b56d6..f402d2700d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayType.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayType.java @@ -1,4 +1,4 @@ -package org.hibernate.test.type.contributor; +package org.hibernate.orm.test.type.contributor; import java.util.Map; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeContributorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeContributorTest.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeContributorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeContributorTest.java index 1f414e45d7..dacec8827e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeContributorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeContributorTest.java @@ -4,16 +4,13 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.contributor; +package org.hibernate.orm.test.type.contributor; -import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.Properties; import javax.persistence.Entity; import javax.persistence.Id; -import org.hibernate.Session; import org.hibernate.annotations.Type; import org.hibernate.boot.spi.MetadataBuilderContributor; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeDescriptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeDescriptor.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeDescriptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeDescriptor.java index 306d67ba8f..d8e08d9096 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypeDescriptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypeDescriptor.java @@ -1,4 +1,4 @@ -package org.hibernate.test.type.contributor; +package org.hibernate.orm.test.type.contributor; import java.util.Arrays; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypePropertiesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypePropertiesTest.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypePropertiesTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypePropertiesTest.java index 1f17b0d305..f43a6b8763 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypePropertiesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/ArrayTypePropertiesTest.java @@ -4,23 +4,16 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.contributor; +package org.hibernate.orm.test.type.contributor; import java.util.List; import java.util.Map; -import java.util.Properties; import javax.persistence.Entity; import javax.persistence.Id; -import javax.transaction.Transactional; -import org.hibernate.Session; -import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; -import org.hibernate.boot.spi.MetadataBuilderContributor; -import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.query.Query;