org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
JPMS Module-Info's for a few of the Jakarta Libraries just until they add them in themselves
/*
* 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 .
*/
package org.hibernate.boot.registry.internal;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.selector.internal.StrategySelectorImpl;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.integrator.internal.IntegratorServiceImpl;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceBinding;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceInitiator;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable;
/**
* {@link ServiceRegistry} implementation containing specialized "bootstrap" services, specifically:
* - {@link ClassLoaderService}
* - {@link IntegratorService}
* - {@link StrategySelector}
*
*
* @author Steve Ebersole
*/
public class BootstrapServiceRegistryImpl
implements ServiceRegistryImplementor, BootstrapServiceRegistry, ServiceBinding.ServiceLifecycleOwner {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( BootstrapServiceRegistryImpl.class );
private final boolean autoCloseRegistry;
private boolean active = true;
private static final LinkedHashSet NO_INTEGRATORS = new LinkedHashSet();
private final ServiceBinding classLoaderServiceBinding;
private final ServiceBinding strategySelectorBinding;
private final ServiceBinding integratorServiceBinding;
private Set childRegistries;
/**
* Constructs a BootstrapServiceRegistryImpl.
*
* Do not use directly generally speaking. Use {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder}
* instead.
*
* @see org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
*/
public BootstrapServiceRegistryImpl() {
this( new ClassLoaderServiceImpl(), NO_INTEGRATORS );
}
/**
* Constructs a BootstrapServiceRegistryImpl.
*
* Do not use directly generally speaking. Use {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder}
* instead.
*
* @param classLoaderService The ClassLoaderService to use
* @param providedIntegrators The group of explicitly provided integrators
*
* @see org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
*/
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
LinkedHashSet providedIntegrators) {
this( true, classLoaderService, providedIntegrators );
}
/**
* Constructs a BootstrapServiceRegistryImpl.
*
* Do not use directly generally speaking. Use {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder}
* instead.
*
* @param autoCloseRegistry See discussion on
* {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder#disableAutoClose}
* @param classLoaderService The ClassLoaderService to use
* @param providedIntegrators The group of explicitly provided integrators
*
* @see org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
*/
public BootstrapServiceRegistryImpl(
boolean autoCloseRegistry,
ClassLoaderService classLoaderService,
LinkedHashSet providedIntegrators) {
this.autoCloseRegistry = autoCloseRegistry;
this.classLoaderServiceBinding = new ServiceBinding(
this,
ClassLoaderService.class,
classLoaderService
);
final StrategySelectorImpl strategySelector = new StrategySelectorImpl( classLoaderService );
this.strategySelectorBinding = new ServiceBinding(
this,
StrategySelector.class,
strategySelector
);
this.integratorServiceBinding = new ServiceBinding(
this,
IntegratorService.class,
new IntegratorServiceImpl( providedIntegrators, classLoaderService )
);
}
/**
* Constructs a BootstrapServiceRegistryImpl.
*
* Do not use directly generally speaking. Use {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder}
* instead.
*
* @param classLoaderService The ClassLoaderService to use
* @param strategySelector The StrategySelector to use
* @param integratorService The IntegratorService to use
*
* @see org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
*/
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
StrategySelector strategySelector,
IntegratorService integratorService) {
this( true, classLoaderService, strategySelector, integratorService );
}
/**
* Constructs a BootstrapServiceRegistryImpl.
*
* Do not use directly generally speaking. Use {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder}
* instead.
*
* @param autoCloseRegistry See discussion on
* {@link org.hibernate.boot.registry.BootstrapServiceRegistryBuilder#disableAutoClose}
* @param classLoaderService The ClassLoaderService to use
* @param strategySelector The StrategySelector to use
* @param integratorService The IntegratorService to use
*
* @see org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
*/
public BootstrapServiceRegistryImpl(
boolean autoCloseRegistry,
ClassLoaderService classLoaderService,
StrategySelector strategySelector,
IntegratorService integratorService) {
this.autoCloseRegistry = autoCloseRegistry;
this.classLoaderServiceBinding = new ServiceBinding(
this,
ClassLoaderService.class,
classLoaderService
);
this.strategySelectorBinding = new ServiceBinding(
this,
StrategySelector.class,
strategySelector
);
this.integratorServiceBinding = new ServiceBinding(
this,
IntegratorService.class,
integratorService
);
}
@Override
public R getService(Class serviceRole) {
final ServiceBinding binding = locateServiceBinding( serviceRole );
return binding == null ? null : binding.getService();
}
@Override
@SuppressWarnings( {"unchecked"})
public ServiceBinding locateServiceBinding(Class serviceRole) {
if ( ClassLoaderService.class.equals( serviceRole ) ) {
return (ServiceBinding) classLoaderServiceBinding;
}
else if ( StrategySelector.class.equals( serviceRole) ) {
return (ServiceBinding) strategySelectorBinding;
}
else if ( IntegratorService.class.equals( serviceRole ) ) {
return (ServiceBinding) integratorServiceBinding;
}
return null;
}
@Override
public synchronized void destroy() {
if ( !active ) {
return;
}
active = false;
destroy( classLoaderServiceBinding );
destroy( strategySelectorBinding );
destroy( integratorServiceBinding );
if ( childRegistries != null ) {
for(ServiceRegistry serviceRegistry : childRegistries) {
if(serviceRegistry instanceof ServiceRegistryImplementor) {
ServiceRegistryImplementor serviceRegistryImplementor = (ServiceRegistryImplementor) serviceRegistry;
serviceRegistryImplementor.destroy();
}
}
}
}
private synchronized void destroy(ServiceBinding serviceBinding) {
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
}
public boolean isActive() {
return active;
}
@Override
public ServiceRegistry getParentServiceRegistry() {
return null;
}
@Override
public R initiateService(ServiceInitiator serviceInitiator) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void configureService(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void injectDependencies(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void startService(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public synchronized void stopService(ServiceBinding binding) {
final Service service = binding.getService();
if ( Stoppable.class.isInstance( service ) ) {
try {
( (Stoppable) service ).stop();
}
catch ( Exception e ) {
LOG.unableToStopService( service.getClass(), e );
}
}
}
@Override
public synchronized void registerChild(ServiceRegistryImplementor child) {
if ( childRegistries == null ) {
childRegistries = new HashSet();
}
if ( !childRegistries.add( child ) ) {
LOG.warnf(
"Child ServiceRegistry [%s] was already registered; this will end badly later...",
child
);
}
}
@Override
public synchronized void deRegisterChild(ServiceRegistryImplementor child) {
if ( childRegistries == null ) {
throw new IllegalStateException( "No child ServiceRegistry registrations found" );
}
childRegistries.remove( child );
if ( childRegistries.isEmpty() ) {
if ( autoCloseRegistry ) {
LOG.debug(
"Implicitly destroying Boot-strap registry on de-registration " +
"of all child ServiceRegistries"
);
destroy();
}
else {
LOG.debug(
"Skipping implicitly destroying Boot-strap registry on de-registration " +
"of all child ServiceRegistries"
);
}
}
}
}