HHH-8053 HibernateBundleActivator should not register itself as a

PersistenceProvider
This commit is contained in:
Brett Meyer 2013-03-05 16:23:30 -05:00
parent 18d2f136f8
commit ae978df496
2 changed files with 26 additions and 27 deletions

View File

@ -42,6 +42,16 @@ public class HibernatePersistence extends AvailableSettings implements Persisten
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;
}
/**
* Get an entity manager factory by its entity manager name, using the specified
* properties (they override any found in the peristence.xml file).
@ -54,6 +64,9 @@ public class HibernatePersistence extends AvailableSettings implements Persisten
* @return initialized EntityManagerFactory
*/
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
if ( environmentProperties != null ) {
properties.putAll( environmentProperties );
}
Ejb3Configuration cfg = new Ejb3Configuration();
Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
return configured != null ? configured.buildEntityManagerFactory() : null;
@ -71,6 +84,9 @@ public class HibernatePersistence extends AvailableSettings implements Persisten
* @return initialized EntityManagerFactory
*/
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
if ( environmentProperties != null ) {
properties.putAll( environmentProperties );
}
Ejb3Configuration cfg = new Ejb3Configuration();
Ejb3Configuration configured = cfg.configure( info, properties );
return configured != null ? configured.buildEntityManagerFactory() : null;

View File

@ -23,16 +23,13 @@
*/
package org.hibernate.osgi;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.ejb.HibernatePersistence;
import org.hibernate.internal.util.ClassLoaderHelper;
import org.osgi.framework.Bundle;
@ -46,19 +43,14 @@ import org.osgi.framework.BundleListener;
* @author Martin Neimeier
*/
public class HibernateBundleActivator
extends HibernatePersistence
implements BundleActivator, /*ServiceListener,*/ BundleListener {
private BundleContext context;
private OsgiClassLoader osgiClassLoader;
private OsgiClassLoader osgiClassLoader;
@Override
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
context.addBundleListener(this);
@ -69,14 +61,15 @@ public class HibernateBundleActivator
for ( Bundle bundle : context.getBundles() ) {
handleBundleChange( bundle );
}
HibernatePersistence hp = new HibernatePersistence();
Map map = new HashMap();
map.put( AvailableSettings.JTA_PLATFORM, new OsgiJtaPlatform( context ) );
hp.setEnvironmentProperties( map );
Properties properties = new Properties();
properties.put( "javax.persistence.provider", HibernateBundleActivator.class.getName() );
context.registerService(
PersistenceProvider.class.getName(),
this,
properties
);
properties.put( "javax.persistence.provider", HibernatePersistence.class.getName() );
context.registerService( PersistenceProvider.class.getName(), hp, properties );
}
@Override
@ -101,14 +94,4 @@ public class HibernateBundleActivator
}
}
@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
Ejb3Configuration cfg = new Ejb3Configuration();
if ( info.getTransactionType().equals( PersistenceUnitTransactionType.JTA ) ) {
map.put( AvailableSettings.JTA_PLATFORM, new OsgiJtaPlatform( context ) );
}
Ejb3Configuration configured = cfg.configure( info, map );
return configured != null ? configured.buildEntityManagerFactory() : null;
}
}