HHH-9492 - Migrate to new bootstrap API (MetadataSources, etc)

This commit is contained in:
Steve Ebersole 2015-03-19 15:36:49 -05:00
parent ffce4ffc2c
commit 8b9fc2d63a
2 changed files with 37 additions and 31 deletions

View File

@ -25,13 +25,13 @@ package org.hibernate.osgi;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.persistence.spi.PersistenceProvider;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.internal.util.ClassLoaderHelper;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
@ -51,7 +51,7 @@ import org.osgi.framework.ServiceRegistration;
* 3.) Client bundles create and manage their own SessionFactory. A
* SessionFactory is registered as an OSGi ServiceFactory -- each requesting
* bundle gets its own instance of a SessionFactory. The use of services,
* rather than direct use of Configuration, is necessary to shield users
* rather than manual building of the SessionFactory, is necessary to shield users
* from ClassLoader issues. See {@link OsgiSessionFactoryService} for more
* information.
*

View File

@ -23,16 +23,20 @@ package org.hibernate.osgi;
import java.util.Collection;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.service.ServiceRegistry;
import org.jboss.logging.Logger;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
@ -40,8 +44,8 @@ import org.osgi.framework.wiring.BundleWiring;
/**
* Hibernate 4.2 and 4.3 still heavily rely on TCCL for ClassLoading. Although
* our ClassLoaderService removed some of the reliance, the service is
* unfortunately not available during Configuration. An OSGi
* our ClassLoaderService removed some of the reliance, access to the proper ClassLoader
* via TCCL is still required in a few cases where we call out to external libs. An OSGi
* bundle manually creating a SessionFactory would require numerous ClassLoader
* tricks (or may be impossible altogether).
* <p/>
@ -69,7 +73,7 @@ public class OsgiSessionFactoryService implements ServiceFactory {
*
* @param osgiClassLoader The OSGi-specific ClassLoader created in HibernateBundleActivator
* @param osgiJtaPlatform The OSGi-specific JtaPlatform created in HibernateBundleActivator
* @param context The OSGi context
* @param osgiServiceUtil Util object built in HibernateBundleActivator
*/
public OsgiSessionFactoryService(
OsgiClassLoader osgiClassLoader,
@ -84,47 +88,49 @@ public class OsgiSessionFactoryService implements ServiceFactory {
public Object getService(Bundle requestingBundle, ServiceRegistration registration) {
osgiClassLoader.addBundle( requestingBundle );
final Configuration configuration = new Configuration();
configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
bsrBuilder.with( new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ) );
final Integrator[] integrators = osgiServiceUtil.getServiceImpls( Integrator.class );
for ( Integrator integrator : integrators ) {
bsrBuilder.with( integrator );
}
final StrategyRegistrationProvider[] strategyRegistrationProviders
= osgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class );
for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders ) {
bsrBuilder.withStrategySelectors( strategyRegistrationProvider );
}
final BootstrapServiceRegistry bsr = bsrBuilder.build();
final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder( bsr );
// Allow bundles to put the config file somewhere other than the root level.
final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt( BundleWiring.class );
final Collection<String> cfgResources = bundleWiring.listResources( "/", "hibernate.cfg.xml",
BundleWiring.LISTRESOURCES_RECURSE );
BundleWiring.LISTRESOURCES_RECURSE );
if (cfgResources.size() == 0) {
configuration.configure();
ssrBuilder.configure();
}
else {
if (cfgResources.size() > 1) {
LOG.warn( "Multiple hibernate.cfg.xml files found in the persistence bundle. Using the first one discovered." );
}
String cfgResource = "/" + cfgResources.iterator().next();
configuration.configure( cfgResource );
ssrBuilder.configure( cfgResource );
}
final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
final OSGiClassLoaderServiceImpl classLoaderService = new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil );
builder.with( classLoaderService );
ssrBuilder.applySetting( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
final Integrator[] integrators = osgiServiceUtil.getServiceImpls( Integrator.class );
for ( Integrator integrator : integrators ) {
builder.with( integrator );
}
final StrategyRegistrationProvider[] strategyRegistrationProviders
= osgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class );
for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders ) {
builder.withStrategySelectors( strategyRegistrationProvider );
}
final StandardServiceRegistry ssr = ssrBuilder.build();
final MetadataBuilder metadataBuilder = new MetadataSources( ssr ).getMetadataBuilder();
final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls( TypeContributor.class );
for ( TypeContributor typeContributor : typeContributors ) {
configuration.registerTypeContributor( typeContributor );
metadataBuilder.with( typeContributor );
}
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() )
.applySettings( configuration.getProperties() ).build();
return configuration.buildSessionFactory( serviceRegistry );
return metadataBuilder.build().buildSessionFactory();
}
@Override