Support resource bundle custom file extensions
This commit allows to configure custom file extensions in ReloadableResourceBundleMessageSource thanks to a new setFileExtensions setter. Combined with setPropertiesPersister, it allows custom implementations supporting any kind of property file. Closes gh-18990
This commit is contained in:
parent
ad80b94e14
commit
c942c04aa0
|
|
@ -21,9 +21,11 @@ import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
@ -34,6 +36,8 @@ import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.DefaultPropertiesPersister;
|
import org.springframework.util.DefaultPropertiesPersister;
|
||||||
import org.springframework.util.PropertiesPersister;
|
import org.springframework.util.PropertiesPersister;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
@ -74,6 +78,7 @@ import org.springframework.util.StringUtils;
|
||||||
* this message source!
|
* this message source!
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @see #setCacheSeconds
|
* @see #setCacheSeconds
|
||||||
* @see #setBasenames
|
* @see #setBasenames
|
||||||
* @see #setDefaultEncoding
|
* @see #setDefaultEncoding
|
||||||
|
|
@ -87,10 +92,10 @@ import org.springframework.util.StringUtils;
|
||||||
public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource
|
public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource
|
||||||
implements ResourceLoaderAware {
|
implements ResourceLoaderAware {
|
||||||
|
|
||||||
private static final String PROPERTIES_SUFFIX = ".properties";
|
private static final String XML_EXTENSION = ".xml";
|
||||||
|
|
||||||
private static final String XML_SUFFIX = ".xml";
|
|
||||||
|
|
||||||
|
private List<String> fileExtensions = List.of(".properties", XML_EXTENSION);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Properties fileEncodings;
|
private Properties fileEncodings;
|
||||||
|
|
@ -111,6 +116,22 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
|
||||||
private final ConcurrentMap<Locale, PropertiesHolder> cachedMergedProperties = new ConcurrentHashMap<>();
|
private final ConcurrentMap<Locale, PropertiesHolder> cachedMergedProperties = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the list of supported file extensions.
|
||||||
|
* <p>The default is a list containing {@code .properties} and {@code .xml}.
|
||||||
|
* @param fileExtensions the file extensions (starts with a dot)
|
||||||
|
* @since 6.1.0
|
||||||
|
*/
|
||||||
|
public void setFileExtensions(List<String> fileExtensions) {
|
||||||
|
Assert.isTrue(!CollectionUtils.isEmpty(fileExtensions), "At least one file extension is required");
|
||||||
|
for (String extension : fileExtensions) {
|
||||||
|
if (!extension.startsWith(".")) {
|
||||||
|
throw new IllegalArgumentException("File extension '" + extension + "' should start with '.'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.fileExtensions = Collections.unmodifiableList(fileExtensions);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set per-file charsets to use for parsing properties files.
|
* Set per-file charsets to use for parsing properties files.
|
||||||
* <p>Only applies to classic properties files, not to XML files.
|
* <p>Only applies to classic properties files, not to XML files.
|
||||||
|
|
@ -471,9 +492,9 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
|
||||||
* JSON.
|
* JSON.
|
||||||
* <p>The default implementation delegates to the configured
|
* <p>The default implementation delegates to the configured
|
||||||
* {@link #setResourceLoader(ResourceLoader) ResourceLoader} to resolve
|
* {@link #setResourceLoader(ResourceLoader) ResourceLoader} to resolve
|
||||||
* resources, first checking for an existing {@code Resource} with a
|
* resources, checking in order for existing {@code Resource} with extensions defined
|
||||||
* {@code .properties} extension, and otherwise returning a {@code Resource}
|
* by {@link #setFileExtensions(List)} ({@code .properties} and {@code .xml}
|
||||||
* with a {@code .xml} extension.
|
* by default).
|
||||||
* <p>When overriding this method, {@link #loadProperties(Resource, String)}
|
* <p>When overriding this method, {@link #loadProperties(Resource, String)}
|
||||||
* <strong>must</strong> be capable of loading properties from any type of
|
* <strong>must</strong> be capable of loading properties from any type of
|
||||||
* {@code Resource} returned by this method. As a consequence, implementors
|
* {@code Resource} returned by this method. As a consequence, implementors
|
||||||
|
|
@ -491,11 +512,14 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
|
||||||
* @since 6.1
|
* @since 6.1
|
||||||
*/
|
*/
|
||||||
protected Resource resolveResource(String filename) {
|
protected Resource resolveResource(String filename) {
|
||||||
Resource propertiesResource = this.resourceLoader.getResource(filename + PROPERTIES_SUFFIX);
|
Resource resource = null;
|
||||||
if (propertiesResource.exists()) {
|
for (String fileExtension : this.fileExtensions) {
|
||||||
return propertiesResource;
|
resource = this.resourceLoader.getResource(filename + fileExtension);
|
||||||
|
if (resource.exists()) {
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.resourceLoader.getResource(filename + XML_SUFFIX);
|
return Objects.requireNonNull(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -509,7 +533,7 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
|
||||||
Properties props = newProperties();
|
Properties props = newProperties();
|
||||||
try (InputStream is = resource.getInputStream()) {
|
try (InputStream is = resource.getInputStream()) {
|
||||||
String resourceFilename = resource.getFilename();
|
String resourceFilename = resource.getFilename();
|
||||||
if (resourceFilename != null && resourceFilename.endsWith(XML_SUFFIX)) {
|
if (resourceFilename != null && resourceFilename.endsWith(XML_EXTENSION)) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Loading properties [" + resource.getFilename() + "]");
|
logger.debug("Loading properties [" + resource.getFilename() + "]");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.context.support;
|
package org.springframework.context.support;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
@ -31,9 +32,11 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @since 03.02.2004
|
* @since 03.02.2004
|
||||||
*/
|
*/
|
||||||
class ResourceBundleMessageSourceTests {
|
class ResourceBundleMessageSourceTests {
|
||||||
|
|
@ -417,6 +420,31 @@ class ResourceBundleMessageSourceTests {
|
||||||
assertThat(filenames).isEmpty();
|
assertThat(filenames).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reloadableResourceBundleMessageSourceWithCustomFileExtensions() {
|
||||||
|
ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
|
||||||
|
ms.setBasename("org/springframework/context/support/messages");
|
||||||
|
ms.setFileExtensions(List.of(".toskip", ".custom"));
|
||||||
|
assertThat(ms.getMessage("code1", null, Locale.ENGLISH)).isEqualTo("message1");
|
||||||
|
assertThat(ms.getMessage("code2", null, Locale.GERMAN)).isEqualTo("nachricht2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reloadableResourceBundleMessageSourceWithEmptyCustomFileExtensions() {
|
||||||
|
ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
|
||||||
|
assertThatThrownBy(() -> ms.setFileExtensions(Collections.emptyList()))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasMessage("At least one file extension is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reloadableResourceBundleMessageSourceWithInvalidCustomFileExtensions() {
|
||||||
|
ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
|
||||||
|
assertThatThrownBy(() -> ms.setFileExtensions(List.of("invalid")))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasMessage("File extension 'invalid' should start with '.'");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void messageSourceResourceBundle() {
|
void messageSourceResourceBundle() {
|
||||||
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
code1=message1
|
||||||
|
code2=message2
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
code2=nachricht2
|
||||||
Loading…
Reference in New Issue