Skip to content

Commit 047d308

Browse files
committed
HHH-8893 delay binding of xml resources until building Metadata,
introduce ClassLoaderAccess into transformer, removed old XmlBinder
1 parent 95d66ed commit 047d308

File tree

14 files changed

+131
-640
lines changed

14 files changed

+131
-640
lines changed

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
import org.hibernate.metamodel.source.internal.jaxb.JaxbMappedSuperclass;
6262
import org.hibernate.metamodel.source.internal.jaxb.JaxbPersistenceUnitDefaults;
6363
import org.hibernate.metamodel.source.internal.jaxb.JaxbPersistenceUnitMetadata;
64-
import org.hibernate.metamodel.source.spi.InvalidMappingException;
6564
import org.hibernate.metamodel.source.spi.MappingException;
6665
import org.hibernate.metamodel.source.spi.MappingNotFoundException;
66+
import org.hibernate.metamodel.spi.ClassLoaderAccess;
6767
import org.hibernate.service.ServiceRegistry;
6868
import org.hibernate.type.SerializationException;
6969
import org.hibernate.xml.internal.jaxb.UnifiedMappingBinder;
@@ -86,16 +86,13 @@ public class MetadataSources {
8686
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( MetadataSources.class );
8787

8888
private final ServiceRegistry serviceRegistry;
89-
private final UnifiedMappingBinder jaxbProcessor;
9089
private List<BindResult> bindResultList = new ArrayList<BindResult>();
9190
private LinkedHashSet<Class<?>> annotatedClasses = new LinkedHashSet<Class<?>>();
9291
private LinkedHashSet<String> annotatedClassNames = new LinkedHashSet<String>();
9392
private LinkedHashSet<String> annotatedPackages = new LinkedHashSet<String>();
9493

9594
private List<Class<? extends AttributeConverter>> converterClasses;
9695

97-
private boolean hasOrmXmlJaxbRoots;
98-
9996
public MetadataSources() {
10097
this( new BootstrapServiceRegistryBuilder().build() );
10198
}
@@ -115,13 +112,19 @@ public MetadataSources(ServiceRegistry serviceRegistry) {
115112
);
116113
}
117114
this.serviceRegistry = serviceRegistry;
118-
this.jaxbProcessor = new UnifiedMappingBinder();
119115
}
120116

121117
protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) {
122118
return BootstrapServiceRegistry.class.isInstance( serviceRegistry )
123119
|| StandardServiceRegistry.class.isInstance( serviceRegistry );
124120
}
121+
122+
public void buildBindResults(ClassLoaderAccess classLoaderAccess) {
123+
final UnifiedMappingBinder jaxbProcessor = new UnifiedMappingBinder( classLoaderAccess );
124+
for ( BindResult bindResult : bindResultList ) {
125+
bindResult.bind( jaxbProcessor );
126+
}
127+
}
125128

