Skip to content

Commit 1e73378

Browse files
committed
minor code changes in SessionFactoryOptionsBuilder
1 parent 605b5cb commit 1e73378

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
8181
import org.hibernate.stat.Statistics;
8282
import org.hibernate.type.format.FormatMapper;
83-
import org.hibernate.type.format.jackson.JacksonIntegration;
8483
import org.hibernate.type.format.jaxb.JaxbXmlFormatMapper;
8584

8685
import jakarta.persistence.criteria.Nulls;
@@ -118,6 +117,7 @@
118117
import static org.hibernate.type.format.jackson.JacksonIntegration.getJsonJacksonFormatMapperOrNull;
119118
import static org.hibernate.type.format.jackson.JacksonIntegration.getOsonJacksonFormatMapperOrNull;
120119
import static org.hibernate.type.format.jackson.JacksonIntegration.getXMLJacksonFormatMapperOrNull;
120+
import static org.hibernate.type.format.jackson.JacksonIntegration.isJacksonOsonExtensionAvailable;
121121
import static org.hibernate.type.format.jakartajson.JakartaJsonIntegration.getJakartaJsonBFormatMapperOrNull;
122122

123123
/**
@@ -309,13 +309,13 @@ public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry, Boo
309309
settings.get( AvailableSettings.JAKARTA_VALIDATION_FACTORY )
310310
);
311311

312-
jsonFormatMapper = determineJsonFormatMapper(
312+
jsonFormatMapper = jsonFormatMapper(
313313
settings.get( AvailableSettings.JSON_FORMAT_MAPPER ),
314314
!getBoolean( ORACLE_OSON_DISABLED ,settings),
315315
strategySelector
316316
);
317317

318-
xmlFormatMapper = determineXmlFormatMapper(
318+
xmlFormatMapper = xmlFormatMapper(
319319
settings.get( AvailableSettings.XML_FORMAT_MAPPER ),
320320
strategySelector,
321321
xmlFormatMapperLegacyFormatEnabled =
@@ -620,17 +620,20 @@ private SqmMultiTableMutationStrategy resolveSqmMutationStrategy(
620620
strategyName,
621621
(SqmMultiTableMutationStrategy) null,
622622
strategyClass -> {
623-
Constructor<SqmMultiTableMutationStrategy> dialectConstructor = null;
624-
Constructor<SqmMultiTableMutationStrategy> emptyConstructor = null;
623+
Constructor<? extends SqmMultiTableMutationStrategy> dialectConstructor = null;
624+
Constructor<? extends SqmMultiTableMutationStrategy> emptyConstructor = null;
625625
// todo (6.0) : formalize the allowed constructor parameterizations
626-
for ( Constructor<?> declaredConstructor : strategyClass.getDeclaredConstructors() ) {
627-
final Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
626+
for ( var declaredConstructor : strategyClass.getDeclaredConstructors() ) {
627+
final var parameterTypes = declaredConstructor.getParameterTypes();
628+
final var constructor =
629+
(Constructor<? extends SqmMultiTableMutationStrategy>)
630+
declaredConstructor;
628631
if ( parameterTypes.length == 1 && parameterTypes[0] == Dialect.class ) {
629-
dialectConstructor = (Constructor<SqmMultiTableMutationStrategy>) declaredConstructor;
632+
dialectConstructor = constructor;
630633
break;
631634
}
632635
else if ( parameterTypes.length == 0 ) {
633-
emptyConstructor = (Constructor<SqmMultiTableMutationStrategy>) declaredConstructor;
636+
emptyConstructor = constructor;
634637
}
635638
}
636639

@@ -646,11 +649,13 @@ else if ( emptyConstructor != null ) {
646649
}
647650
catch (Exception e) {
648651
throw new StrategySelectionException(
649-
String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ),
652+
"Could not instantiate named strategy class [" + strategyClass.getName() + "]",
650653
e
651654
);
652655
}
653-
throw new IllegalArgumentException( "Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor" );
656+
throw new IllegalArgumentException( "Cannot instantiate the class ["
657+
+ strategyClass.getName()
658+
+ "] because it does not have a constructor that accepts a dialect or an empty constructor" );
654659
}
655660
);
656661
}
@@ -669,17 +674,20 @@ private SqmMultiTableInsertStrategy resolveSqmInsertStrategy(
669674
strategyName,
670675
(SqmMultiTableInsertStrategy) null,
671676
strategyClass -> {
672-
Constructor<SqmMultiTableInsertStrategy> dialectConstructor = null;
673-
Constructor<SqmMultiTableInsertStrategy> emptyConstructor = null;
677+
Constructor<? extends SqmMultiTableInsertStrategy> dialectConstructor = null;
678+
Constructor<? extends SqmMultiTableInsertStrategy> emptyConstructor = null;
674679
// todo (6.0) : formalize the allowed constructor parameterizations
675-
for ( Constructor<?> declaredConstructor : strategyClass.getDeclaredConstructors() ) {
676-
final Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
680+
for ( var declaredConstructor : strategyClass.getDeclaredConstructors() ) {
681+
final var parameterTypes = declaredConstructor.getParameterTypes();
682+
final var constructor =
683+
(Constructor<? extends SqmMultiTableInsertStrategy>)
684+
declaredConstructor;
677685
if ( parameterTypes.length == 1 && parameterTypes[0] == Dialect.class ) {
678-
dialectConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
686+
dialectConstructor = constructor;
679687
break;
680688
}
681689
else if ( parameterTypes.length == 0 ) {
682-
emptyConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
690+
emptyConstructor = constructor;
683691
}
684692
}
685693

@@ -695,11 +703,13 @@ else if ( emptyConstructor != null ) {
695703
}
696704
catch (Exception e) {
697705
throw new StrategySelectionException(
698-
String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ),
706+
"Could not instantiate named strategy class [" + strategyClass.getName() + "]",
699707
e
700708
);
701709
}
702-
throw new IllegalArgumentException( "Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor" );
710+
throw new IllegalArgumentException( "Cannot instantiate the class ["
711+
+ strategyClass.getName()
712+
+ "] because it does not have a constructor that accepts a dialect or an empty constructor" );
703713
}
704714
);
705715
}
@@ -708,23 +718,15 @@ private HqlTranslator resolveHqlTranslator(
708718
String producerName,
709719
StandardServiceRegistry serviceRegistry,
710720
StrategySelector strategySelector) {
711-
if ( isEmpty( producerName ) ) {
712-
return null;
713-
}
714-
else {
715-
//noinspection Convert2Lambda
716-
return strategySelector.resolveDefaultableStrategy(
717-
HqlTranslator.class,
718-
producerName,
719-
new Callable<>() {
720-
@Override
721-
public HqlTranslator call() throws Exception {
722-
return (HqlTranslator) serviceRegistry.requireService( ClassLoaderService.class )
723-
.classForName( producerName ).newInstance();
724-
}
725-
}
726-
);
727-
}
721+
return isEmpty( producerName )
722+
? null
723+
: strategySelector.<HqlTranslator>resolveDefaultableStrategy(
724+
HqlTranslator.class,
725+
producerName,
726+
() -> (HqlTranslator)
727+
serviceRegistry.requireService( ClassLoaderService.class )
728+
.classForName( producerName ).newInstance()
729+
);
728730
}
729731

730732
private SqmTranslatorFactory resolveSqmTranslator(
@@ -754,8 +756,7 @@ private static Interceptor determineInterceptor(
754756
private static Supplier<? extends Interceptor> determineStatelessInterceptor(
755757
Map<String,Object> configurationSettings,
756758
StrategySelector strategySelector) {
757-
Object setting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );
758-
759+
final Object setting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );
759760
if ( setting == null ) {
760761
return null;
761762
}
@@ -782,7 +783,7 @@ private static Supplier<? extends Interceptor> interceptorSupplier(Class<? exten
782783
return clazz.newInstance();
783784
}
784785
catch (InstantiationException | IllegalAccessException e) {
785-
throw new HibernateException( "Could not supply session-scoped SessionFactory Interceptor", e );
786+
throw new org.hibernate.InstantiationException( "Could not instantiate session-scoped Interceptor", clazz, e );
786787
}
787788
};
788789
}
@@ -798,32 +799,41 @@ private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
798799
.getDefaultConnectionHandlingMode();
799800
}
800801

801-
private static FormatMapper determineJsonFormatMapper(Object setting, boolean osonExtensionEnabled, StrategySelector strategySelector) {
802-
return strategySelector.resolveDefaultableStrategy(
803-
FormatMapper.class,
802+
private static FormatMapper jsonFormatMapper(Object setting, boolean osonExtensionEnabled, StrategySelector selector) {
803+
return formatMapper(
804804
setting,
805-
(Callable<FormatMapper>) () -> {
805+
selector,
806+
() -> {
806807
// Prefer the OSON Jackson FormatMapper by default if available
807808
final FormatMapper jsonJacksonFormatMapper =
808-
(osonExtensionEnabled && JacksonIntegration.isJacksonOsonExtensionAvailable())
809+
osonExtensionEnabled && isJacksonOsonExtensionAvailable()
809810
? getOsonJacksonFormatMapperOrNull()
810811
: getJsonJacksonFormatMapperOrNull();
811-
return jsonJacksonFormatMapper != null ? jsonJacksonFormatMapper : getJakartaJsonBFormatMapperOrNull();
812+
return jsonJacksonFormatMapper != null
813+
? jsonJacksonFormatMapper
814+
: getJakartaJsonBFormatMapperOrNull();
812815
}
813816
);
814817
}
815818

816-
private static FormatMapper determineXmlFormatMapper(Object setting, StrategySelector strategySelector, boolean legacyFormat) {
817-
return strategySelector.resolveDefaultableStrategy(
818-
FormatMapper.class,
819+
private static FormatMapper xmlFormatMapper(Object setting, StrategySelector selector, boolean legacyFormat) {
820+
return formatMapper(
819821
setting,
820-
(Callable<FormatMapper>) () -> {
821-
final FormatMapper jacksonFormatMapper = getXMLJacksonFormatMapperOrNull( legacyFormat );
822-
return jacksonFormatMapper != null ? jacksonFormatMapper : new JaxbXmlFormatMapper( legacyFormat );
822+
selector,
823+
() -> {
824+
final FormatMapper jacksonFormatMapper =
825+
getXMLJacksonFormatMapperOrNull( legacyFormat );
826+
return jacksonFormatMapper != null
827+
? jacksonFormatMapper
828+
: new JaxbXmlFormatMapper( legacyFormat );
823829
}
824830
);
825831
}
826832

833+
private static FormatMapper formatMapper(Object setting, StrategySelector selector, Callable<FormatMapper> defaultResolver) {
834+
return selector.resolveDefaultableStrategy( FormatMapper.class, setting, defaultResolver );
835+
}
836+
827837

828838
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
829839
// SessionFactoryOptionsState
@@ -1601,8 +1611,8 @@ public void disableJtaTransactionAccess() {
16011611
}
16021612

16031613
public SessionFactoryOptions buildOptions() {
1604-
if ( jpaCompliance instanceof MutableJpaCompliance ) {
1605-
jpaCompliance = mutableJpaCompliance().immutableCopy();
1614+
if ( jpaCompliance instanceof MutableJpaCompliance mutableJpaCompliance ) {
1615+
jpaCompliance = mutableJpaCompliance.immutableCopy();
16061616
}
16071617
return this;
16081618
}
@@ -1692,7 +1702,7 @@ private Map<String, Object> initializeDefaultSessionProperties(ConfigurationServ
16921702
LegacySpecHints.HINT_JAVAEE_QUERY_TIMEOUT
16931703
};
16941704

1695-
final Map<String, Object> configurationServiceSettings = configurationService.getSettings();
1705+
final var configurationServiceSettings = configurationService.getSettings();
16961706
for ( String key : ENTITY_MANAGER_SPECIFIC_PROPERTIES ) {
16971707
if ( configurationServiceSettings.containsKey( key ) ) {
16981708
settings.put( key, configurationServiceSettings.get( key ) );

0 commit comments

Comments
 (0)