HHH-8553 hibernate-osgi needs to support alternative locations for

hibernate.cfg.xml
Conflicts:
	documentation/src/main/docbook/quickstart/tutorials/osgi/managed-jpa/features.xml
	documentation/src/main/docbook/quickstart/tutorials/osgi/unmanaged-jpa/features.xml
	documentation/src/main/docbook/quickstart/tutorials/osgi/unmanaged-native/features.xml
	hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java
This commit is contained in:
Brett Meyer 2013-09-25 00:05:29 -04:00
parent 8b406db4a9
commit c2ea55b0bd
1 changed files with 21 additions and 1 deletions

View File

@ -20,21 +20,25 @@
*/
package org.hibernate.osgi;
import java.util.Collection;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metamodel.spi.TypeContributor;
import org.hibernate.osgi.util.OsgiServiceUtil;
import org.hibernate.service.BootstrapServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.jboss.logging.Logger;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.wiring.BundleWiring;
/**
* Hibernate 4.2 and 4.3 still heavily rely on TCCL for ClassLoading. Although
@ -55,6 +59,8 @@ import org.osgi.framework.ServiceRegistration;
* @author Tim Ward
*/
public class OsgiSessionFactoryService implements ServiceFactory {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
OsgiSessionFactoryService.class.getName());
private OsgiClassLoader osgiClassLoader;
@ -75,7 +81,21 @@ public class OsgiSessionFactoryService implements ServiceFactory {
Configuration configuration = new Configuration();
configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
configuration.configure();
// 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 );
if (cfgResources.size() == 0) {
configuration.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 );
}
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
builder.with( osgiClassLoader );