diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java index e985566ab6..c85dba1e34 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java @@ -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; diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java index 808591c270..8cd3dd679f 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java @@ -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; - } - } \ No newline at end of file