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

View File

@ -159,17 +159,24 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
} }
public EntityManagerFactoryBuilderImpl(PersistenceUnitDescriptor persistenceUnit, Map integrationSettings) { public EntityManagerFactoryBuilderImpl(PersistenceUnitDescriptor persistenceUnit, Map integrationSettings) {
this( persistenceUnit, integrationSettings, null ); this( persistenceUnit, integrationSettings, null, null );
} }
public EntityManagerFactoryBuilderImpl( public EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit, PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings, Map integrationSettings,
ClassLoader providedClassLoader ) { ClassLoader providedClassLoader ) {
this(persistenceUnit,integrationSettings,providedClassLoader,null); this( persistenceUnit, integrationSettings, providedClassLoader, null);
} }
public EntityManagerFactoryBuilderImpl( public EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings,
ClassLoaderService providedClassLoaderService ) {
this( persistenceUnit, integrationSettings, null, providedClassLoaderService);
}
private EntityManagerFactoryBuilderImpl(
PersistenceUnitDescriptor persistenceUnit, PersistenceUnitDescriptor persistenceUnit,
Map integrationSettings, Map integrationSettings,
ClassLoader providedClassLoader, ClassLoader providedClassLoader,
@ -324,9 +331,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
ClassLoader providedClassLoader, ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) { ClassLoaderService providedClassLoaderService) {
final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder(); final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
if (providedClassLoaderService != null) {
bsrBuilder.applyClassLoaderService(providedClassLoaderService);
}
bsrBuilder.applyIntegrator( new JpaIntegrator() ); bsrBuilder.applyIntegrator( new JpaIntegrator() );
final IntegratorProvider integratorProvider = (IntegratorProvider) integrationSettings.get( INTEGRATOR_PROVIDER ); final IntegratorProvider integratorProvider = (IntegratorProvider) integrationSettings.get( INTEGRATOR_PROVIDER );
@ -346,37 +351,43 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
// ClassLoaders ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ClassLoaders ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NOTE: See BootstrapServiceRegistryBuilder#build. providedClassLoaderService and providedClassLoaders are
// mutually exclusive concepts, with priority given to the former
if ( persistenceUnit.getClassLoader() != null ) { if ( providedClassLoaderService != null ) {
bsrBuilder.applyClassLoader( persistenceUnit.getClassLoader() ); bsrBuilder.applyClassLoaderService( providedClassLoaderService );
} } else {
if ( persistenceUnit.getClassLoader() != null ) {
if ( providedClassLoader != null ) { bsrBuilder.applyClassLoader( persistenceUnit.getClassLoader() );
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 );
}
} }
else if ( classLoadersSetting.getClass().isArray() ) {
for ( ClassLoader classLoader : (ClassLoader[]) classLoadersSetting ) { if ( providedClassLoader != null ) {
bsrBuilder.applyClassLoader( classLoader ); 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( public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
PersistenceUnitDescriptor persistenceUnitDescriptor, PersistenceUnitDescriptor persistenceUnitDescriptor,
Map integration, Map integration,
ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) { ClassLoaderService providedClassLoaderService) {
return new EntityManagerFactoryBuilderImpl( persistenceUnitDescriptor, integration, providedClassLoader, providedClassLoaderService ); return new EntityManagerFactoryBuilderImpl( persistenceUnitDescriptor, integration, providedClassLoaderService );
} }
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder( public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
@ -60,8 +59,7 @@ public final class Bootstrap {
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder( public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
PersistenceUnitInfo persistenceUnitInfo, PersistenceUnitInfo persistenceUnitInfo,
Map integration, Map integration,
ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) { 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 osgiClassLoader The ClassLoader we built from OSGi Bundles
* @param osgiJtaPlatform The OSGi-specific JtaPlatform impl we built * @param osgiJtaPlatform The OSGi-specific JtaPlatform impl we built
* @param requestingBundle The OSGi Bundle requesting the PersistenceProvider * @param requestingBundle The OSGi Bundle requesting the PersistenceProvider
* @param context The OSGi context
*/ */
public OsgiPersistenceProvider( public OsgiPersistenceProvider(
OsgiClassLoader osgiClassLoader, OsgiClassLoader osgiClassLoader,
@ -75,7 +74,8 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
osgiClassLoader.addBundle( requestingBundle ); 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(); return builder == null ? null : builder.build();
} }
@ -92,7 +92,8 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
osgiClassLoader.addClassLoader( info.getClassLoader() ); 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") @SuppressWarnings("unchecked")