FileDocCategorySizeDatePackage
PersistenceXmlLoader.javaAPI DocJBoss 4.2.110141Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3.entity

PersistenceXmlLoader

public final class PersistenceXmlLoader extends Object
Persistence.xml handler THIS CLASS SHOULD BE REMOVED WHEN HEM FIXES default transactiontype bug
author
Bill Burke
author
Emmanuel Bernard

Fields Summary
private static Log
log
Constructors Summary
private PersistenceXmlLoader()


	  
	
Methods Summary
public static java.util.Listdeploy(java.net.URL url, java.util.Map overrides, org.xml.sax.EntityResolver resolver)

      return deploy(url, overrides, resolver, PersistenceUnitTransactionType.RESOURCE_LOCAL);
   
public static java.util.Listdeploy(java.net.URL url, java.util.Map overrides, org.xml.sax.EntityResolver resolver, javax.persistence.spi.PersistenceUnitTransactionType defaultTransactionType)

		Document doc = loadURL( url, resolver );
		Element top = doc.getDocumentElement();
		NodeList children = top.getChildNodes();
		ArrayList<PersistenceMetadata> units = new ArrayList<PersistenceMetadata>();
		for ( int i = 0; i < children.getLength() ; i++ ) {
			if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
				Element element = (Element) children.item( i );
				String tag = element.getTagName();
				if ( tag.equals( "persistence-unit" ) ) {
					PersistenceMetadata metadata = parsePersistenceUnit( element, defaultTransactionType);
					//override properties of metadata if needed
					String provider = (String) overrides.get( HibernatePersistence.PROVIDER );
					if ( provider != null ) {
						metadata.setProvider( provider );
					}
					String transactionType = (String) overrides.get( HibernatePersistence.TRANSACTION_TYPE );
					if ( StringHelper.isNotEmpty( transactionType ) ) {
						metadata.setTransactionType( getTransactionType( transactionType ) );
					}
					String dataSource = (String) overrides.get( HibernatePersistence.JTA_DATASOURCE );
					if ( dataSource != null ) {
						metadata.setJtaDatasource( dataSource );
					}
					dataSource = (String) overrides.get( HibernatePersistence.NON_JTA_DATASOURCE );
					if ( dataSource != null ) {
						metadata.setNonJtaDatasource( dataSource );
					}
					Properties properties = metadata.getProps();
					ConfigurationHelper.overrideProperties( properties, overrides );
					units.add( metadata );
				}
			}
		}

		return units;
	
public static javax.persistence.spi.PersistenceUnitTransactionTypegetTransactionType(java.lang.String elementContent)

		if ( StringHelper.isEmpty( elementContent ) ) {
			return null; //PersistenceUnitTransactionType.JTA;
		}
		else if ( elementContent.equalsIgnoreCase( "JTA" ) ) {
			return PersistenceUnitTransactionType.JTA;
		}
		else if ( elementContent.equalsIgnoreCase( "RESOURCE_LOCAL" ) ) {
			return PersistenceUnitTransactionType.RESOURCE_LOCAL;
		}
		else {
			throw new PersistenceException( "Unknown TransactionType: " + elementContent );
		}
	
private static org.w3c.dom.DocumentloadURL(java.net.URL configURL, org.xml.sax.EntityResolver resolver)

		InputStream is = configURL != null ? configURL.openStream() : null;
		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 );
		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 );
		}
		InputSource source = new InputSource( is );
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		docBuilder.setEntityResolver( resolver );
		docBuilder.setErrorHandler( new PersistenceXmlLoader.ErrorLogger( "XML InputStream", errors, resolver ) );
		Document doc = docBuilder.parse( source );
		if ( errors.size() != 0 ) {
			throw new PersistenceException( "invalid persistence.xml", (Throwable) errors.get( 0 ) );
		}
		return doc;
	
private static org.hibernate.ejb.packaging.PersistenceMetadataparsePersistenceUnit(org.w3c.dom.Element top, javax.persistence.spi.PersistenceUnitTransactionType defaultTransactionType)

		PersistenceMetadata metadata = new PersistenceMetadata();
		String puName = top.getAttribute( "name" );
		if ( StringHelper.isNotEmpty( puName ) ) {
			log.trace( "Persistent Unit name from persistence.xml: " + puName );
			metadata.setName( puName );
		}
		PersistenceUnitTransactionType transactionType = getTransactionType( top.getAttribute( "transaction-type" ) );
		//parsing a persistence.xml means we are in a JavaSE environment
		transactionType = transactionType != null ? transactionType : defaultTransactionType;
		metadata.setTransactionType( transactionType );
		NodeList children = top.getChildNodes();
		for ( int i = 0; i < children.getLength() ; i++ ) {
			if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
				Element element = (Element) children.item( i );
				String tag = element.getTagName();
//				if ( tag.equals( "name" ) ) {
//					String puName = XmlHelper.getElementContent( element );
//					log.trace( "FOUND PU NAME: " + puName );
//					metadata.setName( puName );
//				}
//				else
				if ( tag.equals( "non-jta-data-source" ) ) {
					metadata.setNonJtaDatasource( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "jta-data-source" ) ) {
					metadata.setJtaDatasource( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "provider" ) ) {
					metadata.setProvider( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "class" ) ) {
					metadata.getClasses().add( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "mapping-file" ) ) {
					metadata.getMappingFiles().add( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "jar-file" ) ) {
					metadata.getJarFiles().add( XmlHelper.getElementContent( element ) );
				}
				else if ( tag.equals( "exclude-unlisted-classes" ) ) {
					metadata.setExcludeUnlistedClasses( true );
				}
				else if ( tag.equals( "properties" ) ) {
					NodeList props = element.getChildNodes();
					for ( int j = 0; j < props.getLength() ; j++ ) {
						if ( props.item( j ).getNodeType() == Node.ELEMENT_NODE ) {
							Element propElement = (Element) props.item( j );
							if ( !"property".equals( propElement.getTagName() ) ) continue;
							String propName = propElement.getAttribute( "name" ).trim();
							String propValue = propElement.getAttribute( "value" ).trim();
							if ( StringHelper.isEmpty( propValue ) ) {
								//fall back to the natural (Hibernate) way of description
								propValue = XmlHelper.getElementContent( propElement, "" );
							}
							metadata.getProps().put( propName, propValue );
						}
					}

				}
			}
		}

		return metadata;