HHH-8159 - Apply fixups indicated by analysis tools

This commit is contained in:
Steve Ebersole 2013-04-30 14:40:44 -05:00
parent 97e28200f0
commit 20866585f3
11 changed files with 195 additions and 118 deletions

View File

@ -51,37 +51,40 @@ import org.osgi.framework.FrameworkUtil;
* SessionFactory is registered as an OSGi ServiceFactory -- each requesting * SessionFactory is registered as an OSGi ServiceFactory -- each requesting
* bundle gets its own instance of a SessionFactory. The use of services, * bundle gets its own instance of a SessionFactory. The use of services,
* rather than direct use of Configuration, is necessary to shield users * rather than direct use of Configuration, is necessary to shield users
* from ClassLoader issues. See {@link #OsgiSessionFactoryService} for more * from ClassLoader issues. See {@link OsgiSessionFactoryService} for more
* information. * information.
* *
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
@SuppressWarnings("UnusedDeclaration")
public class HibernateBundleActivator implements BundleActivator { public class HibernateBundleActivator implements BundleActivator {
private OsgiClassLoader osgiClassLoader;
private OsgiJtaPlatform osgiJtaPlatform;
@Override @Override
@SuppressWarnings("unchecked")
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
// build a ClassLoader that uses all the necessary OSGi bundles, and place it into
osgiClassLoader = new OsgiClassLoader(); // a well-known location so internals can access it
final OsgiClassLoader osgiClassLoader = new OsgiClassLoader();
osgiClassLoader.addBundle( FrameworkUtil.getBundle( Session.class ) ); osgiClassLoader.addBundle( FrameworkUtil.getBundle( Session.class ) );
osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernatePersistenceProvider.class ) ); osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernatePersistenceProvider.class ) );
ClassLoaderHelper.overridenClassLoader = osgiClassLoader; ClassLoaderHelper.overridenClassLoader = osgiClassLoader;
osgiJtaPlatform = new OsgiJtaPlatform( context ); // Build a JtaPlatform specific for this OSGi context
final OsgiJtaPlatform osgiJtaPlatform = new OsgiJtaPlatform( context );
Dictionary properties = new Hashtable(); final Dictionary properties = new Hashtable();
// In order to support existing persistence.xml files, register // In order to support existing persistence.xml files, register using the legacy provider name.
// using the legacy provider name.
properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() ); properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
context.registerService( PersistenceProvider.class.getName(), context.registerService(
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), properties ); PersistenceProvider.class.getName(),
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ),
context.registerService( SessionFactory.class.getName(), properties
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), new Hashtable()); );
context.registerService(
SessionFactory.class.getName(),
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ),
new Hashtable()
);
} }
@Override @Override

View File

