From 20866585f35691fb98b69cff352a4a7ee3f6edcb Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 30 Apr 2013 14:40:44 -0500 Subject: [PATCH] HHH-8159 - Apply fixups indicated by analysis tools --- .../osgi/HibernateBundleActivator.java | 37 ++++----- .../hibernate/osgi/OsgiArchiveDescriptor.java | 26 ++++--- .../osgi/OsgiArchiveDescriptorFactory.java | 8 +- .../org/hibernate/osgi/OsgiClassLoader.java | 39 ++++++---- .../org/hibernate/osgi/OsgiJtaPlatform.java | 10 ++- .../osgi/OsgiPersistenceProvider.java | 60 +++++++++------ .../osgi/OsgiPersistenceProviderService.java | 20 +++-- .../java/org/hibernate/osgi/OsgiScanner.java | 6 +- .../osgi/{util => }/OsgiServiceUtil.java | 27 +++++-- .../osgi/OsgiSessionFactoryService.java | 76 ++++++++++--------- .../java/org/hibernate/osgi/package-info.java | 4 + 11 files changed, 195 insertions(+), 118 deletions(-) rename hibernate-osgi/src/main/java/org/hibernate/osgi/{util => }/OsgiServiceUtil.java (72%) create mode 100644 hibernate-osgi/src/main/java/org/hibernate/osgi/package-info.java 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 8bd6824e48..fdf0d1ea89 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java @@ -51,37 +51,40 @@ import org.osgi.framework.FrameworkUtil; * SessionFactory is registered as an OSGi ServiceFactory -- each requesting * bundle gets its own instance of a SessionFactory. The use of services, * 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. * * @author Brett Meyer * @author Tim Ward */ +@SuppressWarnings("UnusedDeclaration") public class HibernateBundleActivator implements BundleActivator { - - private OsgiClassLoader osgiClassLoader; - - private OsgiJtaPlatform osgiJtaPlatform; - @Override + @SuppressWarnings("unchecked") public void start(BundleContext context) throws Exception { - - osgiClassLoader = new OsgiClassLoader(); + // build a ClassLoader that uses all the necessary OSGi bundles, and place it into + // a well-known location so internals can access it + final OsgiClassLoader osgiClassLoader = new OsgiClassLoader(); osgiClassLoader.addBundle( FrameworkUtil.getBundle( Session.class ) ); osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernatePersistenceProvider.class ) ); 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(); - // In order to support existing persistence.xml files, register - // using the legacy provider name. + final Dictionary properties = new Hashtable(); + // In order to support existing persistence.xml files, register using the legacy provider name. properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() ); - context.registerService( PersistenceProvider.class.getName(), - new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), properties ); - - context.registerService( SessionFactory.class.getName(), - new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), new Hashtable()); + context.registerService( + PersistenceProvider.class.getName(), + new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), + properties + ); + context.registerService( + SessionFactory.class.getName(), + new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), + new Hashtable() + ); } @Override diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptor.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptor.java index 6a73acca0a..fc554e4216 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptor.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptor.java @@ -37,18 +37,26 @@ import org.osgi.framework.Bundle; import org.osgi.framework.wiring.BundleWiring; /** + * ArchiveDescriptor implementation for describing archives in the OSGi sense + * * @author Brett Meyer * @author Tim Ward */ 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, - OsgiArchiveDescriptor.class.getName() ); - - private BundleWiring bundleWiring; - - private Bundle persistenceBundle; + private final Bundle persistenceBundle; + private final BundleWiring bundleWiring; + /** + * Creates a OsgiArchiveDescriptor + * + * @param persistenceBundle The bundle being described as an archive + */ + @SuppressWarnings("RedundantCast") public OsgiArchiveDescriptor(Bundle persistenceBundle) { this.persistenceBundle = persistenceBundle; bundleWiring = (BundleWiring) persistenceBundle.adapt( BundleWiring.class ); @@ -56,14 +64,12 @@ public class OsgiArchiveDescriptor implements ArchiveDescriptor { @Override public void visitArchive(ArchiveContext context) { - Collection resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE ); + final Collection resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE ); for ( final String resource : resources ) { - // TODO: Is there a better way to check this? Karaf is including - // directories. + // TODO: Is there a better way to check this? Karaf is including directories. if ( !resource.endsWith( "/" ) ) { try { // TODO: Is using resource as the names correct? - final InputStreamAccess inputStreamAccess = new InputStreamAccess() { @Override public String getStreamName() { diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptorFactory.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptorFactory.java index 97264cf5b4..81489715a7 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptorFactory.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiArchiveDescriptorFactory.java @@ -27,13 +27,19 @@ import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptorFactory; import org.osgi.framework.Bundle; /** + * ArchiveDescriptorFactory implementation for OSGi environments + * * @author Brett Meyer * @author Tim Ward */ public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory { - private Bundle persistenceBundle; + /** + * Creates a OsgiArchiveDescriptorFactory + * + * @param persistenceBundle The OSGi bundle being scanned + */ public OsgiArchiveDescriptorFactory(Bundle persistenceBundle) { this.persistenceBundle = persistenceBundle; } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java index 2088742d53..d8babd732c 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java @@ -41,15 +41,11 @@ import org.osgi.framework.Bundle; * @author Tim Ward */ public class OsgiClassLoader extends ClassLoader { - private List classLoaders = new ArrayList(); - private List bundles = new ArrayList(); - + private Map> classCache = new HashMap>(); - private Map resourceCache = new HashMap(); - private Map> resourceListCache = new HashMap>(); /** @@ -57,8 +53,8 @@ public class OsgiClassLoader extends ClassLoader { * TODO: Should this throw a different exception or warn if multiple * classes were found? Naming collisions can and do happen in OSGi... */ - @SuppressWarnings("rawtypes") @Override + @SuppressWarnings("rawtypes") protected Class findClass(String name) throws ClassNotFoundException { if ( classCache.containsKey( name ) ) { return classCache.get( name ); @@ -66,7 +62,7 @@ public class OsgiClassLoader extends ClassLoader { for ( Bundle bundle : bundles ) { try { - Class clazz = bundle.loadClass( name ); + final Class clazz = bundle.loadClass( name ); if ( clazz != null ) { classCache.put( name, clazz ); return clazz; @@ -78,7 +74,7 @@ public class OsgiClassLoader extends ClassLoader { for ( ClassLoader classLoader : classLoaders ) { try { - Class clazz = classLoader.loadClass( name ); + final Class clazz = classLoader.loadClass( name ); if ( clazz != null ) { classCache.put( name, clazz ); return clazz; @@ -104,7 +100,7 @@ public class OsgiClassLoader extends ClassLoader { for ( Bundle bundle : bundles ) { try { - URL resource = bundle.getResource( name ); + final URL resource = bundle.getResource( name ); if ( resource != null ) { resourceCache.put( name, resource ); return resource; @@ -116,7 +112,7 @@ public class OsgiClassLoader extends ClassLoader { for ( ClassLoader classLoader : classLoaders ) { try { - URL resource = classLoader.getResource( name ); + final URL resource = classLoader.getResource( name ); if ( resource != null ) { resourceCache.put( name, resource ); return resource; @@ -135,8 +131,8 @@ public class OsgiClassLoader extends ClassLoader { * TODO: Should this throw a different exception or warn if multiple * classes were found? Naming collisions can and do happen in OSGi... */ - @SuppressWarnings("unchecked") @Override + @SuppressWarnings("unchecked") protected Enumeration findResources(String name) { if ( resourceListCache.containsKey( name ) ) { return resourceListCache.get( name ); @@ -146,7 +142,7 @@ public class OsgiClassLoader extends ClassLoader { for ( Bundle bundle : bundles ) { try { - Enumeration resources = bundle.getResources( name ); + final Enumeration resources = bundle.getResources( name ); if ( resources != null ) { enumerations.add( resources ); } @@ -157,7 +153,7 @@ public class OsgiClassLoader extends ClassLoader { for ( ClassLoader classLoader : classLoaders ) { try { - Enumeration resources = classLoader.getResources( name ); + final Enumeration resources = classLoader.getResources( name ); if ( resources != null ) { enumerations.add( resources ); } @@ -166,7 +162,7 @@ public class OsgiClassLoader extends ClassLoader { } } - Enumeration aggEnumeration = new Enumeration() { + final Enumeration aggEnumeration = new Enumeration() { @Override public boolean hasMoreElements() { for ( Enumeration enumeration : enumerations ) { @@ -193,14 +189,27 @@ public class OsgiClassLoader extends ClassLoader { return aggEnumeration; } + /** + * Adds a ClassLoader to the wrapped set of ClassLoaders + * + * @param classLoader The ClassLoader to add + */ public void addClassLoader( ClassLoader classLoader ) { classLoaders.add( classLoader ); } + /** + * Adds a Bundle to the wrapped set of Bundles + * + * @param bundle The Bundle to add + */ public void addBundle( Bundle bundle ) { bundles.add( bundle ); } - + + /** + * Clear all resources. + */ public void clear() { classCache.clear(); resourceCache.clear(); diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiJtaPlatform.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiJtaPlatform.java index 29ae4707d4..5dd4888037 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiJtaPlatform.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiJtaPlatform.java @@ -38,24 +38,28 @@ import org.osgi.framework.ServiceReference; * @author Brett Meyer */ public class OsgiJtaPlatform implements JtaPlatform { - private static final long serialVersionUID = 1L; private BundleContext bundleContext; + /** + * Constructs a OsgiJtaPlatform + * + * @param bundleContext The OSGi bundle context + */ public OsgiJtaPlatform(BundleContext bundleContext) { this.bundleContext = bundleContext; } @Override public TransactionManager retrieveTransactionManager() { - ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() ); + final ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() ); return (TransactionManager) bundleContext.getService( sr ); } @Override public UserTransaction retrieveUserTransaction() { - ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() ); + final ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() ); return (UserTransaction) bundleContext.getService( sr ); } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java index 01ccff48ee..23cc77d8b7 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java @@ -37,27 +37,36 @@ import org.hibernate.jpa.HibernatePersistenceProvider; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; import org.hibernate.jpa.boot.spi.IntegratorProvider; import org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList; -import org.hibernate.osgi.util.OsgiServiceUtil; + import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleReference; /** + * Acts as the PersistenceProvider service in OSGi environments + * * @author Brett Meyer * @author Tim Ward */ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { - private OsgiClassLoader osgiClassLoader; - private OsgiJtaPlatform osgiJtaPlatform; - private Bundle requestingBundle; - 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.osgiJtaPlatform = osgiJtaPlatform; this.requestingBundle = requestingBundle; @@ -68,14 +77,15 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { // EMFBuilder somehow? @Override + @SuppressWarnings("unchecked") public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) { - properties = generateProperties( properties ); + final Map settings = generateSettings( properties ); // 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 // ClassLoaderServiceImpl#fromConfigSettings - properties.put( AvailableSettings.ENVIRONMENT_CLASSLOADER, osgiClassLoader ); + settings.put( AvailableSettings.ENVIRONMENT_CLASSLOADER, osgiClassLoader ); osgiClassLoader.addBundle( requestingBundle ); @@ -83,45 +93,49 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { } @Override + @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { - properties = generateProperties( properties ); + final Map settings = generateSettings( properties ); // OSGi ClassLoaders must implement BundleReference - properties.put( org.hibernate.jpa.AvailableSettings.SCANNER, - new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) ); + settings.put( + org.hibernate.jpa.AvailableSettings.SCANNER, + new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) + ); osgiClassLoader.addClassLoader( info.getClassLoader() ); return super.createContainerEntityManagerFactory( info, properties ); } - private Map generateProperties(Map properties) { - if ( properties == null ) { - properties = new HashMap(); + @SuppressWarnings("unchecked") + private Map generateSettings(Map properties) { + 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 integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); - IntegratorProvider integratorProvider = new IntegratorProvider() { + final IntegratorProvider integratorProvider = new IntegratorProvider() { @Override public List getIntegrators() { return integrators; } }; - properties.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider ); + settings.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider ); final List strategyRegistrationProviders = OsgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class, context ); - StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() { + final StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() { @Override public List getStrategyRegistrationProviders() { return strategyRegistrationProviders; } }; - properties.put( EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, - strategyRegistrationProviderList ); + settings.put( EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, strategyRegistrationProviderList ); - return properties; + return settings; } } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java index 5ab228568f..f3bc51c9a5 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java @@ -26,22 +26,28 @@ import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceRegistration; /** - * See the description on {@link #OsgiSessionFactoryService}. This class - * is similar, providing an PersistenceProvider as an OSGi Service. + * See the description on {@link OsgiSessionFactoryService}. This class is similar, providing an + * PersistenceProvider as an OSGi Service. * * @author Brett Meyer * @author Tim Ward */ public class OsgiPersistenceProviderService implements ServiceFactory { - private OsgiClassLoader osgiClassLoader; - private OsgiJtaPlatform osgiJtaPlatform; - 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.osgiJtaPlatform = osgiJtaPlatform; this.context = context; diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiScanner.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiScanner.java index ac753466ae..aa570348b8 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiScanner.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiScanner.java @@ -31,7 +31,11 @@ import org.osgi.framework.Bundle; * @author Tim Ward */ 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) { super( new OsgiArchiveDescriptorFactory( persistenceBundle ) ); } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiServiceUtil.java similarity index 72% rename from hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java rename to hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiServiceUtil.java index 0b9ddc0ed4..05e4603d92 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiServiceUtil.java @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ -package org.hibernate.osgi.util; +package org.hibernate.osgi; import java.util.ArrayList; import java.util.Collection; @@ -30,17 +30,29 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; /** + * Utilities for dealing with OSGi environments + * * @author Brett Meyer */ 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 The Java type of the service to locate + * + * @return All know implementors + */ public static List getServiceImpls(Class contract, BundleContext context) { - List serviceImpls = new ArrayList(); + final List serviceImpls = new ArrayList(); try { - Collection> serviceRefs = context.getServiceReferences( contract, null ); + final Collection> serviceRefs = context.getServiceReferences( contract, null ); for ( ServiceReference serviceRef : serviceRefs ) { serviceImpls.add( context.getService( serviceRef ) ); } @@ -50,4 +62,7 @@ public class OsgiServiceUtil { } return serviceImpls; } + + private OsgiServiceUtil() { + } } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java index c878136711..af6c57be4d 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java @@ -22,6 +22,11 @@ package org.hibernate.osgi; 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.boot.registry.BootstrapServiceRegistryBuilder; 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.Configuration; import org.hibernate.integrator.spi.Integrator; -import org.hibernate.osgi.util.OsgiServiceUtil; 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 @@ -42,28 +42,34 @@ import org.osgi.framework.ServiceRegistration; * unfortunately not available during Configuration. An OSGi * bundle manually creating a SessionFactory would require numerous ClassLoader * tricks (or may be impossible altogether). - * + *

* In order to fully control the TCCL issues and shield users from the * knowledge, we're requiring that bundles use this OSGi ServiceFactory. It * configures and provides a SessionFactory as an OSGi service. - * + *

* Note that an OSGi ServiceFactory differs from a Service. The ServiceFactory * allows individual instances of Services to be created and provided to * multiple client Bundles. - * + * * @author Brett Meyer * @author Tim Ward */ public class OsgiSessionFactoryService implements ServiceFactory { - private OsgiClassLoader osgiClassLoader; - private OsgiJtaPlatform osgiJtaPlatform; - 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.osgiJtaPlatform = osgiJtaPlatform; this.context = context; @@ -72,33 +78,33 @@ public class OsgiSessionFactoryService implements ServiceFactory { @Override public Object getService(Bundle requestingBundle, ServiceRegistration registration) { osgiClassLoader.addBundle( requestingBundle ); - - Configuration configuration = new Configuration(); + + final Configuration configuration = new Configuration(); configuration.getProperties().put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); - configuration.configure(); - - BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); - builder.with( osgiClassLoader ); - - List integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); - for (Integrator integrator : integrators) { - builder.with( integrator ); - } - - List strategyRegistrationProviders - = OsgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class, context ); - for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders) { - builder.withStrategySelectors( strategyRegistrationProvider ); - } - - ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() ) - .applySettings(configuration.getProperties()).build(); - return configuration.buildSessionFactory(serviceRegistry); + configuration.configure(); + + final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); + builder.with( osgiClassLoader ); + + final List integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); + for ( Integrator integrator : integrators ) { + builder.with( integrator ); + } + + final List strategyRegistrationProviders + = OsgiServiceUtil.getServiceImpls( StrategyRegistrationProvider.class, context ); + for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders ) { + builder.withStrategySelectors( strategyRegistrationProvider ); + } + + final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() ) + .applySettings( configuration.getProperties() ).build(); + return configuration.buildSessionFactory( serviceRegistry ); } @Override public void ungetService(Bundle requestingBundle, ServiceRegistration registration, Object service) { - ( (SessionFactory) service).close(); + ((SessionFactory) service).close(); } } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/package-info.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/package-info.java new file mode 100644 index 0000000000..bd787f7262 --- /dev/null +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/package-info.java @@ -0,0 +1,4 @@ +/** + * Main OSGi support classes + */ +package org.hibernate.osgi;