HHH-6114 Using the ClassLoaderService available via the service registry in Metadata for class loading

This commit is contained in:
Hardy Ferentschik 2011-04-12 18:07:48 +02:00
parent ad17f89c4c
commit 4d24c16b49
5 changed files with 38 additions and 26 deletions

View File

@ -76,6 +76,10 @@ public Metadata(BasicServiceRegistry serviceRegistry) {
this.metadataSourceQueue = new MetadataSourceQueue( this ); this.metadataSourceQueue = new MetadataSourceQueue( this );
} }
public BasicServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
public HibernateXmlBinder getHibernateXmlBinder() { public HibernateXmlBinder getHibernateXmlBinder() {
return hibernateXmlBinder; return hibernateXmlBinder;
} }

View File

@ -8,8 +8,10 @@
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.metamodel.source.Metadata;
import org.hibernate.metamodel.source.annotation.xml.EntityMappings; import org.hibernate.metamodel.source.annotation.xml.EntityMappings;
import org.hibernate.metamodel.source.util.xml.XmlHelper; import org.hibernate.metamodel.source.util.xml.XmlHelper;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
@ -19,6 +21,12 @@ public class OrmXmlParser {
private static final String ORM1_MAPPING_XSD = "org/hibernate/ejb/orm_1_0.xsd"; private static final String ORM1_MAPPING_XSD = "org/hibernate/ejb/orm_1_0.xsd";
private static final String ORM2_MAPPING_XSD = "org/hibernate/ejb/orm_2_0.xsd"; private static final String ORM2_MAPPING_XSD = "org/hibernate/ejb/orm_2_0.xsd";
private final Metadata meta;
public OrmXmlParser(Metadata meta) {
this.meta = meta;
}
/** /**
* Parses the given xml configuration files and returns a updated annotation index * Parses the given xml configuration files and returns a updated annotation index
* *
@ -28,18 +36,22 @@ public class OrmXmlParser {
* @return a new updated annotation index, enhancing and modifying the existing ones according to the jpa xml rules * @return a new updated annotation index, enhancing and modifying the existing ones according to the jpa xml rules
*/ */
public Index parseAndUpdateIndex(Set<String> mappingFileNames, Index annotationIndex) { public Index parseAndUpdateIndex(Set<String> mappingFileNames, Index annotationIndex) {
ClassLoaderService classLoaderService = meta.getServiceRegistry().getService( ClassLoaderService.class );
Set<InputStream> mappingStreams = new HashSet<InputStream>(); Set<InputStream> mappingStreams = new HashSet<InputStream>();
for ( String fileName : mappingFileNames ) { for ( String fileName : mappingFileNames ) {
EntityMappings entityMappings; EntityMappings entityMappings;
try { try {
entityMappings = XmlHelper.unmarshallXml( fileName, ORM2_MAPPING_XSD, EntityMappings.class ).getRoot(); entityMappings = XmlHelper.unmarshallXml(
fileName, ORM2_MAPPING_XSD, EntityMappings.class, classLoaderService
).getRoot();
} }
catch ( JAXBException orm2Exception ) { catch ( JAXBException orm2Exception ) {
// if we cannot parse against orm_2_0.xsd we try orm_1_0.xsd for backwards compatibility // if we cannot parse against orm_2_0.xsd we try orm_1_0.xsd for backwards compatibility
try { try {
entityMappings = XmlHelper.unmarshallXml( fileName, ORM1_MAPPING_XSD, EntityMappings.class ).getRoot(); entityMappings = XmlHelper.unmarshallXml(
fileName, ORM1_MAPPING_XSD, EntityMappings.class, classLoaderService
).getRoot();
} }
catch ( JAXBException orm1Exception ) { catch ( JAXBException orm1Exception ) {
throw new AnnotationException( "Unable to parse xml configuration.", orm1Exception ); throw new AnnotationException( "Unable to parse xml configuration.", orm1Exception );

View File

@ -37,6 +37,8 @@
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
@ -46,10 +48,10 @@ public class XmlHelper {
private XmlHelper() { private XmlHelper() {
} }
public static <T> JaxbRoot<T> unmarshallXml(String fileName, String schemaName, Class<T> clazz) public static <T> JaxbRoot<T> unmarshallXml(String fileName, String schemaName, Class<T> clazz, ClassLoaderService classLoaderService)
throws JAXBException { throws JAXBException {
Schema schema = getMappingSchema( schemaName ); Schema schema = getMappingSchema( schemaName, classLoaderService );
InputStream in = getInputStreamForPath( fileName ); InputStream in = classLoaderService.locateResourceStream( fileName );
JAXBContext jc = JAXBContext.newInstance( clazz ); JAXBContext jc = JAXBContext.newInstance( clazz );
Unmarshaller unmarshaller = jc.createUnmarshaller(); Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema( schema ); unmarshaller.setSchema( schema );
@ -59,10 +61,8 @@ public static <T> JaxbRoot<T> unmarshallXml(String fileName, String schemaName,
return new JaxbRootImpl<T>( elem.getValue(), origin ); return new JaxbRootImpl<T>( elem.getValue(), origin );
} }
private static Schema getMappingSchema(String schemaVersion) { private static Schema getMappingSchema(String schemaVersion, ClassLoaderService classLoaderService) {
// todo - think about class loading. does this have to go via the class loader service? URL schemaUrl = classLoaderService.locateResource( schemaVersion );
ClassLoader loader = XmlHelper.class.getClassLoader();
URL schemaUrl = loader.getResource( schemaVersion );
SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = null; Schema schema = null;
try { try {
@ -73,17 +73,6 @@ private static Schema getMappingSchema(String schemaVersion) {
} }
return schema; return schema;
} }
private static InputStream getInputStreamForPath(String path) {
// try the context class loader first
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
// try the current class loader
if ( inputStream == null ) {
inputStream = XmlHelper.class.getResourceAsStream( path );
}
return inputStream;
}
} }

View File

@ -28,7 +28,6 @@
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ConfigHelper; import org.hibernate.internal.util.ConfigHelper;
import org.hibernate.internal.util.xml.MappingReader; import org.hibernate.internal.util.xml.MappingReader;
import org.hibernate.internal.util.xml.Origin; import org.hibernate.internal.util.xml.Origin;
@ -37,6 +36,7 @@
import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.source.Metadata;
import org.hibernate.metamodel.source.hbm.xml.mapping.HibernateMapping; import org.hibernate.metamodel.source.hbm.xml.mapping.HibernateMapping;
import org.hibernate.metamodel.source.util.xml.XmlHelper; import org.hibernate.metamodel.source.util.xml.XmlHelper;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -64,11 +64,15 @@ public EntityBinding buildSimpleVersionedEntityBinding() {
metadata.getHibernateXmlBinder().bindRoot( xmlDocument ); metadata.getHibernateXmlBinder().bindRoot( xmlDocument );
// todo - just temporary to show how things would look like with JAXB // todo - just temporary to show how things would look like with JAXB
fileName = "/org/hibernate/metamodel/binding/SimpleVersionedEntity.xml"; fileName = "org/hibernate/metamodel/binding/SimpleVersionedEntity.xml";
final String HIBERNATE_MAPPING_XSD = "org/hibernate/hibernate-mapping-3.0.xsd"; final String HIBERNATE_MAPPING_XSD = "org/hibernate/hibernate-mapping-3.0.xsd";
HibernateMapping mapping = null; HibernateMapping mapping = null;
try { try {
mapping = XmlHelper.unmarshallXml( fileName, HIBERNATE_MAPPING_XSD, HibernateMapping.class ).getRoot(); ClassLoaderService classLoaderService = metadata.getServiceRegistry()
.getService( ClassLoaderService.class );
mapping = XmlHelper.unmarshallXml(
fileName, HIBERNATE_MAPPING_XSD, HibernateMapping.class, classLoaderService
).getRoot();
} }
catch ( JAXBException e ) { catch ( JAXBException e ) {
log.debug( e.getMessage() ); log.debug( e.getMessage() );

View File

@ -1,10 +1,13 @@
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.junit.Test; import org.junit.Test;
import org.hibernate.metamodel.source.Metadata;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
/** /**
@ -13,7 +16,7 @@
public class OrmXmlParserTests extends BaseUnitTestCase { public class OrmXmlParserTests extends BaseUnitTestCase {
@Test @Test
public void testSingleOrmXml() { public void testSingleOrmXml() {
OrmXmlParser parser = new OrmXmlParser(); OrmXmlParser parser = new OrmXmlParser( new Metadata( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) );
Set<String> xmlFiles = new HashSet<String>(); Set<String> xmlFiles = new HashSet<String>();
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" ); xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" );
parser.parseAndUpdateIndex( xmlFiles, null ); parser.parseAndUpdateIndex( xmlFiles, null );
@ -21,7 +24,7 @@ public void testSingleOrmXml() {
@Test @Test
public void testOrmXmlWithOldSchema() { public void testOrmXmlWithOldSchema() {
OrmXmlParser parser = new OrmXmlParser(); OrmXmlParser parser = new OrmXmlParser( new Metadata( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) );
Set<String> xmlFiles = new HashSet<String>(); Set<String> xmlFiles = new HashSet<String>();
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" ); xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" );
parser.parseAndUpdateIndex( xmlFiles, null ); parser.parseAndUpdateIndex( xmlFiles, null );