HHH-7042 - Clean up MetadataSources

This commit is contained in:
Steve Ebersole 2012-02-09 01:37:01 -06:00
parent ffb8432548
commit 6efab8895e
6 changed files with 104 additions and 102 deletions

View File

@ -26,7 +26,6 @@ package org.hibernate.metamodel.internal;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -50,8 +49,12 @@ import org.hibernate.metamodel.MetadataSourceProcessingOrder;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.SessionFactoryBuilder; import org.hibernate.metamodel.SessionFactoryBuilder;
import org.hibernate.metamodel.internal.source.AssociationResolver; import org.hibernate.metamodel.internal.source.AssociationResolver;
import org.hibernate.metamodel.internal.source.Binder;
import org.hibernate.metamodel.internal.source.HibernateTypeResolver; import org.hibernate.metamodel.internal.source.HibernateTypeResolver;
import org.hibernate.metamodel.internal.source.IdentifierGeneratorResolver; import org.hibernate.metamodel.internal.source.IdentifierGeneratorResolver;
import org.hibernate.metamodel.internal.source.annotations.AnnotationMetadataSourceProcessorImpl;
import org.hibernate.metamodel.internal.source.hbm.HbmMetadataSourceProcessorImpl;
import org.hibernate.metamodel.spi.MetadataSourceProcessor;
import org.hibernate.metamodel.spi.binding.AttributeBinding; import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.FetchProfile; import org.hibernate.metamodel.spi.binding.FetchProfile;
@ -61,12 +64,11 @@ import org.hibernate.metamodel.spi.binding.TypeDef;
import org.hibernate.metamodel.spi.domain.BasicType; import org.hibernate.metamodel.spi.domain.BasicType;
import org.hibernate.metamodel.spi.domain.Type; import org.hibernate.metamodel.spi.domain.Type;
import org.hibernate.metamodel.spi.relational.Database; import org.hibernate.metamodel.spi.relational.Database;
import org.hibernate.metamodel.spi.source.EntityHierarchy;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.MappingDefaults; import org.hibernate.metamodel.spi.source.MappingDefaults;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.MetadataSourceProcessor;
import org.hibernate.metamodel.internal.source.annotations.AnnotationMetadataSourceProcessorImpl;
import org.hibernate.metamodel.internal.source.hbm.HbmMetadataSourceProcessorImpl;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource; import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
import org.hibernate.persister.spi.PersisterClassResolver; import org.hibernate.persister.spi.PersisterClassResolver;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -161,14 +163,15 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
); );
final ArrayList<String> processedEntityNames = new ArrayList<String>();
prepare( metadataSourceProcessors, metadataSources ); prepare( metadataSourceProcessors, metadataSources );
processTypeDescriptors( metadataSourceProcessors, metadataSources ); processTypeDescriptors( metadataSourceProcessors, metadataSources );
processFilterDefs( metadataSourceProcessors, metadataSources );
processIdentifierGenerators( metadataSourceProcessors, metadataSources );
processMappings( metadataSourceProcessors, metadataSources );
bindTypeDependentMetadata( metadataSourceProcessors, metadataSources );
bindMappingMetadata( metadataSourceProcessors, metadataSources, processedEntityNames );
bindMappingDependentMetadata( metadataSourceProcessors, metadataSources ); bindMappingDependentMetadata( metadataSourceProcessors, metadataSources );
// todo : remove this by coordinated ordering of entity processing // todo : remove this by coordinated ordering of entity processing
@ -202,17 +205,50 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
} }
} }
private void bindTypeDependentMetadata(MetadataSourceProcessor[] metadataSourceProcessors, MetadataSources metadataSources) {
for ( MetadataSourceProcessor metadataSourceProcessor : metadataSourceProcessors ) { // filter-defs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
metadataSourceProcessor.processTypeDependentMetadata( metadataSources );
private void processFilterDefs(
MetadataSourceProcessor[] metadataSourceProcessors,
MetadataSources metadataSources) {
for ( MetadataSourceProcessor processor : metadataSourceProcessors ) {
for ( FilterDefSource filterDefSource : processor.extractFilterDefSources( metadataSources ) ) {
addFilterDefinition(
new FilterDefinition(
filterDefSource.getName(),
filterDefSource.getCondition(),
null // the params, todo : need to figure out how to handle the type portion
)
);
}
} }
} }
private void bindMappingMetadata(MetadataSourceProcessor[] metadataSourceProcessors, MetadataSources metadataSources, List<String> processedEntityNames) {
for ( MetadataSourceProcessor metadataSourceProcessor : metadataSourceProcessors ) { // identifier generators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
metadataSourceProcessor.processMappingMetadata( metadataSources, processedEntityNames );
private void processIdentifierGenerators(
MetadataSourceProcessor[] metadataSourceProcessors,
MetadataSources metadataSources) {
// HHH-7040
}
private void processMappings(
MetadataSourceProcessor[] metadataSourceProcessors,
MetadataSources metadataSources) {
final ArrayList<String> processedEntityNames = new ArrayList<String>();
final Binder binder = new Binder( this, processedEntityNames );
for ( MetadataSourceProcessor processor : metadataSourceProcessors ) {
for ( EntityHierarchy entityHierarchy : processor.extractEntityHierarchies( metadataSources ) ) {
binder.processEntityHierarchy( entityHierarchy );
} }
} }
}
private void bindMappingDependentMetadata(MetadataSourceProcessor[] metadataSourceProcessors, MetadataSources metadataSources) { private void bindMappingDependentMetadata(MetadataSourceProcessor[] metadataSourceProcessors, MetadataSources metadataSources) {
for ( MetadataSourceProcessor metadataSourceProcessor : metadataSourceProcessors ) { for ( MetadataSourceProcessor metadataSourceProcessor : metadataSourceProcessors ) {

View File

@ -27,7 +27,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
@ -39,19 +38,17 @@ import org.hibernate.HibernateException;
import org.hibernate.internal.jaxb.JaxbRoot; import org.hibernate.internal.jaxb.JaxbRoot;
import org.hibernate.internal.jaxb.mapping.orm.JaxbEntityMappings; import org.hibernate.internal.jaxb.mapping.orm.JaxbEntityMappings;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.internal.source.Binder;
import org.hibernate.metamodel.internal.MetadataImpl; import org.hibernate.metamodel.internal.MetadataImpl;
import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.MetadataSourceProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.FetchProfileProcessor; import org.hibernate.metamodel.internal.source.annotations.global.FetchProfileProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.FilterDefProcessor; import org.hibernate.metamodel.internal.source.annotations.global.FilterDefProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.IdGeneratorProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.QueryProcessor; import org.hibernate.metamodel.internal.source.annotations.global.QueryProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.TableProcessor; import org.hibernate.metamodel.internal.source.annotations.global.TableProcessor;
import org.hibernate.metamodel.internal.source.annotations.global.TypeDefProcessor;
import org.hibernate.metamodel.internal.source.annotations.xml.PseudoJpaDotNames; import org.hibernate.metamodel.internal.source.annotations.xml.PseudoJpaDotNames;
import org.hibernate.metamodel.internal.source.annotations.xml.mocker.EntityMappingsMocker; import org.hibernate.metamodel.internal.source.annotations.xml.mocker.EntityMappingsMocker;
import org.hibernate.metamodel.spi.MetadataSourceProcessor;
import org.hibernate.metamodel.spi.source.EntityHierarchy; import org.hibernate.metamodel.spi.source.EntityHierarchy;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource; import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -138,21 +135,34 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc
} }
@Override @Override
public void processTypeDependentMetadata(MetadataSources sources) { public Iterable<FilterDefSource> extractFilterDefSources(MetadataSources sources) {
assertBindingContextExists(); assertBindingContextExists();
IdGeneratorProcessor.bind( bindingContext );
List<FilterDefSource> filterDefSources = new ArrayList<FilterDefSource>();
List<AnnotationInstance> annotations = bindingContext.getIndex().getAnnotations( HibernateDotNames.FILTER_DEF );
for ( AnnotationInstance filterDef : annotations ) {
filterDefSources.add( new FilterDefSourceImpl( filterDef ) );
}
annotations = bindingContext.getIndex().getAnnotations( HibernateDotNames.FILTER_DEFS );
for ( AnnotationInstance filterDefs : annotations ) {
AnnotationInstance[] filterDefAnnotations = JandexHelper.getValue(
filterDefs,
"value",
AnnotationInstance[].class
);
for ( AnnotationInstance filterDef : filterDefAnnotations ) {
filterDefSources.add( new FilterDefSourceImpl( filterDef ) );
}
}
return filterDefSources;
} }
@Override @Override
public void processMappingMetadata(MetadataSources sources, List<String> processedEntityNames) { public Iterable<? extends EntityHierarchy> extractEntityHierarchies(MetadataSources sources) {
assertBindingContextExists(); assertBindingContextExists();
// need to order our annotated entities into an order we can process // need to order our annotated entities into an order we can process
Set<EntityHierarchy> hierarchies = EntityHierarchyBuilder.createEntityHierarchies( bindingContext ); return EntityHierarchyBuilder.createEntityHierarchies( bindingContext );
Binder binder = new Binder( bindingContext.getMetadataImplementor(), new ArrayList<String>() );
for ( EntityHierarchy hierarchy : hierarchies ) {
binder.processEntityHierarchy( hierarchy );
}
} }
@Override @Override

View File

@ -29,13 +29,13 @@ import java.util.List;
import org.hibernate.internal.jaxb.JaxbRoot; import org.hibernate.internal.jaxb.JaxbRoot;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.internal.source.Binder; import org.hibernate.metamodel.spi.MetadataSourceProcessor;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.MetadataSourceProcessor;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource; import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
/** /**
* The {@link org.hibernate.metamodel.spi.source.MetadataSourceProcessor} implementation responsible for processing {@code hbm.xml} sources. * The {@link org.hibernate.metamodel.spi.MetadataSourceProcessor} implementation responsible for processing {@code hbm.xml} sources.
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@ -80,18 +80,17 @@ public class HbmMetadataSourceProcessorImpl implements MetadataSourceProcessor {
} }
@Override @Override
public void processTypeDependentMetadata(MetadataSources sources) { public Iterable<FilterDefSource> extractFilterDefSources(MetadataSources sources) {
final List<FilterDefSource> filterDefSources = new ArrayList<FilterDefSource>();
for ( HibernateMappingProcessor processor : processors ) { for ( HibernateMappingProcessor processor : processors ) {
processor.processTypeDependentMetadata(); processor.collectFilterDefSources( filterDefSources );
} }
return filterDefSources;
} }
@Override @Override
public void processMappingMetadata(MetadataSources sources, List<String> processedEntityNames) { public Iterable<EntityHierarchyImpl> extractEntityHierarchies(MetadataSources sources) {
Binder binder = new Binder( metadata, processedEntityNames ); return entityHierarchies;
for ( EntityHierarchyImpl entityHierarchy : entityHierarchies ) {
binder.processEntityHierarchy( entityHierarchy );
}
} }
@Override @Override

View File

@ -23,37 +23,31 @@
*/ */
package org.hibernate.metamodel.internal.source.hbm; package org.hibernate.metamodel.internal.source.hbm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.Origin;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbParamElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.Value; import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.spi.binding.FetchProfile; import org.hibernate.metamodel.spi.binding.FetchProfile;
import org.hibernate.metamodel.spi.binding.TypeDef;
import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject; import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.spi.relational.BasicAuxiliaryDatabaseObjectImpl; import org.hibernate.metamodel.spi.relational.BasicAuxiliaryDatabaseObjectImpl;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.MappingException; import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource; import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException; import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.type.Type;
/** /**
* Responsible for processing a {@code <hibernate-mapping/>} element. Allows processing to be coordinated across * Responsible for processing a {@code <hibernate-mapping/>} element. Allows processing to be coordinated across
* all hbm files in an ordered fashion. The order is essentially the same as defined in * all hbm files in an ordered fashion. The order is essentially the same as defined in
* {@link org.hibernate.metamodel.spi.source.MetadataSourceProcessor} * {@link org.hibernate.metamodel.spi.MetadataSourceProcessor}
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@ -73,6 +67,7 @@ public class HibernateMappingProcessor {
public HibernateMappingProcessor(MetadataImplementor metadata, MappingDocument mappingDocument) { public HibernateMappingProcessor(MetadataImplementor metadata, MappingDocument mappingDocument) {
this.metadata = metadata; this.metadata = metadata;
this.mappingDocument = mappingDocument; this.mappingDocument = mappingDocument;
processDatabaseObjectDefinitions();
} }
private JaxbHibernateMapping mappingRoot() { private JaxbHibernateMapping mappingRoot() {
@ -91,10 +86,6 @@ public class HibernateMappingProcessor {
return classLoaderService.getValue().classForName( bindingContext().qualifyClassName( name ) ); return classLoaderService.getValue().classForName( bindingContext().qualifyClassName( name ) );
} }
public void processIndependentMetadata() {
processDatabaseObjectDefinitions();
}
private void processDatabaseObjectDefinitions() { private void processDatabaseObjectDefinitions() {
if ( mappingRoot().getDatabaseObject() == null ) { if ( mappingRoot().getDatabaseObject() == null ) {
return; return;
@ -140,53 +131,22 @@ public class HibernateMappingProcessor {
return; return;
} }
for ( JaxbHibernateMapping.JaxbTypedef typedef : mappingRoot().getTypedef() ) { for ( JaxbHibernateMapping.JaxbTypedef typeDefElement : mappingRoot().getTypedef() ) {
typeDescriptorSources.add( new TypeDescriptorSourceImpl( typedef ) ); typeDescriptorSources.add( new TypeDescriptorSourceImpl( typeDefElement ) );
} }
} }
public void processTypeDependentMetadata() { public void collectFilterDefSources(List<FilterDefSource> filterDefSources) {
processFilterDefinitions();
processIdentifierGenerators();
}
private void processFilterDefinitions() {
if ( mappingRoot().getFilterDef() == null ) { if ( mappingRoot().getFilterDef() == null ) {
return; return;
} }
for ( JaxbHibernateMapping.JaxbFilterDef filterDefinition : mappingRoot().getFilterDef() ) { for ( JaxbHibernateMapping.JaxbFilterDef filterDefElement : mappingRoot().getFilterDef() ) {
final String name = filterDefinition.getName(); filterDefSources.add( new FilterDefSourceImpl( filterDefElement ) );
final Map<String,Type> parameters = new HashMap<String, Type>();
String condition = null;
for ( Object o : filterDefinition.getContent() ) {
if ( o instanceof String ) {
// represents the condition
if ( condition != null ) {
// log?
}
condition = (String) o;
}
else if ( o instanceof JaxbHibernateMapping.JaxbFilterDef.JaxbFilterParam ) {
final JaxbHibernateMapping.JaxbFilterDef.JaxbFilterParam paramElement =
JaxbHibernateMapping.JaxbFilterDef.JaxbFilterParam.class.cast( o );
// todo : should really delay this resolution until later to allow typedef names
parameters.put(
paramElement.getName(),
metadata.getTypeResolver().heuristicType( paramElement.getType() )
);
}
else {
throw new MappingException( "Unrecognized nested filter content", origin() );
}
}
if ( condition == null ) {
condition = filterDefinition.getCondition();
}
metadata.addFilterDefinition( new FilterDefinition( name, condition, parameters ) );
} }
} }
private void processIdentifierGenerators() { private void processIdentifierGenerators() {
if ( mappingRoot().getIdentifierGenerator() == null ) { if ( mappingRoot().getIdentifierGenerator() == null ) {
return; return;

View File

@ -21,11 +21,12 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.metamodel.spi.source; package org.hibernate.metamodel.spi;
import java.util.List;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.spi.source.EntityHierarchy;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
/** /**
* Handles the processing of metadata sources in a dependency-ordered manner. * Handles the processing of metadata sources in a dependency-ordered manner.
@ -47,33 +48,31 @@ public interface MetadataSourceProcessor {
* *
* @return The type descriptor sources. * @return The type descriptor sources.
*/ */
public Iterable<TypeDescriptorSource> extractTypeDescriptorSources(MetadataSources sources); public Iterable<? extends TypeDescriptorSource> extractTypeDescriptorSources(MetadataSources sources);
/** /**
* Process the parts of the metadata that depend on type information (type definitions) having been processed * Retrieve the sources pertaining to filter defs.
* and available.
* *
* @param sources The metadata sources. * @param sources The metadata sources.
*
* @return The filter def sources.
*/ */
public void processTypeDependentMetadata(MetadataSources sources); public Iterable<? extends FilterDefSource> extractFilterDefSources(MetadataSources sources);
/** /**
* Process the mapping (entities, et al) metadata. * Retrieve the entity hierarchies.
* *
* @param sources The metadata sources. * @param sources The metadata sources.
* @param processedEntityNames Collection of any already processed entity names.
* *
* @see #processTypeDependentMetadata * @return The entity hierarchies
*/ */
public void processMappingMetadata(MetadataSources sources, List<String> processedEntityNames); public Iterable<? extends EntityHierarchy> extractEntityHierarchies(MetadataSources sources);
/** /**
* Process the parts of the metadata that depend on mapping (entities, et al) information having been * Process the parts of the metadata that depend on mapping (entities, et al) information having been
* processed and available. * processed and available.
* *
* @param sources The metadata sources. * @param sources The metadata sources.
*
* @see #processMappingMetadata
*/ */
public void processMappingDependentMetadata(MetadataSources sources); public void processMappingDependentMetadata(MetadataSources sources);
} }

View File

@ -53,8 +53,6 @@ public interface FilterDefSource {
* Retrieve parameter sources associated with this filer def. * Retrieve parameter sources associated with this filer def.
* *
* @return The parameter sources. Can be null. * @return The parameter sources. Can be null.
*
* @see FilterSource#getParameterSources()
*/ */
public Iterable<FilterParameterSource> getParameterSources(); public Iterable<FilterParameterSource> getParameterSources();
} }