From 4d24c16b49d20f85173f9998555198e51a12d818 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 12 Apr 2011 18:07:48 +0200 Subject: [PATCH] HHH-6114 Using the ClassLoaderService available via the service registry in Metadata for class loading --- .../hibernate/metamodel/source/Metadata.java | 4 +++ .../source/annotations/OrmXmlParser.java | 18 ++++++++++--- .../metamodel/source/util/xml/XmlHelper.java | 25 ++++++------------- .../binding/BasicHbmBindingTests.java | 10 +++++--- .../source/annotations/OrmXmlParserTests.java | 7 ++++-- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java index 165ecfa34b..e19d7def70 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java @@ -76,6 +76,10 @@ public class Metadata implements Serializable { this.metadataSourceQueue = new MetadataSourceQueue( this ); } + public BasicServiceRegistry getServiceRegistry() { + return serviceRegistry; + } + public HibernateXmlBinder getHibernateXmlBinder() { return hibernateXmlBinder; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java index 5ea79fbe3a..d07dd8f529 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java @@ -8,8 +8,10 @@ import javax.xml.bind.JAXBException; import org.jboss.jandex.Index; import org.hibernate.AnnotationException; +import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.source.annotation.xml.EntityMappings; import org.hibernate.metamodel.source.util.xml.XmlHelper; +import org.hibernate.service.classloading.spi.ClassLoaderService; /** * @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 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 * @@ -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 */ public Index parseAndUpdateIndex(Set mappingFileNames, Index annotationIndex) { - + ClassLoaderService classLoaderService = meta.getServiceRegistry().getService( ClassLoaderService.class ); Set mappingStreams = new HashSet(); for ( String fileName : mappingFileNames ) { EntityMappings entityMappings; 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 ) { // if we cannot parse against orm_2_0.xsd we try orm_1_0.xsd for backwards compatibility 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 ) { throw new AnnotationException( "Unable to parse xml configuration.", orm1Exception ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java index d5b2c08c0a..0cfb7ba67f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java @@ -37,6 +37,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; +import org.hibernate.service.classloading.spi.ClassLoaderService; + /** * @author Hardy Ferentschik */ @@ -46,10 +48,10 @@ public class XmlHelper { private XmlHelper() { } - public static JaxbRoot unmarshallXml(String fileName, String schemaName, Class clazz) + public static JaxbRoot unmarshallXml(String fileName, String schemaName, Class clazz, ClassLoaderService classLoaderService) throws JAXBException { - Schema schema = getMappingSchema( schemaName ); - InputStream in = getInputStreamForPath( fileName ); + Schema schema = getMappingSchema( schemaName, classLoaderService ); + InputStream in = classLoaderService.locateResourceStream( fileName ); JAXBContext jc = JAXBContext.newInstance( clazz ); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setSchema( schema ); @@ -59,10 +61,8 @@ public class XmlHelper { return new JaxbRootImpl( elem.getValue(), origin ); } - private static Schema getMappingSchema(String schemaVersion) { - // todo - think about class loading. does this have to go via the class loader service? - ClassLoader loader = XmlHelper.class.getClassLoader(); - URL schemaUrl = loader.getResource( schemaVersion ); + private static Schema getMappingSchema(String schemaVersion, ClassLoaderService classLoaderService) { + URL schemaUrl = classLoaderService.locateResource( schemaVersion ); SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); Schema schema = null; try { @@ -73,17 +73,6 @@ public class XmlHelper { } 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; - } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java index fb5e580eec..a2b236f511 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java @@ -28,7 +28,6 @@ import javax.xml.bind.JAXBException; import org.jboss.logging.Logger; import org.xml.sax.InputSource; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.ConfigHelper; import org.hibernate.internal.util.xml.MappingReader; import org.hibernate.internal.util.xml.Origin; @@ -37,6 +36,7 @@ import org.hibernate.internal.util.xml.XmlDocument; import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.source.hbm.xml.mapping.HibernateMapping; import org.hibernate.metamodel.source.util.xml.XmlHelper; +import org.hibernate.service.classloading.spi.ClassLoaderService; import static org.junit.Assert.fail; @@ -64,11 +64,15 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests { metadata.getHibernateXmlBinder().bindRoot( xmlDocument ); // 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"; HibernateMapping mapping = null; 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 ) { log.debug( e.getMessage() ); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java index ace3da43b4..cfad6c17e6 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/OrmXmlParserTests.java @@ -1,10 +1,13 @@ package org.hibernate.metamodel.source.annotations; +import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.junit.Test; +import org.hibernate.metamodel.source.Metadata; +import org.hibernate.service.internal.BasicServiceRegistryImpl; import org.hibernate.testing.junit4.BaseUnitTestCase; /** @@ -13,7 +16,7 @@ import org.hibernate.testing.junit4.BaseUnitTestCase; public class OrmXmlParserTests extends BaseUnitTestCase { @Test public void testSingleOrmXml() { - OrmXmlParser parser = new OrmXmlParser(); + OrmXmlParser parser = new OrmXmlParser( new Metadata( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) ); Set xmlFiles = new HashSet(); xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm.xml" ); parser.parseAndUpdateIndex( xmlFiles, null ); @@ -21,7 +24,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase { @Test public void testOrmXmlWithOldSchema() { - OrmXmlParser parser = new OrmXmlParser(); + OrmXmlParser parser = new OrmXmlParser( new Metadata( new BasicServiceRegistryImpl( Collections.emptyMap() ) ) ); Set xmlFiles = new HashSet(); xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" ); parser.parseAndUpdateIndex( xmlFiles, null );