1 25 26 package org.objectweb.easybeans.persistence.xml; 27 28 import static javax.persistence.spi.PersistenceUnitTransactionType.JTA; 29 import static javax.persistence.spi.PersistenceUnitTransactionType.RESOURCE_LOCAL; 30 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.Properties ; 35 36 import org.objectweb.easybeans.log.JLog; 37 import org.objectweb.easybeans.log.JLogFactory; 38 import org.objectweb.easybeans.util.xml.DocumentParser; 39 import org.objectweb.easybeans.util.xml.DocumentParserException; 40 import org.objectweb.easybeans.util.xml.XMLUtils; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.NodeList ; 44 45 50 public final class JPersistenceUnitInfoLoader { 51 52 55 private static final String PERSISTENCE_NS = "http://java.sun.com/xml/ns/persistence"; 56 57 60 private static final String PERSISTENCE_UNIT = "persistence-unit"; 61 62 65 private static JLog logger = JLogFactory.getLog(JPersistenceUnitInfoLoader.class); 66 67 70 private static boolean validating = true; 71 72 75 private JPersistenceUnitInfoLoader() { 76 77 } 78 79 85 public static JPersistenceUnitInfo[] loadPersistenceUnitInfoImpl(final URL url) 86 throws JPersistenceUnitInfoException { 87 88 89 logger.debug("Analyzing url {0}", url); 90 91 List <JPersistenceUnitInfo> jPersistenceUnitInfos = new ArrayList <JPersistenceUnitInfo>(); 93 94 Document document = null; 96 try { 97 document = DocumentParser.getDocument(url, validating, new PersistenceUnitEntityResolver()); 98 } catch (DocumentParserException e) { 99 throw new JPersistenceUnitInfoException("Cannot parse the url", e); 100 } 101 102 Element persistenceRootElement = document.getDocumentElement(); 104 105 NodeList persistenceUnitInfoList = persistenceRootElement.getElementsByTagNameNS(PERSISTENCE_NS, PERSISTENCE_UNIT); 106 107 for (int i = 0; i < persistenceUnitInfoList.getLength(); i++) { 109 Element pUnitElement = (Element ) persistenceUnitInfoList.item(i); 110 111 JPersistenceUnitInfo persistenceUnitInfo = new JPersistenceUnitInfo(); 113 114 persistenceUnitInfo.setPersistenceXmlFileUrl(url); 116 117 String className = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "provider"); 119 persistenceUnitInfo.setPersistenceProviderClassName(className); 120 121 String jtaDataSourceName = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "jta-data-source"); 123 persistenceUnitInfo.setJtaDataSourceName(jtaDataSourceName); 124 125 String nonJtaDataSourceName = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "non-jta-data-source"); 127 persistenceUnitInfo.setNonJtaDataSourceName(nonJtaDataSourceName); 128 129 List <String > mappingFiles = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "mapping-file"); 131 for (String mappingFileName : mappingFiles) { 132 persistenceUnitInfo.addMappingFileName(mappingFileName); 133 } 134 135 List <String > jarFiles = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "jar-file"); 137 for (String jarFileName : jarFiles) { 138 logger.warn("JarFile found with name {0}. But not yet supported.", jarFileName); 139 } 142 143 144 List <String > classes = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "class"); 146 for (String managedClassName : classes) { 147 persistenceUnitInfo.addClass(managedClassName); 148 } 149 150 String excluded = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "exclude-unlisted-classes"); 152 persistenceUnitInfo.setExcludeUnlistedClasses("true".equals(excluded)); 153 154 Properties props = XMLUtils.getPropertiesValueElement(PERSISTENCE_NS, pUnitElement, "properties"); 156 persistenceUnitInfo.setProperties(props); 157 158 String name = XMLUtils.getAttributeValue(pUnitElement, "name"); 160 persistenceUnitInfo.setPersistenceUnitName(name); 161 162 String transactionType = XMLUtils.getAttributeValue(pUnitElement, "transaction-type"); 164 if ("JTA".equals(transactionType)) { 165 persistenceUnitInfo.setTransactionType(JTA); 166 } else if ("RESOURCE_LOCAL".equals(transactionType)) { 167 persistenceUnitInfo.setTransactionType(RESOURCE_LOCAL); 168 } else { 169 logger.info("No transaction-type defined. Set to default JTA transaction-type"); 170 persistenceUnitInfo.setTransactionType(JTA); 171 } 172 173 jPersistenceUnitInfos.add(persistenceUnitInfo); 174 175 } 176 return jPersistenceUnitInfos.toArray(new JPersistenceUnitInfo[jPersistenceUnitInfos.size()]); 177 } 178 179 } 180 | Popular Tags |