Skip to content

Commit 165472c

Browse files
committed
HHH-19692 - Drop org.hibernate.boot.jaxb.spi.XmlSource
1 parent 045a83a commit 165472c

File tree

15 files changed

+394
-459
lines changed

15 files changed

+394
-459
lines changed

hibernate-core/src/main/java/org/hibernate/boot/MetadataSources.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@
44
*/
55
package org.hibernate.boot;
66

7-
import java.io.File;
8-
import java.io.InputStream;
9-
import java.io.Serializable;
10-
import java.net.URL;
11-
import java.util.ArrayList;
12-
import java.util.Collection;
13-
import java.util.HashMap;
14-
import java.util.LinkedHashSet;
15-
import java.util.List;
16-
import java.util.Map;
17-
187
import org.hibernate.HibernateException;
198
import org.hibernate.Internal;
209
import org.hibernate.boot.archive.spi.InputStreamAccess;
2110
import org.hibernate.boot.internal.MetadataBuilderImpl;
2211
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
23-
import org.hibernate.boot.jaxb.internal.XmlSources;
12+
import org.hibernate.boot.jaxb.internal.CacheableFileXmlSource;
13+
import org.hibernate.boot.jaxb.internal.FileXmlSource;
14+
import org.hibernate.boot.jaxb.internal.InputStreamAccessXmlSource;
15+
import org.hibernate.boot.jaxb.internal.InputStreamXmlSource;
16+
import org.hibernate.boot.jaxb.internal.JarFileEntryXmlSource;
17+
import org.hibernate.boot.jaxb.internal.UrlXmlSource;
2418
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
2519
import org.hibernate.boot.jaxb.spi.Binding;
2620
import org.hibernate.boot.jaxb.spi.JaxbBindableMappingDescriptor;
27-
import org.hibernate.boot.jaxb.spi.XmlSource;
2821
import org.hibernate.boot.registry.BootstrapServiceRegistry;
2922
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
3023
import org.hibernate.boot.registry.StandardServiceRegistry;
@@ -36,6 +29,17 @@
3629
import org.hibernate.service.ServiceRegistry;
3730
import org.hibernate.type.SerializationException;
3831

