HHH-10365 added OsgiClassLoaderService support for non-container-managed JPA, tweaked method overloading

This commit is contained in:
Brett Meyer 2016-05-26 10:49:03 -04:00
parent 585a8aa757
commit 69bdf979cb
4 changed files with 78 additions and 48 deletions

View File

@ -16,8 +16,8 @@ import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.ProviderUtil;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
@ -68,10 +68,21 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties) {
return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null );
return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null, null );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties, ClassLoader providedClassLoader) {
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
ClassLoader providedClassLoader) {
return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, providedClassLoader, null );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
ClassLoaderService providedClassLoaderService) {
return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null, providedClassLoaderService );
}
private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
ClassLoader providedClassLoader, ClassLoaderService providedClassLoaderService) {
log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName );
final Map integration = wrap( properties );
@ -111,7 +122,11 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
continue;
}
return getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoader );
if (providedClassLoaderService != null) {
return getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoaderService );
} else {
return getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoader );
}
}
log.debug( "Found no matching persistence units" );
@ -156,8 +171,8 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
return true;
}
private EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
return getEntityManagerFactoryBuilder( new PersistenceUnitInfoDescriptor( info ), integration, null );
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
return Bootstrap.getEntityManagerFactoryBuilder( info, integration );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
@ -165,6 +180,11 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoader );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
Map integration, ClassLoaderService providedClassLoaderService) {
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoaderService );
}
private final ProviderUtil providerUtil = new ProviderUtil() {
@Override
public LoadState isLoadedWithoutReference(Object proxy, String property) {

View File

@ -159,17 +159,24 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
}
public EntityManagerFactoryBuilderImpl(PersistenceUnitDescriptor persistenceUnit, Map integrationSettings) {
this( persistenceUnit, integrationSettings, null );
this( persistenceUnit, integrationSettings, null, null );
}
public EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings,
ClassLoader providedClassLoader ) {
this(persistenceUnit,integrationSettings,providedClassLoader,null);
this( persistenceUnit, integrationSettings, providedClassLoader, null);
}
public EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings,
ClassLoaderService providedClassLoaderService ) {
this( persistenceUnit, integrationSettings, null, providedClassLoaderService);
}
public EntityManagerFactoryBuilderImpl(
private EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings,
ClassLoader providedClassLoader,
@ -321,12 +328,10 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
*/
private BootstrapServiceRegistry buildBootstrapServiceRegistry(
Map integrationSettings,
ClassLoader providedClassLoader,
ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) {
final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
if (providedClassLoaderService != null) {
bsrBuilder.applyClassLoaderService(providedClassLoaderService);
}
bsrBuilder.applyIntegrator( new JpaIntegrator() );
final IntegratorProvider integratorProvider = (IntegratorProvider) integrationSettings.get( INTEGRATOR_PROVIDER );
@ -346,37 +351,43 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
// ClassLoaders ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NOTE: See BootstrapServiceRegistryBuilder#build. providedClassLoaderService and providedClassLoaders are
// mutually exclusive concepts, with priority given to the former
if ( persistenceUnit.getClassLoader() != null ) {
bsrBuilder.applyClassLoader( persistenceUnit.getClassLoader() );
}
if ( providedClassLoader != null ) {
bsrBuilder.applyClassLoader( providedClassLoader );
}
final ClassLoader appClassLoader = (ClassLoader) integrationSettings.get( org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER );
if ( appClassLoader != null ) {
LOG.debugf(
"Found use of deprecated `%s` setting; use `%s` instead.",
org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER,
org.hibernate.cfg.AvailableSettings.CLASSLOADERS
);
}
final Object classLoadersSetting = integrationSettings.get( org.hibernate.cfg.AvailableSettings.CLASSLOADERS );
if ( classLoadersSetting != null ) {
if ( java.util.Collection.class.isInstance( classLoadersSetting ) ) {
for ( ClassLoader classLoader : (java.util.Collection<ClassLoader>) classLoadersSetting ) {
bsrBuilder.applyClassLoader( classLoader );
}
if ( providedClassLoaderService != null ) {
bsrBuilder.applyClassLoaderService( providedClassLoaderService );
} else {
if ( persistenceUnit.getClassLoader() != null ) {
bsrBuilder.applyClassLoader( persistenceUnit.getClassLoader() );
}
else if ( classLoadersSetting.getClass().isArray() ) {
for ( ClassLoader classLoader : (ClassLoader[]) classLoadersSetting ) {
bsrBuilder.applyClassLoader( classLoader );
}
if ( providedClassLoader != null ) {
bsrBuilder.applyClassLoader( providedClassLoader );
}
else if ( ClassLoader.class.isInstance( classLoadersSetting ) ) {
bsrBuilder.applyClassLoader( (ClassLoader) classLoadersSetting );
final ClassLoader appClassLoader = (ClassLoader) integrationSettings.get( org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER );
if ( appClassLoader != null ) {
LOG.debugf(
"Found use of deprecated `%s` setting; use `%s` instead.",
org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER,
org.hibernate.cfg.AvailableSettings.CLASSLOADERS
);
}
final Object classLoadersSetting = integrationSettings.get( org.hibernate.cfg.AvailableSettings.CLASSLOADERS );
if ( classLoadersSetting != null ) {
if ( java.util.Collection.class.isInstance( classLoadersSetting ) ) {
for ( ClassLoader classLoader : (java.util.Collection<ClassLoader>) classLoadersSetting ) {
bsrBuilder.applyClassLoader( classLoader );
}
}
else if ( classLoadersSetting.getClass().isArray() ) {
for ( ClassLoader classLoader : (ClassLoader[]) classLoadersSetting ) {
bsrBuilder.applyClassLoader( classLoader );
}
}
else if ( ClassLoader.class.isInstance( classLoadersSetting ) ) {
bsrBuilder.applyClassLoader( (ClassLoader) classLoadersSetting );
}
}
}

View File

@ -39,9 +39,8 @@ public final class Bootstrap {
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
PersistenceUnitDescriptor persistenceUnitDescriptor,
Map integration,
ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) {
return new EntityManagerFactoryBuilderImpl( persistenceUnitDescriptor, integration, providedClassLoader, providedClassLoaderService );
return new EntityManagerFactoryBuilderImpl( persistenceUnitDescriptor, integration, providedClassLoaderService );
}
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
@ -60,8 +59,7 @@ public final class Bootstrap {
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
PersistenceUnitInfo persistenceUnitInfo,
Map integration,
ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) {
return getEntityManagerFactoryBuilder( new PersistenceUnitInfoDescriptor( persistenceUnitInfo ), integration, providedClassLoader, providedClassLoaderService );
return getEntityManagerFactoryBuilder( new PersistenceUnitInfoDescriptor( persistenceUnitInfo ), integration, providedClassLoaderService );
}
}

View File

@ -46,7 +46,6 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
* @param osgiClassLoader The ClassLoader we built from OSGi Bundles
* @param osgiJtaPlatform The OSGi-specific JtaPlatform impl we built
* @param requestingBundle The OSGi Bundle requesting the PersistenceProvider
* @param context The OSGi context
*/
public OsgiPersistenceProvider(
OsgiClassLoader osgiClassLoader,
@ -75,7 +74,8 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
osgiClassLoader.addBundle( requestingBundle );
final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, settings, osgiClassLoader );
final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, settings,
new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ) );
return builder == null ? null : builder.build();
}
@ -92,7 +92,8 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
osgiClassLoader.addClassLoader( info.getClassLoader() );
return Bootstrap.getEntityManagerFactoryBuilder( info, settings, osgiClassLoader, new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ) ).build();
return Bootstrap.getEntityManagerFactoryBuilder( info, settings,
new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ) ).build();
}
@SuppressWarnings("unchecked")