@ -37,18 +37,26 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring; import org.osgi.framework.wiring.BundleWiring;
/** /**
* ArchiveDescriptor implementation for describing archives in the OSGi sense
*
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiArchiveDescriptor implements ArchiveDescriptor { public class OsgiArchiveDescriptor implements ArchiveDescriptor {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
OsgiArchiveDescriptor.class.getName()
);
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, private final Bundle persistenceBundle;
OsgiArchiveDescriptor.class.getName() ); private final BundleWiring bundleWiring;
private BundleWiring bundleWiring;
private Bundle persistenceBundle;
/**
* Creates a OsgiArchiveDescriptor
*
* @param persistenceBundle The bundle being described as an archive
*/
@SuppressWarnings("RedundantCast")
public OsgiArchiveDescriptor(Bundle persistenceBundle) { public OsgiArchiveDescriptor(Bundle persistenceBundle) {
this.persistenceBundle = persistenceBundle; this.persistenceBundle = persistenceBundle;
bundleWiring = (BundleWiring) persistenceBundle.adapt( BundleWiring.class ); bundleWiring = (BundleWiring) persistenceBundle.adapt( BundleWiring.class );
@ -56,14 +64,12 @@ public class OsgiArchiveDescriptor implements ArchiveDescriptor {
@Override @Override
public void visitArchive(ArchiveContext context) { public void visitArchive(ArchiveContext context) {
Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE ); final Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );
for ( final String resource : resources ) { for ( final String resource : resources ) {
// TODO: Is there a better way to check this? Karaf is including // TODO: Is there a better way to check this? Karaf is including directories.
// directories.
if ( !resource.endsWith( "/" ) ) { if ( !resource.endsWith( "/" ) ) {
try { try {
// TODO: Is using resource as the names correct? // TODO: Is using resource as the names correct?
final InputStreamAccess inputStreamAccess = new InputStreamAccess() { final InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@Override @Override
public String getStreamName() { public String getStreamName() {

View File

@ -27,13 +27,19 @@ import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptorFactory;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
/** /**
* ArchiveDescriptorFactory implementation for OSGi environments
*
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory { public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory {
private Bundle persistenceBundle; private Bundle persistenceBundle;
/**
* Creates a OsgiArchiveDescriptorFactory
*
* @param persistenceBundle The OSGi bundle being scanned
*/
public OsgiArchiveDescriptorFactory(Bundle persistenceBundle) { public OsgiArchiveDescriptorFactory(Bundle persistenceBundle) {
this.persistenceBundle = persistenceBundle; this.persistenceBundle = persistenceBundle;
} }

View File

@ -41,15 +41,11 @@ import org.osgi.framework.Bundle;
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiClassLoader extends ClassLoader { public class OsgiClassLoader extends ClassLoader {
private List<ClassLoader> classLoaders = new ArrayList<ClassLoader>(); private List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();
private List<Bundle> bundles = new ArrayList<Bundle>(); private List<Bundle> bundles = new ArrayList<Bundle>();
private Map<String, Class<?>> classCache = new HashMap<String, Class<?>>(); private Map<String, Class<?>> classCache = new HashMap<String, Class<?>>();
private Map<String, URL> resourceCache = new HashMap<String, URL>(); private Map<String, URL> resourceCache = new HashMap<String, URL>();
private Map<String, Enumeration<URL>> resourceListCache = new HashMap<String, Enumeration<URL>>(); private Map<String, Enumeration<URL>> resourceListCache = new HashMap<String, Enumeration<URL>>();
/** /**
@ -57,8 +53,8 @@ public class OsgiClassLoader extends ClassLoader {
* TODO: Should this throw a different exception or warn if multiple * TODO: Should this throw a different exception or warn if multiple
* classes were found? Naming collisions can and do happen in OSGi... * classes were found? Naming collisions can and do happen in OSGi...
*/ */
@SuppressWarnings("rawtypes")
@Override @Override
@SuppressWarnings("rawtypes")
protected Class<?> findClass(String name) throws ClassNotFoundException { protected Class<?> findClass(String name) throws ClassNotFoundException {
if ( classCache.containsKey( name ) ) { if ( classCache.containsKey( name ) ) {
return classCache.get( name ); return classCache.get( name );
@ -66,7 +62,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( Bundle bundle : bundles ) { for ( Bundle bundle : bundles ) {
try { try {
Class clazz = bundle.loadClass( name ); final Class clazz = bundle.loadClass( name );
if ( clazz != null ) { if ( clazz != null ) {
classCache.put( name, clazz ); classCache.put( name, clazz );
return clazz; return clazz;
@ -78,7 +74,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( ClassLoader classLoader : classLoaders ) { for ( ClassLoader classLoader : classLoaders ) {
try { try {
Class clazz = classLoader.loadClass( name ); final Class clazz = classLoader.loadClass( name );
if ( clazz != null ) { if ( clazz != null ) {
classCache.put( name, clazz ); classCache.put( name, clazz );
return clazz; return clazz;
@ -104,7 +100,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( Bundle bundle : bundles ) { for ( Bundle bundle : bundles ) {
try { try {
URL resource = bundle.getResource( name ); final URL resource = bundle.getResource( name );
if ( resource != null ) { if ( resource != null ) {
resourceCache.put( name, resource ); resourceCache.put( name, resource );
return resource; return resource;
@ -116,7 +112,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( ClassLoader classLoader : classLoaders ) { for ( ClassLoader classLoader : classLoaders ) {
try { try {
URL resource = classLoader.getResource( name ); final URL resource = classLoader.getResource( name );
if ( resource != null ) { if ( resource != null ) {
resourceCache.put( name, resource ); resourceCache.put( name, resource );
return resource; return resource;
@ -135,8 +131,8 @@ public class OsgiClassLoader extends ClassLoader {
* TODO: Should this throw a different exception or warn if multiple * TODO: Should this throw a different exception or warn if multiple
* classes were found? Naming collisions can and do happen in OSGi... * classes were found? Naming collisions can and do happen in OSGi...
*/ */
@SuppressWarnings("unchecked")
@Override @Override
@SuppressWarnings("unchecked")
protected Enumeration<URL> findResources(String name) { protected Enumeration<URL> findResources(String name) {
if ( resourceListCache.containsKey( name ) ) { if ( resourceListCache.containsKey( name ) ) {
return resourceListCache.get( name ); return resourceListCache.get( name );
@ -146,7 +142,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( Bundle bundle : bundles ) { for ( Bundle bundle : bundles ) {
try { try {
Enumeration<URL> resources = bundle.getResources( name ); final Enumeration<URL> resources = bundle.getResources( name );
if ( resources != null ) { if ( resources != null ) {
enumerations.add( resources ); enumerations.add( resources );
} }
@ -157,7 +153,7 @@ public class OsgiClassLoader extends ClassLoader {
for ( ClassLoader classLoader : classLoaders ) { for ( ClassLoader classLoader : classLoaders ) {
try { try {
Enumeration<URL> resources = classLoader.getResources( name ); final Enumeration<URL> resources = classLoader.getResources( name );
if ( resources != null ) { if ( resources != null ) {
enumerations.add( resources ); enumerations.add( resources );
} }
@ -166,7 +162,7 @@ public class OsgiClassLoader extends ClassLoader {
} }
} }
Enumeration<URL> aggEnumeration = new Enumeration<URL>() { final Enumeration<URL> aggEnumeration = new Enumeration<URL>() {
@Override @Override
public boolean hasMoreElements() { public boolean hasMoreElements() {
for ( Enumeration<URL> enumeration : enumerations ) { for ( Enumeration<URL> enumeration : enumerations ) {
@ -193,14 +189,27 @@ public class OsgiClassLoader extends ClassLoader {
return aggEnumeration; return aggEnumeration;
} }
/**
* Adds a ClassLoader to the wrapped set of ClassLoaders
*
* @param classLoader The ClassLoader to add
*/
public void addClassLoader( ClassLoader classLoader ) { public void addClassLoader( ClassLoader classLoader ) {
classLoaders.add( classLoader ); classLoaders.add( classLoader );
} }
/**
* Adds a Bundle to the wrapped set of Bundles
*
* @param bundle The Bundle to add
*/
public void addBundle( Bundle bundle ) { public void addBundle( Bundle bundle ) {
bundles.add( bundle ); bundles.add( bundle );
} }
/**
* Clear all resources.
*/
public void clear() { public void clear() {
classCache.clear(); classCache.clear();
resourceCache.clear(); resourceCache.clear();

View File

@ -38,24 +38,28 @@ import org.osgi.framework.ServiceReference;
* @author Brett Meyer * @author Brett Meyer
*/ */
public class OsgiJtaPlatform implements JtaPlatform { public class OsgiJtaPlatform implements JtaPlatform {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private BundleContext bundleContext; private BundleContext bundleContext;
/**
* Constructs a OsgiJtaPlatform
*
* @param bundleContext The OSGi bundle context
*/
public OsgiJtaPlatform(BundleContext bundleContext) { public OsgiJtaPlatform(BundleContext bundleContext) {
this.bundleContext = bundleContext; this.bundleContext = bundleContext;
} }
@Override @Override
public TransactionManager retrieveTransactionManager() { public TransactionManager retrieveTransactionManager() {
ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() ); final ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() );
return (TransactionManager) bundleContext.getService( sr ); return (TransactionManager) bundleContext.getService( sr );
} }
@Override @Override
public UserTransaction retrieveUserTransaction() { public UserTransaction retrieveUserTransaction() {
ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() ); final ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() );
return (UserTransaction) bundleContext.getService( sr ); return (UserTransaction) bundleContext.getService( sr );
} }

View File

@ -37,27 +37,36 @@ import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.IntegratorProvider; import org.hibernate.jpa.boot.spi.IntegratorProvider;
import org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList; import org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList;
import org.hibernate.osgi.util.OsgiServiceUtil;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleReference; import org.osgi.framework.BundleReference;
/** /**
* Acts as the PersistenceProvider service in OSGi environments
*
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiPersistenceProvider extends HibernatePersistenceProvider { public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
private OsgiClassLoader osgiClassLoader; private OsgiClassLoader osgiClassLoader;
private OsgiJtaPlatform osgiJtaPlatform; private OsgiJtaPlatform osgiJtaPlatform;
private Bundle requestingBundle; private Bundle requestingBundle;
private BundleContext context; private BundleContext context;
public OsgiPersistenceProvider(OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform, /**
Bundle requestingBundle, BundleContext context) { * Constructs a OsgiPersistenceProvider
*
* @param osgiClassLoader The ClassLoader we built from OSGi Bundles
* @param osgiJtaPlatform The OSGi-specific JtaPlatform impl we built
* @param requestingBundle The OSGi Bundle requesting the PersistenceProvider
* @param context The OSGi context
*/
public OsgiPersistenceProvider(
OsgiClassLoader osgiClassLoader,
OsgiJtaPlatform osgiJtaPlatform,
Bundle requestingBundle,
BundleContext context) {
this.osgiClassLoader = osgiClassLoader; this.osgiClassLoader = osgiClassLoader;
this.osgiJtaPlatform = osgiJtaPlatform; this.osgiJtaPlatform = osgiJtaPlatform;
this.requestingBundle = requestingBundle; this.requestingBundle = requestingBundle;
@ -68,14 +77,15 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
// EMFBuilder somehow? // EMFBuilder somehow?
@Override @Override
@SuppressWarnings("unchecked")
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) { public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
properties = generateProperties( properties ); final Map settings = generateSettings( properties );
// TODO: This needs tested. // TODO: This needs tested.
properties.put( org.hibernate.jpa.AvailableSettings.SCANNER, new OsgiScanner( requestingBundle ) ); settings.put( org.hibernate.jpa.AvailableSettings.SCANNER, new OsgiScanner( requestingBundle ) );
// TODO: This is temporary -- for PersistenceXmlParser's use of // TODO: This is temporary -- for PersistenceXmlParser's use of
// ClassLoaderServiceImpl#fromConfigSettings // ClassLoaderServiceImpl#fromConfigSettings
properties.put( AvailableSettings.ENVIRONMENT_CLASSLOADER, osgiClassLoader ); settings.put( AvailableSettings.ENVIRONMENT_CLASSLOADER, osgiClassLoader );
osgiClassLoader.addBundle( requestingBundle ); osgiClassLoader.addBundle( requestingBundle );
@ -83,45 +93,49 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
} }
@Override @Override
@SuppressWarnings("unchecked")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
properties = generateProperties( properties ); final Map settings = generateSettings( properties );
// OSGi ClassLoaders must implement BundleReference // OSGi ClassLoaders must implement BundleReference
properties.put( org.hibernate.jpa.AvailableSettings.SCANNER, settings.put(
new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) ); org.hibernate.jpa.AvailableSettings.SCANNER,
new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() )
);
osgiClassLoader.addClassLoader( info.getClassLoader() ); osgiClassLoader.addClassLoader( info.getClassLoader() );
return super.createContainerEntityManagerFactory( info, properties ); return super.createContainerEntityManagerFactory( info, properties );
} }
private Map generateProperties(Map properties) { @SuppressWarnings("unchecked")
if ( properties == null ) { private Map generateSettings(Map properties) {
properties = new HashMap(); final Map settings = new HashMap();
if ( properties != null ) {
settings.putAll( properties );
} }
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); settings.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
final List<Integrator> integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); final List<Integrator> integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context );
IntegratorProvider integratorProvider = new IntegratorProvider() { final IntegratorProvider integratorProvider = new IntegratorProvider() {
@Override @Override
public List<Integrator> getIntegrators() { public List<Integrator> getIntegrators() {
return integrators; return integrators;
} }
}; };
properties.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider ); settings.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider );
final List<StrategyRegistrationProvider> strategyRegistrationProviders = OsgiServiceUtil.getServiceImpls( final List<StrategyRegistrationProvider> strategyRegistrationProviders = OsgiServiceUtil.getServiceImpls(
StrategyRegistrationProvider.class, context ); StrategyRegistrationProvider.class, context );
StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() { final StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() {
@Override @Override
public List<StrategyRegistrationProvider> getStrategyRegistrationProviders() { public List<StrategyRegistrationProvider> getStrategyRegistrationProviders() {
return strategyRegistrationProviders; return strategyRegistrationProviders;
} }
}; };
properties.put( EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, settings.put( EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, strategyRegistrationProviderList );
strategyRegistrationProviderList );
return properties; return settings;
} }
} }

View File

@ -26,22 +26,28 @@ import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration; import org.osgi.framework.ServiceRegistration;
/** /**
* See the description on {@link #OsgiSessionFactoryService}. This class * See the description on {@link OsgiSessionFactoryService}. This class is similar, providing an
* is similar, providing an PersistenceProvider as an OSGi Service. * PersistenceProvider as an OSGi Service.
* *
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiPersistenceProviderService implements ServiceFactory { public class OsgiPersistenceProviderService implements ServiceFactory {
private OsgiClassLoader osgiClassLoader; private OsgiClassLoader osgiClassLoader;
private OsgiJtaPlatform osgiJtaPlatform; private OsgiJtaPlatform osgiJtaPlatform;
private BundleContext context; private BundleContext context;
public OsgiPersistenceProviderService( OsgiClassLoader osgiClassLoader, /**
OsgiJtaPlatform osgiJtaPlatform, BundleContext context ) { * Constructs a OsgiPersistenceProviderService
*
* @param osgiClassLoader The OSGi-specific ClassLoader created in HibernateBundleActivator
* @param osgiJtaPlatform The OSGi-specific JtaPlatform created in HibernateBundleActivator
* @param context The OSGi context
*/
public OsgiPersistenceProviderService(
OsgiClassLoader osgiClassLoader,
OsgiJtaPlatform osgiJtaPlatform,
BundleContext context) {
this.osgiClassLoader = osgiClassLoader; this.osgiClassLoader = osgiClassLoader;
this.osgiJtaPlatform = osgiJtaPlatform; this.osgiJtaPlatform = osgiJtaPlatform;
this.context = context; this.context = context;

View File

@ -31,7 +31,11 @@ import org.osgi.framework.Bundle;
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiScanner extends AbstractScannerImpl { public class OsgiScanner extends AbstractScannerImpl {
/**
* Constructs the scanner for finding things in a OSGi bundle
*
* @param persistenceBundle The OSGi Bundle to scan
*/
public OsgiScanner(Bundle persistenceBundle) { public OsgiScanner(Bundle persistenceBundle) {
super( new OsgiArchiveDescriptorFactory( persistenceBundle ) ); super( new OsgiArchiveDescriptorFactory( persistenceBundle ) );
} }

View File

@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. * MA 02110-1301, USA.
*/ */
package org.hibernate.osgi.util; package org.hibernate.osgi;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -30,17 +30,29 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceReference;
/** /**
* Utilities for dealing with OSGi environments
*
* @author Brett Meyer * @author Brett Meyer
*/ */
public class OsgiServiceUtil { public class OsgiServiceUtil {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
OsgiServiceUtil.class.getName()
);
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, /**
OsgiServiceUtil.class.getName() ); * Locate all implementors of the given service contract in the given OSGi buindle context
*
* @param contract The service contract for which to locate implementors
* @param context The OSGi bundle context
* @param <T> The Java type of the service to locate
*
* @return All know implementors
*/
public static <T> List<T> getServiceImpls(Class<T> contract, BundleContext context) { public static <T> List<T> getServiceImpls(Class<T> contract, BundleContext context) {
List<T> serviceImpls = new ArrayList<T>(); final List<T> serviceImpls = new ArrayList<T>();
try { try {
Collection<ServiceReference<T>> serviceRefs = context.getServiceReferences( contract, null ); final Collection<ServiceReference<T>> serviceRefs = context.getServiceReferences( contract, null );
for ( ServiceReference<T> serviceRef : serviceRefs ) { for ( ServiceReference<T> serviceRef : serviceRefs ) {
serviceImpls.add( context.getService( serviceRef ) ); serviceImpls.add( context.getService( serviceRef ) );
} }
@ -50,4 +62,7 @@ public class OsgiServiceUtil {
} }
return serviceImpls; return serviceImpls;
} }
private OsgiServiceUtil() {
}
} }

View File

@ -22,6 +22,11 @@ package org.hibernate.osgi;
import java.util.List; import java.util.List;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -29,12 +34,7 @@ import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
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.osgi.util.OsgiServiceUtil;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
/** /**
* 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
@ -42,28 +42,34 @@ import org.osgi.framework.ServiceRegistration;
* unfortunately not available during Configuration. An OSGi * unfortunately not available during Configuration. An OSGi
* bundle manually creating a SessionFactory would require numerous ClassLoader * bundle manually creating a SessionFactory would require numerous ClassLoader
* tricks (or may be impossible altogether). * tricks (or may be impossible altogether).
* * <p/>
* In order to fully control the TCCL issues and shield users from the * In order to fully control the TCCL issues and shield users from the
* knowledge, we're requiring that bundles use this OSGi ServiceFactory. It * knowledge, we're requiring that bundles use this OSGi ServiceFactory. It
* configures and provides a SessionFactory as an OSGi service. * configures and provides a SessionFactory as an OSGi service.
* * <p/>
* Note that an OSGi ServiceFactory differs from a Service. The ServiceFactory * Note that an OSGi ServiceFactory differs from a Service. The ServiceFactory
* allows individual instances of Services to be created and provided to * allows individual instances of Services to be created and provided to
* multiple client Bundles. * multiple client Bundles.
* *
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiSessionFactoryService implements ServiceFactory { public class OsgiSessionFactoryService implements ServiceFactory {
private OsgiClassLoader osgiClassLoader; private OsgiClassLoader osgiClassLoader;
private OsgiJtaPlatform osgiJtaPlatform; private OsgiJtaPlatform osgiJtaPlatform;
private BundleContext context; private BundleContext context;
public OsgiSessionFactoryService( OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform, /**
BundleContext context ) { * Constructs a OsgiSessionFactoryService
*
* @param osgiClassLoader The OSGi-specific ClassLoader created in HibernateBundleActivator
* @param osgiJtaPlatform The OSGi-specific JtaPlatform created in HibernateBundleActivator
* @param context The OSGi context
*/
public OsgiSessionFactoryService(
OsgiClassLoader osgiClassLoader,
OsgiJtaPlatform osgiJtaPlatform,
BundleContext context) {
this.osgiClassLoader = osgiClassLoader; this.osgiClassLoader = osgiClassLoader;
this.osgiJtaPlatform = osgiJtaPlatform; this.osgiJtaPlatform = osgiJtaPlatform;
this.context = context; this.context = context;
@ -72,33 +78,33 @@ public class OsgiSessionFactoryService implements ServiceFactory {
@Override @Override
public Object getService(Bundle requestingBundle, ServiceRegistration registration) { public Object getService(Bundle requestingBundle, ServiceRegistration registration) {
osgiClassLoader.addBundle( requestingBundle ); osgiClassLoader.addBundle( requestingBundle );
Configuration configuration = new Configuration(); final Configuration configuration = new Configuration();
configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
configuration.configure(); configuration.configure();
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
builder.with( osgiClassLoader ); builder.with( osgiClassLoader );
List<Integrator> integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); final List<Integrator> integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context );
for (Integrator integrator : integrators) { for ( Integrator integrator : integrators ) {
builder.with( integrator ); builder.with( integrator );
} }
List<StrategyRegistrationProvider> strategyRegistrationProviders final List<StrategyRegistrationProvider> strategyRegistrationProviders
= OsgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class, context ); = OsgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class, context );
for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders) { for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders ) {
builder.withStrategySelectors( strategyRegistrationProvider ); builder.withStrategySelectors( strategyRegistrationProvider );
} }
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() ) final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() )
.applySettings(configuration.getProperties()).build(); .applySettings( configuration.getProperties() ).build();
return configuration.buildSessionFactory(serviceRegistry); return configuration.buildSessionFactory( serviceRegistry );
} }
@Override @Override
public void ungetService(Bundle requestingBundle, ServiceRegistration registration, Object service) { public void ungetService(Bundle requestingBundle, ServiceRegistration registration, Object service) {
( (SessionFactory) service).close(); ((SessionFactory) service).close();
} }
} }

View File

@ -0,0 +1,4 @@
/**
* Main OSGi support classes
*/
package org.hibernate.osgi;