HHH-13662 Avoid initializing XmlMappingBinderAccess when no XML mappings are defined

This commit is contained in:
Sanne Grinovero 2019-10-10 09:38:22 +01:00
parent 404bc196f7
commit 9d6463eab6
1 changed files with 60 additions and 29 deletions

View File

@ -14,6 +14,7 @@ import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -59,10 +60,10 @@ public class MetadataSources implements Serializable {
private XmlMappingBinderAccess xmlMappingBinderAccess; private XmlMappingBinderAccess xmlMappingBinderAccess;
private List<Binding> xmlBindings = new ArrayList<>(); private List<Binding> xmlBindings;
private LinkedHashSet<Class<?>> annotatedClasses = new LinkedHashSet<>(); private LinkedHashSet<Class<?>> annotatedClasses;
private LinkedHashSet<String> annotatedClassNames = new LinkedHashSet<>(); private LinkedHashSet<String> annotatedClassNames;
private LinkedHashSet<String> annotatedPackages = new LinkedHashSet<>(); private LinkedHashSet<String> annotatedPackages;
public MetadataSources() { public MetadataSources() {
this( new BootstrapServiceRegistryBuilder().build() ); this( new BootstrapServiceRegistryBuilder().build() );
@ -76,14 +77,15 @@ public class MetadataSources implements Serializable {
public MetadataSources(ServiceRegistry serviceRegistry) { public MetadataSources(ServiceRegistry serviceRegistry) {
// service registry really should be either BootstrapServiceRegistry or StandardServiceRegistry type... // service registry really should be either BootstrapServiceRegistry or StandardServiceRegistry type...
if ( ! isExpectedServiceRegistryType( serviceRegistry ) ) { if ( ! isExpectedServiceRegistryType( serviceRegistry ) ) {
LOG.debugf( if ( LOG.isDebugEnabled() ) {
"Unexpected ServiceRegistry type [%s] encountered during building of MetadataSources; may cause " + LOG.debugf(
"problems later attempting to construct MetadataBuilder", "Unexpected ServiceRegistry type [%s] encountered during building of MetadataSources; may cause " +
serviceRegistry.getClass().getName() "problems later attempting to construct MetadataBuilder",
); serviceRegistry.getClass().getName()
);
}
} }
this.serviceRegistry = serviceRegistry; this.serviceRegistry = serviceRegistry;
this.xmlMappingBinderAccess = new XmlMappingBinderAccess( serviceRegistry );
} }
protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) { protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) {
@ -92,23 +94,26 @@ public class MetadataSources implements Serializable {
} }
public XmlMappingBinderAccess getXmlMappingBinderAccess() { public XmlMappingBinderAccess getXmlMappingBinderAccess() {
if ( xmlMappingBinderAccess == null ) {
xmlMappingBinderAccess = new XmlMappingBinderAccess( serviceRegistry );
}
return xmlMappingBinderAccess; return xmlMappingBinderAccess;
} }
public List<Binding> getXmlBindings() { public List<Binding> getXmlBindings() {
return xmlBindings; return xmlBindings == null ? Collections.emptyList() : xmlBindings;
} }
public Collection<String> getAnnotatedPackages() { public Collection<String> getAnnotatedPackages() {
return annotatedPackages; return annotatedPackages == null ? Collections.emptySet() : annotatedPackages;
} }
public Collection<Class<?>> getAnnotatedClasses() { public Collection<Class<?>> getAnnotatedClasses() {
return annotatedClasses; return annotatedClasses == null ? Collections.emptySet() : annotatedClasses;
} }
public Collection<String> getAnnotatedClassNames() { public Collection<String> getAnnotatedClassNames() {
return annotatedClassNames; return annotatedClassNames == null ? Collections.emptySet() : annotatedClassNames;
} }
public ServiceRegistry getServiceRegistry() { public ServiceRegistry getServiceRegistry() {
@ -192,6 +197,9 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining) * @return this (for method chaining)
*/ */
public MetadataSources addAnnotatedClass(Class annotatedClass) { public MetadataSources addAnnotatedClass(Class annotatedClass) {
if ( annotatedClasses == null ) {
annotatedClasses = new LinkedHashSet<>();
}
annotatedClasses.add( annotatedClass ); annotatedClasses.add( annotatedClass );
return this; return this;
} }
@ -206,6 +214,9 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining) * @return this (for method chaining)
*/ */
public MetadataSources addAnnotatedClassName(String annotatedClassName) { public MetadataSources addAnnotatedClassName(String annotatedClassName) {
if ( annotatedClassNames == null ) {
annotatedClassNames = new LinkedHashSet<>();
}
annotatedClassNames.add( annotatedClassName ); annotatedClassNames.add( annotatedClassName );
return this; return this;
} }
@ -226,11 +237,17 @@ public class MetadataSources implements Serializable {
packageName = packageName.substring( 0, packageName.length() - 1 ); packageName = packageName.substring( 0, packageName.length() - 1 );
} }
annotatedPackages.add( packageName ); addPackageInternal( packageName );
return this; return this;
} }
private void addPackageInternal(String packageName) {
if ( annotatedPackages == null ) {
annotatedPackages = new LinkedHashSet<>();
}
annotatedPackages.add( packageName );
}
/** /**
* Read package-level metadata. * Read package-level metadata.
* *
@ -239,7 +256,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining) * @return this (for method chaining)
*/ */
public MetadataSources addPackage(Package packageRef) { public MetadataSources addPackage(Package packageRef) {
annotatedPackages.add( packageRef.getName() ); addPackageInternal( packageRef.getName() );
return this; return this;
} }
@ -258,7 +275,9 @@ public class MetadataSources implements Serializable {
if ( entityClass == null ) { if ( entityClass == null ) {
throw new IllegalArgumentException( "The specified class cannot be null" ); throw new IllegalArgumentException( "The specified class cannot be null" );
} }
LOG.debugf( "adding resource mappings from class convention : %s", entityClass.getName() ); if ( LOG.isDebugEnabled() ) {
LOG.debugf( "adding resource mappings from class convention : %s", entityClass.getName() );
}
final String mappingResourceName = entityClass.getName().replace( '.', '/' ) + ".hbm.xml"; final String mappingResourceName = entityClass.getName().replace( '.', '/' ) + ".hbm.xml";
addResource( mappingResourceName ); addResource( mappingResourceName );
return this; return this;
@ -272,7 +291,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addResource(String name) { public MetadataSources addResource(String name) {
xmlBindings.add( getXmlMappingBinderAccess().bind( name ) ); getXmlBindingsForWrite().add( getXmlMappingBinderAccess().bind( name ) );
return this; return this;
} }
@ -298,7 +317,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addFile(File file) { public MetadataSources addFile(File file) {
xmlBindings.add( getXmlMappingBinderAccess().bind( file ) ); getXmlBindingsForWrite().add( getXmlMappingBinderAccess().bind( file ) );
return this; return this;
} }
@ -318,7 +337,7 @@ public class MetadataSources implements Serializable {
} }
private void addCacheableFile(Origin origin, File file) { private void addCacheableFile(Origin origin, File file) {
xmlBindings.add( new CacheableFileXmlSource( origin, file, false ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) ); getXmlBindingsForWrite().add( new CacheableFileXmlSource( origin, file, false ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) );
} }
/** /**
@ -355,7 +374,7 @@ public class MetadataSources implements Serializable {
*/ */
public MetadataSources addCacheableFileStrictly(File file) throws SerializationException, FileNotFoundException { public MetadataSources addCacheableFileStrictly(File file) throws SerializationException, FileNotFoundException {
final Origin origin = new Origin( SourceType.FILE, file.getAbsolutePath() ); final Origin origin = new Origin( SourceType.FILE, file.getAbsolutePath() );
xmlBindings.add( new CacheableFileXmlSource( origin, file, true ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) ); getXmlBindingsForWrite().add( new CacheableFileXmlSource( origin, file, true ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) );
return this; return this;
} }
@ -367,7 +386,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) { public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) {
xmlBindings.add( getXmlMappingBinderAccess().bind( xmlInputStreamAccess ) ); getXmlBindingsForWrite().add( getXmlMappingBinderAccess().bind( xmlInputStreamAccess ) );
return this; return this;
} }
@ -379,7 +398,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addInputStream(InputStream xmlInputStream) { public MetadataSources addInputStream(InputStream xmlInputStream) {
xmlBindings.add( getXmlMappingBinderAccess().bind( xmlInputStream ) ); getXmlBindingsForWrite().add( getXmlMappingBinderAccess().bind( xmlInputStream ) );
return this; return this;
} }
@ -391,7 +410,7 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addURL(URL url) { public MetadataSources addURL(URL url) {
xmlBindings.add( getXmlMappingBinderAccess().bind( url ) ); getXmlBindingsForWrite().add( getXmlMappingBinderAccess().bind( url ) );
return this; return this;
} }
@ -407,7 +426,7 @@ public class MetadataSources implements Serializable {
@Deprecated @Deprecated
public MetadataSources addDocument(Document document) { public MetadataSources addDocument(Document document) {
final Origin origin = new Origin( SourceType.DOM, Origin.UNKNOWN_FILE_PATH ); final Origin origin = new Origin( SourceType.DOM, Origin.UNKNOWN_FILE_PATH );
xmlBindings.add( new JaxpSourceXmlSource( origin, new DOMSource( document ) ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) ); getXmlBindingsForWrite().add( new JaxpSourceXmlSource( origin, new DOMSource( document ) ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) );
return this; return this;
} }
@ -421,17 +440,22 @@ public class MetadataSources implements Serializable {
* @return this (for method chaining purposes) * @return this (for method chaining purposes)
*/ */
public MetadataSources addJar(File jar) { public MetadataSources addJar(File jar) {
LOG.debugf( "Seeking mapping documents in jar file : %s", jar.getName() ); if ( LOG.isDebugEnabled() ) {
LOG.debugf( "Seeking mapping documents in jar file : %s", jar.getName() );
}
final Origin origin = new Origin( SourceType.JAR, jar.getAbsolutePath() ); final Origin origin = new Origin( SourceType.JAR, jar.getAbsolutePath() );
try { try {
JarFile jarFile = new JarFile( jar ); JarFile jarFile = new JarFile( jar );
final boolean TRACE = LOG.isTraceEnabled();
try { try {
Enumeration jarEntries = jarFile.entries(); Enumeration jarEntries = jarFile.entries();
while ( jarEntries.hasMoreElements() ) { while ( jarEntries.hasMoreElements() ) {
final ZipEntry zipEntry = (ZipEntry) jarEntries.nextElement(); final ZipEntry zipEntry = (ZipEntry) jarEntries.nextElement();
if ( zipEntry.getName().endsWith( ".hbm.xml" ) ) { if ( zipEntry.getName().endsWith( ".hbm.xml" ) ) {
LOG.tracef( "found mapping document : %s", zipEntry.getName() ); if ( TRACE ) {
xmlBindings.add( LOG.tracef( "found mapping document : %s", zipEntry.getName() );
}
getXmlBindingsForWrite().add(
new JarFileEntryXmlSource( origin, jarFile, zipEntry ).doBind( getXmlMappingBinderAccess().getMappingBinder() ) new JarFileEntryXmlSource( origin, jarFile, zipEntry ).doBind( getXmlMappingBinderAccess().getMappingBinder() )
); );
} }
@ -451,6 +475,13 @@ public class MetadataSources implements Serializable {
return this; return this;
} }
private <Binding> List getXmlBindingsForWrite() {
if ( xmlBindings == null ) {
xmlBindings = new ArrayList<>();
}
return xmlBindings;
}
/** /**
* Read all mapping documents from a directory tree. * Read all mapping documents from a directory tree.
* <p/> * <p/>