32+
import java.io.File;
33+
import java.io.InputStream;
34+
import java.io.Serializable;
35+
import java.net.URL;
36+
import java.util.ArrayList;
37+
import java.util.Collection;
38+
import java.util.HashMap;
39+
import java.util.LinkedHashSet;
40+
import java.util.List;
41+
import java.util.Map;
42+
3943
import static java.util.Collections.addAll;
4044
import static java.util.Collections.emptyList;
4145
import static java.util.Collections.emptySet;
@@ -358,9 +362,8 @@ public MetadataSources addPackage(Package packageRef) {
358362
* @return this (for method chaining purposes)
359363
*/
360364
public MetadataSources addResource(String name) {
361-
final XmlSource xmlSource = XmlSources.fromResource( name, classLoaderService );
362365
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
363-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
366+
addXmlBinding( UrlXmlSource.fromResource( name, classLoaderService, binderAccess.getMappingBinder() ) );
364367
return this;
365368
}
366369

@@ -388,9 +391,8 @@ public MetadataSources addFile(String path) {
388391
* @return this (for method chaining purposes)
389392
*/
390393
public MetadataSources addFile(File file) {
391-
final XmlSource xmlSource = XmlSources.fromFile( file );
392394
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
393-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
395+
addXmlBinding( FileXmlSource.fromFile( file, binderAccess.getMappingBinder() ) );
394396
return this;
395397
}
396398

@@ -510,9 +512,13 @@ public MetadataSources addCacheableFile(File file) {
510512
* @return this (for method chaining purposes)
511513
*/
512514
public MetadataSources addCacheableFile(File file, File cacheDirectory) {
513-
final XmlSource xmlSource = XmlSources.fromCacheableFile( file, cacheDirectory );
514515
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
515-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
516+
addXmlBinding( CacheableFileXmlSource.fromCacheableFile(
517+
file,
518+
cacheDirectory,
519+
false,
520+
binderAccess.getMappingBinder()
521+
) );
516522
return this;
517523
}
518524

@@ -530,9 +536,13 @@ public MetadataSources addCacheableFile(File file, File cacheDirectory) {
530536
* @throws MappingNotFoundException Indicates that the cached file was not found or was not usable.
531537
*/
532538
public MetadataSources addCacheableFileStrictly(File file) throws SerializationException {
533-
final XmlSource xmlSource = XmlSources.fromCacheableFile( file, true );
534539
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
535-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
540+
addXmlBinding( CacheableFileXmlSource.fromCacheableFile(
541+
file,
542+
null,
543+
true,
544+
binderAccess.getMappingBinder()
545+
) );
536546
return this;
537547
}
538548

@@ -550,9 +560,13 @@ public MetadataSources addCacheableFileStrictly(File file) throws SerializationE
550560
* @throws MappingNotFoundException Indicates that the cached file was not found or was not usable.
551561
*/
552562
public MetadataSources addCacheableFileStrictly(File file, File cacheDir) throws SerializationException {
553-
final XmlSource xmlSource = XmlSources.fromCacheableFile( file, cacheDir, true );
554563
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
555-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
564+
addXmlBinding( CacheableFileXmlSource.fromCacheableFile(
565+
file,
566+
cacheDir,
567+
true,
568+
binderAccess.getMappingBinder()
569+
) );
556570
return this;
557571
}
558572

@@ -564,9 +578,8 @@ public MetadataSources addCacheableFileStrictly(File file, File cacheDir) throws
564578
* @return this (for method chaining purposes)
565579
*/
566580
public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) {
567-
final XmlSource xmlSource = XmlSources.fromStream( xmlInputStreamAccess );
568581
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
569-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
582+
addXmlBinding( InputStreamAccessXmlSource.fromStreamAccess( xmlInputStreamAccess, binderAccess.getMappingBinder() ) );
570583
return this;
571584
}
572585

@@ -578,9 +591,8 @@ public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) {
578591
* @return this (for method chaining purposes)
579592
*/
580593
public MetadataSources addInputStream(InputStream xmlInputStream) {
581-
final XmlSource xmlSource = XmlSources.fromStream( xmlInputStream );
582594
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
583-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
595+
addXmlBinding( InputStreamXmlSource.fromStream( xmlInputStream, binderAccess.getMappingBinder() ) );
584596
return this;
585597
}
586598

@@ -592,9 +604,8 @@ public MetadataSources addInputStream(InputStream xmlInputStream) {
592604
* @return this (for method chaining purposes)
593605
*/
594606
public MetadataSources addURL(URL url) {
595-
final XmlSource xmlSource = XmlSources.fromUrl( url );
596607
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
597-
addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) );
608+
addXmlBinding( UrlXmlSource.fromUrl( url, binderAccess.getMappingBinder() ) );
598609
return this;
599610
}
600611

@@ -610,10 +621,7 @@ public MetadataSources addURL(URL url) {
610621
*/
611622
public MetadataSources addJar(File jar) {
612623
final XmlMappingBinderAccess binderAccess = getXmlMappingBinderAccess();
613-
XmlSources.fromJar(
614-
jar,
615-
xmlSource -> addXmlBinding( xmlSource.doBind( binderAccess.getMappingBinder() ) )
616-
);
624+
JarFileEntryXmlSource.fromJar( jar, binderAccess.getMappingBinder(), this::addXmlBinding );
617625
return this;
618626
}
619627

hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,74 @@
44
*/
55
package org.hibernate.boot.jaxb.internal;
66

7-
import java.io.File;
8-
import java.io.FileInputStream;
9-
import java.io.FileNotFoundException;
10-
import java.io.FileOutputStream;
11-
import java.io.Serializable;
12-
137
import org.hibernate.boot.MappingException;
148
import org.hibernate.boot.jaxb.Origin;
159
import org.hibernate.boot.jaxb.SourceType;
16-
import org.hibernate.boot.jaxb.spi.Binder;
1710
import org.hibernate.boot.jaxb.spi.Binding;
18-
import org.hibernate.boot.jaxb.spi.XmlSource;
11+
import org.hibernate.boot.jaxb.spi.JaxbBindableMappingDescriptor;
1912
import org.hibernate.internal.CoreLogging;
2013
import org.hibernate.internal.CoreMessageLogger;
2114
import org.hibernate.internal.util.SerializationHelper;
2215
import org.hibernate.type.SerializationException;
2316

17+
import java.io.File;
18+
import java.io.FileInputStream;
19+
import java.io.FileNotFoundException;
20+
import java.io.FileOutputStream;
21+
2422
/**
23+
* Support for creating a mapping {@linkplain Binding binding} from "cached" XML files.
24+
* <p/>
25+
* This is a legacy feature, caching a serialized form of the {@linkplain JaxbBindableMappingDescriptor JAXB model}
26+
* into a file for later use. While not deprecated per se, its use is discouraged.
27+
*
28+
* @see MappingBinder
29+
*
2530
* @author Steve Ebersole
2631
*/
27-
public class CacheableFileXmlSource extends XmlSource {
32+
public class CacheableFileXmlSource {
2833
private static final CoreMessageLogger log = CoreLogging.messageLogger( CacheableFileXmlSource.class );
2934

30-
private final File xmlFile;
31-
private final File serFile;
32-
private final boolean strict;
33-
34-
public CacheableFileXmlSource(Origin origin, File xmlFile, File cachedFileDir, boolean strict) {
35-
super( origin );
36-
this.xmlFile = xmlFile;
37-
this.strict = strict;
38-
39-
this.serFile = new File( cachedFileDir, xmlFile.getName() + ".bin" );
40-
41-
if ( strict ) {
42-
if ( !serFile.exists() ) {
43-
throw new MappingException(
44-
String.format( "Cached file [%s] could not be found", origin.getName() ),
45-
origin
46-
);
47-
}
48-
if ( isSerfileObsolete() ) {
49-
throw new MappingException(
50-
String.format( "Cached file [%s] could not be used as the mapping file is newer", origin.getName() ),
51-
origin
52-
);
53-
}
54-
}
35+
public static Binding<? extends JaxbBindableMappingDescriptor> fromCacheableFile(
36+
File xmlFile,
37+
File serLocation,
38+
boolean strict,
39+
MappingBinder binder) {
40+
final Origin origin = new Origin( SourceType.FILE, xmlFile.getAbsolutePath() );
41+
return fromCacheableFile( xmlFile, serLocation, origin, strict, binder );
5542
}
5643

57-
public static File determineCachedFile(File xmlFile) {
58-
return new File( xmlFile.getAbsolutePath() + ".bin" );
59-
}
44+
public static Binding<? extends JaxbBindableMappingDescriptor> fromCacheableFile(
45+
File xmlFile,
46+
File serLocation,
47+
Origin origin,
48+
boolean strict,
49+
MappingBinder binder) {
50+
final File serFile = resolveSerFile( xmlFile, serLocation );
6051

61-
@Override
62-
public <T> Binding<T> doBind(Binder<T> binder) {
6352
if ( strict ) {
6453
try {
65-
return new Binding<>( readSerFile(), getOrigin() );
54+
return new Binding<>( readSerFile( serFile ), origin );
6655
}
6756
catch ( SerializationException e ) {
6857
throw new MappingException(
69-
String.format( "Unable to deserialize from cached file [%s]", getOrigin().getName() ) ,
58+
String.format( "Unable to deserialize from cached file [%s]", origin.getName() ) ,
7059
e,
71-
getOrigin()
60+
origin
7261
);
7362
}
7463
catch ( FileNotFoundException e ) {
7564
throw new MappingException(
76-
String.format( "Unable to locate cached file [%s]", getOrigin().getName() ) ,
65+
String.format( "Unable to locate cached file [%s]", origin.getName() ) ,
7766
e,
78-
getOrigin()
67+
origin
7968
);
8069
}
8170
}
8271
else {
83-
if ( !isSerfileObsolete() ) {
72+
if ( !isSerfileObsolete( xmlFile, serFile ) ) {
8473
try {
85-
return new Binding<>( readSerFile(), getOrigin() );
74+
return new Binding<>( readSerFile( serFile ), origin );
8675
}
8776
catch ( SerializationException e ) {
8877
log.unableToDeserializeCache( serFile.getName(), e );
@@ -96,32 +85,59 @@ public <T> Binding<T> doBind(Binder<T> binder) {
9685
}
9786

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

101-
writeSerFile( binding );
90+
writeSerFile( binding.getRoot(), xmlFile, serFile );
10291

10392
return binding;
10493
}
10594
}
10695

107-
private <T> T readSerFile() throws SerializationException, FileNotFoundException {
108-
log.readingCachedMappings( serFile );
109-
return SerializationHelper.deserialize( new FileInputStream( serFile ) );
96+
/**
97+
* Determine the ser file for a given mapping XML file.
98+
*
99+
* @param xmlFile The source mapping XML file
100+
* @param serLocation The location details about the ser file. Can be one of 3 things:<ul>
101+
* <li>{@code null} indicating we should {@linkplain #determineCachedFile(File) calculate} the File reference in the same directory as the {@code xmlFile}
102+
* <li>a {@linkplain File#isDirectory() directory} indicating we should {@linkplain #determineCachedFile(File,File) calculate} the File reference in the given directory
103+
* <il>the {@linkplain File#isFile() file} to use
104+
* </ul>
105+
*
106+
* @return The ser file reference.
107+
*/
108+
public static File resolveSerFile(File xmlFile, File serLocation) {
109+
if ( serLocation == null ) {
110+
return determineCachedFile( xmlFile );
111+
}
112+
if ( serLocation.isDirectory() ) {
113+
return determineCachedFile( xmlFile, serLocation );
114+
}
115+
assert serLocation.isFile();
116+
return serLocation;
110117
}
111118

112-
private void writeSerFile(Object binding) {
113-
writeSerFile( (Serializable) binding, xmlFile, serFile );
119+
public static File determineCachedFile(File xmlFile) {
120+
return new File( xmlFile.getAbsolutePath() + ".bin" );
114121
}
115122

116-
private static void writeSerFile(Serializable binding, File xmlFile, File serFile) {
117-
if ( binding instanceof Binding<?> bindingWrapper ) {
118-
binding = (Serializable) bindingWrapper.getRoot();
119-
}
123+
public static File determineCachedFile(File xmlFile, File serDirectory) {
124+
return new File( serDirectory, xmlFile.getName() + ".bin" );
125+
}
126+
127+
private static <T extends JaxbBindableMappingDescriptor> T readSerFile(File serFile) throws SerializationException, FileNotFoundException {
128+
log.readingCachedMappings( serFile );
129+
return SerializationHelper.deserialize( new FileInputStream( serFile ) );
130+
}
131+
132+
private static <T extends JaxbBindableMappingDescriptor> void writeSerFile(
133+
T jaxbModel,
134+
File xmlFile,
135+
File serFile) {
120136
try ( FileOutputStream fos = new FileOutputStream( serFile ) ) {
121137
if ( log.isTraceEnabled() ) {
122138
log.tracef( "Writing cache file for: %s to: %s", xmlFile.getAbsolutePath(), serFile.getAbsolutePath() );
123139
}
124-
SerializationHelper.serialize( binding, fos );
140+
SerializationHelper.serialize( jaxbModel, fos );
125141
boolean success = serFile.setLastModified( System.currentTimeMillis() );
126142
if ( !success ) {
127143
log.warn( "Could not update cacheable hbm.xml bin file timestamp" );
@@ -132,21 +148,19 @@ private static void writeSerFile(Serializable binding, File xmlFile, File serFil
132148
}
133149
}
134150

135-
public static void createSerFile(File xmlFile, Binder binder) {
151+
public static void createSerFile(File xmlFile, MappingBinder binder) {
136152
createSerFile( xmlFile, determineCachedFile( xmlFile ), binder );
137153
}
138154

139-
public static void createSerFile(File xmlFile, File outputFile, Binder binder) {
140-
final Origin origin = new Origin( SourceType.FILE, xmlFile.getAbsolutePath() );
141-
writeSerFile(
142-
FileXmlSource.doBind( binder, xmlFile, origin ),
143-
xmlFile,
144-
outputFile
145-
);
155+
public static void createSerFile(File xmlFile, File outputFile, MappingBinder binder) {
156+
final Binding<? extends JaxbBindableMappingDescriptor> binding = FileXmlSource.fromFile( xmlFile, binder );
157+
writeSerFile( binding.getRoot(), xmlFile, outputFile );
146158
}
147159

148-
private boolean isSerfileObsolete() {
149-
return xmlFile.exists() && serFile.exists() && xmlFile.lastModified() > serFile.lastModified();
160+
public static boolean isSerfileObsolete(File xmlFile, File serFile) {
161+
return xmlFile.exists()
162+
&& serFile.exists()
163+
&& xmlFile.lastModified() > serFile.lastModified();
150164
}
151165

152166
}

0 commit comments

Comments
 (0)