Skip to content

Commit ce4d209

Browse files
SanneDavideD
authored andcommitted
Making it somewhat easier to manage difference with ORM among Service Initiators
1 parent 12daa09 commit ce4d209

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/impl/ReactiveServiceInitiators.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ private ReactiveServiceInitiators() {
6060
// Note that Quarkus uses a different list for the initialization of the services:
6161
// If you update this list makes sure to check that reactive still works with Quarkus.
6262
// Also: please try to maintain the same order as Hibernate ORM for sake of comparisons.
63+
// See also test ServiceInitiatorsTest; among basic validations it prints a sorted summary
64+
// for convenience.
6365
private static List<StandardServiceInitiator<?>> buildInitialServiceInitiatorList() {
6466
final ArrayList<StandardServiceInitiator<?>> serviceInitiators = new ArrayList<>();
6567

@@ -152,22 +154,26 @@ private static List<StandardServiceInitiator<?>> buildInitialServiceInitiatorLis
152154
// Custom for Hibernate Reactive: SqmMultiTableMutationStrategyProvider
153155
serviceInitiators.add( ReactiveSqmMultiTableMutationStrategyProviderInitiator.INSTANCE );
154156

155-
// Custom for Hibernate Reactive: NativeParametersRendering [Could be used by ORM too? TBD]
157+
// Custom for Hibernate Reactive: NativeParametersRendering
156158
serviceInitiators.add( NativeParametersRendering.INSTANCE );
157159

158160
// --- end of services defined by Hibernate ORM
159161

160162
// --- custom ones follow:
161163

162-
// Definitely exclusive to Hibernate Reactive, as it marks this particular registry as Reactive:
164+
// ReactiveMarkerService - Definitely exclusive to Hibernate Reactive, as it marks this particular registry as Reactive:
163165
serviceInitiators.add( ReactiveMarkerServiceInitiator.INSTANCE );
164166

165-
// Exclusive to Hibernate Reactive:
167+
// VertxInstance - Exclusive to Hibernate Reactive:
166168
serviceInitiators.add( VertxInstanceInitiator.INSTANCE );
169+
170+
// Context - Exclusive to Hibernate Reactive:
167171
serviceInitiators.add( VertxContextInitiator.INSTANCE );
168172

169-
// Exclusive to Hibernate Reactive:
173+
// SqlClientPoolConfiguration - Exclusive to Hibernate Reactive:
170174
serviceInitiators.add( SqlClientPoolConfigurationInitiator.INSTANCE );
175+
176+
// ReactiveConnectionPool - Exclusive to Hibernate Reactive:
171177
serviceInitiators.add( ReactiveConnectionPoolInitiator.INSTANCE );
172178

173179
// --- end of custom services.
@@ -176,4 +182,5 @@ private static List<StandardServiceInitiator<?>> buildInitialServiceInitiatorLis
176182

177183
return unmodifiableList( serviceInitiators );
178184
}
185+
179186
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.services;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Objects;
12+
import java.util.Set;
13+
import java.util.TreeMap;
14+
import java.util.TreeSet;
15+
16+
import org.hibernate.boot.registry.StandardServiceInitiator;
17+
import org.hibernate.reactive.provider.impl.ReactiveServiceInitiators;
18+
import org.hibernate.service.StandardServiceInitiators;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/**
24+
* Useful to spot inconsistencies in the default ServiceInitiator lists
25+
* used by Hibernate Reactive compared to Hibernate ORM.
26+
* N.B. Hibernate Reactive defines some additional services.
27+
*/
28+
public class ServiceInitiatorsTest {
29+
30+
private static final Map<String,String> HR_SERVICES = toServicesMap( ReactiveServiceInitiators.LIST );
31+
private static final Map<String,String> ORM_SERVICES = toServicesMap( StandardServiceInitiators.LIST );
32+
33+
// These services are NOT provided by the Hibernate Reactive default initiators, and that should be fine:
34+
private static final Set<String> HR_INTENTIONALLY_OMITTED = Set.of( "org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver" );
35+
36+
@Test
37+
public void serviceInitiatorsAreUnique() {
38+
Assert.assertEquals( HR_SERVICES.size(), ReactiveServiceInitiators.LIST.size() );
39+
Assert.assertEquals( ORM_SERVICES.size(), StandardServiceInitiators.LIST.size() );
40+
}
41+
42+
@Test
43+
public void allORMServicesAreDefined() {
44+
Set<String> sharedServiceImplementations = new TreeSet<>();
45+
Set<String> notSharedServiceImplementations = new TreeSet<>();
46+
reportDivider( "All Services in sorted order" );
47+
int i = 1;
48+
for ( String key : ORM_SERVICES.keySet() ) {
49+
String error = "ORM service '" + key + "' is not defined by the HR services list";
50+
Assert.assertTrue( error, HR_SERVICES.containsKey( key ) || HR_INTENTIONALLY_OMITTED.contains( key ) );
51+
if ( Objects.equals( HR_SERVICES.get( key ), ORM_SERVICES.get( key ) ) ) {
52+
sharedServiceImplementations.add( key );
53+
}
54+
else {
55+
notSharedServiceImplementations.add( key );
56+
}
57+
StringBuilder entry = new StringBuilder();
58+
entry.append( " #" ).append( i++ ).append( '\t' ).append( "service role " ).append( key ).append( '\n' );
59+
entry.append( "\tORM: " ).append( ORM_SERVICES.get( key ) ).append( '\n' );
60+
entry.append( "\tHR: " ).append( HR_SERVICES.get( key ) ).append( '\n' );
61+
System.out.println( entry );
62+
}
63+
reportDivider( "All Services which are shared among ORM and HR:" );
64+
for ( String key : sharedServiceImplementations ) {
65+
System.out.println( key );
66+
}
67+
reportDivider( "All Services which are overridden in HR:" );
68+
for ( String key : notSharedServiceImplementations ) {
69+
StringBuilder entry = new StringBuilder();
70+
entry.append( " service role " ).append( key ).append( '\n' );
71+
entry.append( "\tORM: " ).append( ORM_SERVICES.get( key ) ).append( '\n' );
72+
entry.append( "\tHR: " ).append( HR_SERVICES.get( key ) ).append( '\n' );
73+
System.out.println( entry );
74+
}
75+
}
76+
77+
private void reportDivider(String title) {
78+
System.out.println( "\n ########### " + title + "\n" );
79+
}
80+
81+
//N.B. service identity and class identity are handled by their name:
82+
//this is possibly more restrictive than strictly required in certain environments,
83+
//but we need to ensure safety in all classloading models.
84+
private static Map<String, String> toServicesMap(List<StandardServiceInitiator<?>> list) {
85+
TreeMap<String,String> rolesToImplMap = new TreeMap<>();
86+
for ( StandardServiceInitiator<?> initiator : list ) {
87+
final String serviceRole = initiator.getServiceInitiated().getName();
88+
rolesToImplMap.put( serviceRole, initiator.getClass().getName() );
89+
}
90+
return Collections.unmodifiableMap( rolesToImplMap );
91+
}
92+
93+
}

0 commit comments

Comments
 (0)