From 7fd71efd045ae7b3a4a6facdb8e6120e39ce21e8 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 24 Jun 2014 23:55:52 +0100 Subject: [PATCH] HHH-9258 OsgiSessionFactoryService does not handle ServiceContributors --- .../osgi/OSGiClassLoaderServiceImpl.java | 61 +++++++++++++++++++ .../osgi/OsgiSessionFactoryService.java | 5 +- .../org/hibernate/osgi/test/OsgiTestCase.java | 21 ++++++- .../osgi/test/client/SomeService.java | 31 ++++++++++ .../test/client/SomeServiceContributor.java | 38 ++++++++++++ .../osgi/test/client/TestService.java | 2 + .../osgi/test/client/TestServiceImpl.java | 8 +++ .../OSGI-INF/blueprint/blueprint.xml | 10 +++ 8 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 hibernate-osgi/src/main/java/org/hibernate/osgi/OSGiClassLoaderServiceImpl.java create mode 100644 hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeService.java create mode 100644 hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeServiceContributor.java create mode 100644 hibernate-osgi/src/testClientBundle/resources/OSGI-INF/blueprint/blueprint.xml diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OSGiClassLoaderServiceImpl.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OSGiClassLoaderServiceImpl.java new file mode 100644 index 0000000000..e7fe9eeb37 --- /dev/null +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OSGiClassLoaderServiceImpl.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2014 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; + +import java.util.Collections; +import java.util.LinkedHashSet; + +import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; + +/** + * The ClassLoaderService that should be used when running in OSGi; + * this helps for example to load the Services correctly. + * + * @author Sanne Grinovero + */ +public class OSGiClassLoaderServiceImpl extends ClassLoaderServiceImpl implements ClassLoaderService { + + private final OsgiServiceUtil osgiServiceUtil; + private final OsgiClassLoader osgiClassLoader; + + public OSGiClassLoaderServiceImpl(OsgiClassLoader osgiClassLoader, OsgiServiceUtil osgiServiceUtil) { + super( osgiClassLoader ); + this.osgiClassLoader = osgiClassLoader; + this.osgiServiceUtil = osgiServiceUtil; + } + + @Override + public LinkedHashSet loadJavaServices(Class serviceContract) { + LinkedHashSet parentDiscoveredServices = super.loadJavaServices( serviceContract ); + S[] serviceImpls = osgiServiceUtil.getServiceImpls(serviceContract); + Collections.addAll( parentDiscoveredServices, serviceImpls ); + return parentDiscoveredServices; + } + + @Override + public void stop() { + super.stop(); + osgiClassLoader.stop(); + osgiServiceUtil.stop(); + } + +} 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 958997c23d..1268c799e1 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java @@ -103,7 +103,8 @@ public class OsgiSessionFactoryService implements ServiceFactory { } final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); - builder.with( osgiClassLoader ); + final OSGiClassLoaderServiceImpl classLoaderService = new OSGiClassLoaderServiceImpl( osgiClassLoader, osgiServiceUtil ); + builder.with( classLoaderService ); final Integrator[] integrators = osgiServiceUtil.getServiceImpls( Integrator.class ); for ( Integrator integrator : integrators ) { @@ -115,7 +116,7 @@ public class OsgiSessionFactoryService implements ServiceFactory { for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders ) { builder.withStrategySelectors( strategyRegistrationProvider ); } - + final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls( TypeContributor.class ); for ( TypeContributor typeContributor : typeContributors ) { configuration.registerTypeContributor( typeContributor ); diff --git a/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiTestCase.java b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiTestCase.java index c97127c51d..cf32c4fc60 100644 --- a/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiTestCase.java +++ b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiTestCase.java @@ -216,7 +216,26 @@ public class OsgiTestCase { assertNotNull( testService.getTestTypeContributor() ); assertTrue( testService.getTestTypeContributor().passed() ); } - + + /* + * FIXME: Arquillian testing infrastructure is not ready to + * handle the blueprints configuration yet. + * + * This implies the following test might be wrong as it never worked; + * we know the feature works because of a different test in the + * Hibernate Search project which is using PAX rather than Arquillian. + * + @Test + @InSequence(2) + public void testServiceContributorDiscovery() throws Exception { + commonTests(); + + final TestService testService = getTestService(); + + assertTrue( testService.isCustomServiceContributorRegistered() ); + } + */ + /** * Test that stopping the hibernate-osgi bundle happens cleanly. * diff --git a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeService.java b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeService.java new file mode 100644 index 0000000000..fbe4ad0f73 --- /dev/null +++ b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeService.java @@ -0,0 +1,31 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2014 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.test.client; + +import org.hibernate.service.Service; + +/** + * A no-op {@link Service} implementation to verify registration of discovered + * extension points. + */ +public class SomeService implements Service { + +} diff --git a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeServiceContributor.java b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeServiceContributor.java new file mode 100644 index 0000000000..9c8c1cf588 --- /dev/null +++ b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/SomeServiceContributor.java @@ -0,0 +1,38 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2014 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.test.client; + +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.spi.ServiceContributor; + +/** + * A ServiceContributor to register service {@link SomeService}, to test service contributor + * discovery. + */ +public class SomeServiceContributor implements ServiceContributor { + + @Override + public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) { + serviceRegistryBuilder.addService( SomeService.class, new SomeService() ); + } + +} diff --git a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestService.java b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestService.java index 3d430ca7c8..b5996c61ca 100644 --- a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestService.java +++ b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestService.java @@ -53,4 +53,6 @@ public interface TestService { public TestStrategyRegistrationProvider getTestStrategyRegistrationProvider(); public TestTypeContributor getTestTypeContributor(); + + public boolean isCustomServiceContributorRegistered(); } diff --git a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestServiceImpl.java b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestServiceImpl.java index d10c29e003..165ad88d06 100644 --- a/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestServiceImpl.java +++ b/hibernate-osgi/src/testClientBundle/java/org/hibernate/osgi/test/client/TestServiceImpl.java @@ -27,6 +27,7 @@ import javax.persistence.spi.PersistenceProvider; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -147,4 +148,11 @@ public class TestServiceImpl implements TestService { public TestTypeContributor getTestTypeContributor() { return testTypeContributor; } + + @Override + public boolean isCustomServiceContributorRegistered() { + SessionFactoryImplementor implementor = (SessionFactoryImplementor) sf; + SomeService service = implementor.getServiceRegistry().getService( SomeService.class ); + return service != null; + } } diff --git a/hibernate-osgi/src/testClientBundle/resources/OSGI-INF/blueprint/blueprint.xml b/hibernate-osgi/src/testClientBundle/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..9a8c3e3e27 --- /dev/null +++ b/hibernate-osgi/src/testClientBundle/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,10 @@ + + + + + + + +