diff --git a/engine/src/main/java/org/hibernate/validator/internal/engine/AbstractConfigurationImpl.java b/engine/src/main/java/org/hibernate/validator/internal/engine/AbstractConfigurationImpl.java index e1996ff504..7c5d580c95 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/engine/AbstractConfigurationImpl.java +++ b/engine/src/main/java/org/hibernate/validator/internal/engine/AbstractConfigurationImpl.java @@ -576,7 +576,7 @@ public final ConstraintValidatorFactory getDefaultConstraintValidatorFactory() { public final ResourceBundleLocator getDefaultResourceBundleLocator() { if ( defaultResourceBundleLocator == null ) { defaultResourceBundleLocator = new PlatformResourceBundleLocator( - ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES, preloadResourceBundles(), getAllSupportedLocales() ); + ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES, preloadResourceBundles() ? getAllSupportedLocales() : Collections.emptySet() ); } return defaultResourceBundleLocator; @@ -726,14 +726,12 @@ private MessageInterpolator getDefaultMessageInterpolatorConfiguredWithClassLoad if ( externalClassLoader != null ) { PlatformResourceBundleLocator userResourceBundleLocator = new PlatformResourceBundleLocator( ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES, - preloadResourceBundles(), - getAllSupportedLocales(), + preloadResourceBundles() ? getAllSupportedLocales() : Collections.emptySet(), externalClassLoader ); PlatformResourceBundleLocator contributorResourceBundleLocator = new PlatformResourceBundleLocator( ResourceBundleMessageInterpolator.CONTRIBUTOR_VALIDATION_MESSAGES, - preloadResourceBundles(), - getAllSupportedLocales(), + preloadResourceBundles() ? getAllSupportedLocales() : Collections.emptySet(), externalClassLoader, true ); diff --git a/engine/src/main/java/org/hibernate/validator/messageinterpolation/AbstractMessageInterpolator.java b/engine/src/main/java/org/hibernate/validator/messageinterpolation/AbstractMessageInterpolator.java index c11da82c84..bbb7f8bd65 100644 --- a/engine/src/main/java/org/hibernate/validator/messageinterpolation/AbstractMessageInterpolator.java +++ b/engine/src/main/java/org/hibernate/validator/messageinterpolation/AbstractMessageInterpolator.java @@ -280,7 +280,7 @@ public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocat if ( userResourceBundleLocator == null ) { this.userResourceBundleLocator = new PlatformResourceBundleLocator( USER_VALIDATION_MESSAGES, - preloadResourceBundles, allLocalesToInitialize ); + allLocalesToInitialize ); } else { this.userResourceBundleLocator = userResourceBundleLocator; @@ -289,7 +289,6 @@ public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocat if ( contributorResourceBundleLocator == null ) { this.contributorResourceBundleLocator = new PlatformResourceBundleLocator( CONTRIBUTOR_VALIDATION_MESSAGES, - preloadResourceBundles, allLocalesToInitialize, null, true @@ -299,7 +298,7 @@ public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocat this.contributorResourceBundleLocator = contributorResourceBundleLocator; } - this.defaultResourceBundleLocator = new PlatformResourceBundleLocator( DEFAULT_VALIDATION_MESSAGES, preloadResourceBundles, allLocalesToInitialize ); + this.defaultResourceBundleLocator = new PlatformResourceBundleLocator( DEFAULT_VALIDATION_MESSAGES, allLocalesToInitialize ); this.cachingEnabled = cacheMessages; if ( cachingEnabled ) { diff --git a/engine/src/main/java/org/hibernate/validator/resourceloading/AggregateResourceBundleLocator.java b/engine/src/main/java/org/hibernate/validator/resourceloading/AggregateResourceBundleLocator.java index 225d84eb1c..dc2ec57957 100644 --- a/engine/src/main/java/org/hibernate/validator/resourceloading/AggregateResourceBundleLocator.java +++ b/engine/src/main/java/org/hibernate/validator/resourceloading/AggregateResourceBundleLocator.java @@ -151,7 +151,8 @@ public AggregateResourceBundleLocator(List bundleNames, List tmpBundleLocators = new ArrayList<>( bundleNames.size() ); for ( String bundleName : bundleNames ) { - tmpBundleLocators.add( new PlatformResourceBundleLocator( bundleName, preloadResourceBundles, localesToInitialize, classLoader ) ); + tmpBundleLocators + .add( new PlatformResourceBundleLocator( bundleName, preloadResourceBundles ? localesToInitialize : Collections.emptySet(), classLoader ) ); } this.resourceBundleLocators = CollectionHelper.toImmutableList( tmpBundleLocators ); } diff --git a/engine/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java b/engine/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java index e804fe8adc..b78b263ab3 100644 --- a/engine/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java +++ b/engine/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java @@ -43,6 +43,7 @@ * * @author Hardy Ferentschik * @author Gunnar Morling + * @author Guillaume Smet */ public class PlatformResourceBundleLocator implements ResourceBundleLocator { @@ -53,7 +54,6 @@ public class PlatformResourceBundleLocator implements ResourceBundleLocator { private final ClassLoader classLoader; private final boolean aggregate; - private final boolean preloadResourceBundles; @Immutable private final Map preloadedResourceBundles; @@ -63,7 +63,7 @@ public class PlatformResourceBundleLocator implements ResourceBundleLocator { * @param bundleName the name of the bundle to load */ public PlatformResourceBundleLocator(String bundleName) { - this( bundleName, false, Collections.emptySet() ); + this( bundleName, Collections.emptySet() ); } /** @@ -77,7 +77,7 @@ public PlatformResourceBundleLocator(String bundleName) { * @since 5.2 */ public PlatformResourceBundleLocator(String bundleName, ClassLoader classLoader) { - this( bundleName, false, Collections.emptySet(), classLoader ); + this( bundleName, Collections.emptySet(), classLoader ); } /** @@ -92,46 +92,42 @@ public PlatformResourceBundleLocator(String bundleName, ClassLoader classLoader) * @since 5.2 */ public PlatformResourceBundleLocator(String bundleName, ClassLoader classLoader, boolean aggregate) { - this( bundleName, false, Collections.emptySet(), classLoader, aggregate ); + this( bundleName, Collections.emptySet(), classLoader, aggregate ); } /** * Creates a new {@link PlatformResourceBundleLocator}. * * @param bundleName the name of the bundle to load - * @param preloadResourceBundles if resource bundles should be initialized when initializing the locator * @param localesToInitialize the set of locales to initialize at bootstrap * * @since 6.1.1 */ @Incubating - public PlatformResourceBundleLocator(String bundleName, boolean preloadResourceBundles, Set localesToInitialize) { - this( bundleName, preloadResourceBundles, localesToInitialize, null ); + public PlatformResourceBundleLocator(String bundleName, Set localesToInitialize) { + this( bundleName, localesToInitialize, null ); } /** * Creates a new {@link PlatformResourceBundleLocator}. * * @param bundleName the name of the bundle to load - * @param preloadResourceBundles if resource bundles should be initialized when initializing the locator * @param localesToInitialize the set of locales to initialize at bootstrap * @param classLoader the classloader to be used for loading the bundle. If {@code null}, the current thread context * classloader and finally Hibernate Validator's own classloader will be used for loading the specified * bundle. - * @param preloadResourceBundles if resource bundles should be initialized when initializing the locator * * @since 6.1.1 */ @Incubating - public PlatformResourceBundleLocator(String bundleName, boolean preloadResourceBundles, Set localesToInitialize, ClassLoader classLoader) { - this( bundleName, preloadResourceBundles, localesToInitialize, classLoader, false ); + public PlatformResourceBundleLocator(String bundleName, Set localesToInitialize, ClassLoader classLoader) { + this( bundleName, localesToInitialize, classLoader, false ); } /** * Creates a new {@link PlatformResourceBundleLocator}. * * @param bundleName the name of the bundle to load - * @param preloadResourceBundles if resource bundles should be initialized when initializing the locator * @param localesToInitialize the set of locales to initialize at bootstrap * @param classLoader the classloader to be used for loading the bundle. If {@code null}, the current thread context * classloader and finally Hibernate Validator's own classloader will be used for loading the specified @@ -142,7 +138,6 @@ public PlatformResourceBundleLocator(String bundleName, boolean preloadResourceB */ @Incubating public PlatformResourceBundleLocator(String bundleName, - boolean preloadResourceBundles, Set localesToInitialize, ClassLoader classLoader, boolean aggregate) { @@ -153,11 +148,7 @@ public PlatformResourceBundleLocator(String bundleName, this.aggregate = aggregate && RESOURCE_BUNDLE_CONTROL_INSTANTIABLE; - this.preloadResourceBundles = preloadResourceBundles; - - if ( preloadResourceBundles ) { - Contracts.assertNotEmpty( localesToInitialize, "localesToInitialize may not be empty if resource bundles have to be preloaded" ); - + if ( !localesToInitialize.isEmpty() ) { Map tmpPreloadedResourceBundles = CollectionHelper.newHashMap( localesToInitialize.size() ); for ( Locale localeToPreload : localesToInitialize ) { tmpPreloadedResourceBundles.put( localeToPreload, doGetResourceBundle( localeToPreload ) ); @@ -179,7 +170,7 @@ public PlatformResourceBundleLocator(String bundleName, */ @Override public ResourceBundle getResourceBundle(Locale locale) { - if ( preloadResourceBundles ) { + if ( !preloadedResourceBundles.isEmpty() ) { // we need to use containsKey() as the cached resource bundle can be null if ( preloadedResourceBundles.containsKey( locale ) ) { return preloadedResourceBundles.get( locale );