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 class Metadata implements Serializable {
this.metadataSourceQueue = new MetadataSourceQueue( this );
}
public BasicServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
public HibernateXmlBinder getHibernateXmlBinder() {
return hibernateXmlBinder;
}

View File

@ -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<String> mappingFileNames, Index annotationIndex) {
ClassLoaderService classLoaderService = meta.getServiceRegistry().getService( ClassLoaderService.class );
Set<InputStream> mappingStreams = new HashSet<InputStream>();
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 );

View File

@ -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 <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 {
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<T>( 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;
}
}

View File

@ -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() );

View File

@ -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<String> xmlFiles = new HashSet<String>();
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<String> xmlFiles = new HashSet<String>();
xmlFiles.add( "org/hibernate/metamodel/source/annotations/orm2.xml" );
parser.parseAndUpdateIndex( xmlFiles, null );