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:
parent
8b406db4a9
commit
c2ea55b0bd
|
@ -20,21 +20,25 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.osgi;
|
package org.hibernate.osgi;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.integrator.spi.Integrator;
|
import org.hibernate.integrator.spi.Integrator;
|
||||||
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
import org.hibernate.metamodel.spi.TypeContributor;
|
import org.hibernate.metamodel.spi.TypeContributor;
|
||||||
import org.hibernate.osgi.util.OsgiServiceUtil;
|
import org.hibernate.osgi.util.OsgiServiceUtil;
|
||||||
import org.hibernate.service.BootstrapServiceRegistryBuilder;
|
import org.hibernate.service.BootstrapServiceRegistryBuilder;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.hibernate.service.ServiceRegistryBuilder;
|
import org.hibernate.service.ServiceRegistryBuilder;
|
||||||
|
import org.jboss.logging.Logger;
|
||||||
import org.osgi.framework.Bundle;
|
import org.osgi.framework.Bundle;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
import org.osgi.framework.ServiceFactory;
|
import org.osgi.framework.ServiceFactory;
|
||||||
import org.osgi.framework.ServiceRegistration;
|
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
|
* 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
|
* @author Tim Ward
|
||||||
*/
|
*/
|
||||||
public class OsgiSessionFactoryService implements ServiceFactory {
|
public class OsgiSessionFactoryService implements ServiceFactory {
|
||||||
|
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
|
||||||
|
OsgiSessionFactoryService.class.getName());
|
||||||
|
|
||||||
private OsgiClassLoader osgiClassLoader;
|
private OsgiClassLoader osgiClassLoader;
|
||||||
|
|
||||||
|
@ -75,7 +81,21 @@ public class OsgiSessionFactoryService implements ServiceFactory {
|
||||||
|
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
|
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();
|
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
|
||||||
builder.with( osgiClassLoader );
|
builder.with( osgiClassLoader );
|
||||||
|
|
Loading…
Reference in New Issue