diff --git a/hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java b/hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java index 48a6b5187487..4879626636a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java @@ -4,27 +4,20 @@ */ package org.hibernate.boot; -import java.io.File; -import java.io.InputStream; -import java.io.Serializable; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; - import org.hibernate.HibernateException; import org.hibernate.Internal; import org.hibernate.boot.archive.spi.InputStreamAccess; import org.hibernate.boot.internal.MetadataBuilderImpl; import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; -import org.hibernate.boot.jaxb.internal.XmlSources; +import org.hibernate.boot.jaxb.internal.CacheableFileXmlSource; +import org.hibernate.boot.jaxb.internal.FileXmlSource; +import org.hibernate.boot.jaxb.internal.InputStreamAccessXmlSource; +import org.hibernate.boot.jaxb.internal.InputStreamXmlSource; +import org.hibernate.boot.jaxb.internal.JarFileEntryXmlSource; +import org.hibernate.boot.jaxb.internal.UrlXmlSource; import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl; import org.hibernate.boot.jaxb.spi.Binding; import org.hibernate.boot.jaxb.spi.JaxbBindableMappingDescriptor; -import org.hibernate.boot.jaxb.spi.XmlSource; import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistry; @@ -36,6 +29,17 @@ import org.hibernate.service.ServiceRegistry; import org.hibernate.type.SerializationException; +import java.io.File; +import java.io.InputStream; +import java.io.Serializable; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; + import static java.util.Collections.addAll; import static java.util.Collections.emptyList; import static java.util.Collections.emptySet; @@ -358,9 +362,8 @@ public MetadataSources addPackage(Package packageRef) { * @return this (for method chaining purposes) */ public MetadataSources addResource(String name) { - final XmlSource xmlSource = XmlSources.fromResource( name, classLoaderService ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( UrlXmlSource.fromResource( name, classLoaderService, binderAccess.getMappingBinder() ) ); return this; } @@ -388,9 +391,8 @@ public MetadataSources addFile(String path) { * @return this (for method chaining purposes) */ public MetadataSources addFile(File file) { - final XmlSource xmlSource = XmlSources.fromFile( file ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( FileXmlSource.fromFile( file, binderAccess.getMappingBinder() ) ); return this; } @@ -510,9 +512,13 @@ public MetadataSources addCacheableFile(File file) { * @return this (for method chaining purposes) */ public MetadataSources addCacheableFile(File file, File cacheDirectory) { - final XmlSource xmlSource = XmlSources.fromCacheableFile( file, cacheDirectory ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( CacheableFileXmlSource.fromCacheableFile( + file, + cacheDirectory, + false, + binderAccess.getMappingBinder() + ) ); return this; } @@ -530,9 +536,13 @@ public MetadataSources addCacheableFile(File file, File cacheDirectory) { * @throws MappingNotFoundException Indicates that the cached file was not found or was not usable. */ public MetadataSources addCacheableFileStrictly(File file) throws SerializationException { - final XmlSource xmlSource = XmlSources.fromCacheableFile( file, true ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( CacheableFileXmlSource.fromCacheableFile( + file, + null, + true, + binderAccess.getMappingBinder() + ) ); return this; } @@ -550,9 +560,13 @@ public MetadataSources addCacheableFileStrictly(File file) throws SerializationE * @throws MappingNotFoundException Indicates that the cached file was not found or was not usable. */ public MetadataSources addCacheableFileStrictly(File file, File cacheDir) throws SerializationException { - final XmlSource xmlSource = XmlSources.fromCacheableFile( file, cacheDir, true ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( CacheableFileXmlSource.fromCacheableFile( + file, + cacheDir, + true, + binderAccess.getMappingBinder() + ) ); return this; } @@ -564,9 +578,8 @@ public MetadataSources addCacheableFileStrictly(File file, File cacheDir) throws * @return this (for method chaining purposes) */ public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) { - final XmlSource xmlSource = XmlSources.fromStream( xmlInputStreamAccess ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( InputStreamAccessXmlSource.fromStreamAccess( xmlInputStreamAccess, binderAccess.getMappingBinder() ) ); return this; } @@ -578,9 +591,8 @@ public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) { * @return this (for method chaining purposes) */ public MetadataSources addInputStream(InputStream xmlInputStream) { - final XmlSource xmlSource = XmlSources.fromStream( xmlInputStream ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( InputStreamXmlSource.fromStream( xmlInputStream, binderAccess.getMappingBinder() ) ); return this; } @@ -592,9 +604,8 @@ public MetadataSources addInputStream(InputStream xmlInputStream) { * @return this (for method chaining purposes) */ public MetadataSources addURL(URL url) { - final XmlSource xmlSource = XmlSources.fromUrl( url ); final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ); + addXmlBinding( UrlXmlSource.fromUrl( url, binderAccess.getMappingBinder() ) ); return this; } @@ -610,10 +621,7 @@ public MetadataSources addURL(URL url) { */ public MetadataSources addJar(File jar) { final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess(); - XmlSources.fromJar( - jar, - xmlSource -> addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) ) - ); + JarFileEntryXmlSource.fromJar( jar, binderAccess.getMappingBinder(), this::addXmlBinding ); return this; } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java index 0cfa4a06d2e6..15109c4cb83b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java @@ -4,85 +4,74 @@ */ package org.hibernate.boot.jaxb.internal; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.Serializable; - import org.hibernate.boot.MappingException; import org.hibernate.boot.jaxb.Origin; import org.hibernate.boot.jaxb.SourceType; -import org.hibernate.boot.jaxb.spi.Binder; import org.hibernate.boot.jaxb.spi.Binding; -import org.hibernate.boot.jaxb.spi.XmlSource; +import org.hibernate.boot.jaxb.spi.JaxbBindableMappingDescriptor; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.SerializationHelper; import org.hibernate.type.SerializationException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + /** + * Support for creating a mapping {@linkplain Binding binding} from "cached" XML files. + *
+ * This is a legacy feature, caching a serialized form of the {@linkplain JaxbBindableMappingDescriptor JAXB model} + * into a file for later use. While not deprecated per se, its use is discouraged. + * + * @see MappingBinder + * * @author Steve Ebersole */ -public class CacheableFileXmlSource extends XmlSource { +public class CacheableFileXmlSource { private static final CoreMessageLogger log = CoreLogging.messageLogger( CacheableFileXmlSource.class ); - private final File xmlFile; - private final File serFile; - private final boolean strict; - - public CacheableFileXmlSource(Origin origin, File xmlFile, File cachedFileDir, boolean strict) { - super( origin ); - this.xmlFile = xmlFile; - this.strict = strict; - - this.serFile = new File( cachedFileDir, xmlFile.getName() + ".bin" ); - - if ( strict ) { - if ( !serFile.exists() ) { - throw new MappingException( - String.format( "Cached file [%s] could not be found", origin.getName() ), - origin - ); - } - if ( isSerfileObsolete() ) { - throw new MappingException( - String.format( "Cached file [%s] could not be used as the mapping file is newer", origin.getName() ), - origin - ); - } - } + public static Binding extends JaxbBindableMappingDescriptor> fromCacheableFile( + File xmlFile, + File serLocation, + boolean strict, + MappingBinder binder) { + final Origin origin = new Origin( SourceType.FILE, xmlFile.getAbsolutePath() ); + return fromCacheableFile( xmlFile, serLocation, origin, strict, binder ); } - public static File determineCachedFile(File xmlFile) { - return new File( xmlFile.getAbsolutePath() + ".bin" ); - } + public static Binding extends JaxbBindableMappingDescriptor> fromCacheableFile( + File xmlFile, + File serLocation, + Origin origin, + boolean strict, + MappingBinder binder) { + final File serFile = resolveSerFile( xmlFile, serLocation ); - @Override - public- * An {@code XmlSource} represents an XML document containing - * O/R mapping metadata, either a JPA {@code orm.xml} file, or a - * Hibernate {@code .hbm.xml} file. - * - * @author Steve Ebersole - */ -public class XmlSources { - /** - * Create an {@link XmlSource} from a named resource - */ - public static XmlSource fromResource(String resourceName, ClassLoaderService classLoaderService) { - JAXB_LOGGER.tracef( "Reading mappings from resource: %s", resourceName ); - - final Origin origin = new Origin( SourceType.RESOURCE, resourceName ); - final URL url = classLoaderService.locateResource( resourceName ); - if ( url == null ) { - throw new MappingNotFoundException( origin ); - } - - return new UrlXmlSource( origin, url ); - } - - /** - * Create an {@link XmlSource} from a URL - */ - public static XmlSource fromUrl(URL url) { - final String urlExternalForm = url.toExternalForm(); - JAXB_LOGGER.tracef( "Reading mapping document from URL: %s", urlExternalForm ); - - final Origin origin = new Origin( SourceType.URL, urlExternalForm ); - return new UrlXmlSource( origin, url ); - } - - public static XmlSource fromFile(File file) { - final String filePath = file.getPath(); - JAXB_LOGGER.tracef( "Reading mappings from file: %s", filePath ); - - final Origin origin = new Origin( SourceType.FILE, filePath ); - - if ( !file.exists() ) { - throw new MappingNotFoundException( origin ); - } - - return new FileXmlSource( origin, file ); - } - - public static XmlSource fromCacheableFile(File file) { - return fromCacheableFile( file, file.getParentFile() ); - } - - public static XmlSource fromCacheableFile(File file, File cacheableDir) { - return fromCacheableFile( file, cacheableDir, false ); - } - - public static XmlSource fromCacheableFile(File file, boolean strict) { - return fromCacheableFile( file, file.getParentFile(), strict ); - } - - public static XmlSource fromCacheableFile(File file, File cacheableDir, boolean strict) { - final String filePath = file.getPath(); - JAXB_LOGGER.tracef( "Reading mappings from cacheable file: %s", filePath ); - - final Origin origin = new Origin( SourceType.FILE, filePath ); - return new CacheableFileXmlSource( origin, file, cacheableDir, strict ); - } - - public static XmlSource fromStream(InputStreamAccess inputStreamAccess) { - final String streamName = inputStreamAccess.getStreamName(); - JAXB_LOGGER.tracef( "Reading mappings from InputStreamAccess: %s", streamName ); - - final Origin origin = new Origin( SourceType.INPUT_STREAM, streamName ); - return new InputStreamAccessXmlSource( origin, inputStreamAccess ); - } - - public static XmlSource fromStream(InputStream inputStream) { - JAXB_LOGGER.trace( "reading mappings from InputStream" ); - - final Origin origin = new Origin( SourceType.INPUT_STREAM, null ); - return new InputStreamXmlSource( origin, inputStream, false ); - } - - public static XmlSource fromDocument(Document document) { - JAXB_LOGGER.trace( "reading mappings from DOM" ); - final Origin origin = new Origin( SourceType.DOM, Origin.UNKNOWN_FILE_PATH ); - return new JaxpSourceXmlSource( origin, new DOMSource( document ) ); - } - - /** - * Read all {@code .hbm.xml} mappings from a jar file and pass them - * to the given {@link Consumer}. - *
- * Assumes that any file named {@code *.hbm.xml} is a mapping document.
- * This method does not support {@code orm.xml} files!
- *
- * @param jar a jar file
- * @param consumer a consumer of the resulting {@linkplain XmlSource XML sources}
- */
- public static void fromJar(File jar, Consumer
- *
- *
- * @author Steve Ebersole
- */
-public abstract class XmlSource {
- private final Origin origin;
-
- protected XmlSource(Origin origin) {
- this.origin = origin;
- }
-
- public Origin getOrigin() {
- return origin;
- }
-
- public abstract