From 30d63bf58fe9b15208340b860787d985f032d11a Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Mon, 22 Apr 2013 13:54:40 -0400 Subject: [PATCH] HHH-7995 Auto-discovery of Hibernate extension points in OSGi --- .../hibernate/internal/CoreMessageLogger.java | 4 ++ .../OSGI-INF/blueprint/blueprint.xml | 10 ++++ hibernate-osgi/hibernate-osgi.gradle | 4 +- .../osgi/HibernateBundleActivator.java | 4 +- .../osgi/OsgiPersistenceProvider.java | 60 +++++++++++++------ .../osgi/OsgiPersistenceProviderService.java | 9 ++- .../osgi/OsgiSessionFactoryService.java | 16 ++++- .../hibernate/osgi/util/OsgiServiceUtil.java | 53 ++++++++++++++++ 8 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 hibernate-envers/src/main/resources/OSGI-INF/blueprint/blueprint.xml create mode 100644 hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 955c5c0b04..20f6388fb2 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -1623,4 +1623,8 @@ public interface CoreMessageLogger extends BasicLogger { @LogMessage(level = WARN) @Message(value = "Exception while loading a class or resource found during scanning", id = 449) void unableToLoadScannedClassOrResource(@Cause Exception e); + + @LogMessage(level = WARN) + @Message(value = "Exception while discovering OSGi service implementations : %s", id = 450) + void unableToDiscoverOsgiService(String service, @Cause Exception e); } diff --git a/hibernate-envers/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/hibernate-envers/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..07c01bfc9f --- /dev/null +++ b/hibernate-envers/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/hibernate-osgi/hibernate-osgi.gradle b/hibernate-osgi/hibernate-osgi.gradle index 8a01360d3c..cefe0b6449 100644 --- a/hibernate-osgi/hibernate-osgi.gradle +++ b/hibernate-osgi/hibernate-osgi.gradle @@ -1,7 +1,9 @@ dependencies { compile( project( ':hibernate-core' ) ) compile( project( ':hibernate-entitymanager' ) ) - compile( "org.osgi:org.osgi.core:4.3.0" ) + // MUST use 4.3.1! 4.3.0 was compiled with "-target jsr14". + // http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html + compile( "org.osgi:org.osgi.core:4.3.1" ) } def pomName() { 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 c1f862fb98..8bd6824e48 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java @@ -78,10 +78,10 @@ public class HibernateBundleActivator implements BundleActivator { // using the legacy provider name. properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() ); context.registerService( PersistenceProvider.class.getName(), - new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform ), properties ); + new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), properties ); context.registerService( SessionFactory.class.getName(), - new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform ), new Hashtable()); + new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), new Hashtable()); } @Override 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 e90c2cbef4..885533f745 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java @@ -24,14 +24,20 @@ package org.hibernate.osgi; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.persistence.EntityManagerFactory; import javax.persistence.spi.PersistenceUnitInfo; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.integrator.spi.Integrator; import org.hibernate.jpa.HibernatePersistenceProvider; +import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; +import org.hibernate.jpa.boot.spi.IntegratorProvider; +import org.hibernate.osgi.util.OsgiServiceUtil; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.BundleReference; /** @@ -43,45 +49,42 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { private OsgiClassLoader osgiClassLoader; private OsgiJtaPlatform osgiJtaPlatform; - + private Bundle requestingBundle; - - public OsgiPersistenceProvider (OsgiClassLoader osgiClassLoader, - OsgiJtaPlatform osgiJtaPlatform, - Bundle requestingBundle ) { + + private BundleContext context; + + public OsgiPersistenceProvider(OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform, + Bundle requestingBundle, BundleContext context) { this.osgiClassLoader = osgiClassLoader; this.osgiJtaPlatform = osgiJtaPlatform; this.requestingBundle = requestingBundle; + this.context = context; } - + // TODO: Does "hibernate.classloaders" and osgiClassLoader need added to the // EMFBuilder somehow? @Override public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) { - if ( properties == null ) { - properties = new HashMap(); - } - properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); + generateProperties( properties ); + // TODO: This needs tested. - properties.put( org.hibernate.jpa.AvailableSettings.SCANNER, - new OsgiScanner( requestingBundle ) ); + properties.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 ); - + osgiClassLoader.addBundle( requestingBundle ); return super.createEntityManagerFactory( persistenceUnitName, properties ); } - + @Override public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { - if ( properties == null ) { - properties = new HashMap(); - } - properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); - // OSGi ClassLoaders must implement BundleReference + generateProperties( properties ); + + // OSGi ClassLoaders must implement BundleReference properties.put( org.hibernate.jpa.AvailableSettings.SCANNER, new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) ); @@ -89,4 +92,23 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { return super.createContainerEntityManagerFactory( info, properties ); } + + private void generateProperties(Map properties) { + if ( properties == null ) { + properties = new HashMap(); + } + + properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); + + final List integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); + IntegratorProvider integratorProvider = new IntegratorProvider() { + @Override + public List getIntegrators() { + return integrators; + } + }; + properties.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider ); + + // TODO: other types of services? + } } 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 bb9008c33b..5ab228568f 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProviderService.java @@ -21,6 +21,7 @@ package org.hibernate.osgi; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceRegistration; @@ -37,14 +38,18 @@ public class OsgiPersistenceProviderService implements ServiceFactory { private OsgiJtaPlatform osgiJtaPlatform; - public OsgiPersistenceProviderService( OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform ) { + private BundleContext context; + + public OsgiPersistenceProviderService( OsgiClassLoader osgiClassLoader, + OsgiJtaPlatform osgiJtaPlatform, BundleContext context ) { this.osgiClassLoader = osgiClassLoader; this.osgiJtaPlatform = osgiJtaPlatform; + this.context = context; } @Override public Object getService(Bundle requestingBundle, ServiceRegistration registration) { - return new OsgiPersistenceProvider(osgiClassLoader, osgiJtaPlatform, requestingBundle); + return new OsgiPersistenceProvider(osgiClassLoader, osgiJtaPlatform, requestingBundle, context); } @Override 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 db5b75b4cf..8770e55231 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java @@ -20,13 +20,18 @@ */ package org.hibernate.osgi; +import java.util.List; + import org.hibernate.SessionFactory; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 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; @@ -54,9 +59,13 @@ public class OsgiSessionFactoryService implements ServiceFactory { private OsgiJtaPlatform osgiJtaPlatform; - public OsgiSessionFactoryService( OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform ) { + private BundleContext context; + + public OsgiSessionFactoryService( OsgiClassLoader osgiClassLoader, OsgiJtaPlatform osgiJtaPlatform, + BundleContext context ) { this.osgiClassLoader = osgiClassLoader; this.osgiJtaPlatform = osgiJtaPlatform; + this.context = context; } @Override @@ -70,6 +79,11 @@ public class OsgiSessionFactoryService implements ServiceFactory { BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); builder.with( osgiClassLoader ); + List integrators = OsgiServiceUtil.getServiceImpls( Integrator.class, context ); + for (Integrator integrator : integrators) { + builder.with( integrator ); + } + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( builder.build() ) .applySettings(configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java new file mode 100644 index 0000000000..0b9ddc0ed4 --- /dev/null +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/util/OsgiServiceUtil.java @@ -0,0 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.osgi.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.hibernate.internal.CoreMessageLogger; +import org.jboss.logging.Logger; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +/** + * @author Brett Meyer + */ +public class OsgiServiceUtil { + + private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, + OsgiServiceUtil.class.getName() ); + + public static List getServiceImpls(Class contract, BundleContext context) { + List serviceImpls = new ArrayList(); + try { + Collection> serviceRefs = context.getServiceReferences( contract, null ); + for ( ServiceReference serviceRef : serviceRefs ) { + serviceImpls.add( context.getService( serviceRef ) ); + } + } + catch ( Exception e ) { + LOG.unableToDiscoverOsgiService( contract.getName(), e ); + } + return serviceImpls; + } +}