44 */
55package 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-
137import org .hibernate .boot .MappingException ;
148import org .hibernate .boot .jaxb .Origin ;
159import org .hibernate .boot .jaxb .SourceType ;
16- import org .hibernate .boot .jaxb .spi .Binder ;
1710import org .hibernate .boot .jaxb .spi .Binding ;
18- import org .hibernate .boot .jaxb .spi .XmlSource ;
11+ import org .hibernate .boot .jaxb .spi .JaxbBindableMappingDescriptor ;
1912import org .hibernate .internal .CoreLogging ;
2013import org .hibernate .internal .CoreMessageLogger ;
2114import org .hibernate .internal .util .SerializationHelper ;
2215import 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