// Try with the new JPA 2.0 API
try {
List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
.getPersistenceProviderResolver().getPersistenceProviders();
if (persistenceProviders != null && persistenceProviders.size() > 0) {
PersistenceProvider provider = persistenceProviders.get(0);
persistenceUnitInfo.setPersistenceProvider(provider);
persistenceUnitInfo.setPersistenceProviderClassName(provider.getClass().getName());
} else {
throw new PersistenceXmlFileAnalyzerException("No Persistence provider has been set");
}
} catch (Error e) {
throw new PersistenceXmlFileAnalyzerException(
"No Persistence provider has been set and no JPA 2.0 API found so no access to "
+ "PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders() was available");
}
}
// Instatiate only if this was not already done
if (persistenceUnitInfo.getPersistenceProvider() == null) {
// instantiate persistence provider
Class<?> persistenceProviderClass;
try {
persistenceProviderClass = ClassUtils.forName(persistenceUnitInfo
.getPersistenceProviderClassName(), PersistenceXmlFileAnalyzer.class);
} catch (ClassNotFoundException e) {
throw new PersistenceXmlFileAnalyzerException("Cannot load the persistence provider class '"
+ persistenceUnitInfo.getPersistenceProviderClassName() + "'.");
}
PersistenceProvider persistenceProvider;
try {
persistenceProvider = (PersistenceProvider) persistenceProviderClass.newInstance();
} catch (InstantiationException e) {
throw new PersistenceXmlFileAnalyzerException("Cannot instantiate the persistence provider class '"
+ persistenceUnitInfo.getPersistenceProviderClassName() + "'.", e);