HHH-6240 - Add access to configuration options targetting metamodel building

This commit is contained in:
Steve Ebersole 2011-05-20 12:22:33 -05:00
parent 65c2c831ca
commit 52b2930b47
12 changed files with 181 additions and 60 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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;

View File

@ -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<String> uniqueKeys = new HashSet<String>();
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();

View File

@ -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 );

View File

@ -133,7 +133,7 @@ public class HibernateMappingBinder implements MappingDefaults {
}
public NamingStrategy getNamingStrategy() {
return hibernateXmlBinder.getMetadata().getNamingStrategy();
return hibernateXmlBinder.getMetadata().getOptions().getNamingStrategy();
}
public String getPackageName() {

View File

@ -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<String, MetaAttribute> globalMetas;
public HibernateXmlBinder(MetadataImpl metadata) {
public HibernateXmlBinder(MetadataImplementor metadata) {
this( metadata, Collections.<String, MetaAttribute>emptyMap() );
}
public HibernateXmlBinder(MetadataImpl metadata, Map<String, MetaAttribute> globalMetas) {
public HibernateXmlBinder(MetadataImplementor metadata, Map<String, MetaAttribute> globalMetas) {
this.metadata = metadata;
this.globalMetas = globalMetas;
}
@ -85,7 +85,7 @@ public class HibernateXmlBinder {
mappingBinder.processHibernateMapping();
}
MetadataImpl getMetadata() {
MetadataImplementor getMetadata() {
return metadata;
}

View File

@ -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<AccessType>() {
@Override
public AccessType convert(Object value) {
return AccessType.fromExternalName( value.toString() );
}
}
);
useNewIdentifierGenerators = configService.getSetting(
AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
new ConfigurationService.Converter<Boolean>() {
@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;
}
}
}

View File

@ -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<String, TypeDef> typeDefs = new HashMap<String, TypeDef>();
private Map<String, String> 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<String> processedEntityNames = new ArrayList<String>();
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 );
}

View File

@ -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<EntityBinding> 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);
}

View File

@ -43,4 +43,19 @@ public class ConfigurationServiceImpl implements ConfigurationService {
public Map getSettings() {
return settings;
}
@Override
public <T> T getSetting(String name, Converter<T> converter) {
return getSetting( name, converter, null );
}
@Override
public <T> T getSetting(String name, Converter<T> converter, T defaultValue) {
final Object value = settings.get( name );
if ( value == null ) {
return defaultValue;
}
return converter.convert( value );
}
}

View File

@ -34,4 +34,11 @@ import org.hibernate.service.Service;
*/
public interface ConfigurationService extends Service {
public Map getSettings();
public <T> T getSetting(String name, Converter<T> converter);
public <T> T getSetting(String name, Converter<T> converter, T defaultValue);
public static interface Converter<T> {
public T convert(Object value);
}
}