minor cleanups to ServiceRegistryImpls

This commit is contained in:
Gavin King 2024-11-30 21:32:48 +01:00
parent 985740ecb9
commit dbbe61c5a0
2 changed files with 35 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.Internal;
import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
@ -77,14 +78,16 @@ public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl imp
List<ProvidedService<?>> providedServices, List<ProvidedService<?>> providedServices,
Map<String,Object> configurationValues) { Map<String,Object> configurationValues) {
StandardServiceRegistryImpl instance = new StandardServiceRegistryImpl( autoCloseRegistry, bootstrapServiceRegistry, configurationValues ); final StandardServiceRegistryImpl instance =
new StandardServiceRegistryImpl( autoCloseRegistry, bootstrapServiceRegistry, configurationValues );
instance.initialize(); instance.initialize();
instance.applyServiceRegistrations( serviceInitiators, providedServices ); instance.applyServiceRegistrations( serviceInitiators, providedServices );
return instance; return instance;
} }
protected void applyServiceRegistrations(List<StandardServiceInitiator<?>> serviceInitiators, List<ProvidedService<?>> providedServices) { protected void applyServiceRegistrations(
List<StandardServiceInitiator<?>> serviceInitiators, List<ProvidedService<?>> providedServices) {
try { try {
// process initiators // process initiators
for ( ServiceInitiator<?> initiator : serviceInitiators ) { for ( ServiceInitiator<?> initiator : serviceInitiators ) {
@ -105,10 +108,12 @@ public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl imp
} }
/** /**
* Not intended for general use. We need the ability to stop and "reactivate" a registry to allow * Not intended for general use. We need the ability to stop and "reactivate" a registry
* experimentation with technologies such as GraalVM, Quarkus and Cri-O. * to allow experimentation with technologies such as GraalVM, Quarkus and Cri-O.
*/ */
public synchronized void resetAndReactivate(BootstrapServiceRegistry bootstrapServiceRegistry, @Internal
public synchronized void resetAndReactivate(
BootstrapServiceRegistry bootstrapServiceRegistry,
List<StandardServiceInitiator<?>> serviceInitiators, List<StandardServiceInitiator<?>> serviceInitiators,
List<ProvidedService<?>> providedServices, List<ProvidedService<?>> providedServices,
Map<?, ?> configurationValues) { Map<?, ?> configurationValues) {
@ -130,8 +135,8 @@ public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl imp
@Override @Override
public synchronized <R extends Service> void configureService(ServiceBinding<R> serviceBinding) { public synchronized <R extends Service> void configureService(ServiceBinding<R> serviceBinding) {
if ( serviceBinding.getService() instanceof Configurable ) { if ( serviceBinding.getService() instanceof Configurable configurable ) {
( (Configurable) serviceBinding.getService() ).configure( configurationValues ); configurable.configure( configurationValues );
} }
} }

View File

@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.Internal;
import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
@ -281,22 +282,20 @@ public abstract class AbstractServiceRegistryImpl
applyInjections( service ); applyInjections( service );
if ( service instanceof ServiceRegistryAwareService ) { if ( service instanceof ServiceRegistryAwareService serviceRegistryAwareService ) {
( (ServiceRegistryAwareService) service ).injectServices( this ); serviceRegistryAwareService.injectServices( this );
} }
} }
private <R extends Service> void applyInjections(R service) { private <R extends Service> void applyInjections(R service) {
try { try {
for ( Method method : service.getClass().getMethods() ) { for ( Method method : service.getClass().getMethods() ) {
InjectService injectService = method.getAnnotation( InjectService.class ); final InjectService injectService = method.getAnnotation( InjectService.class );
if ( injectService == null ) { if ( injectService != null ) {
continue;
}
processInjection( service, method, injectService ); processInjection( service, method, injectService );
} }
} }
}
catch (NullPointerException e) { catch (NullPointerException e) {
log.error( "NPE injecting service deps : " + service.getClass().getName() ); log.error( "NPE injecting service deps : " + service.getClass().getName() );
} }
@ -339,8 +338,8 @@ public abstract class AbstractServiceRegistryImpl
@Override @Override
public <R extends Service> void startService(ServiceBinding<R> serviceBinding) { public <R extends Service> void startService(ServiceBinding<R> serviceBinding) {
if ( serviceBinding.getService() instanceof Startable ) { if ( serviceBinding.getService() instanceof Startable startable ) {
( (Startable) serviceBinding.getService() ).start(); startable.start();
} }
} }
@ -356,9 +355,8 @@ public abstract class AbstractServiceRegistryImpl
//threads not owning the synchronization lock can't get an invalid Service: //threads not owning the synchronization lock can't get an invalid Service:
initializedServiceByRole.clear(); initializedServiceByRole.clear();
synchronized (serviceBindingList) { synchronized (serviceBindingList) {
ListIterator<ServiceBinding<?>> serviceBindingsIterator = serviceBindingList.listIterator( final ListIterator<ServiceBinding<?>> serviceBindingsIterator =
serviceBindingList.size() serviceBindingList.listIterator( serviceBindingList.size() );
);
while ( serviceBindingsIterator.hasPrevious() ) { while ( serviceBindingsIterator.hasPrevious() ) {
final ServiceBinding<?> serviceBinding = serviceBindingsIterator.previous(); final ServiceBinding<?> serviceBinding = serviceBindingsIterator.previous();
serviceBinding.getLifecycleOwner().stopService( serviceBinding ); serviceBinding.getLifecycleOwner().stopService( serviceBinding );
@ -378,9 +376,9 @@ public abstract class AbstractServiceRegistryImpl
@Override @Override
public synchronized <R extends Service> void stopService(ServiceBinding<R> binding) { public synchronized <R extends Service> void stopService(ServiceBinding<R> binding) {
final Service service = binding.getService(); final Service service = binding.getService();
if ( service instanceof Stoppable ) { if ( service instanceof Stoppable stoppable ) {
try { try {
( (Stoppable) service ).stop(); stoppable.stop();
} }
catch ( Exception e ) { catch ( Exception e ) {
log.unableToStopService( service.getClass(), e ); log.unableToStopService( service.getClass(), e );
@ -429,18 +427,18 @@ public abstract class AbstractServiceRegistryImpl
* experimentation with technologies such as GraalVM, Quarkus and Cri-O. * experimentation with technologies such as GraalVM, Quarkus and Cri-O.
*/ */
public synchronized void resetParent(@Nullable BootstrapServiceRegistry newParent) { public synchronized void resetParent(@Nullable BootstrapServiceRegistry newParent) {
if ( this.parent != null ) { if ( parent != null ) {
this.parent.deRegisterChild( this ); parent.deRegisterChild( this );
} }
if ( newParent != null ) { if ( newParent != null ) {
if ( !(newParent instanceof ServiceRegistryImplementor) ) { if ( !(newParent instanceof ServiceRegistryImplementor) ) {
throw new IllegalArgumentException( "ServiceRegistry parent needs to implement ServiceRegistryImplementor" ); throw new IllegalArgumentException( "ServiceRegistry parent needs to implement ServiceRegistryImplementor" );
} }
this.parent = (ServiceRegistryImplementor) newParent; parent = (ServiceRegistryImplementor) newParent;
this.parent.registerChild( this ); parent.registerChild( this );
} }
else { else {
this.parent = null; parent = null;
} }
} }
@ -472,9 +470,10 @@ public abstract class AbstractServiceRegistryImpl
} }
/** /**
* Not intended for general use. We need the ability to stop and "reactivate" a registry to allow * Not intended for general use. We need the ability to stop and "reactivate" a registry
* experimentation with technologies such as GraalVM, Quarkus and Cri-O. * to allow experimentation with technologies such as GraalVM, Quarkus and Cri-O.
*/ */
@Internal
public synchronized void reactivate() { public synchronized void reactivate() {
if ( !active.compareAndSet( false, true ) ) { if ( !active.compareAndSet( false, true ) ) {
throw new IllegalStateException( "Was not inactive, could not reactivate" ); throw new IllegalStateException( "Was not inactive, could not reactivate" );