Skip to content

Commit 0d6393a

Browse files
committed
HHH-9887 - Make sure the JPA temp ClassLoader is not used to load Class definitions that are then held on to
1 parent abd0461 commit 0d6393a

35 files changed

+880
-673
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.boot;
88

9-
import java.sql.DatabaseMetaData;
109
import javax.persistence.AttributeConverter;
1110
import javax.persistence.SharedCacheMode;
1211

@@ -24,7 +23,6 @@
2423
import org.hibernate.cfg.AttributeConverterDefinition;
2524
import org.hibernate.cfg.MetadataSourceType;
2625
import org.hibernate.dialect.function.SQLFunction;
27-
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
2826
import org.hibernate.type.BasicType;
2927
import org.hibernate.usertype.CompositeUserType;
3028
import org.hibernate.usertype.UserType;

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

Lines changed: 29 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@
2626
import org.hibernate.boot.jaxb.Origin;
2727
import org.hibernate.boot.jaxb.SourceType;
2828
import org.hibernate.boot.jaxb.internal.CacheableFileXmlSource;
29-
import org.hibernate.boot.jaxb.internal.FileXmlSource;
30-
import org.hibernate.boot.jaxb.internal.InputStreamXmlSource;
3129
import org.hibernate.boot.jaxb.internal.JarFileEntryXmlSource;
3230
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;
3631
import org.hibernate.boot.jaxb.spi.Binding;
3732
import org.hibernate.boot.registry.BootstrapServiceRegistry;
3833
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
3934
import org.hibernate.boot.registry.StandardServiceRegistry;
40-
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
35+
import org.hibernate.boot.spi.XmlMappingBinderAccess;
4136
import org.hibernate.internal.CoreLogging;
4237
import org.hibernate.internal.CoreMessageLogger;
4338
import org.hibernate.service.ServiceRegistry;
@@ -59,7 +54,7 @@ public class MetadataSources implements Serializable {
5954

6055
private final ServiceRegistry serviceRegistry;
6156

62-
private Binder mappingsBinder;
57+
private XmlMappingBinderAccess xmlMappingBinderAccess;
6358

6459
private List<Binding> xmlBindings = new ArrayList<Binding>();
6560
private LinkedHashSet<Class<?>> annotatedClasses = new LinkedHashSet<Class<?>>();
@@ -85,18 +80,18 @@ public MetadataSources(ServiceRegistry serviceRegistry) {
8580
);
8681
}
8782
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 );
9384
}
9485

9586
protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) {
9687
return BootstrapServiceRegistry.class.isInstance( serviceRegistry )
9788
|| StandardServiceRegistry.class.isInstance( serviceRegistry );
9889
}
9990

91+
public XmlMappingBinderAccess getXmlMappingBinderAccess() {
92+
return xmlMappingBinderAccess;
93+
}
94+
10095
public List<Binding> getXmlBindings() {
10196
return xmlBindings;
10297
}
@@ -209,31 +204,6 @@ public MetadataSources addPackage(Package packageRef) {
209204
return this;
210205
}
211206

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-
237207
/**
238208
* Read a mapping as an application resource using the convention that a class named {@code foo.bar.Foo} is
239209
* 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) {
255225
return this;
256226
}
257227

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+
258240
/**
259241
* Read mappings from a particular XML file
260242
*
@@ -265,23 +247,10 @@ public MetadataSources addClass(Class entityClass) {
265247
* @see #addFile(java.io.File)
266248
*/
267249
public MetadataSources addFile(String path) {
268-
addFile(
269-
new Origin( SourceType.FILE, path ),
270-
new File( path )
271-
);
250+
addFile( new File( path ) );
272251
return this;
273252
}
274253

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-
285254
/**
286255
* Read mappings from a particular XML file
287256
*
@@ -290,10 +259,7 @@ private void addFile(Origin origin, File file) {
290259
* @return this (for method chaining purposes)
291260
*/
292261
public MetadataSources addFile(File file) {
293-
addFile(
294-
new Origin( SourceType.FILE, file.getPath() ),
295-
file
296-
);
262+
xmlBindings.add( getXmlMappingBinderAccess().bind( file ) );
297263
return this;
298264
}
299265

@@ -313,7 +279,7 @@ public MetadataSources addCacheableFile(String path) {
313279
}
314280

315281
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() ) );
317283
}
318284

319285
/**
@@ -350,7 +316,7 @@ public MetadataSources addCacheableFile(File file) {
350316
*/
351317
public MetadataSources addCacheableFileStrictly(File file) throws SerializationException, FileNotFoundException {
352318
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() ) );
354320
return this;
355321
}
356322

@@ -362,19 +328,7 @@ public MetadataSources addCacheableFileStrictly(File file) throws SerializationE
362328
* @return this (for method chaining purposes)
363329
*/
364330
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 ) );
378332
return this;
379333
}
380334

@@ -386,8 +340,7 @@ public MetadataSources addInputStream(InputStreamAccess xmlInputStreamAccess) {
386340
* @return this (for method chaining purposes)
387341
*/
388342
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 ) );
391344
return this;
392345
}
393346

@@ -399,11 +352,7 @@ public MetadataSources addInputStream(InputStream xmlInputStream) {
399352
* @return this (for method chaining purposes)
400353
*/
401354
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 ) );
407356
return this;
408357
}
409358

@@ -419,7 +368,7 @@ public MetadataSources addURL(URL url) {
419368
@Deprecated
420369
public MetadataSources addDocument(Document document) {
421370
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() ) );
423372
return this;
424373
}
425374

