Search in sources :

Example 1 with PersistenceProviderResolver

use of javax.persistence.spi.PersistenceProviderResolver in project OpenAttestation by OpenAttestation.

the class PersistenceManager method createEntityManagerFactory.

/**
     * The new form of this method.  The old form simply wraps this one now.
     * Loads persistence.xml from the classpath and creates an entity manager
     * with those settings, which can now include Apache Commons Pool and JDBC.
     * 
     * @param persistenceUnitName
     * @param properties
     * @return 
     */
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Properties jpaProperties) {
    log.debug("Loading database driver {} for persistence unit {}", new String[] { jpaProperties.getProperty("javax.persistence.jdbc.driver"), persistenceUnitName });
    try {
        Class.forName(jpaProperties.getProperty("javax.persistence.jdbc.driver"));
    } catch (ClassNotFoundException ex) {
        log.error("Cannot load JDBC Driver for persistence unit", ex);
    }
    PersistenceUnitInfo persistenceUnitInfo = null;
    try {
        persistenceUnitInfo = getPersistenceUnitInfo(persistenceUnitName, jpaProperties);
    } catch (IOException e) {
        throw new PersistenceException("Cannot load PersistenceUnit named " + persistenceUnitName, e);
    }
    if (persistenceUnitInfo == null) {
        throw new PersistenceException("Cannot find PersistenceUnit named " + persistenceUnitName);
    }
    EntityManagerFactory emf = null;
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
    List<PersistenceProvider> providers = resolver.getPersistenceProviders();
    // check if we have the requested provider
    if (persistenceUnitInfo.getPersistenceProviderClassName() != null) {
        log.info("Looking for specific JPA provider: {}", persistenceUnitInfo.getPersistenceProviderClassName());
        for (PersistenceProvider provider : providers) {
            log.info("Looking at provider: {}", provider.getClass().getName());
            if (provider.getClass().getName().equals(persistenceUnitInfo.getPersistenceProviderClassName())) {
                // important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically:  remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
                emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
                if (emf != null) {
                    log.info("Found requested persistence provider");
                    return emf;
                }
            }
        }
    }
    // check if any other provider can accomodate the persistence unit
    log.info("Looking for any compatible JPA provider");
    for (PersistenceProvider provider : providers) {
        log.info("Looking at provider: {}", provider.getClass().getName());
        // important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically:  remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
        emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
        if (emf != null) {
            log.info("Found compatible persistence provider");
            return emf;
        }
    }
    throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
}
Also used : PersistenceProviderResolver(javax.persistence.spi.PersistenceProviderResolver) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceProvider(javax.persistence.spi.PersistenceProvider) PersistenceException(javax.persistence.PersistenceException) IOException(java.io.IOException) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo)

Aggregations

IOException (java.io.IOException)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 PersistenceException (javax.persistence.PersistenceException)1 PersistenceProvider (javax.persistence.spi.PersistenceProvider)1 PersistenceProviderResolver (javax.persistence.spi.PersistenceProviderResolver)1 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)1