Skip to content

HHH-19692 - Drop org.hibernate.boot.jaxb.spi.XmlSource #10708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p/>
* 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 <T> Binding<T> doBind(Binder<T> binder) {
if ( strict ) {
try {
return new Binding<>( readSerFile(), getOrigin() );
return new Binding<>( readSerFile( serFile ), origin );
}
catch ( SerializationException e ) {
throw new MappingException(
String.format( "Unable to deserialize from cached file [%s]", getOrigin().getName() ) ,
String.format( "Unable to deserialize from cached file [%s]", origin.getName() ) ,
e,
getOrigin()
origin
);
}
catch ( FileNotFoundException e ) {
throw new MappingException(
String.format( "Unable to locate cached file [%s]", getOrigin().getName() ) ,
String.format( "Unable to locate cached file [%s]", origin.getName() ) ,
e,
getOrigin()
origin
);
}
}
else {
if ( !isSerfileObsolete() ) {
if ( !isSerfileObsolete( xmlFile, serFile ) ) {
try {
return new Binding<>( readSerFile(), getOrigin() );
return new Binding<>( readSerFile( serFile ), origin );
}
catch ( SerializationException e ) {
log.unableToDeserializeCache( serFile.getName(), e );
Expand All @@ -96,32 +85,59 @@ public <T> Binding<T> doBind(Binder<T> binder) {
}

log.readingMappingsFromFile( xmlFile.getPath() );
final Binding<T> binding = FileXmlSource.doBind( binder, xmlFile, getOrigin() );
final Binding<? extends JaxbBindableMappingDescriptor> binding = FileXmlSource.fromFile( xmlFile, binder );

writeSerFile( binding );
writeSerFile( binding.getRoot(), xmlFile, serFile );

return binding;
}
}

private <T> T readSerFile() throws SerializationException, FileNotFoundException {
log.readingCachedMappings( serFile );
return SerializationHelper.deserialize( new FileInputStream( serFile ) );
/**
* Determine the ser file for a given mapping XML file.
*
* @param xmlFile The source mapping XML file
* @param serLocation The location details about the ser file. Can be one of 3 things:<ul>
* <li>{@code null} indicating we should {@linkplain #determineCachedFile(File) calculate} the File reference in the same directory as the {@code xmlFile}
* <li>a {@linkplain File#isDirectory() directory} indicating we should {@linkplain #determineCachedFile(File,File) calculate} the File reference in the given directory
* <il>the {@linkplain File#isFile() file} to use
* </ul>
*
* @return The ser file reference.
*/
public static File resolveSerFile(File xmlFile, File serLocation) {
if ( serLocation == null ) {
return determineCachedFile( xmlFile );
}
if ( serLocation.isDirectory() ) {
return determineCachedFile( xmlFile, serLocation );
}
assert serLocation.isFile();
return serLocation;
}

private void writeSerFile(Object binding) {
writeSerFile( (Serializable) binding, xmlFile, serFile );
public static File determineCachedFile(File xmlFile) {
return new File( xmlFile.getAbsolutePath() + ".bin" );
}

private static void writeSerFile(Serializable binding, File xmlFile, File serFile) {
if ( binding instanceof Binding<?> bindingWrapper ) {
binding = (Serializable) bindingWrapper.getRoot();
}
public static File determineCachedFile(File xmlFile, File serDirectory) {
return new File( serDirectory, xmlFile.getName() + ".bin" );
}

private static <T extends JaxbBindableMappingDescriptor> T readSerFile(File serFile) throws SerializationException, FileNotFoundException {
log.readingCachedMappings( serFile );
return SerializationHelper.deserialize( new FileInputStream( serFile ) );
}

private static <T extends JaxbBindableMappingDescriptor> void writeSerFile(
T jaxbModel,
File xmlFile,
File serFile) {
try ( FileOutputStream fos = new FileOutputStream( serFile ) ) {
if ( log.isTraceEnabled() ) {
log.tracef( "Writing cache file for: %s to: %s", xmlFile.getAbsolutePath(), serFile.getAbsolutePath() );
}
SerializationHelper.serialize( binding, fos );
SerializationHelper.serialize( jaxbModel, fos );
boolean success = serFile.setLastModified( System.currentTimeMillis() );
if ( !success ) {
log.warn( "Could not update cacheable hbm.xml bin file timestamp" );
Expand All @@ -132,21 +148,19 @@ private static void writeSerFile(Serializable binding, File xmlFile, File serFil
}
}

public static void createSerFile(File xmlFile, Binder binder) {
public static void createSerFile(File xmlFile, MappingBinder binder) {
createSerFile( xmlFile, determineCachedFile( xmlFile ), binder );
}

public static void createSerFile(File xmlFile, File outputFile, Binder binder) {
final Origin origin = new Origin( SourceType.FILE, xmlFile.getAbsolutePath() );
writeSerFile(
FileXmlSource.doBind( binder, xmlFile, origin ),
xmlFile,
outputFile
);
public static void createSerFile(File xmlFile, File outputFile, MappingBinder binder) {
final Binding<? extends JaxbBindableMappingDescriptor> binding = FileXmlSource.fromFile( xmlFile, binder );
writeSerFile( binding.getRoot(), xmlFile, outputFile );
}

private boolean isSerfileObsolete() {
return xmlFile.exists() && serFile.exists() && xmlFile.lastModified() > serFile.lastModified();
public static boolean isSerfileObsolete(File xmlFile, File serFile) {
return xmlFile.exists()
&& serFile.exists()
&& xmlFile.lastModified() > serFile.lastModified();
}

}
Loading