1 package org.hibernate.ejb.packaging; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.URL ; 6 import javax.xml.parsers.DocumentBuilder ; 7 import javax.xml.parsers.DocumentBuilderFactory ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 import org.xml.sax.InputSource ; 16 17 23 public final class PersistenceXmlLoader { 24 private static Log log = LogFactory.getLog( PersistenceXmlLoader.class ); 25 26 private PersistenceXmlLoader() { 27 } 28 29 private static Document loadURL(URL configURL) throws Exception { 30 InputStream is = configURL != null ? configURL.openStream() : null; 31 if ( is == null ) { 32 throw new IOException ( "Failed to obtain InputStream from url: " + configURL ); 33 } 34 35 DocumentBuilderFactory docBuilderFactory = null; 36 docBuilderFactory = DocumentBuilderFactory.newInstance(); 37 docBuilderFactory.setValidating( false ); 38 InputSource source = new InputSource ( is ); 39 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 42 Document doc = docBuilder.parse( source ); 45 return doc; 46 } 47 48 public static PersistenceMetadata deploy(URL url) throws Exception { 49 PersistenceMetadata metadata = new PersistenceMetadata(); 50 Document doc = loadURL( url ); 51 Element top = doc.getDocumentElement(); 52 NodeList children = top.getChildNodes(); 53 for ( int i = 0 ; i < children.getLength() ; i++ ) { 54 if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) { 55 Element element = (Element ) children.item( i ); 56 String tag = element.getTagName(); 57 if ( tag.equals( "name" ) ) { 58 metadata.setName( XmlHelper.getElementContent( element ) ); 59 } 60 else if ( tag.equals( "non-jta-data-source" ) ) { 61 metadata.setDatasource( XmlHelper.getElementContent( element ) ); 62 } 63 else if ( tag.equals( "jta-data-source" ) ) { 64 metadata.setJtaDatasource( XmlHelper.getElementContent( element ) ); 65 } 66 else if ( tag.equals( "provider" ) ) { 67 metadata.setProvider( XmlHelper.getElementContent( element ) ); 68 } 69 else if ( tag.equals( "class" ) ) { 70 metadata.getClasses().add( XmlHelper.getElementContent( element ) ); 71 } 72 else if ( tag.equals( "mapping-file" ) ) { 73 metadata.getMappingFiles().add( XmlHelper.getElementContent( element ) ); 74 } 75 else if ( tag.equals( "jar-file" ) ) { 76 metadata.getJarFiles().add( XmlHelper.getElementContent( element ) ); 77 } 78 else if ( tag.equals( "properties" ) ) { 79 NodeList props = element.getChildNodes(); 80 for ( int j = 0 ; j < props.getLength() ; j++ ) { 81 if ( props.item( j ).getNodeType() == Node.ELEMENT_NODE ) { 82 Element propElement = (Element ) props.item( j ); 83 if ( !"property".equals( propElement.getTagName() ) ) continue; 84 String propName = propElement.getAttribute( "name" ).trim(); 85 String propValue = propElement.getAttribute( "value" ).trim(); 86 metadata.getProps().put( propName, propValue ); 87 } 88 } 89 90 } 91 } 92 } 93 return metadata; 94 } 95 96 } 97 | Popular Tags |