26
26
import org .hibernate .boot .jaxb .Origin ;
27
27
import org .hibernate .boot .jaxb .SourceType ;
28
28
import org .hibernate .boot .jaxb .internal .CacheableFileXmlSource ;
29
- import org .hibernate .boot .jaxb .internal .FileXmlSource ;
30
- import org .hibernate .boot .jaxb .internal .InputStreamXmlSource ;
31
29
import org .hibernate .boot .jaxb .internal .JarFileEntryXmlSource ;
32
30
import org .hibernate .boot .jaxb .internal .JaxpSourceXmlSource ;
33
- import org .hibernate .boot .jaxb .internal .MappingBinder ;
34
- import org .hibernate .boot .jaxb .internal .UrlXmlSource ;
35
- import org .hibernate .boot .jaxb .spi .Binder ;
36
31
import org .hibernate .boot .jaxb .spi .Binding ;
37
32
import org .hibernate .boot .registry .BootstrapServiceRegistry ;
38
33
import org .hibernate .boot .registry .BootstrapServiceRegistryBuilder ;
39
34
import org .hibernate .boot .registry .StandardServiceRegistry ;
40
- import org .hibernate .boot .registry . classloading . spi .ClassLoaderService ;
35
+ import org .hibernate .boot .spi .XmlMappingBinderAccess ;
41
36
import org .hibernate .internal .CoreLogging ;
42
37
import org .hibernate .internal .CoreMessageLogger ;
43
38
import org .hibernate .service .ServiceRegistry ;
@@ -59,7 +54,7 @@ public class MetadataSources implements Serializable {
59
54
60
55
private final ServiceRegistry serviceRegistry ;
61
56
62
- private Binder mappingsBinder ;
57
+ private XmlMappingBinderAccess xmlMappingBinderAccess ;
63
58
64
59
private List <Binding > xmlBindings = new ArrayList <Binding >();
65
60
private LinkedHashSet <Class <?>> annotatedClasses = new LinkedHashSet <Class <?>>();
@@ -85,18 +80,18 @@ public MetadataSources(ServiceRegistry serviceRegistry) {
85
80
);
86
81
}
87
82
this .serviceRegistry = serviceRegistry ;
88
-
89
- // NOTE : The boolean here indicates whether or not to perform validation as we load XML documents.
90
- // Should we expose this setting? Disabling would speed up JAXP and JAXB at runtime, but potentially
91
- // at the cost of less obvious errors when a document is not valid.
92
- this .mappingsBinder = new MappingBinder ( serviceRegistry .getService ( ClassLoaderService .class ), true );
83
+ this .xmlMappingBinderAccess = new XmlMappingBinderAccess ( serviceRegistry );
93
84
}
94
85
95
86
protected static boolean isExpectedServiceRegistryType (ServiceRegistry serviceRegistry ) {
96
87
return BootstrapServiceRegistry .class .isInstance ( serviceRegistry )
97
88
|| StandardServiceRegistry .class .isInstance ( serviceRegistry );
98
89
}
99
90
91
+ public XmlMappingBinderAccess getXmlMappingBinderAccess () {
92
+ return xmlMappingBinderAccess ;
93
+ }
94
+
100
95
public List <Binding > getXmlBindings () {
101
96
return xmlBindings ;
102
97
}
@@ -209,31 +204,6 @@ public MetadataSources addPackage(Package packageRef) {
209
204
return this ;
210
205
}
211
206
212
- /**
213
- * Read mappings as a application resourceName (i.e. classpath lookup).
214
- *
215
- * @param name The resource name
216
- *
217
- * @return this (for method chaining purposes)
218
- */
219
- public MetadataSources addResource (String name ) {
220
- LOG .tracef ( "reading mappings from resource : %s" , name );
221
-
222
- final Origin origin = new Origin ( SourceType .RESOURCE , name );
223
- final URL url = classLoaderService ().locateResource ( name );
224
- if ( url == null ) {
225
- throw new MappingNotFoundException ( origin );
226
- }
227
-
228
- xmlBindings .add ( new UrlXmlSource ( origin , url ).doBind ( mappingsBinder ) );
229
-
230
- return this ;
231
- }
232
-
233
- private ClassLoaderService classLoaderService () {
234
- return serviceRegistry .getService ( ClassLoaderService .class );
235
- }
236
-
237
207
/**
238
208
* Read a mapping as an application resource using the convention that a class named {@code foo.bar.Foo} is
239
209
* mapped by a file named {@code foo/bar/Foo.hbm.xml} which can be resolved as a classpath resource.
@@ -255,6 +225,18 @@ public MetadataSources addClass(Class entityClass) {
255
225
return this ;
256
226
}
257
227
228
+ /**
229
+ * Read mappings as a application resourceName (i.e. classpath lookup).
230
+ *
231
+ * @param name The resource name
232
+ *
233
+ * @return this (for method chaining purposes)
234
+ */
235
+ public MetadataSources addResource (String name ) {
236
+ xmlBindings .add ( getXmlMappingBinderAccess ().bind ( name ) );
237
+ return this ;
238
+ }
239
+
258
240
/**
259
241
* Read mappings from a particular XML file
260
242
*
@@ -265,23 +247,10 @@ public MetadataSources addClass(Class entityClass) {
265
247
* @see #addFile(java.io.File)
266
248
*/
267
249
public MetadataSources addFile (String path ) {
268
- addFile (
269
- new Origin ( SourceType .FILE , path ),
270
- new File ( path )
271
- );
250
+ addFile ( new File ( path ) );
272
251
return this ;
273
252
}
274
253
275
- private void addFile (Origin origin , File file ) {
276
- LOG .tracef ( "reading mappings from file : %s" , origin .getName () );
277
-
278
- if ( !file .exists () ) {
279
- throw new MappingNotFoundException ( origin );
280
- }
281
-
282
- xmlBindings .add ( new FileXmlSource ( origin , file ).doBind ( mappingsBinder ) );
283
- }
284
-
285
254
/**
286
255
* Read mappings from a particular XML file
287
256
*
@@ -290,10 +259,7 @@ private void addFile(Origin origin, File file) {
290
259
* @return this (for method chaining purposes)
291
260
*/
292
261
public MetadataSources addFile (File file ) {
293
- addFile (
294
- new Origin ( SourceType .FILE , file .getPath () ),
295
- file
296
- );
262
+ xmlBindings .add ( getXmlMappingBinderAccess ().bind ( file ) );
297
263
return this ;
298
264
}
299
265
@@ -313,7 +279,7 @@ public MetadataSources addCacheableFile(String path) {
313
279
}
314
280
315
281
private void addCacheableFile (Origin origin , File file ) {
316
- xmlBindings .add ( new CacheableFileXmlSource ( origin , file , false ).doBind ( mappingsBinder ) );
282
+ xmlBindings .add ( new CacheableFileXmlSource ( origin , file , false ).doBind ( getXmlMappingBinderAccess (). getMappingBinder () ) );
317
283
}
318
284
319
285
/**
@@ -350,7 +316,7 @@ public MetadataSources addCacheableFile(File file) {
350
316
*/
351
317
public MetadataSources addCacheableFileStrictly (File file ) throws SerializationException , FileNotFoundException {
352
318
final Origin origin = new Origin ( SourceType .FILE , file .getAbsolutePath () );
353
- xmlBindings .add ( new CacheableFileXmlSource ( origin , file , true ).doBind ( mappingsBinder ) );
319
+ xmlBindings .add ( new CacheableFileXmlSource ( origin , file , true ).doBind ( getXmlMappingBinderAccess (). getMappingBinder () ) );
354
320
return this ;
355
321
}
356
322
@@ -362,19 +328,7 @@ public MetadataSources addCacheableFileStrictly(File file) throws SerializationE
362
328
* @return this (for method chaining purposes)
363
329
*/
364
330
public MetadataSources addInputStream (InputStreamAccess xmlInputStreamAccess ) {
365
- final Origin origin = new Origin ( SourceType .INPUT_STREAM , xmlInputStreamAccess .getStreamName () );
366
- InputStream xmlInputStream = xmlInputStreamAccess .accessInputStream ();
367
- try {
368
- xmlBindings .add ( new InputStreamXmlSource ( origin , xmlInputStream , false ).doBind ( mappingsBinder ) );
369
- }
370
- finally {
371
- try {
372
- xmlInputStream .close ();
373
- }
374
- catch (IOException e ) {
375
- LOG .debugf ( "Unable to close InputStream obtained from InputStreamAccess : " + xmlInputStreamAccess .getStreamName () );
376
- }
377
- }
331
+ xmlBindings .add ( getXmlMappingBinderAccess ().bind ( xmlInputStreamAccess ) );
378
332
return this ;
379
333
}
380
334
@@ -386,8 +340,7 @@ public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) {
386
340
* @return this (for method chaining purposes)
387
341
*/
388
342
public MetadataSources addInputStream (InputStream xmlInputStream ) {
389
- final Origin origin = new Origin ( SourceType .INPUT_STREAM , null );
390
- xmlBindings .add ( new InputStreamXmlSource ( origin , xmlInputStream , false ).doBind ( mappingsBinder ) );
343
+ xmlBindings .add ( getXmlMappingBinderAccess ().bind ( xmlInputStream ) );
391
344
return this ;
392
345
}
393
346
@@ -399,11 +352,7 @@ public MetadataSources addInputStream(InputStream xmlInputStream) {
399
352
* @return this (for method chaining purposes)
400
353
*/
401
354
public MetadataSources addURL (URL url ) {
402
- final String urlExternalForm = url .toExternalForm ();
403
- LOG .debugf ( "Reading mapping document from URL : %s" , urlExternalForm );
404
-
405
- final Origin origin = new Origin ( SourceType .URL , urlExternalForm );
406
- xmlBindings .add ( new UrlXmlSource ( origin , url ).doBind ( mappingsBinder ) );
355
+ xmlBindings .add ( getXmlMappingBinderAccess ().bind ( url ) );
407
356
return this ;
408
357
}
409
358
@@ -419,7 +368,7 @@ public MetadataSources addURL(URL url) {
419
368
@ Deprecated
420
369
public MetadataSources addDocument (Document document ) {
421
370
final Origin origin = new Origin ( SourceType .DOM , Origin .UNKNOWN_FILE_PATH );
422
- xmlBindings .add ( new JaxpSourceXmlSource ( origin , new DOMSource ( document ) ).doBind ( mappingsBinder ) );
371
+ xmlBindings .add ( new JaxpSourceXmlSource ( origin , new DOMSource ( document ) ).doBind ( getXmlMappingBinderAccess (). getMappingBinder () ) );
423
372
return this ;
424
373
}
425
374
@@ -444,7 +393,7 @@ public MetadataSources addJar(File jar) {
444
393
if ( zipEntry .getName ().endsWith ( ".hbm.xml" ) ) {
445
394
LOG .tracef ( "found mapping document : %s" , zipEntry .getName () );
446
395
xmlBindings .add (
447
- new JarFileEntryXmlSource ( origin , jarFile , zipEntry ).doBind ( mappingsBinder )
396
+ new JarFileEntryXmlSource ( origin , jarFile , zipEntry ).doBind ( getXmlMappingBinderAccess (). getMappingBinder () )
448
397
);
449
398
}
450
399
}
@@ -489,4 +438,5 @@ else if ( file.getName().endsWith( ".hbm.xml" ) ) {
489
438
}
490
439
return this ;
491
440
}
441
+
492
442
}
0 commit comments