Author: steve.ebersole(a)jboss.com
Date: 2010-09-08 16:20:32 -0400 (Wed, 08 Sep 2010)
New Revision: 20322
Added:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/ErrorLogger.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/MappingReader.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/Origin.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/OriginImpl.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocument.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocumentImpl.java
core/branches/Branch_3_5/entitymanager/src/test/java/org/hibernate/ejb/test/jee/
core/branches/Branch_3_5/entitymanager/src/test/java/org/hibernate/ejb/test/jee/OrmVersionTest.java
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/invalid-orm-1.xml
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-1.xml
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-2.xml
Modified:
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
Log:
HHH-5310 - orm_2_0.xsd compliant mapping files break in JEE use cases
Added:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/ErrorLogger.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/ErrorLogger.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/ErrorLogger.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,80 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.Serializable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Implements an {@link ErrorHandler} that mainly just logs errors/warnings. However, it
does track
+ * the intial error it encounters and makes it available via {@link #getError}.
+ *
+ * @author Steve Ebersole
+ */
+public class ErrorLogger implements ErrorHandler, Serializable {
+ private static final Logger log = LoggerFactory.getLogger( ErrorLogger.class );
+
+ private SAXParseException error; // capture the initial error
+
+ /**
+ * Retrieve the initial error encountered, or null if no error was encountered.
+ *
+ * @return The initial error, or null if none.
+ */
+ public SAXParseException getError() {
+ return error;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void error(SAXParseException error) {
+ log.error( "Error parsing XML (" + error.getLineNumber() + ") : " +
error.getMessage() );
+ if ( this.error == null ) {
+ this.error = error;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void fatalError(SAXParseException error) {
+ error( error );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void warning(SAXParseException warn) {
+ log.error( "Warning parsing XML (" + error.getLineNumber() + ") : "
+ error.getMessage() );
+ }
+
+ public void reset() {
+ error = null;
+ }
+}
Added:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/MappingReader.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/MappingReader.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/MappingReader.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,257 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.StringReader;
+
+import org.dom4j.Document;
+import org.dom4j.io.SAXReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import org.hibernate.InvalidMappingException;
+
+/**
+ * Handles reading mapping documents, both {@code hbm} and {@code orm} varieties.
+ *
+ * @author Steve Ebersole
+ */
+public class MappingReader {
+ private static final Logger log = LoggerFactory.getLogger( MappingReader.class );
+
+ public static final String ASSUMED_ORM_XSD_VERSION = "2.0";
+ public static final MappingReader INSTANCE = new MappingReader();
+
+ /**
+ * Disallow direct instantiation.
+ * <p/>
+ * Eventually we perhaps need to have this configurable by the "configuration"
and simply reference it
+ * from there (registry). This would allow, for example, injection of the entity
resolver to use as
+ * instance state.
+ */
+ private MappingReader() {
+ }
+
+ public XmlDocument readMappingDocument(EntityResolver entityResolver, InputSource
source, Origin origin) {
+ // IMPL NOTE : this is the legacy logic as pulled from the old AnnotationConfiguration
code
+
+ Exception failure;
+ ErrorLogger errorHandler = new ErrorLogger();
+
+ SAXReader saxReader = new SAXReader();
+ saxReader.setEntityResolver( entityResolver );
+ saxReader.setErrorHandler( errorHandler );
+ saxReader.setMergeAdjacentText( true );
+ saxReader.setValidation( true );
+
+ Document document = null;
+ try {
+ // first try with orm 2.0 xsd validation
+ setValidationFor( saxReader, "orm_2_0.xsd" );
+ document = saxReader.read( source );
+ if ( errorHandler.getError() != null ) {
+ throw errorHandler.getError();
+ }
+ return new XmlDocumentImpl( document, origin.getType(), origin.getName() );
+ }
+ catch ( Exception orm2Problem ) {
+ log.debug( "Problem parsing XML using orm 2 xsd : {}",
orm2Problem.getMessage() );
+ failure = orm2Problem;
+ errorHandler.reset();
+
+ if ( document != null ) {
+ // next try with orm 1.0 xsd validation
+ try {
+ setValidationFor( saxReader, "orm_1_0.xsd" );
+ document = saxReader.read( new StringReader( document.asXML() ) );
+ if ( errorHandler.getError() != null ) {
+ throw errorHandler.getError();
+ }
+ return new XmlDocumentImpl( document, origin.getType(), origin.getName() );
+ }
+ catch ( Exception orm1Problem ) {
+ log.debug( "Problem parsing XML using orm 1 xsd : {}",
orm1Problem.getMessage() );
+ }
+ }
+ }
+ throw new InvalidMappingException( "Unable to read XML", origin.getType(),
origin.getName(), failure );
+ }
+
+ private void setValidationFor(SAXReader saxReader, String xsd) {
+ try {
+ saxReader.setFeature( "http://apache.org/xml/features/validation/schema",
true );
+ //saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic",
true );
+ //set the default schema locators
+ saxReader.setProperty(
+ "http://apache.org/xml/properties/schema/external-schemaLocation",
+ "http://java.sun.com/xml/ns/persistence/orm " + xsd
+ );
+ }
+ catch ( SAXException e ) {
+ saxReader.setValidation( false );
+ }
+ }
+
+ // this is the version of the code I'd like to use, but it unfortunately works very
differently between
+ // JDK 1.5 ad JDK 1.6. On 1.5 the vaildation "passes" even with invalid
content.
+ //
+ // Options:
+ // 1) continue using the code above
+ // 2) Document the issue on 1.5 and how to fix (specifying alternate SchemaFactory
instance)
+ // 3) Use a specific JAXP library (Xerces2, Saxon, Jing, MSV) and its SchemaFactory
instance directly
+
+// public XmlDocument readMappingDocument(EntityResolver entityResolver, InputSource
source, Origin origin) {
+// ErrorLogger errorHandler = new ErrorLogger();
+//
+// SAXReader saxReader = new SAXReader( new DOMDocumentFactory() );
+// saxReader.setEntityResolver( entityResolver );
+// saxReader.setErrorHandler( errorHandler );
+// saxReader.setMergeAdjacentText( true );
+//
+// Document documentTree = null;
+//
+// // IMPL NOTE : here we enable DTD validation in case the mapping is a HBM file. This
will validate
+// // the document as it is parsed. This is needed because the DTD defines default
values that have to be
+// // applied as the document is parsed, so thats something we need to account for as we
(if we) transition
+// // to XSD.
+// saxReader.setValidation( true );
+// try {
+// documentTree = saxReader.read( source );
+// }
+// catch ( DocumentException e ) {
+// // we had issues reading the input, most likely malformed document or validation
error against DTD
+// throw new InvalidMappingException( "Unable to read XML", origin.getType(),
origin.getName(), e );
+// }
+//
+// Element rootElement = documentTree.getRootElement();
+// if ( rootElement == null ) {
+// throw new InvalidMappingException( "No root element", origin.getType(),
origin.getName() );
+// }
+//
+// if ( "entity-mappings".equals( rootElement.getName() ) ) {
+// final String explicitVersion = rootElement.attributeValue( "version" );
+// final String xsdVersionString = explicitVersion == null ? ASSUMED_ORM_XSD_VERSION :
explicitVersion;
+// final SupportedOrmXsdVersion xsdVersion = SupportedOrmXsdVersion.parse(
xsdVersionString );
+// final Schema schema = xsdVersion == SupportedOrmXsdVersion.ORM_1_0 ? orm1Schema() :
orm2Schema();
+// try {
+// schema.newValidator().validate( new DOMSource( (org.w3c.dom.Document) documentTree
) );
+// }
+// catch ( SAXException e ) {
+// throw new InvalidMappingException( "Validation problem",
origin.getType(), origin.getName(), e );
+// }
+// catch ( IOException e ) {
+// throw new InvalidMappingException( "Validation problem",
origin.getType(), origin.getName(), e );
+// }
+// }
+// else {
+// if ( errorHandler.getError() != null ) {
+// throw new InvalidMappingException(
+// "Error validating hibernate-mapping against DTD",
+// origin.getType(),
+// origin.getName(),
+// errorHandler.getError()
+// );
+// }
+// }
+//
+// return new XmlDocumentImpl( documentTree, origin );
+// }
+//
+// public static enum SupportedOrmXsdVersion {
+// ORM_1_0,
+// ORM_2_0;
+//
+// public static SupportedOrmXsdVersion parse(String name) {
+// if ( "1.0".equals( name ) ) {
+// return ORM_1_0;
+// }
+// else if ( "2.0".equals( name ) ) {
+// return ORM_2_0;
+// }
+// throw new IllegalArgumentException( "Unsupported orm.xml XSD version
encountered [" + name + "]" );
+// }
+// }
+//
+//
+// public static final String ORM_1_SCHEMA_NAME =
"org/hibernate/ejb/orm_1_0.xsd";
+// public static final String ORM_2_SCHEMA_NAME =
"org/hibernate/ejb/orm_2_0.xsd";
+//
+// private static Schema orm1Schema;
+//
+// private static Schema orm1Schema() {
+// if ( orm1Schema == null ) {
+// orm1Schema = resolveLocalSchema( ORM_1_SCHEMA_NAME );
+// }
+// return orm1Schema;
+// }
+//
+// private static Schema orm2Schema;
+//
+// private static Schema orm2Schema() {
+// if ( orm2Schema == null ) {
+// orm2Schema = resolveLocalSchema( ORM_2_SCHEMA_NAME );
+// }
+// return orm2Schema;
+// }
+//
+// private static Schema resolveLocalSchema(String schemaName) {
+// return resolveLocalSchema( schemaName, XMLConstants.W3C_XML_SCHEMA_NS_URI );
+// }
+//
+// private static Schema resolveLocalSchema(String schemaName, String schemaLanguage) {
+// URL url = ConfigHelper.findAsResource( schemaName );
+// if ( url == null ) {
+// throw new MappingException( "Unable to locate schema [" + schemaName +
"] via classpath" );
+// }
+// try {
+// InputStream schemaStream = url.openStream();
+// try {
+// StreamSource source = new StreamSource(url.openStream());
+// SchemaFactory schemaFactory = SchemaFactory.newInstance( schemaLanguage );
+// return schemaFactory.newSchema(source);
+// }
+// catch ( SAXException e ) {
+// throw new MappingException( "Unable to load schema [" + schemaName +
"]", e );
+// }
+// catch ( IOException e ) {
+// throw new MappingException( "Unable to load schema [" + schemaName +
"]", e );
+// }
+// finally {
+// try {
+// schemaStream.close();
+// }
+// catch ( IOException e ) {
+// log.warn( "Problem closing schema stream [{}]", e.toString() );
+// }
+// }
+// }
+// catch ( IOException e ) {
+// throw new MappingException( "Stream error handling schema url [" +
url.toExternalForm() + "]" );
+// }
+//
+// }
+}
Added: core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/Origin.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/Origin.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/Origin.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,49 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.Serializable;
+
+/**
+ * Describes the origin of an xml document
+ *
+ * @author Steve Ebersole
+ */
+public interface Origin extends Serializable {
+ /**
+ * Retrieve the type of origin. This is not a discrete set, but might be somethign
like
+ * {@code file} for file protocol URLs, or {@code resource} for classpath resource
lookups.
+ *
+ * @return The origin type.
+ */
+ public String getType();
+
+ /**
+ * The name of the document origin. Interpretation is relative to the type, but might
be the
+ * resource name or file URL.
+ *
+ * @return The name.
+ */
+ public String getName();
+}
Added: core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/OriginImpl.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/OriginImpl.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/OriginImpl.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,55 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.Serializable;
+
+/**
+ * Basic implementation of {@link Origin}
+ *
+ * @author Steve Ebersole
+ */
+public class OriginImpl implements Origin, Serializable {
+ private final String type;
+ private final String name;
+
+ public OriginImpl(String type, String name) {
+ this.type = type;
+ this.name = name;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getName() {
+ return name;
+ }
+}
Added:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocument.java
===================================================================
--- core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocument.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocument.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,49 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.Serializable;
+
+import org.dom4j.Document;
+
+/**
+ * Describes a parsed xml document.
+ *
+ * @author Steve Ebersole
+ */
+public interface XmlDocument extends Serializable {
+ /**
+ * Retrieve the parsed DOM tree.
+ *
+ * @return the DOM tree
+ */
+ public Document getDocumentTree();
+
+ /**
+ * Retrieve the document's origin.
+ *
+ * @return The origin
+ */
+ public Origin getOrigin();
+}
Added:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocumentImpl.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocumentImpl.java
(rev 0)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/util/xml/XmlDocumentImpl.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,61 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util.xml;
+
+import java.io.Serializable;
+
+import org.dom4j.Document;
+
+/**
+ * Basic implemementation of {@link XmlDocument}
+ *
+ * @author Steve Ebersole
+ */
+public class XmlDocumentImpl implements XmlDocument, Serializable {
+ private final Document documentTree;
+ private final Origin origin;
+
+ public XmlDocumentImpl(Document documentTree, String originType, String originName) {
+ this( documentTree, new OriginImpl( originType, originName ) );
+ }
+
+ public XmlDocumentImpl(Document documentTree, Origin origin) {
+ this.documentTree = documentTree;
+ this.origin = origin;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Document getDocumentTree() {
+ return documentTree;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Origin getOrigin() {
+ return origin;
+ }
+}
Modified:
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java 2010-09-08
18:03:28 UTC (rev 20321)
+++
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -64,6 +64,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.hibernate.HibernateException;
@@ -102,6 +103,9 @@
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.StringHelper;
import org.hibernate.util.XMLHelper;
+import org.hibernate.util.xml.MappingReader;
+import org.hibernate.util.xml.OriginImpl;
+import org.hibernate.util.xml.XmlDocument;
/**
* Allow a fine tuned configuration of an EJB 3.0 EntityManagerFactory
@@ -691,37 +695,26 @@
private void addXMLEntities(List<String> xmlFiles, PersistenceUnitInfo info,
List<String> entities) {
//TODO handle inputstream related hbm files
- ClassLoader newTempClassLoader = info.getNewTempClassLoader();
- if (newTempClassLoader == null) {
+ ClassLoader classLoaderToUse = info.getNewTempClassLoader();
+ if ( classLoaderToUse == null ) {
log.warn( "Persistence provider caller does not implement the EJB3 spec
correctly. PersistenceUnitInfo.getNewTempClassLoader() is null." );
return;
}
- XMLHelper xmlHelper = new XMLHelper();
- List errors = new ArrayList();
- SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors,
cfg.getEntityResolver() );
- try {
- saxReader.setFeature( "http://apache.org/xml/features/validation/schema",
true );
- //saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic",
true );
- //set the default schema locators
- saxReader.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
- "http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd");
- }
- catch (SAXException e) {
- saxReader.setValidation( false );
- }
for ( String xmlFile : xmlFiles ) {
-
- InputStream resourceAsStream = newTempClassLoader.getResourceAsStream( xmlFile );
- if (resourceAsStream == null) continue;
- BufferedInputStream is = new BufferedInputStream( resourceAsStream );
+ InputStream resourceAsStream = classLoaderToUse.getResourceAsStream( xmlFile );
+ if ( resourceAsStream == null ) {
+ continue;
+ }
+ final BufferedInputStream is = new BufferedInputStream( resourceAsStream );
try {
- errors.clear();
- org.dom4j.Document doc = saxReader.read( is );
- if ( errors.size() != 0 ) {
- throw new MappingException( "invalid mapping: " + xmlFile, (Throwable)
errors.get( 0 ) );
- }
- Element rootElement = doc.getRootElement();
+ final InputSource inputSource = new InputSource( is );
+ XmlDocument xmlDocument = MappingReader.INSTANCE.readMappingDocument(
+ cfg.getEntityResolver(),
+ inputSource,
+ new OriginImpl( "persistence-unit-info", xmlFile )
+ );
+ Element rootElement = xmlDocument.getDocumentTree().getRootElement();
if ( rootElement != null && "entity-mappings".equals(
rootElement.getName() ) ) {
Element element = rootElement.element( "package" );
String defaultPackage = element != null ? element.getTextTrim() : null;
@@ -751,9 +744,6 @@
//FIXME include hbm xml entities to enhance them but entities is also used to
collect annotated entities
}
}
- catch (DocumentException e) {
- throw new MappingException( "Could not parse mapping document in input
stream", e );
- }
finally {
try {
is.close();
Added:
core/branches/Branch_3_5/entitymanager/src/test/java/org/hibernate/ejb/test/jee/OrmVersionTest.java
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/test/java/org/hibernate/ejb/test/jee/OrmVersionTest.java
(rev 0)
+++
core/branches/Branch_3_5/entitymanager/src/test/java/org/hibernate/ejb/test/jee/OrmVersionTest.java 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,172 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.ejb.test.jee;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.SharedCacheMode;
+import javax.persistence.ValidationMode;
+import javax.persistence.spi.ClassTransformer;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.hibernate.InvalidMappingException;
+import org.hibernate.ejb.HibernatePersistence;
+
+/**
+ * "smoke" tests for JEE bootstrapping of HEM via a {@link
PersistenceUnitInfo}
+ *
+ * @author Steve Ebersole
+ */
+public class OrmVersionTest extends TestCase {
+ public void testOrm1() {
+ PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl( "orm1-test",
"1.0" )
+ .addMappingFileName( "org/hibernate/ejb/test/jee/valid-orm-1.xml" );
+ HibernatePersistence hp = new HibernatePersistence();
+ EntityManagerFactory emf = hp.createContainerEntityManagerFactory( pui,
Collections.EMPTY_MAP );
+ emf.getMetamodel().entity( org.hibernate.ejb.test.pack.defaultpar_1_0.Lighter1.class );
// exception if not entity
+ }
+
+ public void testOrm2() {
+ PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl( "orm2-test",
"2.0" )
+ .addMappingFileName( "org/hibernate/ejb/test/jee/valid-orm-2.xml" );
+ HibernatePersistence hp = new HibernatePersistence();
+ EntityManagerFactory emf = hp.createContainerEntityManagerFactory( pui,
Collections.EMPTY_MAP );
+ emf.getMetamodel().entity( org.hibernate.ejb.test.pack.defaultpar.Lighter.class ); //
exception if not entity
+ }
+
+ public void testInvalidOrm1() {
+ PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl(
"invalid-orm1-test", "1.0" )
+ .addMappingFileName( "org/hibernate/ejb/test/jee/invalid-orm-1.xml" );
+ HibernatePersistence hp = new HibernatePersistence();
+ try {
+ hp.createContainerEntityManagerFactory( pui, Collections.EMPTY_MAP );
+ fail( "expecting 'invalid content' error" );
+ }
+ catch ( InvalidMappingException expected ) {
+ // expected condition
+ }
+ }
+
+ public static class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
+ private final String name;
+ private final String persistenceSchemaVersion;
+
+ public PersistenceUnitInfoImpl(String name) {
+ this( name, "2.0" );
+ }
+
+ public PersistenceUnitInfoImpl(String name, String persistenceSchemaVersion) {
+ this.name = name;
+ this.persistenceSchemaVersion = persistenceSchemaVersion;
+ }
+
+ public String getPersistenceUnitName() {
+ return name;
+ }
+
+ public String getPersistenceXMLSchemaVersion() {
+ return persistenceSchemaVersion;
+ }
+
+ private final List<String> mappingFileNames = new ArrayList<String>();
+
+ public List<String> getMappingFileNames() {
+ return mappingFileNames;
+ }
+
+ private final List<String> managedClassNames = new ArrayList<String>();
+
+ private PersistenceUnitInfoImpl addMappingFileName(String mappingFileName) {
+ mappingFileNames.add( mappingFileName );
+ return this;
+ }
+
+ public List<String> getManagedClassNames() {
+ return managedClassNames;
+ }
+
+ public String getPersistenceProviderClassName() {
+ return null;
+ }
+
+ public PersistenceUnitTransactionType getTransactionType() {
+ return PersistenceUnitTransactionType.RESOURCE_LOCAL;
+ }
+
+ public DataSource getJtaDataSource() {
+ return null;
+ }
+
+ public DataSource getNonJtaDataSource() {
+ return null;
+ }
+
+ private final List<URL> jarFileUrls = new ArrayList<URL>();
+
+ public List<URL> getJarFileUrls() {
+ return jarFileUrls;
+ }
+
+ public URL getPersistenceUnitRootUrl() {
+ return null;
+ }
+
+ public boolean excludeUnlistedClasses() {
+ return false;
+ }
+
+ public SharedCacheMode getSharedCacheMode() {
+ return null;
+ }
+
+ public ValidationMode getValidationMode() {
+ return null;
+ }
+
+ private final Properties properties = new Properties();
+
+ public Properties getProperties() {
+ return properties;
+ }
+
+ public ClassLoader getClassLoader() {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ public void addTransformer(ClassTransformer transformer) {
+ }
+
+ public ClassLoader getNewTempClassLoader() {
+ return getClassLoader();
+ }
+ }
+}
Added:
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/invalid-orm-1.xml
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/invalid-orm-1.xml
(rev 0)
+++
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/invalid-orm-1.xml 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Hibernate, Relational Persistence for Idiomatic Java
+ ~
+ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ ~ indicated by the @author tags or express copyright attribution
+ ~ statements applied by the authors. All third-party contributions are
+ ~ distributed under license by Red Hat Inc.
+ ~
+ ~ This copyrighted material is made available to anyone wishing to use, modify,
+ ~ copy, or redistribute it subject to the terms and conditions of the GNU
+ ~ Lesser General Public License, as published by the Free Software Foundation.
+ ~
+ ~ This program is distributed in the hope that it will be useful,
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ ~ for more details.
+ ~
+ ~ You should have received a copy of the GNU Lesser General Public License
+ ~ along with this distribution; if not, write to:
+ ~ Free Software Foundation, Inc.
+ ~ 51 Franklin Street, Fifth Floor
+ ~ Boston, MA 02110-1301 USA
+ -->
+<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
orm_1_0.xsd"
+ version="1.0"
+ >
+ <!-- use orm_1_0 on purpose (backward compatibility test -->
+ <persistence-unit-metadata>
+ <persistence-unit-defaults>
+ <delimited-identifiers/>
+ <entity-listeners>
+ <entity-listener
class="org.hibernate.ejb.test.pack.defaultpar_1_0.IncrementListener1">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </persistence-unit-defaults>
+ </persistence-unit-metadata>
+ <package>org.hibernate.ejb.test.pack.defaultpar_1_0</package>
+ <entity class="org.hibernate.ejb.test.pack.defaultpar_1_0.Lighter1"
access="FIELD" metadata-complete="true">
+ <attributes>
+ <id name="name">
+ <column name="fld_id"/>
+ </id>
+ <basic name="power"></basic>
+ </attributes>
+ </entity>
+ <entity
class="org.hibernate.ejb.test.pack.defaultpar_1_0.ApplicationServer1">
+ <entity-listeners>
+ <entity-listener class="OtherIncrementListener1">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </entity>
+</entity-mappings>
\ No newline at end of file
Added:
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-1.xml
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-1.xml
(rev 0)
+++
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-1.xml 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Hibernate, Relational Persistence for Idiomatic Java
+ ~
+ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ ~ indicated by the @author tags or express copyright attribution
+ ~ statements applied by the authors. All third-party contributions are
+ ~ distributed under license by Red Hat Inc.
+ ~
+ ~ This copyrighted material is made available to anyone wishing to use, modify,
+ ~ copy, or redistribute it subject to the terms and conditions of the GNU
+ ~ Lesser General Public License, as published by the Free Software Foundation.
+ ~
+ ~ This program is distributed in the hope that it will be useful,
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ ~ for more details.
+ ~
+ ~ You should have received a copy of the GNU Lesser General Public License
+ ~ along with this distribution; if not, write to:
+ ~ Free Software Foundation, Inc.
+ ~ 51 Franklin Street, Fifth Floor
+ ~ Boston, MA 02110-1301 USA
+ -->
+<entity-mappings version="1.0"
+
xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
orm_1_0.xsd">
+ <persistence-unit-metadata>
+ <persistence-unit-defaults>
+ <entity-listeners>
+ <entity-listener
class="org.hibernate.ejb.test.pack.defaultpar_1_0.IncrementListener1">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </persistence-unit-defaults>
+ </persistence-unit-metadata>
+ <package>org.hibernate.ejb.test.pack.defaultpar_1_0</package>
+ <entity class="org.hibernate.ejb.test.pack.defaultpar_1_0.Lighter1"
access="FIELD" metadata-complete="true">
+ <attributes>
+ <id name="name">
+ <column name="fld_id"/>
+ </id>
+ <basic name="power"></basic>
+ </attributes>
+ </entity>
+ <entity
class="org.hibernate.ejb.test.pack.defaultpar_1_0.ApplicationServer1">
+ <entity-listeners>
+ <entity-listener class="OtherIncrementListener1">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </entity>
+</entity-mappings>
\ No newline at end of file
Added:
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-2.xml
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-2.xml
(rev 0)
+++
core/branches/Branch_3_5/entitymanager/src/test/resources/org/hibernate/ejb/test/jee/valid-orm-2.xml 2010-09-08
20:20:32 UTC (rev 20322)
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Hibernate, Relational Persistence for Idiomatic Java
+ ~
+ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ ~ indicated by the @author tags or express copyright attribution
+ ~ statements applied by the authors. All third-party contributions are
+ ~ distributed under license by Red Hat Inc.
+ ~
+ ~ This copyrighted material is made available to anyone wishing to use, modify,
+ ~ copy, or redistribute it subject to the terms and conditions of the GNU
+ ~ Lesser General Public License, as published by the Free Software Foundation.
+ ~
+ ~ This program is distributed in the hope that it will be useful,
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ ~ for more details.
+ ~
+ ~ You should have received a copy of the GNU Lesser General Public License
+ ~ along with this distribution; if not, write to:
+ ~ Free Software Foundation, Inc.
+ ~ 51 Franklin Street, Fifth Floor
+ ~ Boston, MA 02110-1301 USA
+ -->
+<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
orm_2_0.xsd"
+ version="2.0"
+ >
+ <persistence-unit-metadata>
+ <persistence-unit-defaults>
+ <delimited-identifiers/>
+ <entity-listeners>
+ <entity-listener
class="org.hibernate.ejb.test.pack.defaultpar.IncrementListener">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </persistence-unit-defaults>
+ </persistence-unit-metadata>
+ <package>org.hibernate.ejb.test.pack.defaultpar</package>
+ <entity class="Lighter" access="FIELD"
metadata-complete="true">
+ <attributes>
+ <id name="name">
+ <column name="fld_id"/>
+ </id>
+ <basic name="power"></basic>
+ </attributes>
+ </entity>
+ <entity class="ApplicationServer">
+ <entity-listeners>
+ <entity-listener class="OtherIncrementListener">
+ <pre-persist method-name="increment"/>
+ </entity-listener>
+ </entity-listeners>
+ </entity>
+</entity-mappings>
\ No newline at end of file