4
4
*/
5
5
package org .hibernate .boot .jaxb .internal ;
6
6
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
-
13
7
import org .hibernate .boot .MappingException ;
14
8
import org .hibernate .boot .jaxb .Origin ;
15
9
import org .hibernate .boot .jaxb .SourceType ;
16
- import org .hibernate .boot .jaxb .spi .Binder ;
17
10
import org .hibernate .boot .jaxb .spi .Binding ;
18
- import org .hibernate .boot .jaxb .spi .XmlSource ;
11
+ import org .hibernate .boot .jaxb .spi .JaxbBindableMappingDescriptor ;
19
12
import org .hibernate .internal .CoreLogging ;
20
13
import org .hibernate .internal .CoreMessageLogger ;
21
14
import org .hibernate .internal .util .SerializationHelper ;
22
15
import org .hibernate .type .SerializationException ;
23
16
17
+ import java .io .File ;
18
+ import java .io .FileInputStream ;
19
+ import java .io .FileNotFoundException ;
20
+ import java .io .FileOutputStream ;
21
+
24
22
/**
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
+ *
25
30
* @author Steve Ebersole
26
31
*/
27
- public class CacheableFileXmlSource extends XmlSource {
32
+ public class CacheableFileXmlSource {
28
33
private static final CoreMessageLogger log = CoreLogging .messageLogger ( CacheableFileXmlSource .class );
29
34
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 );
55
42
}
56
43
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 );
60
51
61
- @ Override
62
- public <T > Binding <T > doBind (Binder <T > binder ) {
63
52
if ( strict ) {
64
53
try {
65
- return new Binding <>( readSerFile (), getOrigin () );
54
+ return new Binding <>( readSerFile ( serFile ), origin );
66
55
}
67
56
catch ( SerializationException e ) {
68
57
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 () ) ,
70
59
e ,
71
- getOrigin ()
60
+ origin
72
61
);
73
62
}
74
63
catch ( FileNotFoundException e ) {
75
64
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 () ) ,
77
66
e ,
78
- getOrigin ()
67
+ origin
79
68
);
80
69
}
81
70
}
82
71
else {
83
- if ( !isSerfileObsolete () ) {
72
+ if ( !isSerfileObsolete ( xmlFile , serFile ) ) {
84
73
try {
85
- return new Binding <>( readSerFile (), getOrigin () );
74
+ return new Binding <>( readSerFile ( serFile ), origin );
86
75
}
87
76
catch ( SerializationException e ) {
88
77
log .unableToDeserializeCache ( serFile .getName (), e );
@@ -96,32 +85,59 @@ public <T> Binding<T> doBind(Binder<T> binder) {
96
85
}
97
86
98
87
log .readingMappingsFromFile ( xmlFile .getPath () );
99
- final Binding <T > binding = FileXmlSource .doBind ( binder , xmlFile , getOrigin () );
88
+ final Binding <? extends JaxbBindableMappingDescriptor > binding = FileXmlSource .fromFile ( xmlFile , binder );
100
89
101
- writeSerFile ( binding );
90
+ writeSerFile ( binding . getRoot (), xmlFile , serFile );
102
91
103
92
return binding ;
104
93
}
105
94
}
106
95
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 ;
110
117
}
111
118
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" );
114
121
}
115
122
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 ) {
120
136
try ( FileOutputStream fos = new FileOutputStream ( serFile ) ) {
121
137
if ( log .isTraceEnabled () ) {
122
138
log .tracef ( "Writing cache file for: %s to: %s" , xmlFile .getAbsolutePath (), serFile .getAbsolutePath () );
123
139
}
124
- SerializationHelper .serialize ( binding , fos );
140
+ SerializationHelper .serialize ( jaxbModel , fos );
125
141
boolean success = serFile .setLastModified ( System .currentTimeMillis () );
126
142
if ( !success ) {
127
143
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
132
148
}
133
149
}
134
150
135
- public static void createSerFile (File xmlFile , Binder binder ) {
151
+ public static void createSerFile (File xmlFile , MappingBinder binder ) {
136
152
createSerFile ( xmlFile , determineCachedFile ( xmlFile ), binder );
137
153
}
138
154
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 );
146
158
}
147
159
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 ();
150
164
}
151
165
152
166
}
0 commit comments