@@ -444,7 +393,7 @@ public MetadataSources addJar(File jar) {
444393
if ( zipEntry.getName().endsWith( ".hbm.xml" ) ) {
445394
LOG.tracef( "found mapping document : %s", zipEntry.getName() );
446395
xmlBindings.add(
447-
new JarFileEntryXmlSource( origin, jarFile, zipEntry ).doBind( mappingsBinder )
396+
new JarFileEntryXmlSource( origin, jarFile, zipEntry ).doBind( getXmlMappingBinderAccess().getMappingBinder() )
448397
);
449398
}
450399
}
@@ -489,4 +438,5 @@ else if ( file.getName().endsWith( ".hbm.xml" ) ) {
489438
}
490439
return this;
491440
}
441+
492442
}

hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ClassDescriptorImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
*/
1515
public class ClassDescriptorImpl implements ClassDescriptor {
1616
private final String name;
17+
private final Categorization categorization;
1718
private final InputStreamAccess streamAccess;
1819

19-
public ClassDescriptorImpl(String name, InputStreamAccess streamAccess) {
20+
public ClassDescriptorImpl(String name, Categorization categorization, InputStreamAccess streamAccess) {
2021
this.name = name;
22+
this.categorization = categorization;
2123
this.streamAccess = streamAccess;
2224
}
2325

@@ -26,6 +28,10 @@ public String getName() {
2628
return name;
2729
}
2830

31+
public Categorization getCategorization() {
32+
return categorization;
33+
}
34+
2935
@Override
3036
public InputStreamAccess getStreamAccess() {
3137
return streamAccess;

hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ScanResultCollector.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class ScanResultCollector {
2929
private final ScanEnvironment environment;
3030
private final ScanOptions options;
3131

32-
private final ScanParameters scanParameters;
33-
3432
private final Set<ClassDescriptor> discoveredClasses;
3533
private final Set<PackageDescriptor> discoveredPackages;
3634
private final Set<MappingFileDescriptor> discoveredMappingFiles;
@@ -39,8 +37,6 @@ public ScanResultCollector(ScanEnvironment environment, ScanOptions options, Sca
3937
this.environment = environment;
4038
this.options = options;
4139

42-
this.scanParameters = parameters;
43-
4440
if ( environment.getExplicitlyListedClassNames() == null ) {
4541
throw new IllegalArgumentException( "ScanEnvironment#getExplicitlyListedClassNames should not return null" );
4642
}
@@ -55,19 +51,6 @@ public ScanResultCollector(ScanEnvironment environment, ScanOptions options, Sca
5551
}
5652

5753
public void handleClass(ClassDescriptor classDescriptor, boolean rootUrl) {
58-
// see if "discovery" of this entry is allowed
59-
60-
// final ClassInfo classInfo = scanParameters.getJandexInitializer().handle( classDescriptor );
61-
//
62-
// if ( !isListedOrDetectable( classInfo.name().toString(), rootUrl ) ) {
63-
// return;
64-
// }
65-
//
66-
// if ( !containsClassAnnotationsOfInterest( classInfo ) ) {
67-
// // not strictly needed, but helps cut down on the size of discoveredClasses
68-
// return;
69-
// }
70-
7154
if ( !isListedOrDetectable( classDescriptor.getName(), rootUrl ) ) {
7255
return;
7356
}
@@ -95,21 +78,7 @@ protected boolean isListedOrDetectable(String name, boolean rootUrl) {
9578
}
9679
}
9780

98-
// @SuppressWarnings("SimplifiableIfStatement")
99-
// private boolean containsClassAnnotationsOfInterest(ClassInfo classInfo) {
100-
// if ( classInfo.annotations() == null ) {
101-
// return false;
102-
// }
103-
//
104-
// return classInfo.annotations().containsKey( JPADotNames.ENTITY )
105-
// || classInfo.annotations().containsKey( JPADotNames.MAPPED_SUPERCLASS )
106-
// || classInfo.annotations().containsKey( JPADotNames.EMBEDDABLE )
107-
// || classInfo.annotations().containsKey( JPADotNames.CONVERTER );
108-
// }
109-
11081
public void handlePackage(PackageDescriptor packageDescriptor, boolean rootUrl) {
111-
// final ClassInfo classInfo = scanParameters.getJandexInitializer().handle( packageDescriptor );
112-
11382
if ( !isListedOrDetectable( packageDescriptor.getName(), rootUrl ) ) {
11483
// not strictly needed, but helps cut down on the size of discoveredPackages
11584
return;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.boot.archive.scan.internal;
8+
9+
import org.hibernate.boot.archive.scan.spi.ScanParameters;
10+
11+
/**
12+
* @author Steve Ebersole
13+
*/
14+
public class StandardScanParameters implements ScanParameters {
15+
/**
16+
* Singleton access
17+
*/
18+
public static final StandardScanParameters INSTANCE = new StandardScanParameters();
19+
20+
private StandardScanParameters() {
21+
}
22+
}

hibernate-core/src/main/java/org/hibernate/boot/archive/scan/spi/ClassDescriptor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
* @author Steve Ebersole
1515
*/
1616
public interface ClassDescriptor {
17+
enum Categorization {
18+
MODEL,
19+
CONVERTER,
20+
OTHER
21+
}
22+
1723
/**
1824
* Retrieves the class name, not the file name.
1925
*
2026
* @return The name (FQN) of the class
2127
*/
22-
public String getName();
28+
String getName();
29+
30+
Categorization getCategorization();
2331

2432
/**
2533
* Retrieves access to the InputStream for the class file.
2634
*
2735
* @return Access to the InputStream for the class file.
2836
*/
29-
public InputStreamAccess getStreamAccess();
37+
InputStreamAccess getStreamAccess();
3038
}

0 commit comments

Comments
 (0)