HHH-8053 HibernateBundleActivator should not register itself as a

PersistenceProvider
This commit is contained in:
Brett Meyer 2013-03-05 16:46:21 -05:00
parent 139474624f
commit 5f8ba65ce1
2 changed files with 29 additions and 29 deletions

View File

@ -51,6 +51,16 @@ import org.hibernate.jpa.internal.util.PersistenceUtilHelper;
public class HibernatePersistenceProvider implements PersistenceProvider { public class HibernatePersistenceProvider implements PersistenceProvider {
private final PersistenceUtilHelper.MetadataCache cache = new PersistenceUtilHelper.MetadataCache(); private final PersistenceUtilHelper.MetadataCache cache = new PersistenceUtilHelper.MetadataCache();
/**
* Used for environment-supplied properties. Ex: hibernate-osgi's
* HibernateBundleActivator needs to set a custom JtaPlatform.
*/
private Map environmentProperties;
public void setEnvironmentProperties( Map environmentProperties ) {
this.environmentProperties = environmentProperties;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -59,6 +69,9 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
*/ */
@Override @Override
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) { public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
if ( environmentProperties != null ) {
properties.putAll( environmentProperties );
}
final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties ); final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties );
return builder == null ? null : builder.build(); return builder == null ? null : builder.build();
} }
@ -100,8 +113,11 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
* Note: per-spec, the values passed as {@code properties} override values found in {@link PersistenceUnitInfo} * Note: per-spec, the values passed as {@code properties} override values found in {@link PersistenceUnitInfo}
*/ */
@Override @Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integration) { public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
return Bootstrap.getEntityManagerFactoryBuilder( info, integration ).build(); if ( environmentProperties != null ) {
properties.putAll( environmentProperties );
}
return Bootstrap.getEntityManagerFactoryBuilder( info, properties ).build();
} }
@Override @Override

View File

@ -23,18 +23,15 @@
*/ */
package org.hibernate.osgi; package org.hibernate.osgi;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider; import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.internal.util.ClassLoaderHelper;
import org.hibernate.jpa.HibernatePersistenceProvider; import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
@ -46,19 +43,14 @@ import org.osgi.framework.BundleListener;
* @author Martin Neimeier * @author Martin Neimeier
*/ */
public class HibernateBundleActivator public class HibernateBundleActivator
extends HibernatePersistenceProvider
implements BundleActivator, /*ServiceListener,*/ BundleListener { implements BundleActivator, /*ServiceListener,*/ BundleListener {
private BundleContext context; private OsgiClassLoader osgiClassLoader;
private OsgiClassLoader osgiClassLoader;
@Override @Override
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
this.context = context; // register this instance as a bundle listener to get informed about all
// register this instance as a bundle listener to get informed about all
// bundle live cycle events // bundle live cycle events
context.addBundleListener(this); context.addBundleListener(this);
@ -69,14 +61,15 @@ public class HibernateBundleActivator
for ( Bundle bundle : context.getBundles() ) { for ( Bundle bundle : context.getBundles() ) {
handleBundleChange( bundle ); handleBundleChange( bundle );
} }
HibernatePersistenceProvider hpp = new HibernatePersistenceProvider();
Map map = new HashMap();
map.put( AvailableSettings.JTA_PLATFORM, new OsgiJtaPlatform( context ) );
hpp.setEnvironmentProperties( map );
Properties properties = new Properties(); Properties properties = new Properties();
properties.put( "javax.persistence.provider", HibernateBundleActivator.class.getName() ); properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
context.registerService( context.registerService( PersistenceProvider.class.getName(), hpp, properties );
PersistenceProvider.class.getName(),
this,
properties
);
} }
@Override @Override
@ -100,13 +93,4 @@ public class HibernateBundleActivator
osgiClassLoader.unregisterBundle(bundle); osgiClassLoader.unregisterBundle(bundle);
} }
} }
@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
if ( info.getTransactionType().equals( PersistenceUnitTransactionType.JTA ) ) {
map.put( AvailableSettings.JTA_PLATFORM, new OsgiJtaPlatform( context ) );
}
return Bootstrap.getEntityManagerFactoryBuilder( info, map, osgiClassLoader ).build();
}
} }