HHH-9258 OsgiSessionFactoryService does not handle ServiceContributors

This commit is contained in:
Sanne Grinovero 2014-06-24 23:55:52 +01:00
parent f9a49efd17
commit de16eaf5a8
8 changed files with 173 additions and 3 deletions

View File

@ -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 <S> LinkedHashSet<S> loadJavaServices(Class<S> serviceContract) {
LinkedHashSet<S> 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();
}
}

View File

@ -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 );

View File

@ -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.
*

View File

@ -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 {
}

View File

@ -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() );
}
}

View File

@ -53,4 +53,6 @@ public interface TestService {
public TestStrategyRegistrationProvider getTestStrategyRegistrationProvider();
public TestTypeContributor getTestTypeContributor();
public boolean isCustomServiceContributorRegistered();
}

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="service-contributor" class="org.hibernate.osgi.test.client.SomeServiceContributor"/>
<service ref="service-contributor" interface="org.hibernate.service.spi.ServiceContributor" />
</blueprint>