126129
public List<BindResult> getBindResultList() {
127130
return bindResultList;
@@ -243,24 +246,9 @@ private ClassLoaderService classLoaderService() {
243246
}
244247

245248
private BindResult add(InputStream inputStream, Origin origin, boolean close) {
246-
try {
247-
BindResult bindResult = new BindResult( jaxbProcessor.bind( inputStream, origin ), origin );
248-
addJaxbRoot( bindResult );
249-
return bindResult;
250-
}
251-
catch ( Exception e ) {
252-
throw new InvalidMappingException( origin, e );
253-
}
254-
finally {
255-
if ( close ) {
256-
try {
257-
inputStream.close();
258-
}
259-
catch ( IOException ignore ) {
260-
LOG.trace( "Was unable to close input stream" );
261-
}
262-
}
263-
}
249+
BindResult bindResult = new BindResult( inputStream, origin, close );
250+
bindResultList.add( bindResult );
251+
return bindResult;
264252
}
265253

266254
/**
@@ -419,7 +407,7 @@ public MetadataSources addCacheableFileStrictly(File file) throws SerializationE
419407
}
420408

421409
LOG.readingCachedMappings( cachedFile );
422-
addJaxbRoot( (BindResult) SerializationHelper.deserialize( new FileInputStream( cachedFile ) ) );
410+
bindResultList.add( (BindResult) SerializationHelper.deserialize( new FileInputStream( cachedFile ) ) );
423411
return this;
424412
}
425413

@@ -470,16 +458,10 @@ public MetadataSources addURL(URL url) {
470458
@Deprecated
471459
public MetadataSources addDocument(Document document) {
472460
final Origin origin = new Origin( SourceType.DOM, Origin.UNKNOWN_FILE_PATH );
473-
BindResult bindResult = new BindResult( jaxbProcessor.bind( new DOMSource( document ), origin ), origin );
474-
addJaxbRoot( bindResult );
461+
bindResultList.add( new BindResult( new DOMSource( document ), origin ) );
475462
return this;
476463
}
477464

478-
private void addJaxbRoot(BindResult bindResult) {
479-
hasOrmXmlJaxbRoots = hasOrmXmlJaxbRoots || JaxbEntityMappings.class.isInstance( bindResult.getRoot() );
480-
bindResultList.add( bindResult );
481-
}
482-
483465
/**
484466
* Read all mappings from a jar file.
485467
* <p/>

hibernate-core/src/main/java/org/hibernate/metamodel/internal/ClassLoaderAccessImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525

2626
import java.net.URL;
2727

28-
import org.hibernate.boot.registry.StandardServiceRegistry;
2928
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
3029
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
3130
import org.hibernate.metamodel.spi.ClassLoaderAccess;
32-
31+
import org.hibernate.service.ServiceRegistry;
3332
import org.jboss.logging.Logger;
3433

3534
/**
@@ -48,7 +47,7 @@ public ClassLoaderAccessImpl(
4847
this.classLoaderService = classLoaderService;
4948
}
5049

51-
public ClassLoaderAccessImpl(ClassLoader tempClassLoader, StandardServiceRegistry serviceRegistry) {
50+
public ClassLoaderAccessImpl(ClassLoader tempClassLoader, ServiceRegistry serviceRegistry) {
5251
this( tempClassLoader, serviceRegistry.getService( ClassLoaderService.class ) );
5352
}
5453

hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataBuildingProcess.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ public static MetadataImpl build(MetadataSources sources, final MetadataBuilding
148148
options.getTempClassLoader(),
149149
options.getServiceRegistry()
150150
);
151+
152+
// It's necessary to delay the binding of XML resources until now. ClassLoaderAccess is needed for
153+
// reflection, etc.
154+
sources.buildBindResults( classLoaderAccess );
151155

152156
final JandexInitManager jandexInitializer = buildJandexInitializer( options, classLoaderAccess );
153-
157+
154158
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155159
// scanning - Jandex initialization and source discovery
156160
if ( options.getScanEnvironment() != null ) {

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/jandex/EntityMappingsMocker.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import org.hibernate.metamodel.source.internal.jaxb.JaxbPersistenceUnitMetadata;
4040
import org.hibernate.service.ServiceRegistry;
4141
import org.hibernate.xml.spi.BindResult;
42-
import org.hibernate.xml.spi.Origin;
43-
import org.hibernate.xml.spi.SourceType;
4442
import org.jboss.jandex.Index;
4543
import org.jboss.jandex.IndexView;
4644

@@ -68,30 +66,6 @@ public EntityMappingsMocker(List<BindResult<JaxbEntityMappings>> xmlBindings, In
6866
this.globalAnnotations = new GlobalAnnotations();
6967
}
7068

71-
public EntityMappingsMocker(
72-
List<JaxbEntityMappings> xmlEntityMappingsList,
73-
Index index,
74-
ServiceRegistry serviceRegistry) {
75-
this(
76-
grabJaxbEntityMappings( xmlEntityMappingsList ),
77-
index,
78-
serviceRegistry
79-
);
80-
}
81-
82-
private static List<BindResult<JaxbEntityMappings>> grabJaxbEntityMappings(List<JaxbEntityMappings> xmlEntityMappingsList) {
83-
final List<BindResult<JaxbEntityMappings>> result = new ArrayList<BindResult<JaxbEntityMappings>>();
84-
for ( JaxbEntityMappings binding : xmlEntityMappingsList ) {
85-
result.add(
86-
new BindResult<JaxbEntityMappings>(
87-
binding,
88-
new Origin( SourceType.OTHER, Origin.UNKNOWN_FILE_PATH )
89-
)
90-
);
91-
}
92-
return result;
93-
}
94-
9569
/**
9670
* Create new {@link Index} with mocking JPA annotations from {@link org.hibernate.metamodel.source.internal.jaxb.JaxbEntityMappings}
9771
* and merge them with existing {@link Index}

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/jaxb/hbm/HbmXmlTransformer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import org.hibernate.FlushMode;
3232
import org.hibernate.internal.util.StringHelper;
33-
import org.hibernate.metamodel.source.internal.jandex.MockHelper;
3433
import org.hibernate.metamodel.source.internal.jaxb.JaxbAny;
3534
import org.hibernate.metamodel.source.internal.jaxb.JaxbAttributes;
3635
import org.hibernate.metamodel.source.internal.jaxb.JaxbBasic;
@@ -62,7 +61,6 @@
6261
import org.hibernate.metamodel.source.internal.jaxb.JaxbId;
6362
import org.hibernate.metamodel.source.internal.jaxb.JaxbIdClass;
6463
import org.hibernate.metamodel.source.internal.jaxb.JaxbJoinColumn;
65-
import org.hibernate.metamodel.source.internal.jaxb.JaxbJoinTable;
6664
import org.hibernate.metamodel.source.internal.jaxb.JaxbManyToOne;
6765
import org.hibernate.metamodel.source.internal.jaxb.JaxbNamedNativeQuery;
6866
import org.hibernate.metamodel.source.internal.jaxb.JaxbNamedQuery;
@@ -72,6 +70,7 @@
7270
import org.hibernate.metamodel.source.internal.jaxb.JaxbQueryParamType;
7371
import org.hibernate.metamodel.source.internal.jaxb.JaxbSynchronizeType;
7472
import org.hibernate.metamodel.source.internal.jaxb.JaxbTable;
73+
import org.hibernate.metamodel.spi.ClassLoaderAccess;
7574
import org.hibernate.xml.spi.Origin;
7675
import org.jboss.logging.Logger;
7776

@@ -91,9 +90,12 @@ public class HbmXmlTransformer {
9190

9291
private Origin origin;
9392
private JaxbEntityMappings ormRoot;
93+
private ClassLoaderAccess classLoaderAccess;
9494

95-
public JaxbEntityMappings transform(JaxbHibernateMapping hbmXmlMapping, Origin origin) {
95+
public JaxbEntityMappings transform(JaxbHibernateMapping hbmXmlMapping, Origin origin,
96+
ClassLoaderAccess classLoaderAccess) {
9697
this.origin = origin;
98+
this.classLoaderAccess = classLoaderAccess;
9799

98100
ormRoot = new JaxbEntityMappings();
99101
ormRoot.setDescription(

0 commit comments

Comments
 (0)