Author: epbernard
Date: 2009-12-17 08:29:55 -0500 (Thu, 17 Dec 2009)
New Revision: 18257
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceMetadata.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceXmlLoader.java
Log:
Remove now unneeded flag to avoid getMember population
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceMetadata.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceMetadata.java 2009-12-17
11:05:13 UTC (rev 18256)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceMetadata.java 2009-12-17
13:29:55 UTC (rev 18257)
@@ -52,7 +52,16 @@
private Properties props = new Properties();
private boolean excludeUnlistedClasses = false;
private String validationMode;
+ private String version;
+ public String getVersion() {
+ return version;
+ }
+
+ public void setVersion(String version) {
+ this.version = version;
+ }
+
public String getSharedCacheMode() {
return sharedCacheMode;
}
@@ -176,7 +185,9 @@
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
- sb.append( "PersistenceMetadata [\n")
+ sb.append( "PersistenceMetadata(version=" )
+ .append( version )
+ .append(") [\n")
.append("\tname: ").append(name).append("\n")
.append("\tjtaDataSource: ").append(jtaDatasource).append("\n")
.append("\tnonJtaDataSource:
").append(nonJtaDatasource).append("\n")
@@ -212,8 +223,7 @@
}
sb.append( "\t]\n")
.append("\thbmfiles: ")
- .append( hbmfiles != null ? hbmfiles.size() : 0 ).append("\n")
- .append("\tproperties[\n");
+ .append( hbmfiles != null ? hbmfiles.size() : 0 ).append("\n");
if (validationMode != null) {
sb.append("\tvalidation-mode:
").append(validationMode).append("\n");
@@ -222,6 +232,8 @@
sb.append("\tshared-cache-mode:
").append(sharedCacheMode).append("\n");
}
+ sb.append("\tproperties[\n");
+
if (props != null) {
for ( Map.Entry elt : props.entrySet()) {
sb.append("\t\t").append( elt.getKey() ).append(": ").append(
elt.getValue() ).append("\n");
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceXmlLoader.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceXmlLoader.java 2009-12-17
11:05:13 UTC (rev 18256)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/packaging/PersistenceXmlLoader.java 2009-12-17
13:29:55 UTC (rev 18257)
@@ -33,6 +33,12 @@
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Schema;
+import javax.xml.validation.Validator;
+import javax.xml.validation.ValidatorHandler;
import org.hibernate.cfg.EJB3DTDEntityResolver;
import org.hibernate.ejb.HibernatePersistence;
@@ -71,30 +77,36 @@
if ( is == null ) {
throw new IOException( "Failed to obtain InputStream from url: " + configURL
);
}
- List errors = new ArrayList();
- DocumentBuilderFactory docBuilderFactory = null;
- docBuilderFactory = DocumentBuilderFactory.newInstance();
- docBuilderFactory.setValidating( true );
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware( true );
- try {
- //otherwise Xerces fails in validation
- docBuilderFactory.setAttribute(
"http://apache.org/xml/features/validation/schema", true );
- }
- catch (IllegalArgumentException e) {
- docBuilderFactory.setValidating( false );
- docBuilderFactory.setNamespaceAware( false );
- }
+ final Schema v2Schema = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
+ .newSchema( new StreamSource( getStreamFromClasspath( "persistence_2_0.xsd"
) ) );
+ final Validator v2Validator = v2Schema.newValidator();
+ final Schema v1Schema = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
+ .newSchema( new StreamSource( getStreamFromClasspath( "persistence_1_0.xsd"
) ) );
+ final Validator v1Validator = v2Schema.newValidator();
+
InputSource source = new InputSource( is );
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.setEntityResolver( resolver );
- docBuilder.setErrorHandler( new ErrorLogger( "XML InputStream", errors,
resolver ) );
Document doc = docBuilder.parse( source );
+
+ List errors = new ArrayList();
+ v2Validator.setErrorHandler( new ErrorLogger( "XML InputStream", errors,
resolver ) );
+ v2Validator.validate( new DOMSource( doc ) );
+
if ( errors.size() != 0 ) {
- throw new PersistenceException( "invalid persistence.xml", (Throwable)
errors.get( 0 ) );
+ throw new PersistenceException( "Invlid persistence.xml. Check the error logs for
parsing errors", (Throwable) errors.get( 0 ) );
}
return doc;
}
+ private static InputStream getStreamFromClasspath(String fileName) {
+ String path = "org/hibernate/ejb/" + fileName;
+ InputStream dtdStream =
PersistenceXmlLoader.class.getClassLoader().getResourceAsStream( path );
+ return dtdStream;
+ }
+
/**
* Method used by JBoss AS 4.0.5 for parsing
*/
@@ -109,6 +121,9 @@
PersistenceUnitTransactionType defaultTransactionType) throws Exception {
Document doc = loadURL( url, resolver );
Element top = doc.getDocumentElement();
+ //version is mandatory
+ final String version = top.getAttribute( "version" );
+
NodeList children = top.getChildNodes();
ArrayList<PersistenceMetadata> units = new
ArrayList<PersistenceMetadata>();
for ( int i = 0; i < children.getLength() ; i++ ) {
@@ -117,6 +132,7 @@
String tag = element.getTagName();
if ( tag.equals( "persistence-unit" ) ) {
PersistenceMetadata metadata = parsePersistenceUnit( element );
+ metadata.setVersion(version);
//override properties of metadata if needed
if ( overrides.containsKey( HibernatePersistence.PROVIDER ) ) {
String provider = (String) overrides.get( HibernatePersistence.PROVIDER );
@@ -270,20 +286,33 @@
}
public void error(SAXParseException error) {
- if ( resolver instanceof EJB3DTDEntityResolver ) {
- if ( ( (EJB3DTDEntityResolver) resolver ).isResolved() == false ) return;
- }
- log.error( "Error parsing XML: {}({}) {}", new Object[] { file,
error.getLineNumber(), error.getMessage() } );
+// if ( resolver instanceof EJB3DTDEntityResolver ) {
+// if ( ( (EJB3DTDEntityResolver) resolver ).isResolved() == false ) return;
+// }
+
+ log.error( "Error parsing XML (line {}: column {}): {}",
+ new Object[] {
+ error.getLineNumber(),
+ error.getColumnNumber(),
+ error.getMessage() } );
errors.add( error );
}
public void fatalError(SAXParseException error) {
- log.error( "Error parsing XML: {}({}) {}", new Object[] { file,
error.getLineNumber(), error.getMessage() } );
+ log.error( "Error parsing XML (line {}: column {}): {}",
+ new Object[] {
+ error.getLineNumber(),
+ error.getColumnNumber(),
+ error.getMessage() } );
errors.add( error );
}
public void warning(SAXParseException warn) {
- log.warn( "Warning parsing XML: {}({}) {}", new Object[] { file,
warn.getLineNumber(), warn.getMessage() } );
+ log.warn( "Warning parsing XML (line {}: column {}): {}",
+ new Object[] {
+ warn.getLineNumber(),
+ warn.getColumnNumber(),
+ warn.getMessage() } );
}
}