From 52b2930b4777470653765af5d5cf8ea339fa8fa5 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 20 May 2011 12:22:33 -0500 Subject: [PATCH] HHH-6240 - Add access to configuration options targetting metamodel building --- .../org/hibernate/metamodel/Metadata.java | 19 ++++ .../hibernate/metamodel/MetadataBuilder.java | 5 + .../annotations/entity/EntityBinder.java | 8 +- .../relational/ColumnRelationalStateImpl.java | 6 +- .../source/hbm/AbstractEntityBinder.java | 12 +- .../source/hbm/HibernateMappingBinder.java | 2 +- .../source/hbm/HibernateXmlBinder.java | 10 +- .../source/internal/MetadataBuilderImpl.java | 106 ++++++++++++++---- .../source/internal/MetadataImpl.java | 37 +++--- .../source/spi/MetadataImplementor.java | 14 ++- .../internal/ConfigurationServiceImpl.java | 15 +++ .../config/spi/ConfigurationService.java | 7 ++ 12 files changed, 181 insertions(+), 60 deletions(-) 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 3affc9ea69..0c2b2cf7d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java @@ -24,9 +24,28 @@ package org.hibernate.metamodel; +import javax.persistence.SharedCacheMode; + +import org.hibernate.SessionFactory; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cfg.NamingStrategy; + /** * @author Steve Ebersole */ public interface Metadata { + /** + * Exposes the options used to produce a {@link Metadata} instance. + */ + public static interface Options { + public SourceProcessingOrder getSourceProcessingOrder(); + public NamingStrategy getNamingStrategy(); + public SharedCacheMode getSharedCacheMode(); + public AccessType getDefaultAccessType(); + public boolean useNewIdentifierGenerators(); + } + public Options getOptions(); + + public SessionFactory buildSessionFactory(); } 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 1499f53c6d..0746b152c2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java @@ -25,6 +25,7 @@ package org.hibernate.metamodel; import javax.persistence.SharedCacheMode; +import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.NamingStrategy; /** @@ -38,5 +39,9 @@ public interface MetadataBuilder { public MetadataBuilder with(SharedCacheMode cacheMode); + public MetadataBuilder with(AccessType accessType); + + public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled); + public Metadata buildMetadata(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java index 6ccc1d3a4a..9af7c5c608 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java @@ -52,7 +52,7 @@ import org.hibernate.metamodel.source.annotations.entity.state.binding.Discrimin import org.hibernate.metamodel.source.annotations.entity.state.relational.ColumnRelationalStateImpl; import org.hibernate.metamodel.source.annotations.entity.state.relational.TupleRelationalStateImpl; import org.hibernate.metamodel.source.annotations.util.JandexHelper; -import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.classloading.spi.ClassLoaderService; /** @@ -62,11 +62,11 @@ import org.hibernate.service.classloading.spi.ClassLoaderService; */ public class EntityBinder { private final ConfiguredClass configuredClass; - private final MetadataImpl meta; + private final MetadataImplementor meta; private Schema.Name schemaName; - public EntityBinder(MetadataImpl metadata, ConfiguredClass configuredClass) { + public EntityBinder(MetadataImplementor metadata, ConfiguredClass configuredClass) { this.configuredClass = configuredClass; this.meta = metadata; } @@ -196,7 +196,7 @@ public class EntityBinder { } Caching caching = null; - switch ( meta.getSharedCacheMode() ) { + switch ( meta.getOptions().getSharedCacheMode() ) { case ALL: { caching = createCachingForCacheableAnnotation( entityBinding ); break; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java index e20118b023..fa6aee40dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java @@ -38,7 +38,7 @@ import org.hibernate.metamodel.relational.state.ColumnRelationalState; import org.hibernate.metamodel.source.annotations.HibernateDotNames; import org.hibernate.metamodel.source.annotations.entity.ColumnValues; import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; -import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * @author Hardy Ferentschik @@ -61,9 +61,9 @@ public class ColumnRelationalStateImpl implements ColumnRelationalState { private Set uniqueKeys = new HashSet(); - public ColumnRelationalStateImpl(MappedAttribute attribute, MetadataImpl meta) { + public ColumnRelationalStateImpl(MappedAttribute attribute, MetadataImplementor meta) { ColumnValues columnValues = attribute.getColumnValues(); - namingStrategy = meta.getNamingStrategy(); + namingStrategy = meta.getOptions().getNamingStrategy(); columnName = columnValues.getName().isEmpty() ? attribute.getName() : columnValues.getName(); unique = columnValues.isUnique(); nullable = columnValues.isNullable(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java index 76cdf9320b..d2ab3058ca 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java @@ -70,11 +70,11 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLTuplizerElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement; -import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.binding.state.PluralAttributeBindingState; import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; import org.hibernate.metamodel.relational.state.TupleRelationalState; import org.hibernate.metamodel.relational.state.ValueRelationalState; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * TODO : javadoc @@ -106,7 +106,7 @@ abstract class AbstractEntityBinder { return hibernateMappingBinder.getHibernateXmlBinder(); } - protected MetadataImpl getMetadata() { + protected MetadataImplementor getMetadata() { return hibernateMappingBinder.getHibernateXmlBinder().getMetadata(); } @@ -115,7 +115,7 @@ abstract class AbstractEntityBinder { } protected NamingStrategy getNamingStrategy() { - return getMetadata().getNamingStrategy(); + return getMetadata().getOptions().getNamingStrategy(); } protected void basicEntityBinding(XMLHibernateMapping.XMLClass entityClazz, @@ -243,12 +243,16 @@ abstract class AbstractEntityBinder { if ( entityClazz.getTable() == null ) { logicalTableName = StringHelper.unqualify( entityName ); physicalTableName = getHibernateXmlBinder().getMetadata() + .getOptions() .getNamingStrategy() .classToTableName( entityName ); } else { logicalTableName = entityClazz.getTable(); - physicalTableName = getHibernateXmlBinder().getMetadata().getNamingStrategy().tableName( logicalTableName ); + physicalTableName = getHibernateXmlBinder().getMetadata() + .getOptions() + .getNamingStrategy() + .tableName( logicalTableName ); } // todo : find out the purpose of these logical bindings // mappings.addTableBinding( schema, catalog, logicalTableName, physicalTableName, denormalizedSuperTable ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java index f03287d2e5..9a9b43dbac 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java @@ -133,7 +133,7 @@ public class HibernateMappingBinder implements MappingDefaults { } public NamingStrategy getNamingStrategy() { - return hibernateXmlBinder.getMetadata().getNamingStrategy(); + return hibernateXmlBinder.getMetadata().getOptions().getNamingStrategy(); } public String getPackageName() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java index c3a5c4eda0..d0ff9dd8fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java @@ -40,8 +40,8 @@ import org.hibernate.internal.util.collections.JoinedIterator; import org.hibernate.internal.util.xml.XmlDocument; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.internal.JaxbRoot; -import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * Binder for {@code hbm.xml} files @@ -51,14 +51,14 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; public class HibernateXmlBinder { private static final Logger log = LoggerFactory.getLogger( HibernateXmlBinder.class ); - private final MetadataImpl metadata; + private final MetadataImplementor metadata; private final Map globalMetas; - public HibernateXmlBinder(MetadataImpl metadata) { + public HibernateXmlBinder(MetadataImplementor metadata) { this( metadata, Collections.emptyMap() ); } - public HibernateXmlBinder(MetadataImpl metadata, Map globalMetas) { + public HibernateXmlBinder(MetadataImplementor metadata, Map globalMetas) { this.metadata = metadata; this.globalMetas = globalMetas; } @@ -85,7 +85,7 @@ public class HibernateXmlBinder { mappingBinder.processHibernateMapping(); } - MetadataImpl getMetadata() { + MetadataImplementor getMetadata() { return metadata; } 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 ff15136673..ffedddea1b 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,63 +25,121 @@ package org.hibernate.metamodel.source.internal; import javax.persistence.SharedCacheMode; +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.metamodel.Metadata; import org.hibernate.metamodel.MetadataBuilder; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.SourceProcessingOrder; +import org.hibernate.service.BasicServiceRegistry; +import org.hibernate.service.config.spi.ConfigurationService; /** * @author Steve Ebersole */ public class MetadataBuilderImpl implements MetadataBuilder { private final MetadataSources sources; - - private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE; - private SourceProcessingOrder sourceProcessingOrder = SourceProcessingOrder.HBM_FIRST; - private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE; + private final OptionsImpl options; public MetadataBuilderImpl(MetadataSources sources) { this.sources = sources; - } - - public MetadataSources getSources() { - return sources; - } - - public NamingStrategy getNamingStrategy() { - return namingStrategy; - } - - public SharedCacheMode getSharedCacheMode() { - return sharedCacheMode; - } - - public SourceProcessingOrder getSourceProcessingOrder() { - return sourceProcessingOrder; + this.options = new OptionsImpl( sources.getServiceRegistry() ); } @Override public MetadataBuilder with(NamingStrategy namingStrategy) { - this.namingStrategy = namingStrategy; + this.options.namingStrategy = namingStrategy; return this; } @Override public MetadataBuilder with(SourceProcessingOrder sourceProcessingOrder) { - this.sourceProcessingOrder = sourceProcessingOrder; + this.options.sourceProcessingOrder = sourceProcessingOrder; return this; } @Override public MetadataBuilder with(SharedCacheMode sharedCacheMode) { - this.sharedCacheMode = sharedCacheMode; + this.options.sharedCacheMode = sharedCacheMode; + return this; + } + + @Override + public MetadataBuilder with(AccessType accessType) { + this.options.defaultCacheAccessType = accessType; + return this; + } + + @Override + public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled) { + this.options.useNewIdentifierGenerators = enabled; return this; } @Override public Metadata buildMetadata() { - return new MetadataImpl( this ); + return new MetadataImpl( sources, options ); } + + private static class OptionsImpl implements Metadata.Options { + private SourceProcessingOrder sourceProcessingOrder = SourceProcessingOrder.HBM_FIRST; + private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE; + private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE; + private AccessType defaultCacheAccessType; + private boolean useNewIdentifierGenerators; + + public OptionsImpl(BasicServiceRegistry serviceRegistry) { + ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class ); + + // cache access type + defaultCacheAccessType = configService.getSetting( + AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, + new ConfigurationService.Converter() { + @Override + public AccessType convert(Object value) { + return AccessType.fromExternalName( value.toString() ); + } + } + ); + + useNewIdentifierGenerators = configService.getSetting( + AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, + new ConfigurationService.Converter() { + @Override + public Boolean convert(Object value) { + return Boolean.parseBoolean( value.toString() ); + } + }, + false + ); + } + + + @Override + public SourceProcessingOrder getSourceProcessingOrder() { + return sourceProcessingOrder; + } + + @Override + public NamingStrategy getNamingStrategy() { + return namingStrategy; + } + + @Override + public AccessType getDefaultAccessType() { + return defaultCacheAccessType; + } + + @Override + public SharedCacheMode getSharedCacheMode() { + return sharedCacheMode; + } + + @Override + public boolean useNewIdentifierGenerators() { + return useNewIdentifierGenerators; + } + } } 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 99c5a12956..09005ba8c4 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 @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.persistence.SharedCacheMode; import org.jboss.jandex.Index; import org.jboss.jandex.Indexer; @@ -38,7 +37,7 @@ import org.jboss.logging.Logger; import org.hibernate.DuplicateMappingException; import org.hibernate.HibernateException; -import org.hibernate.cfg.NamingStrategy; +import org.hibernate.SessionFactory; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.mapping.MetadataSource; import org.hibernate.metamodel.Metadata; @@ -70,8 +69,8 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable ); private final BasicServiceRegistry serviceRegistry; - private final NamingStrategy namingStrategy; - private final SharedCacheMode sharedCacheMode; + private final Options options; + private final Database database = new Database(); /** @@ -83,15 +82,12 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable private Map typeDefs = new HashMap(); private Map imports; - public MetadataImpl(MetadataBuilderImpl builder) { - final MetadataSources metadataSources = builder.getSources(); - + public MetadataImpl(MetadataSources metadataSources, Options options) { this.serviceRegistry = metadataSources.getServiceRegistry(); - this.namingStrategy = builder.getNamingStrategy(); - this.sharedCacheMode = builder.getSharedCacheMode(); + this.options = options; final ArrayList processedEntityNames = new ArrayList(); - if ( builder.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) { + if ( options.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) { applyHibernateMappings( metadataSources, processedEntityNames ); applyAnnotationMappings( metadataSources, processedEntityNames ); } @@ -158,22 +154,27 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable } } + @Override + public Options getOptions() { + return options; + } + + @Override + public SessionFactory buildSessionFactory() { + // todo : implement!!!! + return null; + } + + @Override public BasicServiceRegistry getServiceRegistry() { return serviceRegistry; } + @Override public Database getDatabase() { return database; } - public NamingStrategy getNamingStrategy() { - return namingStrategy; - } - - public SharedCacheMode getSharedCacheMode() { - return sharedCacheMode; - } - public EntityBinding getEntityBinding(String entityName) { return entityBindingMap.get( entityName ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java index cc4b3194f2..291f4bf888 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java @@ -23,17 +23,29 @@ */ package org.hibernate.metamodel.source.spi; +import org.hibernate.mapping.MetadataSource; +import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.binding.FetchProfile; +import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.relational.Database; import org.hibernate.service.BasicServiceRegistry; /** * @author Steve Ebersole */ -public interface MetadataImplementor { +public interface MetadataImplementor extends Metadata { public BasicServiceRegistry getServiceRegistry(); public Database getDatabase(); public Iterable getEntityBindings(); public EntityBinding getEntityBinding(String entityName); + + public void addImport(String entityName, String entityName1); + + public void addEntity(EntityBinding entityBinding); + + public void addCollection(PluralAttributeBinding collectionBinding); + + public FetchProfile findOrCreateFetchProfile(String profileName, MetadataSource hbm); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java index 58f23b721d..0bbe388748 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java @@ -43,4 +43,19 @@ public class ConfigurationServiceImpl implements ConfigurationService { public Map getSettings() { return settings; } + + @Override + public T getSetting(String name, Converter converter) { + return getSetting( name, converter, null ); + } + + @Override + public T getSetting(String name, Converter converter, T defaultValue) { + final Object value = settings.get( name ); + if ( value == null ) { + return defaultValue; + } + + return converter.convert( value ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java index a1e2b1c4f3..815a44bdd8 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java @@ -34,4 +34,11 @@ import org.hibernate.service.Service; */ public interface ConfigurationService extends Service { public Map getSettings(); + + public T getSetting(String name, Converter converter); + public T getSetting(String name, Converter converter, T defaultValue); + + public static interface Converter { + public T convert(Object value); + } }