1 25 26 package org.objectweb.easybeans.persistence.xml; 27 28 import java.net.URL ; 29 30 import javax.persistence.spi.PersistenceProvider; 31 32 import org.objectweb.easybeans.api.EZBArchive; 33 import org.objectweb.easybeans.api.EZBArchiveException; 34 import org.objectweb.easybeans.log.JLog; 35 import org.objectweb.easybeans.log.JLogFactory; 36 import org.objectweb.easybeans.persistence.PersistenceUnitManager; 37 38 43 public final class PersistenceXmlFileAnalyzer { 44 45 48 private static final String DIRECTORY_PERSISTENCE_XML_FILE = "META-INF"; 49 50 53 private static final String PERSISTENCE_XML_FILE = "persistence.xml"; 54 55 58 private static JLog logger = JLogFactory.getLog(PersistenceXmlFileAnalyzer.class); 59 60 63 private PersistenceXmlFileAnalyzer() { 64 65 } 66 67 78 public static PersistenceUnitManager analyzePersistenceXmlFile(final EZBArchive archive, final ClassLoader classLoader) 79 throws PersistenceXmlFileAnalyzerException { 80 URL persistenceXmlURL = null; 81 try { 82 persistenceXmlURL = archive.getResource(DIRECTORY_PERSISTENCE_XML_FILE + '/' + PERSISTENCE_XML_FILE); 83 } catch (EZBArchiveException e) { 84 throw new PersistenceXmlFileAnalyzerException("Cannot check if entry '" + DIRECTORY_PERSISTENCE_XML_FILE 85 + '/' + PERSISTENCE_XML_FILE + "' is present on the file '" + archive.getName() + "'.", e); 86 } 87 88 JPersistenceUnitInfo[] persistenceUnitInfos = null; 90 if (persistenceXmlURL != null) { 91 try { 92 persistenceUnitInfos = JPersistenceUnitInfoHelper.getPersistenceUnitInfo(persistenceXmlURL); 93 } catch (JPersistenceUnitInfoException e) { 94 throw new PersistenceXmlFileAnalyzerException("Cannot parse the URL '" + persistenceXmlURL + "'.", e); 95 } 96 97 for (JPersistenceUnitInfo persistenceUnitInfo : persistenceUnitInfos) { 98 try { 99 persistenceUnitInfo.addJarFile(archive.getURL()); 101 102 persistenceUnitInfo.setPersistenceUnitRootUrl(archive.getURL()); 104 } catch (EZBArchiveException e) { 105 throw new PersistenceXmlFileAnalyzerException("Cannot get the URL on the jar file '" + archive.getName() 106 + "'.", e); 107 } 108 109 persistenceUnitInfo.setClassLoader(classLoader); 111 112 Class persistenceProviderClass; 114 try { 115 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); 116 persistenceProviderClass = ctxLoader.loadClass(persistenceUnitInfo 117 .getPersistenceProviderClassName()); 118 } catch (ClassNotFoundException e) { 119 throw new PersistenceXmlFileAnalyzerException("Cannot load the persistence provider class '" 120 + persistenceUnitInfo.getPersistenceProviderClassName() + "'."); 121 } 122 PersistenceProvider persistenceProvider; 123 try { 124 persistenceProvider = (PersistenceProvider) persistenceProviderClass.newInstance(); 125 } catch (InstantiationException e) { 126 throw new PersistenceXmlFileAnalyzerException("Cannot instantiate the persistence provider class '" 127 + persistenceUnitInfo.getPersistenceProviderClassName() + "'.", e); 128 } catch (IllegalAccessException e) { 129 throw new PersistenceXmlFileAnalyzerException("Cannot instantiate the persistence provider class '" 130 + persistenceUnitInfo.getPersistenceProviderClassName() + "'.", e); 131 } 132 133 persistenceUnitInfo.setPersistenceProvider(persistenceProvider); 135 136 } 137 138 return new PersistenceUnitManager(persistenceUnitInfos); 140 } 141 return null; 143 } 144 145 } 146 | Popular Tags |