org.hibernate.testing.boot.ExtraJavaServicesClassLoaderService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-testing-jakarta Show documentation
Show all versions of hibernate-testing-jakarta Show documentation
Support for testing Hibernate ORM Jakarta functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.testing.boot;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
/**
* @author Steve Ebersole
*/
public class ExtraJavaServicesClassLoaderService extends ClassLoaderServiceImpl {
private final List> extraJavaServices;
public ExtraJavaServicesClassLoaderService(List> extraJavaServices) {
this.extraJavaServices = extraJavaServices;
}
@Override
public Collection loadJavaServices(Class serviceContract) {
final Collection baseServices = super.loadJavaServices( serviceContract );
final List services = new ArrayList<>( baseServices );
applyExtraJavaServices( serviceContract, services );
return services;
}
private void applyExtraJavaServices(Class serviceContract, List services) {
extraJavaServices.forEach(
(javaServiceDescriptor) -> {
if ( serviceContract.isAssignableFrom( javaServiceDescriptor.role ) ) {
try {
final Object serviceInstance = javaServiceDescriptor.impl.getDeclaredConstructor().newInstance();
//noinspection unchecked
services.add( (S) serviceInstance );
}
catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException( "Unable to access constructor for specified 'extra' Java service : " + javaServiceDescriptor.impl.getName(), e );
}
catch (InstantiationException | InvocationTargetException e) {
throw new RuntimeException( "Unable to instantiate specified 'extra' Java service : " + javaServiceDescriptor.impl.getName(), e );
}
}
}
);
}
public static class JavaServiceDescriptor {
private final Class role;
private final Class extends ROLE> impl;
public JavaServiceDescriptor(Class role, Class extends ROLE> impl) {
this.role = role;
this.impl = impl;
}
public Class getRole() {
return role;
}
public Class extends ROLE> getImpl() {
return impl;
}
}
}