Skip to content

Commit 1b581a0

Browse files
committed
cleanups around IntegratorServiceImpl
1 parent c68deae commit 1b581a0

File tree

4 files changed

+44
-62
lines changed

4 files changed

+44
-62
lines changed

hibernate-core/src/main/java/org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.hibernate.boot.registry.classloading.internal.TcclLookupPrecedence;
1515
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
1616
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
17-
import org.hibernate.boot.registry.selector.StrategyRegistration;
1817
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
1918
import org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder;
2019
import org.hibernate.integrator.internal.IntegratorServiceImpl;
@@ -129,7 +128,7 @@ public <T> BootstrapServiceRegistryBuilder applyStrategySelector(Class<T> strate
129128
* @see org.hibernate.boot.registry.selector.spi.StrategySelector#registerStrategyImplementor(Class, String, Class)
130129
*/
131130
public BootstrapServiceRegistryBuilder applyStrategySelectors(StrategyRegistrationProvider strategyRegistrationProvider) {
132-
for ( StrategyRegistration<?> strategyRegistration : strategyRegistrationProvider.getStrategyRegistrations() ) {
131+
for ( var strategyRegistration : strategyRegistrationProvider.getStrategyRegistrations() ) {
133132
this.strategySelectorBuilder.addExplicitStrategyRegistration( strategyRegistration );
134133
}
135134
return this;
@@ -169,33 +168,28 @@ public BootstrapServiceRegistryBuilder enableAutoClose() {
169168
* @return The built bootstrap registry
170169
*/
171170
public BootstrapServiceRegistry build() {
172-
final ClassLoaderService classLoaderService;
171+
final var classLoaderService = classLoaderService();
172+
return new BootstrapServiceRegistryImpl(
173+
autoCloseRegistry,
174+
classLoaderService,
175+
strategySelectorBuilder.buildSelector( classLoaderService ),
176+
new IntegratorServiceImpl( providedIntegrators, classLoaderService )
177+
);
178+
}
179+
180+
private ClassLoaderService classLoaderService() {
173181
if ( providedClassLoaderService == null ) {
174182
// Use a set. As an example, in JPA, OsgiClassLoader may be in both
175183
// the providedClassLoaders and the overriddenClassLoader.
176184
final Set<ClassLoader> classLoaders = new HashSet<>();
177-
178185
if ( providedClassLoaders != null ) {
179186
classLoaders.addAll( providedClassLoaders );
180187
}
181-
182-
classLoaderService = new ClassLoaderServiceImpl( classLoaders,tcclLookupPrecedence );
188+
return new ClassLoaderServiceImpl( classLoaders,tcclLookupPrecedence );
183189
}
184190
else {
185-
classLoaderService = providedClassLoaderService;
191+
return providedClassLoaderService;
186192
}
187-
188-
final IntegratorServiceImpl integratorService = IntegratorServiceImpl.create(
189-
providedIntegrators,
190-
classLoaderService
191-
);
192-
193-
return new BootstrapServiceRegistryImpl(
194-
autoCloseRegistry,
195-
classLoaderService,
196-
strategySelectorBuilder.buildSelector( classLoaderService ),
197-
integratorService
198-
);
199193
}
200194

201195
/**
@@ -204,10 +198,8 @@ public BootstrapServiceRegistry build() {
204198
* @param serviceRegistry The registry to be closed.
205199
*/
206200
public static void destroy(ServiceRegistry serviceRegistry) {
207-
if ( serviceRegistry == null ) {
208-
return;
201+
if ( serviceRegistry != null ) {
202+
((BootstrapServiceRegistryImpl) serviceRegistry).destroy();
209203
}
210-
211-
( (BootstrapServiceRegistryImpl) serviceRegistry ).destroy();
212204
}
213205
}

hibernate-core/src/main/java/org/hibernate/boot/registry/internal/BootstrapServiceRegistryImpl.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,16 @@ public BootstrapServiceRegistryImpl(
105105
classLoaderService
106106
);
107107

108-
final StrategySelectorImpl strategySelector = new StrategySelectorImpl( classLoaderService );
109108
this.strategySelectorBinding = new ServiceBinding<>(
110109
this,
111110
StrategySelector.class,
112-
strategySelector
111+
new StrategySelectorImpl( classLoaderService )
113112
);
114113

115114
this.integratorServiceBinding = new ServiceBinding<>(
116115
this,
117116
IntegratorService.class,
118-
IntegratorServiceImpl.create( providedIntegrators, classLoaderService )
117+
new IntegratorServiceImpl( providedIntegrators, classLoaderService )
119118
);
120119
}
121120

@@ -184,7 +183,7 @@ public BootstrapServiceRegistryImpl(
184183

185184
@Override
186185
public <R extends Service> @Nullable R getService(Class<R> serviceRole) {
187-
final ServiceBinding<R> binding = locateServiceBinding( serviceRole );
186+
final var binding = locateServiceBinding( serviceRole );
188187
return binding == null ? null : binding.getService();
189188
}
190189

@@ -206,24 +205,20 @@ else if ( IntegratorService.class.equals( serviceRole ) ) {
206205

207206
@Override
208207
public synchronized void destroy() {
209-
if ( !active ) {
210-
return;
211-
}
212-
active = false;
213-
destroy( classLoaderServiceBinding );
214-
destroy( strategySelectorBinding );
215-
destroy( integratorServiceBinding );
216-
217-
if ( childRegistries != null ) {
218-
for ( ServiceRegistry serviceRegistry : childRegistries ) {
219-
if ( serviceRegistry instanceof ServiceRegistryImplementor serviceRegistryImplementor ) {
220-
serviceRegistryImplementor.destroy();
208+
if ( active ) {
209+
active = false;
210+
destroy( classLoaderServiceBinding );
211+
destroy( strategySelectorBinding );
212+
destroy( integratorServiceBinding );
213+
if ( childRegistries != null ) {
214+
for ( var serviceRegistry : childRegistries ) {
215+
serviceRegistry.destroy();
221216
}
222217
}
223218
}
224219
}
225220

226-
private synchronized void destroy(ServiceBinding serviceBinding) {
221+
private synchronized void destroy(ServiceBinding<?> serviceBinding) {
227222
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
228223
}
229224

hibernate-core/src/main/java/org/hibernate/integrator/internal/IntegratorServiceImpl.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,33 @@
1212
import org.hibernate.integrator.spi.Integrator;
1313
import org.hibernate.integrator.spi.IntegratorService;
1414

15-
import org.jboss.logging.Logger;
15+
import static org.hibernate.service.internal.ServiceLogger.SERVICE_LOGGER;
1616

1717
/**
1818
* @author Steve Ebersole
1919
*/
2020
public class IntegratorServiceImpl implements IntegratorService {
2121

22-
private static final Logger LOG = Logger.getLogger( IntegratorServiceImpl.class );
23-
2422
private final LinkedHashSet<Integrator> integrators = new LinkedHashSet<>();
2523

26-
private IntegratorServiceImpl() {
27-
}
28-
29-
public static IntegratorServiceImpl create(LinkedHashSet<Integrator> providedIntegrators, ClassLoaderService classLoaderService) {
30-
IntegratorServiceImpl instance = new IntegratorServiceImpl();
31-
32-
// register standard integrators. Envers and JPA, for example, need to be handled by discovery because in
33-
// separate project/jars.
34-
instance.addIntegrator( new BeanValidationIntegrator() );
35-
instance.addIntegrator( new CollectionCacheInvalidator() );
24+
public IntegratorServiceImpl(Iterable<Integrator> providedIntegrators, ClassLoaderService classLoaderService) {
25+
// Register standard integrators.
26+
// Envers, for example, needs to be handled by discovery because in separate project/jar.
27+
addIntegrator( integrators, new BeanValidationIntegrator() );
28+
addIntegrator( integrators, new CollectionCacheInvalidator() );
3629

3730
// register provided integrators
38-
for ( Integrator integrator : providedIntegrators ) {
39-
instance.addIntegrator( integrator );
31+
for ( var integrator : providedIntegrators ) {
32+
addIntegrator( integrators, integrator );
4033
}
41-
for ( Integrator integrator : classLoaderService.loadJavaServices( Integrator.class ) ) {
42-
instance.addIntegrator( integrator );
34+
for ( var integrator : classLoaderService.loadJavaServices( Integrator.class ) ) {
35+
addIntegrator( integrators, integrator );
4336
}
44-
45-
return instance;
4637
}
4738

48-
private void addIntegrator(Integrator integrator) {
49-
if ( LOG.isDebugEnabled() ) {
50-
LOG.debugf( "Adding Integrator [%s]", integrator.getClass().getName() );
39+
private static void addIntegrator(LinkedHashSet<Integrator> integrators, Integrator integrator) {
40+
if ( SERVICE_LOGGER.isDebugEnabled() ) {
41+
SERVICE_LOGGER.addingIntegrator( integrator.getClass().getName() );
5142
}
5243
integrators.add( integrator );
5344
}

hibernate-core/src/main/java/org/hibernate/service/internal/ServiceLogger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ public interface ServiceLogger extends BasicLogger {
7373
@Message( id = 10454, value = "EventListenerRegistry access via ServiceRegistry is deprecated - "
7474
+ "use 'sessionFactory.getEventEngine().getListenerRegistry()' instead" )
7575
void eventListenerRegistryAccessDeprecated();
76+
77+
@LogMessage(level = DEBUG)
78+
@Message(id = 10455, value = "Adding integrator: %s")
79+
void addingIntegrator(String name);
7680
}

0 commit comments

Comments
 (0)